-
Notifications
You must be signed in to change notification settings - Fork 52
/
page-search.php
56 lines (48 loc) · 1.73 KB
/
page-search.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
<?php
/**
* 文章索引
*
* @package custom
*/
header("content-type: text/xml; charset=utf-8");
/**
* Adds a CDATA property to an XML document.
* https://stackoverflow.com/a/27306705
*
* @param string $name Name of property that should contain CDATA.
* @param string $value Value that should be inserted into a CDATA child.
* @param object $parent Element that the CDATA child should be attached too.
*/
$add_cdata = function($name, $value, &$parent) {
$child = $parent->addChild($name);
if ($child !== NULL) {
$child_node = dom_import_simplexml($child);
$child_owner = $child_node->ownerDocument;
$child_node->appendChild($child_owner->createCDATASection($value));
}
return $child;
};
$archive = null;
$this->widget('Widget_Contents_Post_Recent', 'pageSize=10000')->to($archive);
$structure = '<?xml version="1.0" encoding="UTF-8"?><!-- This is an index of posts for the search module, generated by idawnlight/typecho-theme-material. --><search></search>';
$xml = new SimpleXMLElement($structure);
while ($archive->next()) {
if ($archive->password != null) continue;
$entry = $xml->addChild('entry');
$add_cdata('title', $archive->title, $entry);
$entry->addChild('url', $archive->permalink);
$add_cdata('content', $archive->content, $entry)->addAttribute('type', 'html');
$categories = $entry->addChild('categories');
if ($archive->categories) {
foreach ($archive->categories as $category) {
$categories->addChild('category', $category['name']);
}
}
$tags = $entry->addChild('tags');
if ($archive->tags) {
foreach ($archive->tags as $tag) {
$tags->addChild('tag', $tag['name']);
}
}
}
echo $xml->asXML();