Exemplo n.º 1
0
 public function actionBuild()
 {
     $batch_data = [];
     $date_now = date("Y-m-d H:i:s");
     $this->echoLog("=========start build({$date_now})==========");
     /*build blog*/
     $post_list = Posts::find()->where(['status' => 1])->orderBy("id asc")->all();
     if ($post_list) {
         foreach ($post_list as $_post_info) {
             $tmp_search_key = $_post_info['title'] . "#@#" . $_post_info['tags'];
             $batch_data[] = ["title" => $_post_info['title'], "description" => strip_tags($_post_info['content']), "book_id" => 0, "post_id" => $_post_info['id'], "search_key" => $tmp_search_key, "image" => $_post_info['image_url']];
         }
     }
     $book_list = Book::find()->where(['status' => 1])->orderBy("id asc")->all();
     if ($book_list) {
         foreach ($book_list as $_book_info) {
             $tmp_search_key = $_book_info['subtitle'] . "#@#" . $_book_info['tags'];
             $batch_data[] = ["title" => $_book_info['subtitle'], "description" => $_book_info['summary'], "book_id" => $_book_info['id'], "post_id" => 0, "search_key" => $tmp_search_key, "image" => GlobalUrlService::buildPic1Static($_book_info['image_url'], ['w' => 600])];
         }
     }
     if ($batch_data && count($batch_data) > 0) {
         //批量插入速度快
         foreach ($batch_data as $_item) {
             if (!$_item['post_id'] && !$_item['book_id']) {
                 $this->echoLog("skip");
                 continue;
             }
             $query = IndexSearch::find();
             if ($_item['post_id']) {
                 $query->where(['post_id' => $_item['post_id']]);
             } else {
                 $query->where(['book_id' => $_item['book_id']]);
             }
             $tmp_info = $query->one();
             if ($tmp_info) {
                 $model_index_search = $tmp_info;
             } else {
                 $model_index_search = new IndexSearch();
                 $model_index_search->created_time = $date_now;
             }
             $tmp_description = str_replace(" ", " ", $_item['description']);
             $model_index_search->title = $_item['title'];
             $model_index_search->description = $tmp_description;
             $model_index_search->book_id = $_item['book_id'];
             $model_index_search->post_id = $_item['post_id'];
             $model_index_search->search_key = $_item['search_key'];
             $model_index_search->image = $_item['image'];
             $model_index_search->updated_time = $date_now;
             $model_index_search->save(0);
         }
     }
 }
Exemplo n.º 2
0
 public function actionDo()
 {
     $kw = trim($this->get("kw", ""));
     $p = intval($this->get("p", 1));
     if (!$p) {
         $p = 1;
     }
     $data = [];
     if (!$kw) {
         return $this->redirect("/");
     }
     $this->setTitle($kw);
     $pagesize = 10;
     $offset = ($p - 1) * $pagesize;
     $search_key = ['LIKE', 'search_key', '%' . strtr($kw, ['%' => '\\%', '_' => '\\_', '\\' => '\\\\']) . '%', false];
     $query = IndexSearch::find()->where($search_key);
     $total_count = $query->count();
     $list = $query->orderBy("id desc")->limit($pagesize)->offset($offset)->all();
     if ($list) {
         $book_mapping = DataHelper::getDicByRelateID($list, Book::className(), "book_id", "id", ["subtitle", "summary", "origin_image_url", "tags"]);
         $post_mapping = DataHelper::getDicByRelateID($list, Posts::className(), "post_id", "id", ["title", "content", "tags"]);
         foreach ($list as $_item) {
             if ($_item['book_id']) {
                 $tmp_target = $book_mapping[$_item['book_id']];
                 $tmp_content = mb_substr($tmp_target['summary'], 0, 105, "utf-8");
                 $tmp_title = DataHelper::encode($tmp_target['subtitle']);
                 $tmp_view_url = Url::toRoute("/library/detail/{$_item['book_id']}");
             } else {
                 $tmp_target = $post_mapping[$_item['post_id']];
                 $tmp_content = UtilHelper::blog_summary($tmp_target['content'], 105);
                 $tmp_title = DataHelper::encode($tmp_target['title']);
                 $tmp_view_url = Url::toRoute("/default/{$_item['post_id']}");
             }
             $tags = explode(",", $tmp_target['tags']);
             $data[] = ['title' => $tmp_title, 'content' => nl2br($tmp_content), 'tags' => $tags, 'date' => date("Y年m月d日"), 'view_url' => $tmp_view_url];
         }
     }
     $page_info = DataHelper::ipagination(["total_count" => $total_count, "page_size" => $pagesize, "page" => $p, "display" => 5]);
     return $this->render("result", ["data" => $data, "page_info" => $page_info, "urls" => ["page_base" => Url::toRoute(["/search/do", "kw" => $kw])]]);
 }
Exemplo n.º 3
0
 public function actionDo()
 {
     $data = [];
     $kw = $this->get("kw", "");
     if ($kw) {
         $this->setTitle($kw);
         $search_key = ['LIKE', 'search_key', '%' . strtr($kw, ['%' => '\\%', '_' => '\\_', '\\' => '\\\\']) . '%', false];
         $query = IndexSearch::find()->where($search_key);
         $list = $query->orderBy("id desc")->all();
         if ($list) {
             foreach ($list as $_item) {
                 if ($_item['book_id']) {
                     $tmp_title = DataHelper::encode($_item['title']);
                     $tmp_view_url = UrlService::buildWapUrl("/library/info", ['id' => $_item['book_id']]);
                 } else {
                     $tmp_title = DataHelper::encode($_item['title']);
                     $tmp_view_url = UrlService::buildWapUrl("/default/info", ['id' => $_item['post_id']]);
                 }
                 $data[] = ['title' => $tmp_title, 'content' => nl2br(UtilHelper::blog_short($_item['description'], 200)), 'image_url' => $_item['image'], 'view_url' => $tmp_view_url];
             }
         }
     }
     return $this->render("do", ["post_list" => $data, 'kw' => $kw]);
 }
Exemplo n.º 4
0
 private function getDataByKeyword($keyword, $fromUsername = '')
 {
     $search_key = ['LIKE', 'search_key', '%' . strtr($keyword, ['%' => '\\%', '_' => '\\_', '\\' => '\\\\']) . '%', false];
     $mixed_list = IndexSearch::find()->where($search_key)->orderBy("id desc")->limit(5)->all();
     $list = [];
     if ($mixed_list) {
         $domain_static = \Yii::$app->params['domains']['static'];
         foreach ($mixed_list as $_item) {
             $tmp_image = "{$domain_static}/wx/" . mt_rand(1, 7) . ".jpg";
             if ($_item['image']) {
                 $tmp_image = $_item['image'];
             }
             if ($_item['post_id']) {
                 $tmp_url = GlobalUrlService::buildWapUrl("/default/info", ['id' => $_item['post_id'], 'woid' => trim($fromUsername)]);
             } else {
                 $tmp_url = GlobalUrlService::buildWapUrl("/library/info", ['id' => $_item['book_id'], 'woid' => trim($fromUsername)]);
             }
             $list[] = ["title" => $_item['title'], "description" => $_item['title'], "picurl" => $tmp_image, "url" => $tmp_url];
         }
     }
     $data = $list ? $this->getRichXml($list) : $this->help();
     $type = $list ? "rich" : "text";
     return ['type' => $type, "data" => $data];
 }