Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sitemap Plugin for Typecho #25

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Sitemap/Action.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
class Sitemap_Action extends Typecho_Widget implements Widget_Interface_Do
{
public function action()
{
$db = Typecho_Db::get();
$options = Typecho_Widget::widget('Widget_Options');

$pages = $db->fetchAll($db->select()->from('table.contents')
->where('table.contents.status = ?', 'publish')
->where('table.contents.created < ?', $options->gmtTime)
->where('table.contents.type = ?', 'page')
->order('table.contents.created', Typecho_Db::SORT_DESC));

$articles = $db->fetchAll($db->select()->from('table.contents')
->where('table.contents.status = ?', 'publish')
->where('table.contents.created < ?', $options->gmtTime)
->where('table.contents.type = ?', 'post')
->order('table.contents.created', Typecho_Db::SORT_DESC));

header("Content-Type: application/xml");
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
echo "<?xml-stylesheet type='text/xsl' href='" . $options->pluginUrl . "/Sitemap/sitemap.xsl'?>\n";
echo "<urlset xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\nxsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\"\nxmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">";
foreach($articles AS $article) {
$type = $article['type'];
$article['categories'] = $db->fetchAll($db->select()->from('table.metas')
->join('table.relationships', 'table.relationships.mid = table.metas.mid')
->where('table.relationships.cid = ?', $article['cid'])
->where('table.metas.type = ?', 'category')
->order('table.metas.order', Typecho_Db::SORT_ASC));
$article['category'] = urlencode(current(Typecho_Common::arrayFlatten($article['categories'], 'slug')));
$article['slug'] = urlencode($article['slug']);
$article['date'] = new Typecho_Date($article['created']);
$article['year'] = $article['date']->year;
$article['month'] = $article['date']->month;
$article['day'] = $article['date']->day;
$routeExists = (NULL != Typecho_Router::get($type));
$article['pathinfo'] = $routeExists ? Typecho_Router::url($type, $article) : '#';
$article['permalink'] = Typecho_Common::url($article['pathinfo'], $options->index);

echo "\t<url>\n";
echo "\t\t<loc>".$article['permalink']."</loc>\n";
echo "\t\t<lastmod>".date('Y-m-d\TH:i:s\Z',$article['modified'])."</lastmod>\n";
echo "\t\t<changefreq>always</changefreq>\n";
echo "\t\t<priority>0.8</priority>\n";
echo "\t</url>\n";
}

foreach($pages AS $page) {
$type = $page['type'];
$routeExists = (NULL != Typecho_Router::get($type));
$page['pathinfo'] = $routeExists ? Typecho_Router::url($type, $page) : '#';
$page['permalink'] = Typecho_Common::url($page['pathinfo'], $options->index);

echo "\t<url>\n";
echo "\t\t<loc>".$page['permalink']."</loc>\n";
echo "\t\t<lastmod>".date('Y-m-d\TH:i:s\Z',$page['modified'])."</lastmod>\n";
echo "\t\t<changefreq>always</changefreq>\n";
echo "\t\t<priority>0.6</priority>\n";
echo "\t</url>\n";
}

echo "</urlset>";
}
}
55 changes: 55 additions & 0 deletions Sitemap/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Sitemap 自动生成器<Br />由<a href="https://tyblog.com.cn" title="Ty">Ty</a>开发
*
* @package XML Sitemap
* @author Ty
* @version 0.0.1
* @link https://tyblog.com.cn
*/
class Sitemap_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Helper::addRoute('sitemap', '/sitemap-maker.xml', 'Sitemap_Action', 'action');
}

/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate()
{
Helper::removeRoute('sitemap');
}

/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form){}

/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}

}
35 changes: 35 additions & 0 deletions Sitemap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## 系统要求
- [CentOS7.0][1]及以后版本(其他Linux系统未验证可用性)
- [宝塔面板][2]
- [PHP7.4][3]及以后版本(PHP8.0未验证可用性)
- Typecho1.1及以后版本
## 插件必要设置
1.打开宝塔面板,点击‘网站’,点击对应网站的‘设置’

2.点击‘伪静态’,左上角选择‘typecho’,点击‘保存’

![伪静态配置][5]

3.进入typecho后台,点击‘设置->永久链接’

4.启用‘地址重写功能’
## 插件安装
1.在GitHub[下载][6]插件

2.将‘sitemap’目录上传到网站服务器‘/usr/plugins’目录下

3.进入Typecho后台启用插件
## 插件使用
**访问http(s)://你的域名/sitemap-maker.xml即可生成XML sitemap**
> Demo:[https://tyblog.com.cn/sitemap-maker.xml][7]
## 支持
在[GitHub][8]提issue


[1]: https://www.centos.org
[2]: https://bt.cn
[3]: https://www.php.net
[5]: http://image-cdn-tyblog.test.upcdn.net/sitemap-plugin/1.png
[6]: https://github.com/ty-yqs/Typecho-Sitemap-Plugin
[7]: https://tyblog.com.cn/sitemap-maker.xml
[8]: https://github.com/ty-yqs/Typecho-Sitemap-Plugin/issues/new
112 changes: 112 additions & 0 deletions Sitemap/sitemap.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:html="http://www.w3.org/TR/REC-html40"
xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XML Sitemap</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body {
font-family:"Lucida Grande","Lucida Sans Unicode",Tahoma,Verdana;
font-size:13px;
}

#intro {
background-color:#CFEBF7;
border:1px #2580B2 solid;
padding:5px 13px 5px 13px;
margin:10px;
}

#intro p {
line-height: 16.8667px;
}

table {
width: 100%;
}

td {
font-size:11px;
}

th {
text-align:left;
padding-right:30px;
font-size:11px;
}

tr.even {
background-color:whitesmoke;
}

#footer {
padding:2px;
margin:10px;
font-size:8pt;
color:gray;
}

#footer a {
color:gray;
}

a {
color:black;
}
</style>
</head>
<body>
<h1>XML Sitemap</h1>
<div id="intro">
<p>
This XML sitemap plugin was made by <a href="https://tyblog.com.cn">Ty</a>
</p>
</div>
<div id="content">
<table cellpadding="5">
<tr style="border-bottom:1px black solid;">
<th>URL</th>
<th>Priority</th>
<th>Change Frequency</th>
<th>LastChange</th>
</tr>
<xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:for-each select="sitemap:urlset/sitemap:url">
<tr>
<xsl:if test="position() mod 2 != 0">
<xsl:attribute name="class">ood</xsl:attribute>
</xsl:if>
<xsl:if test="position() mod 2 != 1">
<xsl:attribute name="class">even</xsl:attribute>
</xsl:if>
<td>
<xsl:variable name="itemURL">
<xsl:value-of select="sitemap:loc"/>
</xsl:variable>
<a href="{$itemURL}">
<xsl:value-of select="sitemap:loc"/>
</a>
</td>
<td>
<xsl:value-of select="concat(sitemap:priority*100,'%')"/>
</td>
<td>
<xsl:value-of select="concat(translate(substring(sitemap:changefreq, 1, 1),concat($lower, $upper),concat($upper, $lower)),substring(sitemap:changefreq, 2))"/>
</td>
<td>
<xsl:value-of select="concat(substring(sitemap:lastmod,0,11),concat(' ', substring(sitemap:lastmod,12,5)))"/>
</td>
</tr>
</xsl:for-each>
</table>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>