Ejemplo n.º 1
0
 /**
  * 每天统计一次就够了
  * 10 3 * * *
  */
 public function actionBlog($date = '')
 {
     $date = $date ? $date : date("Y-m-d", strtotime("-1 day"));
     $date_now = date("Y-m-d H:i:s");
     if (!preg_match("/^\\d{4}-\\d{2}-\\d{2}\$/", $date)) {
         return $this->echoLog("date{$date} is illegal!!");
     }
     $this->echoLog("stat_blog date is {$date}");
     $info = StatBlog::findOne(['date' => $date]);
     if ($info) {
         $model_stat_blog = $info;
     } else {
         $model_stat_blog = new StatBlog();
         $model_stat_blog->date = $date;
         $model_stat_blog->created_time = $date_now;
     }
     /*统计已发布文章数量,未发布文章数量,原创文章数量,热门文章数量*/
     $stat_status = Posts::find()->select(['status', 'count(*) as num'])->groupBy("status")->asArray()->all();
     $stat_status = array_column($stat_status, null, "status");
     $model_stat_blog->total_post_number = $stat_status && isset($stat_status[1]) ? $stat_status[1]['num'] : 0;
     $model_stat_blog->total_unpost_number = $stat_status && isset($stat_status[0]) ? $stat_status[0]['num'] : 0;
     $stat_hot = Posts::find()->where(['hot' => 1])->count();
     $model_stat_blog->total_hot_number = $stat_hot ? $stat_hot : 0;
     $stat_original = Posts::find()->where(['original' => 1])->count();
     $model_stat_blog->total_original_number = $stat_original ? $stat_original : 0;
     $stat_today = Posts::find()->where(['>=', 'created_time', date("Y-m-d 00:00:00", strtotime($date))])->andWhere(['<=', 'created_time', date("Y-m-d 23:59:59", strtotime($date))])->count();
     $model_stat_blog->today_post_number = $stat_today ? $stat_today : 0;
     $model_stat_blog->updated_time = $date_now;
     $model_stat_blog->save(0);
 }
Ejemplo n.º 2
0
 public function actionUse_pic1()
 {
     $post_list = Posts::find()->where(['status' => 1])->orderBy("id desc")->all();
     if ($post_list) {
         foreach ($post_list as $_post_info) {
             $this->echoLog("post_id:{$_post_info['id']}");
             $tmp_content = $_post_info['content'];
             preg_match_all('/<\\s*img\\s+[^>]*?src\\s*=\\s*(\'|\\")(.*?)\\1[^>]*?\\/?\\s*>/i', $tmp_content, $match_img);
             if ($match_img && count($match_img) == 3) {
                 foreach ($match_img[2] as $_img_src) {
                     if (stripos($_img_src, \Yii::$app->params['domains']['pic1']) !== false) {
                         continue;
                     }
                     if (stripos($_img_src, "pic1.vincentguo.cn") !== false) {
                         $tmp_content = str_replace("http://pic1.vincentguo.cn", \Yii::$app->params['domains']['pic1'], $tmp_content);
                         continue;
                     }
                     $full_img_url = $this->downImg($_img_src) . "?format=/w/300";
                     $tmp_content = str_replace($_img_src, $full_img_url, $tmp_content);
                 }
                 $_post_info->content = $tmp_content;
                 $_post_info->update(0);
             }
         }
     }
 }
