-
Notifications
You must be signed in to change notification settings - Fork 15
/
Plugin.php
201 lines (185 loc) · 6.44 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
<?php
/**
* 从 Typecho 导出 Markdown 文件
*
* 导出文章、页面为 Markdown 文件,支持完整的 metadata,包括 slug、分类、标签等等
*
* @package Tp2MD
* @author 熊猫小A
* @version 1.1
* @link https://www.imalan.cn
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
class Tp2MD_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
// 添加路由
Helper::addRoute("route_Tp2MD","/Tp2MD","Tp2MD_Action",'action');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate()
{
// 删除路由
Helper::removeRoute("route_Tp2MD");
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
?>
<p>
使用方法:设置好插件后访问 <mark><?php Helper::options()->index('/Tp2MD?action=save&key=[KEY]'); ?></mark> 即可。记住将 [KEY] 替换为插件中的设置的值。<br>
<mark>请保证 插件目录/cache 文件夹可写</mark>
</p>
<?php
$t = new Typecho_Widget_Helper_Form_Element_Select(
'postInFolder',
array('true' => '是','false' => '否'),
'true',
'文章按分类保存至文件夹',
'是否按分类将文章保存至文件夹。'
);
$form->addInput($t);
$t = new Typecho_Widget_Helper_Form_Element_Text(
'key',
null,
self::generateRandomString(),
'请求 Key',
'设置好后请勿泄露,以防别人盗取文章。'
);
$form->addInput($t);
}
/**
* 生成随机字符串作为 key
*/
private function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
/**
* 执行
*/
public static function save()
{
date_default_timezone_set('Asia/Shanghai');
$db = Typecho_Db::get();
$options = Helper::options()->plugin('Tp2MD');
$folder = $options->postInFolder == 'true';
// 创建路径
$d = date('Y-m-d-H-i-s');
$pathBase = __DIR__.'/cache/'.$d;
mkdir($pathBase);
$pagePath = $pathBase.'/page/';
mkdir($pagePath);
$postPath = $pathBase.'/post/';
mkdir($postPath);
if($folder)
{
$rows = $db->fetchAll($db->select()->from('table.metas')
->where('table.metas.type = ?', 'category'));
foreach ($rows as $row) {
mkdir($postPath.$row['name']);
}
}
// 导出页面
$rows = $db->fetchAll($db->select()->from('table.contents')
->where('table.contents.type != ?', 'attachment'));
foreach ($rows as $row) {
// 建立 yaml front matter
$front_matter = array(
'layout' => $row['type'],
'cid' => $row[cid],
'title' => $row['title'],
'slug' => $row['slug'],
'date' => date('Y/m/d H:i:s', $row['created']),
'updated' => date('Y/m/d H:i:s', $row['modified']),
'status' => $row['status']
);
// 作者
$author = $db->fetchRow($db->select()->from('table.users')
->where('table.users.uid = ?', $row['authorId']));
$front_matter['author'] = $author['screenName'];
// 分类与标签
$cates = array();
$tags = array();
$relations = $db->fetchAll($db->select()->from('table.relationships')
->where('table.relationships.cid = ?',$row['cid']));
foreach ($relations as $relation) {
$meta = $db->fetchRow($db->select()->from('table.metas')
->where('table.metas.mid = ?',$relation['mid']));
if($meta['type'] == 'tag') $tags[] = $meta['name'];
if($meta['type'] == 'category') $cates[] = $meta['name'];
}
$front_matter['categories'] = $cates;
$front_matter['tags'] = $tags;
// 自定义字段
$fields = $db->fetchAll($db->select()->from('table.fields')
->where('table.fields.cid = ?',$row['cid']));
foreach ($fields as $field) {
$front_matter[$field['name']] = $field[$field['type'].'_value'];
}
$output = "---".PHP_EOL;
foreach ($front_matter as $key => $value) {
if($key == 'categories' || $key == 'tags'){
$output .= $key.': '.PHP_EOL;
foreach ($value as $v) {
$output .= ' - '.$v.PHP_EOL;
}
}else{
$output .= $key.': '.$value.PHP_EOL;
}
}
$output .= "---".PHP_EOL.PHP_EOL.PHP_EOL;
$output .= str_replace('<!--markdown-->', '', $row['text']);
$fileName = str_replace(array(' ','/','|'), '_', $row['title']);
$fileName = date('Y-m-d-', $row['created']).$fileName.'.md';
$filePath = '';
if($row['type'] == 'page'){
$filePath = $pagePath;
}
else {
$filePath = $postPath;
if($folder) $filePath .= $cates[0].'/';
}
$filePath .= $fileName;
if(!file_put_contents($filePath,$output))
{
echo '不能写入文件,请检查 cache 文件夹权限!';
die();
}
}
echo "导出成功!位置:{$pathBase}。";
}
}