Skip to content

Commit 322bb4e

Browse files
NHPTNHPT
andauthored
修复:生成规范文章链接并过滤低内容标签 (#214)
Co-authored-by: NHPT <yxiye@qq.com>
1 parent 7cbe1ed commit 322bb4e

4 files changed

Lines changed: 44 additions & 36 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
[RoutesHelper](RoutesHelper) | Typecho路由信息显示修改 | 1.0.3 | [doudou](https://github.com/doudoutime) | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-M_to_R/RoutesHelper.zip)
126126
[SCS](SCS) | 系统附件用新浪云存储SCS | 1.1.1 | [vfhky](https://github.com/vfhky) | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-S_to_Z/SCS.zip)
127127
[Sinauth](Sinauth) | 新浪用户授权注册帐号登录 | 1.0.0 Beta | [jimmy chaw](https://github.com/web3d) | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-S_to_Z/Sinauth.zip)
128-
[Sitemap](Sitemap) | 动态生成Xml标准网站地图 | 1.0.5 | [迷你日志](https://minirizhi.com), [Hanny](http://www.imhan.com) | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-S_to_Z/Sitemap.zip)
128+
[Sitemap](Sitemap) | 动态生成Xml标准网站地图 | 1.0.6 | [迷你日志](https://minirizhi.com), [Hanny](http://www.imhan.com) | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-S_to_Z/Sitemap.zip)
129129
[SlantedExtend](SlantedExtend) | [Slanted主题](https://github.com/typecho-fans/themes/blob/master/Slanted)专用自定义字段 | 1.0.0 | [DT27](https://github.com/DT27) | [Special](https://github.com/typecho-fans/plugins/releases/download/plugins-S_to_Z/SlantedExtend.zip)
130130
[SlimBox2](SlimBox2) | 轻量级jQuery图片灯箱弹窗 | 1.0.7 | [Ryan](https://github.com/ryanfwy), [冰剑](https://github.com/binjoo) | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-S_to_Z/SlimBox2.zip)
131131
[Smilies](Smilies) | Typecho定制图片表情功能 | 1.1.3 | [羽中](https://github.com/jzwalk), Willin Kan | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-S_to_Z/Smilies.zip)

Sitemap/Action.php

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,22 @@ public function action()
1414
->where('table.contents.type = ?', 'page')
1515
->order('table.contents.created', Typecho_Db::SORT_DESC));
1616

17-
$articles = $db->fetchAll($db->select()->from('table.contents')
17+
$articleCount = $db->fetchObject($db->select(array('COUNT(table.contents.cid)' => 'num'))->from('table.contents')
1818
->where('table.contents.status = ?', 'publish')
19-
->where('table.contents.created < ?', $options->gmtTime)
20-
->where('table.contents.type = ?', 'post')
21-
->order('table.contents.created', Typecho_Db::SORT_DESC));
19+
->where('table.contents.created < ?', $options->time)
20+
->where('table.contents.type = ?', 'post'))->num;
21+
22+
Typecho_Widget::widget(
23+
'Widget_Contents_Post_Recent@sitemapArticles',
24+
'pageSize=' . max(1, (int) $articleCount)
25+
)->to($articles);
2226

2327
Typecho_Widget::widget('Widget_Metas_Category_List@cate')->to($cates);
24-
2528

2629
$tags = $db->fetchAll($db->select()->from('table.metas')
2730
->where('table.metas.type = ?', 'tag')
2831
->order('table.metas.mid', Typecho_Db::SORT_DESC));
32+
$minTagCount = max(1, (int) $options->plugin('Sitemap')->minTagCount);
2933

3034
header("Content-Type: application/xml");
3135
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
@@ -49,26 +53,10 @@ public function action()
4953
echo "\t\t<priority>0.6</priority>\n";
5054
echo "\t</url>\n";
5155
}
52-
foreach($articles AS $article) {
53-
$type = $article['type'];
54-
$article['categories'] = $db->fetchAll($db->select()->from('table.metas')
55-
->join('table.relationships', 'table.relationships.mid = table.metas.mid')
56-
->where('table.relationships.cid = ?', $article['cid'])
57-
->where('table.metas.type = ?', 'category')
58-
->order('table.metas.order', Typecho_Db::SORT_ASC));
59-
$article['category'] = urlencode(current(Typecho_Common::arrayFlatten($article['categories'], 'slug')));
60-
$article['slug'] = urlencode($article['slug']);
61-
$article['date'] = new Typecho_Date($article['created']);
62-
$article['year'] = $article['date']->year;
63-
$article['month'] = $article['date']->month;
64-
$article['day'] = $article['date']->day;
65-
$routeExists = (NULL != Typecho_Router::get($type));
66-
$article['pathinfo'] = $routeExists ? Typecho_Router::url($type, $article) : '#';
67-
$article['permalink'] = Typecho_Common::url($article['pathinfo'], $options->index);
68-
56+
while($articles->next()) {
6957
echo "\t<url>\n";
70-
echo "\t\t<loc>".$article['permalink']."</loc>\n";
71-
echo "\t\t<lastmod>".date('Y-m-d',$article['modified'])."</lastmod>\n";
58+
echo "\t\t<loc>".$articles->permalink."</loc>\n";
59+
echo "\t\t<lastmod>".date('Y-m-d',$articles->modified)."</lastmod>\n";
7260
echo "\t\t<changefreq>always</changefreq>\n";
7361
echo "\t\t<priority>0.8</priority>\n";
7462
echo "\t</url>\n";
@@ -91,22 +79,24 @@ public function action()
9179
}
9280
foreach($tags AS $tag) {
9381
$type = $tag['type'];
94-
$art_rt = $db->fetchRow($db->select()->from('table.contents')
82+
$tagArticles = $db->fetchAll($db->select('table.contents.modified')->from('table.contents')
9583
->join('table.relationships', 'table.contents.cid = table.relationships.cid')
9684
->where('table.contents.status = ?', 'publish')
85+
->where('table.contents.created < ?', $options->time)
86+
->where('table.contents.type = ?', 'post')
9787
->where('table.relationships.mid = ?', $tag['mid'])
98-
->order('table.relationships.cid', Typecho_Db::SORT_DESC)
99-
->limit(1));
100-
// 文章的标签跳过
101-
if (empty($art_rt['modified'])) continue;
88+
->order('table.contents.modified', Typecho_Db::SORT_DESC)
89+
->limit($minTagCount));
90+
if (count($tagArticles) < $minTagCount) continue;
91+
$latestTagArticle = current($tagArticles);
10292

10393
$routeExists = (NULL != Typecho_Router::get($type));
10494
$tag['pathinfo'] = $routeExists ? Typecho_Router::url($type, $tag) : '#';
10595
$tag['permalink'] = Typecho_Common::url($tag['pathinfo'], $options->index);
10696

10797
echo "\t<url>\n";
10898
echo "\t\t<loc>".$tag['permalink']."</loc>\n";
109-
echo "\t\t<lastmod>".date('Y-m-d',$art_rt['modified'])."</lastmod>\n";
99+
echo "\t\t<lastmod>".date('Y-m-d',$latestTagArticle['modified'])."</lastmod>\n";
110100
echo "\t\t<changefreq>daily</changefreq>\n";
111101
echo "\t\t<priority>0.5</priority>\n";
112102
echo "\t</url>\n";

Sitemap/Plugin.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
*
77
* @package Sitemap
88
* @author 迷你日志, Hanny
9-
* @version 1.0.5
9+
* @version 1.0.6
1010
* @dependence 9.9.2-*
1111
* @link https://github.com/typecho-fans/plugins/blob/master/Sitemap
1212
*
1313
*
14-
* varsion 1.0.5 at 2022--9-10 by 泽泽社长
14+
* version 1.0.6 at 2026-08-15 by NHPT
15+
* 使用 Typecho 内容组件生成文章永久链接
16+
* 支持按标签文章数量过滤 Sitemap 中的低内容标签页
17+
*
18+
* version 1.0.5 at 2022-09-10 by 泽泽社长
1519
* 分类链接支持{directory}多级分类
1620
*
1721
* version 1.0.4 at 2020-07-02 by Typecho Fans (合并多人修改)
@@ -62,7 +66,17 @@ public static function deactivate()
6266
* @param Typecho_Widget_Helper_Form $form 配置面板
6367
* @return void
6468
*/
65-
public static function config(Typecho_Widget_Helper_Form $form){}
69+
public static function config(Typecho_Widget_Helper_Form $form)
70+
{
71+
$minTagCount = new Typecho_Widget_Helper_Form_Element_Text(
72+
'minTagCount',
73+
NULL,
74+
'1',
75+
_t('标签最少文章数'),
76+
_t('仅输出文章数达到该值的标签页,默认 1 表示保留全部非空标签;建议设置为 2 或更高')
77+
);
78+
$form->addInput($minTagCount);
79+
}
6680

6781
/**
6882
* 个人用户的配置面板

Sitemap/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<img src="https://typecho-fans.github.io/text-logo.svg" alt="TF Logo" title="Typecho Fans开源作品社区" align="right" height="100" />
33
</a>
44

5-
Sitemap v1.0.5 - 社区维护版
5+
Sitemap v1.0.6 - 社区维护版
66
======================
77
<h4 align="center">—— 动态生成符合搜索引擎收录标准的Xml格式站点地图插件,支持输出分类/标签页地址。</h4>
88

@@ -27,14 +27,18 @@ Sitemap v1.0.5 - 社区维护版
2727
**使用方法**
2828
##### 1. 下载本插件,放在 `usr/plugins/` 目录中,确保文件夹名为 Sitemap;
2929
##### 2. 激活插件;
30-
##### 3. 访问http://[example].com/sitemap.xml即可看到页面效果。
30+
##### 3. 可在插件设置中配置“标签最少文章数”,设置为 `2` 可排除只有一篇文章的低内容标签页;
31+
##### 4. 访问http://[example].com/sitemap.xml即可看到页面效果。
3132

3233
</td>
3334
</tr>
3435
</table>
3536

3637
## 版本历史
3738

39+
* v1.0.6 (26-08-15 [@NHPT](https://github.com/NHPT))
40+
* 使用 Typecho 内容组件生成文章永久链接,避免 Sitemap 输出需要重定向的分类路径;
41+
* 支持按标签文章数量过滤低内容标签页,默认保留全部非空标签。
3842
* v1.0.5 (22-09-210 [@jrotty](https://github.com/jrotty))
3943
* 分类链接支持{directory}多级分类
4044
* v1.0.4 (20-7-02 [@jzwalk](https://github.com/jzwalk))

0 commit comments

Comments
 (0)