Ejemplo n.º 3
0
 private function search($params = [])
 {
     $p = isset($params['p']) ? $params['p'] : 1;
     $type = isset($params['type']) ? $params['type'] : 1;
     $offset = ($p - 1) * $this->page_size;
     $query = Posts::find()->where(['status' => 1]);
     switch ($type) {
         case 2:
             $query->orderBy(['view_count' => SORT_DESC]);
             break;
         case 3:
             $query->andWhere(['original' => 1]);
             $query->orderBy(['id' => SORT_DESC]);
             break;
         default:
             $query->orderBy(['id' => SORT_DESC]);
             break;
     }
     $posts_info = $query->offset($offset)->limit($this->page_size)->all();
     $data = [];
     if ($posts_info) {
         foreach ($posts_info as $_post) {
             $tmp_tags = explode(",", $_post['tags']);
             $data[] = ['title' => DataHelper::encode($_post['title']), 'content' => nl2br(UtilHelper::blog_short($_post['content'], 200)), "tags" => $tmp_tags, 'image_url' => $_post['image_url'], 'view_url' => UrlService::buildWapUrl("/default/info", ["id" => $_post['id']])];
         }
     }
     return $data;
 }
Ejemplo n.º 4
0
 public function actionRepair()
 {
     $post_list = Posts::find()->where(['status' => 1])->orderBy("id asc")->all();
     if ($post_list) {
         foreach ($post_list as $_post_info) {
             preg_match('/<\\s*img\\s+[^>]*?src\\s*=\\s*(\'|\\")(.*?)\\1[^>]*?\\/?\\s*>/i', $_post_info['content'], $match_img);
             if ($match_img && count($match_img) == 3) {
                 $_post_info->image_url = $match_img[2];
             } else {
                 $_post_info->image_url = "";
             }
             $_post_info->update(0);
         }
     }
 }
Ejemplo n.º 5
0
 public function actionInfo()
 {
     $id = intval($this->post("id", 0));
     if (!$id) {
         return $this->renderJSON([], "指定博文不存在", -1);
     }
     $post_info = Posts::find()->where(['status' => 1, 'id' => $id])->one();
     if (!$post_info) {
         return $this->renderJSON([], "指定博文不存在", -1);
     }
     $tmp_tags = explode(",", $post_info['tags']);
     $content = preg_replace("/brush:(\\w+);toolbar:false/", "prettyprint linenums", $post_info['content']);
     $info = ['author' => ['name' => DataHelper::getAuthorName()], 'title' => $post_info['title'], 'content' => $content, "tags" => $tmp_tags, 'updated_time' => date("Y-m-d H:i", strtotime($post_info['updated_time']))];
     $share_info = ['title' => $post_info['title'], 'content' => UtilHelper::blog_short($post_info['content'], 200), 'url' => GlobalUrlService::buildWapUrl("/default/info", ['id' => $post_info['id']])];
     return $this->renderJSON(['info' => $info, 'share_info' => $share_info]);
 }
Ejemplo n.º 6
0
 public function actionInit()
 {
     $date_now = date("Y-m-d H:i:s");
     $posts_list = Posts::find()->where(['status' => 1])->orderBy("id asc")->all();
     foreach ($posts_list as $_item) {
         foreach (SyncBlogService::$type_mapping as $_type => $_f) {
             $model_blog_sync_queue = new BlogSyncQueue();
             $model_blog_sync_queue->blog_id = $_item['id'];
             $model_blog_sync_queue->type = $_type;
             $model_blog_sync_queue->updated_time = $date_now;
             $model_blog_sync_queue->created_time = $date_now;
             $model_blog_sync_queue->status = -1;
             $model_blog_sync_queue->save(0);
         }
     }
 }
Ejemplo n.º 7
0
 public static function calculateRecommend($blog_id)
 {
     $post_info = Posts::findOne(['id' => $blog_id, "status" => 1]);
     if (!$post_info) {
         return self::_err("post:{$blog_id} not found");
     }
     $post_list = Posts::find()->where(['status' => 1])->andWhere(['!=', "id", $blog_id])->orderBy("id asc")->all();
     foreach ($post_list as $_relate_post_info) {
         similar_text(strip_tags($post_info['content']), strip_tags($_relate_post_info['content']), $tmp_content_percent);
         similar_text($post_info['title'], $_relate_post_info['title'], $tmp_title_percent);
         similar_text($post_info['tags'], $_relate_post_info['tags'], $tmp_tags_percent);
         $params = ["title_rate" => $tmp_title_percent, "content_rate" => $tmp_content_percent, "tags_rate" => $tmp_tags_percent];
         self::setRecommend($post_info['id'], $_relate_post_info['id'], $params);
     }
     return true;
 }
