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; }
/** * 每天统计一次就够了 * 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); }
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); } } } }
public function actionDaily() { $post_list = Posts::findAll(['status' => 1]); foreach ($post_list as $_post_info) { RecommendService::calculateRecommend($_post_info['id']); } }
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); } } }
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; }
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); } } }
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]); }
public static function buildTags($post_id) { $post_info = Posts::findOne(['id' => $post_id]); if (!$post_info) { return; } $tags_arr = $post_info['tags'] ? explode(",", $post_info['tags']) : []; foreach ($tags_arr as $_tag) { $has_in = PostsTags::findOne(['posts_id' => $post_id, "tag" => $_tag]); if ($has_in) { continue; } $model_posts_tag = new PostsTags(); $model_posts_tag->posts_id = $post_id; $model_posts_tag->tag = $_tag; $model_posts_tag->save(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)]); }
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; }
public function actionOps($id) { $id = intval($id); $act = trim($this->post("act", "online", "down-hot", "go-hot")); if (!$id) { return $this->renderJSON([], "操作的博文可能不是你的吧!!", -1); } $post_info = Posts::findOne(["id" => $id, 'uid' => [0, $this->current_user->uid]]); if (!$post_info) { return $this->renderJSON([], "操作的博文可能不是你的吧!!", -1); } switch ($act) { case "del": $post_info->status = 0; break; case "online": $post_info->status = 1; BlogService::buildTags($id); break; case "go-hot": $post_info->hot = 1; break; case "down-hot": $post_info->hot = 0; } $post_info->updated_time = date("Y-m-d H:i:s"); $post_info->update(0); switch ($act) { case "del": $this->afterDel($id); break; case "online": $this->afterOnline($id); break; } return $this->renderJSON([], "操作成功!!"); }
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]; }
public static function doSync($type, $blog_id) { $charset = "utf-8"; $catlog = [1]; $limit_title_len = 0; //不限制 switch ($type) { case "51cto": $catlog = ["【创作类型:原创】", "开发技术"]; $charset = "gb2312"; break; case "csdn": $catlog = []; //return self::_err("目前还处理不了这个类型!!"); break; case "sina": break; case "163": break; case "oschina": break; case "cnblogs": break; case "chinaunix": $catlog = ["Web开发"]; $limit_title_len = 30; break; default: return self::_err("指定的类型无法处理!!"); break; } $metaweblog_blog_config = \Yii::$app->params['metaweblog']; if (!isset($metaweblog_blog_config[$type])) { return self::_err("指定的类型无法处理!!"); } $metaweblog_config = $metaweblog_blog_config[$type]; $blog_info = Posts::findOne(['id' => $blog_id, "status" => 1]); if (!$blog_info) { return self::_err("博客不存在哦!!"); } $domain_blog = \Yii::$app->params['domains']['blog']; $link = $domain_blog . "/default/{$blog_id}.html?source={$type}"; $content = $blog_info['content']; $title = $blog_info['title']; if ($limit_title_len) { //标题限制长度 $title = mb_substr($title, 0, $limit_title_len, "utf-8"); } $content .= "<br/>原文地址:<a href=\"{$link}\">{$blog_info['title']}</a>"; /*相关标签*/ if ($blog_info['tags']) { $content .= "<br/>标签:"; $tmp_tags = explode(",", $blog_info['tags']); foreach ($tmp_tags as $_tmp_tag) { $tmp_link = $domain_blog . "/search/do?kw={$_tmp_tag}&source_id={$blog_id}&source={$type}"; $content .= "<a href='{$tmp_link}'>{$_tmp_tag}</a> "; } } /*加入推荐*/ $recommend_blogs = RecommendService::getRecommendBlog($blog_id); if ($recommend_blogs) { $content .= "<br/><br/><h3>智能推荐</h3><ul>"; foreach ($recommend_blogs as $_recommend_blog) { $tmp_link = $domain_blog . "/default/{$_recommend_blog['id']}.html?flag=recommend&source={$type}&source_id=" . $_recommend_blog['source_id']; $content .= "<li><a href='{$tmp_link}'>{$_recommend_blog["title"]}</a></li>"; } $content .= "</ul>"; } $content = htmlspecialchars($content); $title = htmlspecialchars($title); if ($charset != "utf-8") { $content = mb_convert_encoding($content, $charset, "utf-8"); $title = mb_convert_encoding($title, $charset, "utf-8"); } $params = ['title' => $title, 'description' => $content, 'categories' => $catlog]; $target = new MetaWeblog($metaweblog_config['url'], $charset); $target->setAuth($metaweblog_config['username'], $metaweblog_config['passwd']); $date_now = date("Y-m-d H:i:s"); $sync_info = BlogSyncMapping::findOne(['blog_id' => $blog_id]); $is_edit = false; if ($sync_info && $sync_info[self::$type_mapping[$type]]) { if (!$target->editPost($sync_info[self::$type_mapping[$type]], $params)) { return self::_err($target->getErrorCode() . ":" . $target->getErrorMessage() . " xml data:" . $target->getResponse()); } $model_blog_sync_mapping = $sync_info; $is_edit = true; } else { if (!$target->newPost($params, $type == "csdn")) { return self::_err($target->getErrorCode() . ":" . $target->getErrorMessage() . " xml data:" . $target->getResponse()); } if ($sync_info) { $model_blog_sync_mapping = $sync_info; } else { $model_blog_sync_mapping = new BlogSyncMapping(); $model_blog_sync_mapping->blog_id = $blog_id; $model_blog_sync_mapping->created_time = $date_now; } } $sync_blog_id = $target->getBlogId(); if (!$is_edit) { $model_blog_sync_mapping[self::$type_mapping[$type]] = $sync_blog_id; } $model_blog_sync_mapping->updated_time = $date_now; if (!$model_blog_sync_mapping->save(0)) { return false; } return $target->getResponse(); }
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"; }
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); }
public function actionAdd() { $referer = trim($this->get("referer", "")); $screen = trim($this->get("screen", "")); $target_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ""; if ($target_url) { $blog_id = 0; preg_match("/\\/default\\/(\\d+)(.html)?/", $target_url, $matches); if ($matches && count($matches) >= 2) { $blog_id = $matches[1]; } $tmp_source = 'direct'; if ($referer) { $tmp_source = parse_url($referer, PHP_URL_HOST); if (stripos($tmp_source, "www.google.") !== false) { $tmp_source = "www.google.com"; } } $uuid = $this->getUUID(); $uuid = ltrim($uuid, "{"); $uuid = rtrim($uuid, "}"); $model_ac_log = new AccessLogs(); $model_ac_log->referer = $referer; $model_ac_log->target_url = $target_url; $model_ac_log->blog_id = $blog_id; $model_ac_log->source = $tmp_source ? $tmp_source : ''; $model_ac_log->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ""; if ($model_ac_log->user_agent) { $tmp_browser = new Browser($model_ac_log->user_agent); $tmp_os = new Os($model_ac_log->user_agent); $tmp_device = new Device($model_ac_log->user_agent); $model_ac_log->client_browser = $tmp_browser->getName() ? $tmp_browser->getName() : ''; $model_ac_log->client_browser_version = $tmp_browser->getVersion() ? $tmp_browser->getVersion() : ''; $model_ac_log->client_os = $tmp_os->getName() ? $tmp_os->getName() : ''; $model_ac_log->client_os_version = $tmp_os->getVersion() ? $tmp_os->getVersion() : ''; $model_ac_log->client_device = $tmp_device->getName() ? $tmp_device->getName() : ''; if ($model_ac_log->client_device == "unknown" && UtilHelper::isPC()) { $model_ac_log->client_device = "pc"; } } $model_ac_log->ip = UtilHelper::getClientIP(); $model_ac_log->uuid = $uuid; if ($screen) { list($client_width, $client_height) = explode("/", $screen); if ($client_width) { $model_ac_log->client_width = $client_width; } if ($client_height) { $model_ac_log->client_height = $client_height; } } $model_ac_log->created_time_min = date("Y-m-d H:i"); $model_ac_log->created_time = date("Y-m-d H:i:s"); $model_ac_log->save(); /*更新文章阅读量*/ if ($blog_id) { $blog_info = Posts::findOne(['id' => $blog_id]); if ($blog_info) { $blog_info->view_count += 1; $blog_info->update(0); } } } }
private function save2blog($content, $title, $url) { if (!$content || !$title) { return false; } $content .= "<p>Robot抓取来源:<a href='{$url}' target='_blank'>去原网站</a></a>"; $model_post = new Posts(); $model_post->uid = 0; $model_post->title = $title; $model_post->type = 1; $model_post->status = 0; //先审核后在展示 $model_post->content = $content; $tags = FenCiService::getTags($content); $model_post->tags = implode(",", $tags); $model_post->updated_time = date("Y-m-d H:i:s"); $model_post->created_time = $model_post->updated_time; if ($model_post->save(0)) { //BlogService::buildTags($model_post->id);//隐藏的不需要tag return $model_post->id; } return false; }