1<?php // Last Change 2023-04-19 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 __construct() { 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 = '•'; 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 if (!is_array($index_links)) 106 return; 107 if (!is_array($index_title)) 108 return; 109 if (!is_array($index_pword)) 110 return; 111 112 $pages = array_reverse($index_links); 113 $titles = array_reverse($index_title); 114 $pagewords = array_reverse($index_pword); 115 $cnt = 0; 116 117 foreach ($pages as $id => $page) { 118 if (strlen($pagewords[$id]) > 2) { 119 $key = trim($page); 120 $title = trim($titles[$id]); 121 if (!empty($title)) 122 $this->pages[$key] = $title; 123 else 124 $this->pages[$key] = $key; 125 $cnt++; 126 if ($cnt == $number_of_pages) 127 break; 128 } 129 } 130 } 131 132 function _formatLatestPages($type_of_list, $separator) { 133 $ret = ''; 134 135 if (is_array($this->pages) && (count($this->pages) > 0)) { 136 if ($type_of_list == 'list') 137 $ret .= '<ul>'; 138 foreach ($this->pages as $key => $title) { 139 if ($type_of_list == 'list') 140 $ret .= '<li>'.html_wikilink($key, $title).'</li>'; 141 else 142 $ret .= $sep.html_wikilink($key, $title); 143 $sep = ' '.$separator.' '; 144 } 145 if ($type_of_list == 'list') 146 $ret .= '</ul>'; 147 } 148 149 return $ret; 150 } 151 152} // syntax_plugin_lastpages 153