Ejemplo n.º 8
0
 public function actionInfo($id)
 {
     $id = intval($id);
     if (!$id) {
         return $this->goHome();
     }
     $post_info = Posts::findOne(['id' => $id, 'status' => 1]);
     if (!$post_info) {
         return $this->goHome();
     }
     $author = Yii::$app->params['author'];
     $tags = explode(",", $post_info['tags']);
     $content = preg_replace("/brush:(\\w+);toolbar:false/", "prettyprint linenums", $post_info['content']);
     $content = str_replace("<pre>", "<pre class='prettyprint linenums'>", $content);
     $data = ["id" => $post_info['id'], "title" => $post_info['title'], "content" => $content, "original" => $post_info['original'], 'view_count' => $post_info['view_count'], "tags" => $tags, 'date' => date("Y.m.d", strtotime($post_info['updated_time'])), 'author' => $author, "url" => UrlService::buildUrl("/default/info", ["id" => $post_info['id']])];
     $prev_info = Posts::find()->where(["<", "id", $id])->andWhere(['status' => 1])->orderBy("id desc")->one();
     $next_info = Posts::find()->where([">", "id", $id])->andWhere(['status' => 1])->orderBy("id asc")->one();
     $this->setTitle($post_info['title']);
     return $this->render("detail", ["info" => $data, "prev_info" => $prev_info, "next_info" => $next_info, "recommend_blogs" => RecommendService::getRecommendBlog($id)]);
 }
Ejemplo n.º 9
0
 public function actionIndex()
 {
     $p = intval($this->get("p", 1));
     $status = intval($this->get("status", -99));
     $order_by = $this->get("order_by", '');
     $kw = trim($this->get("kw", ''));
     if (!$p) {
         $p = 1;
     }
     $data = [];
     $query = Posts::find();
     if ($status >= -2) {
         $query->andWhere(['status' => $status]);
     }
     if ($kw) {
         $query->andWhere(['LIKE', 'title', '%' . strtr($kw, ['%' => '\\%', '_' => '\\_', '\\' => '\\\\']) . '%', false]);
     }
     $total_count = $query->count();
     if ($order_by) {
         $query->orderBy([$order_by => $this->get($order_by) ? SORT_DESC : SORT_ASC]);
     } else {
         $query->orderBy(['id' => SORT_DESC]);
     }
     $offset = ($p - 1) * $this->page_size;
     $posts_info = $query->offset($offset)->limit($this->page_size)->asArray()->all();
     $page_info = DataHelper::ipagination(["total_count" => $total_count, "page_size" => $this->page_size, "page" => $p, "display" => 10]);
     if ($posts_info) {
         $idx = 1;
         $domains = Yii::$app->params['domains'];
         foreach ($posts_info as $_post) {
             $tmp_title = $_post['title'];
             if (mb_strlen($tmp_title, "utf-8") > 30) {
                 $tmp_title = mb_substr($_post['title'], 0, 30, 'utf-8') . "...";
             }
             $data[] = ['idx' => $idx, 'id' => $_post['id'], 'title' => DataHelper::encode($tmp_title), 'status' => $_post['status'], 'hot' => $_post['hot'], 'view_count' => $_post['view_count'], 'status_info' => Constant::$status_desc[$_post['status']], "original_info" => Constant::$original_desc[$_post['original']], "hot_info" => Constant::$hot_desc[$_post['hot']], 'created' => $_post['created_time'], 'edit_url' => Url::toRoute("/posts/set?id={$_post['id']}"), 'view_url' => $domains['blog'] . Url::toRoute("/default/{$_post['id']}")];
             $idx++;
         }
     }
     $search_conditions = ['kw' => $kw, 'status' => $status, 'order_by' => $order_by, $order_by => $this->get($order_by)];
     return $this->render("index", ["data" => $data, "page_info" => $page_info, "search_conditions" => $search_conditions, 'status_mapping' => Constant::$status_desc]);
 }
