制作Typecho sitemap插件 - 代码解读
插件
学过typecho插件制作的人都知道,要制作插件必须要搞一个plugin,这个文件将有typecho框架在激活的时候自动加载,所以命名和里面的函数要符合规范。
1、路径规范。
2、函数命名规范。
更多信息请借鉴文章:Typecho二次开发之插件命名规范及注意事项
Plugin.php
<?php
/**
* Sitemap for Typecho
*
* @package Sitemap
* @author bayunjiang
* @version 1.0.0
* @link https://www.bayun.org
*
*/
class Sitemap_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate(){
Helper::addRoute('sitemap', '/sitemap.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){}
}
按照typecho插件的语法,设置了触发回调的入口还有别的配置信息。当前sitemap将Sitemap_Action的action设置为激活入口。另外,还设置/sitemap.xml作为入参,这个文件将作为模板进行解释。
Action.php
<?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($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',$page['modified'])."</lastmod>\n";
echo "\t\t<changefreq>always</changefreq>\n";
echo "\t\t<priority>0.8</priority>\n";
echo "\t</url>\n";
}
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',$article['modified'])."</lastmod>\n";
echo "\t\t<changefreq>always</changefreq>\n";
echo "\t\t<priority>0.5</priority>\n";
echo "\t</url>\n";
}
echo "</urlset>";
}
}
遍历table.contents的内容,然后将链接和信息生成输出到浏览器或者控制台。至此,一个sitemap制作成功!
文章目录
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。