コード例 #1
0
 public function actionIndex($query = null)
 {
     return ['query' => Html::encode($query), 'results' => Index::searchByQuery($query, Yii::$app->composition->getKey('langShortCode'))];
 }
コード例 #2
0
 public function finish()
 {
     $builder = Builderindex::find()->where(['is_dublication' => 0])->indexBy('url')->asArray()->all();
     $index = Index::find()->asArray()->indexBy('url')->all();
     if (count($builder) == 0) {
         throw new Exception('The crawler have not found any results. Wrong base url? Or set a rule which tracks all urls? Try to enable verbose output.');
     }
     foreach ($builder as $url => $page) {
         if (isset($index[$url])) {
             // page exists in index
             if ($index[$url]['content'] == $page['content']) {
                 $this->addLog('unchanged', $url, $page['title']);
                 $update = Index::findOne(['url' => $url]);
                 $update->updateAttributes(['title' => $page['title']]);
             } else {
                 $this->addLog('update', $url, $page['title']);
                 $update = Index::findOne(['url' => $url]);
                 $update->attributes = $page;
                 $update->last_update = time();
                 $update->save(false);
             }
             unset($index[$url]);
         } else {
             $this->addLog('new', $url, $page['title']);
             $insert = new Index();
             $insert->attributes = $page;
             $insert->added_to_index = time();
             $insert->last_update = time();
             $insert->save(false);
         }
     }
     // delete not unseted urls from index
     foreach ($index as $deleteUrl => $deletePage) {
         $this->addLog('delete', $deleteUrl, $deletePage['title']);
         $model = Index::findOne($deletePage['id']);
         $model->delete(false);
     }
     // delete empty content empty title
     foreach (Index::find()->where(['=', 'content', ''])->orWhere(['=', 'title', ''])->all() as $page) {
         $this->addLog('delete_issue', $page->url, $page->title);
         $page->delete(false);
     }
 }