Ejemplo n.º 10
0
 public static function buildFront($refresh = false)
 {
     $cache = new FileCache();
     $cache_key = "tag_post";
     $root_path = UtilHelper::getRootPath();
     $cache->cachePath = $root_path . '/common/logs/cache';
     $data = $cache[$cache_key];
     if (!$data || $refresh) {
         $data = ["tag" => [], "post_hot" => [], "post_origin" => [], "post_latest" => []];
         $tags = PostsTags::find()->select("tag,count(*) as num ")->groupBy("tag")->orderBy("num desc")->limit(20)->all();
         if ($tags) {
             foreach ($tags as $_tag) {
                 $data['tag'][] = $_tag['tag'];
             }
         }
         $post_hot = Posts::find()->where(['status' => 1])->orderBy(['view_count' => SORT_DESC])->limit(10)->all();
         if ($post_hot) {
             foreach ($post_hot as $_post_info) {
                 $data['post_hot'][] = ["id" => $_post_info['id'], "title" => DataHelper::encode($_post_info["title"]), "detail_url" => UrlService::buildUrl("/default/info", ["id" => $_post_info['id']])];
             }
         }
         $post_origin = Posts::find()->where(['status' => 1, 'original' => 1])->orderBy(['id' => SORT_DESC])->limit(10)->all();
         if ($post_origin) {
             foreach ($post_origin as $_post_info) {
                 $data['post_origin'][] = ["id" => $_post_info['id'], "title" => DataHelper::encode($_post_info["title"]), "detail_url" => UrlService::buildUrl("/default/info", ["id" => $_post_info['id']])];
             }
         }
         $post_latest = Posts::find()->where(['status' => 1])->orderBy(['id' => SORT_DESC])->limit(10)->all();
         if ($post_latest) {
             foreach ($post_latest as $_post_info) {
                 $data['post_latest'][] = ["id" => $_post_info['id'], "title" => DataHelper::encode($_post_info["title"]), "detail_url" => UrlService::buildUrl("/default/info", ["id" => $_post_info['id']])];
             }
         }
         $data = json_encode($data);
         $cache[$cache_key] = $data;
     }
     return $data;
 }
Ejemplo n.º 11
0
 public function actionTopSearch()
 {
     $data = [];
     $kw = trim($this->get("q", ''));
     $query = Posts::find();
     $query->andWhere(['LIKE', 'title', '%' . strtr($kw, ['%' => '\\%', '_' => '\\_', '\\' => '\\\\']) . '%', false]);
     $list = $query->orderBy(['view_count' => SORT_DESC])->asArray()->limit(10)->all();
     if ($list) {
         foreach ($list as $_item) {
             $data[] = ['id' => $_item['id'], 'title' => DataHelper::encode($_item['title']), 'url' => AdminUrlService::buildUrl("/posts/set", ['id' => $_item['id']])];
         }
     }
     return $this->renderJSON($data);
 }
