1<?php	//	Last Change 2021-06-16
2
3if(!defined('DOKU_INC')) {
4    define ('DOKU_INC', realpath(dirname(__FILE__).'/../../').'/');
5}
6if(!defined('DOKU_PLUGIN')) {
7    define ('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
8}
9
10require_once (DOKU_PLUGIN.'syntax.php');
11require_once (DOKU_INC.'inc/search.php');
12require_once (DOKU_INC.'inc/pageutils.php');
13
14/**
15 * The main LASTPAGES plugin class...
16 */
17class syntax_plugin_lastpages extends DokuWiki_Syntax_Plugin {
18	var $indexdir = '';
19	var $pages = array();
20
21    /**
22     * Constructor
23     */
24    function syntax_plugin_lastpages() {
25    	global $conf;
26
27		$this->indexdir = $conf['indexdir'];
28    }
29
30    /**
31     * return some info
32     */
33    function getInfo() {
34    }
35
36    /**
37     * Type of syntax plugin
38     */
39    function getType() {
40        return "substition";
41    }
42
43    /**
44     * Just before build in links
45     */
46    function getSort() {
47        return 299;
48    }
49
50    /**
51     * Register supported keywords
52     */
53    function connectTo($mode) {
54    	$this->Lexer->addSpecialPattern('~~LASTPAGES~~', $mode, 'plugin_lastpages');
55        $this->Lexer->addSpecialPattern('~~LAST10~~', $mode, 'plugin_lastpages');
56        $this->Lexer->addSpecialPattern('~~LAST5~~', $mode, 'plugin_lastpages');
57        $this->Lexer->addSpecialPattern('~~LIST10~~', $mode, 'plugin_lastpages');
58        $this->Lexer->addSpecialPattern('~~LIST5~~', $mode, 'plugin_lastpages');
59    }
60
61    /**
62     * Handle the match
63     */
64    function handle($match, $state, $pos, Doku_Handler $handler) {
65    	return $match;
66    }
67
68    /**
69     * Create output
70     */
71    function render($mode, Doku_Renderer $renderer, $data) {
72		// set options
73		switch ($data) {
74			case '~~LIST5~~':	$number_of_pages = 5;	$type_of_list = 'list'; break;
75			case '~~LIST10~~':	$number_of_pages = 10;	$type_of_list = 'list'; break;
76			case '~~LAST5~~':	$number_of_pages = 5;	$type_of_list = 'inline'; break;
77			case '~~LAST10~~':	$number_of_pages = 10;	$type_of_list = 'inline'; break;
78			default:
79			case '~~LASTPAGES~~': $number_of_pages = $this->getConf('numpages'); $type_of_list = $this->getConf('displist'); break;
80		}
81
82		$separator = $this->getConf('listsep');
83		if (empty($separator))
84			$separator = '&bull;';
85
86    	$this->_getLatestPages($number_of_pages);			// get list of latest pages
87    	$docdata .= $this->_formatLatestPages($type_of_list, $separator);	// format list of pages
88    	$renderer->doc .= $docdata;							// append data to the output stream
89    	return true;
90    }
91
92	function _debugArray(&$arr) {
93		echo highlight_string(print_r($arr, true));
94	}
95
96	function _getLatestPages($number_of_pages) {
97		$index_links_file = $this->indexdir.'/page.idx';
98		$index_title_file = $this->indexdir.'/title.idx';
99		$index_pword_file = $this->indexdir.'/pageword.idx';
100
101		$index_links = file($index_links_file);
102		$index_title = file($index_title_file);
103		$index_pword = file($index_pword_file);
104
105		$pages     = array_reverse($index_links);
106		$titles    = array_reverse($index_title);
107		$pagewords = array_reverse($index_pword);
108		$cnt       = 0;
109
110		foreach ($pages as $id => $page) {
111			if (strlen($pagewords[$id]) > 2) {
112				$key = trim($page);
113				$title = trim($titles[$id]);
114				if (!empty($title))
115					$this->pages[$key] = $title;
116				else
117					$this->pages[$key] = $key;
118				$cnt++;
119				if ($cnt == $number_of_pages)
120					break;
121			}
122		}
123	}
124
125	function _formatLatestPages($type_of_list, $separator) {
126		$ret = '';
127
128		if (is_array($this->pages) && (count($this->pages) > 0)) {
129			if ($type_of_list == 'list')
130				$ret .= '<ul>';
131			foreach ($this->pages as $key => $title) {
132				if ($type_of_list == 'list')
133					$ret .= '<li>'.html_wikilink($key, $title).'</li>';
134				else
135					$ret .= $sep.html_wikilink($key, $title);
136				$sep = ' '.$separator.' ';
137			}
138			if ($type_of_list == 'list')
139				$ret .= '</ul>';
140		}
141
142		return $ret;
143	}
144
145} // syntax_plugin_lastpages
146