-
Notifications
You must be signed in to change notification settings - Fork 1
/
Plugin.php
380 lines (323 loc) · 11.8 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
/**
* Just Archives
*
* @package JustArchives
* @author jKey
* @version 0.2.2
* @link http://typecho.jkey.lu/
* @license GPL3
*/
class JustArchives_Plugin implements Typecho_Plugin_Interface
{
public static $version = '0.2.0';
public static $cache_path = 'usr/plugins/JustArchives/cache/';
public static $cache_file = 'justarchives.cache';
public static $is_archive_page = false;
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
// 把相关的 javascript 添加到头部
Typecho_Plugin::factory('Widget_Archive')->footer = array('JustArchives_Plugin', 'outputCSSJavascript');
// 读取文章时匹配 [justarchives] 替换为归档
Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('JustArchives_Plugin', 'parse');
// 当有新的文章发布时删除归档缓存文件
Typecho_Plugin::factory('Widget_Contents_Post_Edit')->write = array('JustArchives_Plugin', 'deleteCache');
// 当有新的评论发布时删除归档缓存文件
Typecho_Plugin::factory('Widget_Feedback')->finishComment = array('JustArchives_Plugin', 'deleteCache');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate()
{
JustArchives_Plugin::deleteCache();
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$cfg_usejs = new Typecho_Widget_Helper_Form_Element_Checkbox(
'cfg_usejs',
array('usejs' => '使用Javascript折叠显示存档效果'),
array('usejs'),
'是否使用 JavaScript'
);
$form->addInput($cfg_usejs);
$cfg_monthorder = new Typecho_Widget_Helper_Form_Element_Radio(
'cfg_monthorder',
array('month_desc' => '按时间倒叙排列月份(离现在最近的月份排最前)',
'month_asc' => '按时间正序排列月份(离现在最远的月份排最前)'
),
'month_desc',
'存档月份排序'
);
$form->addInput($cfg_monthorder->multiMode());
$cfg_postorder = new Typecho_Widget_Helper_Form_Element_Radio(
'cfg_postorder',
array('post_desc' => '将最新的日志显示在第一位',
'post_asc' => '将最旧的日志显示在第一位'
),
'post_desc',
'存档文章排序'
);
$form->addInput($cfg_postorder->multiMode());
$cfg_postcount = new Typecho_Widget_Helper_Form_Element_Checkbox(
'cfg_postcount',
array('show' => '显示文章数'),
array('show'),
'是否显示文章数'
);
$form->addInput($cfg_postcount);
$cfg_commentcount = new Typecho_Widget_Helper_Form_Element_Checkbox(
'cfg_commentcount',
array('show' => '显示评论数'),
array('show'),
'是否显示评论数'
);
$form->addInput($cfg_commentcount);
$cfg_delcache = new Typecho_Widget_Helper_Form_Element_Checkbox(
'cfg_delcache',
array('comment' => '有新的评论时删除归档缓存'),
array(),
'删除缓存',
'默认只有在你创建文章时删除缓存,勾选后同时在有新评论时也删除缓存,主要是为了显示评论数与数据库的一致'
);
$form->addInput($cfg_delcache);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/*
* Output a little helper CSS and the Javascript for the plugin
* Based on code from http://www.learningjquery.com/2007/03/accordion-madness
*
*/
public static function outputCSSJavascript()
{
if ( !JustArchives_Plugin::$is_archive_page )
return;
$settings = Helper::options()->plugin('JustArchives');
if ( $settings->cfg_usejs == NULL ) {
return;
}
global $cleanarchivesreloaded;
if ( !empty($cleanarchivesreloaded) )
return;
$cleanarchivesreloaded = true;
$options = Typecho_Widget::widget('Widget_Options');
?>
<!-- Just Archives | http://typecho.jkey.lu/ -->
<style type="text/css">.car-collapse .car-yearmonth { cursor: s-resize; } </style>
<script type='text/javascript' src='<?php echo $options->siteUrl; ?>/usr/plugins/JustArchives/js/jquery-1.4.3.min.js'></script>
<script type="text/javascript">
/* <![CDATA[ */
jQuery(document).ready(function() {
//jQuery('.car-collapse').find('.car-monthlisting').hide();
jQuery('.car-collapse').find('.car-monthlisting:first').show();
jQuery('.car-collapse').find('.car-yearmonth').click(function() {
jQuery(this).next('ul').slideToggle('fast');
});
/*
jQuery('.car-collapse').find('.car-toggler').click(function() {
if ( '展开全部' == jQuery(this).text() ) {
jQuery(this).parent('.car-container').find('.car-monthlisting').show();
jQuery(this).text('折叠全部');
}
else {
jQuery(this).parent('.car-container').find('.car-monthlisting').hide();
jQuery(this).text('展开全部');
}
return false;
});
*/
jQuery(this).parent('.car-container').find('.car-monthlisting').show();
});
/* ]]> */
</script>
<?php
}
/**
* Grab all posts and filter them into an array
*
*/
public static function GetPosts()
{
$options = Typecho_Widget::widget('Widget_Options');
/**
* 获取数据库实例化对象
* 用静态变量存储实例化的数据库对象,可以保证数据连接仅进行一次
*/
$db = Typecho_Db::get();
$select = $db->select('cid', 'title', 'slug', 'created', 'allowComment', 'commentsNum')
->from('table.contents')
->where('status = ?', 'publish')
->where('type = ?', 'post');
$rawposts = $db->fetchAll($select);
$posts = array();
// Loop through each post and sort it into a structured array
foreach( $rawposts as $post ) {
/** 取出所有分类 */
$categories = $db->fetchAll($db
->select('slug')->from('table.metas')
->join('table.relationships', 'table.metas.mid = table.relationships.mid')
->where('table.relationships.cid = ?', $post['cid'])
->where('table.metas.type = ?', 'category')
->order('table.metas.order', Typecho_Db::SORT_ASC));
/** 取出第一个分类作为slug条件 */
$post['category'] = current(Typecho_Common::arrayFlatten($categories, 'slug'));
$date = new Typecho_Date($post['created']);
$post['year'] = $date->year;
$post['month'] = $date->month;
$post['day'] = $date->day;
$type = 'post';//$p['type'];
$routeExists = (NULL != Typecho_Router::get($type));
$permalink = $routeExists ? Typecho_Router::url($type, $post, $options->index) : '#';
$post['permalink'] = $permalink;
$posts[ $post['year'] . '.' . $post['month'] ][] = $post;
}
$rawposts = null; // More memory cleanup
return $posts;
}
/**
* Generates the HTML output based on $atts array from the shortcode
*
*/
public static function PostList()
{
$settings = Helper::options()->plugin('JustArchives');
// Set any missing $atts items to the defaults
$atts = array(
'usejs' => ($settings->cfg_usejs != NULL) ? in_array('usejs', $settings->cfg_usejs) : false,
'monthorder' => $settings->cfg_monthorder,
'postorder' => $settings->cfg_postorder,
'postcount' => ($settings->cfg_postcount != NULL) ? (in_array('show', $settings->cfg_postcount) ? '1' : '0') : '0',
'commentcount' => ($settings->cfg_commentcount != NULL) ? (in_array('show', $settings->cfg_commentcount) ? '1' : '0') : '0'
);
// Get the big array of all posts
// 检查是否存在缓存文件,有则读取,没有则创建
if ( file_exists( JustArchives_Plugin::$cache_path . JustArchives_Plugin::$cache_file ) ) {
$posts = unserialize( JustArchives_Plugin::readCache() );
} else {
$posts = JustArchives_Plugin::GetPosts();
JustArchives_Plugin::writeCache( serialize($posts) );
}
// Sort the months based on $atts
( 'month_desc' == $atts['monthorder'] ) ? krsort( $posts ) : ksort( $posts );
// Sort the posts within each month based on $atts
foreach( $posts as $key => $month ) {
$sorter = array();
foreach ( $month as $post )
$sorter[] = $post['created'];
$sortorder = ( 'post_desc' == $atts['postorder'] ) ? SORT_DESC : SORT_ASC;
array_multisort( $sorter, $sortorder, $month );
$posts[$key] = $month;
unset($month);
}
// Generate the HTML
$html = '<div class="car-container';
if ( true == $atts['usejs'] ) $html .= ' car-collapse';
$html .= '">'. "\n";
//if ( true == $atts['usejs'] ) $html .= '<a href="#" class="car-toggler">展开全部' . "</a>\n\n";
$html .= '<ul class="car-list">' . "\n";
$firstmonth = TRUE;
foreach( $posts as $yearmonth => $posts ) {
list( $year, $month ) = explode( '.', $yearmonth );
$firstpost = TRUE;
foreach( $posts as $post ) {
if ( TRUE == $firstpost ) {
//$html .= ' <li><span class="car-yearmonth">' . sprintf( '%1$s %2$d', $month . '月', $year );
$html .= ' <li><span class="car-yearmonth">' . sprintf( '%1$s %2$s', $year . ' 年 ', $month . ' 月' );
//if ( '0' != $atts['postcount'] ) $html .= ' <span title="Post Count">(' . count($posts) . ')</span>';
$html .= "</span>\n <ul class='car-monthlisting'>\n";
$firstpost = FALSE;
}
//$html .= ' <li>' . $post['day'] . ': <a href="' . $post['permalink'] . '">' . $post['title'] . '</a>';
$html .= ' <li><a href="' . $post['permalink'] . '">' . $post['title'] . '</a>';
// Unless comments are closed and there are no comments, show the comment count
/*
if ( '0' != $atts['commentcount'] && ( 0 != $post['commentsNum'] || '0' != $post['allowComment'] ) )
$html .= ' <span title="Comment Count">(' . $post['commentsNum'] . ')</span>';
*/
$html .= "</li>\n";
}
$html .= " </ul>\n </li>\n";
}
$html .= "</ul>\n</div>\n";
return $html;
}
/**
* 插件实现方法
*
* @access public
* @return void
*/
public static function parse($text, $widget, $lastResult)
{
$text = empty($lastResult) ? $text : $lastResult;
if ( $widget instanceof Widget_Archive && ( 1 == preg_match("/(\s*)(<justarchives>)(\s*)/si", $text) ) ) {
JustArchives_Plugin::$is_archive_page = true;
return preg_replace("/(\s*)(<justarchives>)(\s*)/si", "$1" . JustArchives_Plugin::PostList() . "$3", $text);
} else {
return $text;
}
}
/**
* 把缓存写入文件
*/
public static function writeCache($cache)
{
if ( !is_writeable ( JustArchives_Plugin::$cache_path ) ) {
if ( !chmod( JustArchives_Plugin::$cache_path , 777 ) )
return false;
}
$fp = fopen(JustArchives_Plugin::$cache_path . JustArchives_Plugin::$cache_file, 'w');
fwrite($fp, $cache);
fclose($fp);
}
/**
* 读取缓存
*/
public static function readCache()
{
$fp = fopen(JustArchives_Plugin::$cache_path . JustArchives_Plugin::$cache_file, 'r');
$cache = fread($fp, filesize(JustArchives_Plugin::$cache_path . JustArchives_Plugin::$cache_file));
fclose($fp);
return $cache;
}
/**
* 删除缓存
*/
public static function deleteCache($widget = NULL)
{
$settings = Helper::options()->plugin('JustArchives');
if ( !in_array('comment', $settings->cfg_delcache) && $widget instanceof Widget_Abstract_Comments ) {
return $widget;
}
if ( file_exists( JustArchives_Plugin::$cache_path . JustArchives_Plugin::$cache_file ) ) {
unlink(JustArchives_Plugin::$cache_path . JustArchives_Plugin::$cache_file);
}
return $widget;
}
}