Ejemplo n.º 12
0
 public function actionSitemap()
 {
     $data = [];
     $tags = [];
     $type = $this->get("type", "blog");
     switch ($type) {
         case "m":
             $domain_host = Yii::$app->params['domains']['m'];
             $sitemap_filename = "m_sitemap.xml";
             break;
         default:
             $domain_host = Yii::$app->params['domains']['blog'];
             $sitemap_filename = "sitemap.xml";
             break;
     }
     $index_urls = ['m' => ["/default/index?type=1", "/default/index?type=2", "/default/index?type=3", "/library/index", "/richmedia/index", "/my/about", "http://www.vincentguo.cn"], 'blog' => ["/default/index?type=1", "/default/index?type=2", "/default/index?type=3", "/library/index", "/richmedia/index", "/default/donation", "/default/about", "http://m.vincentguo.cn"]];
     if (isset($index_urls[$type])) {
         foreach ($index_urls[$type] as $_index_url) {
             if (preg_match("/^http/", $_index_url)) {
                 $tmp_url = $_index_url;
             } else {
                 if ($type == "m") {
                     $tmp_url = UrlService::buildWapUrl($_index_url);
                 } else {
                     $tmp_url = UrlService::buildUrl($_index_url);
                 }
             }
             $data[] = ["loc" => $tmp_url, "priority" => 1.0, "lastmod" => date("Y-m-d"), "changefreq" => "daily"];
         }
     }
     $post_list = Posts::find()->where(["status" => 1])->orderBy("id desc")->all();
     if ($post_list) {
         foreach ($post_list as $_post_info) {
             if ($type == "m") {
                 $tmp_url = UrlService::buildWapUrl("/default/info", ["id" => $_post_info['id']]);
             } else {
                 $tmp_url = UrlService::buildUrl("/default/info", ["id" => $_post_info['id']]);
             }
             $data[] = ["loc" => $tmp_url, "priority" => 1.0, "lastmod" => date("Y-m-d", strtotime($_post_info['updated_time'])), "changefreq" => "daily"];
             $tmp_tags = explode(",", $_post_info['tags']);
             $tags = array_merge($tags, $tmp_tags);
         }
     }
     $book_list = Book::find()->where(['status' => 1])->orderBy("id desc")->all();
     if ($book_list) {
         foreach ($book_list as $_book_info) {
             if ($type == "m") {
                 $tmp_url = UrlService::buildWapUrl("/library/info", ["id" => $_book_info['id']]);
             } else {
                 $tmp_url = UrlService::buildUrl("/library/info", ["id" => $_book_info['id']]);
             }
             $data[] = ["loc" => $tmp_url, "priority" => 1.0, "lastmod" => date("Y-m-d", strtotime($_book_info['updated_time'])), "changefreq" => "daily"];
             $tmp_tags = explode(",", $_book_info['tags']);
             $tags = array_merge($tags, $tmp_tags);
         }
     }
     $tags = array_unique($tags);
     if ($tags) {
         foreach ($tags as $_tag) {
             if ($type == "m") {
                 $tmp_url = UrlService::buildWapUrl("/search/do", ["kw" => $_tag]);
             } else {
                 $tmp_url = UrlService::buildUrl("/search/do", ["kw" => $_tag]);
             }
             $data[] = ["loc" => $tmp_url, "priority" => 1.0, "lastmod" => date("Y-m-d", time()), "changefreq" => "daily"];
         }
     }
     $xml_content = $this->renderPartial("sitemap", ["data" => $data]);
     $app_root = Yii::$app->getBasePath();
     $file_path = $app_root . "/web/{$sitemap_filename}";
     file_put_contents($file_path, $xml_content);
     $this->layout = false;
     return "ok";
 }
Ejemplo n.º 13
0
 private function getOriginalBlog()
 {
     $post_list = Posts::find()->where(['original' => 1, 'status' => 1])->orderBy("updated_time desc")->limit(5)->all();
     $list = [];
     if ($post_list) {
         $domain_static = \Yii::$app->params['domains']['static'];
         foreach ($post_list as $_item) {
             $tmp_image = "{$domain_static}/wx/" . mt_rand(1, 7) . ".jpg";
             if ($_item['image_url']) {
                 $tmp_image = $_item['image_url'];
             }
             $list[] = ["title" => $_item['title'], "description" => $_item['title'], "picurl" => $tmp_image, "url" => GlobalUrlService::buildWapUrl("/default/info", ['id' => $_item['id']])];
         }
     }
     $data = $list ? $this->getRichXml($list) : $this->help();
     $type = $list ? "rich" : "text";
     return ['type' => $type, "data" => $data];
 }