1<?php 2 3use dokuwiki\File\PageResolver; 4use dokuwiki\Utf8\Sort; 5 6/** 7 * DokuWiki Plugin simplenavi (Syntax Component) 8 * 9 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 10 * @author Andreas Gohr <gohr@cosmocode.de> 11 */ 12class syntax_plugin_simplenavi extends DokuWiki_Syntax_Plugin 13{ 14 private $startpages = []; 15 16 /** @inheritdoc */ 17 public function getType() 18 { 19 return 'substition'; 20 } 21 22 /** @inheritdoc */ 23 public function getPType() 24 { 25 return 'block'; 26 } 27 28 /** @inheritdoc */ 29 public function getSort() 30 { 31 return 155; 32 } 33 34 /** @inheritdoc */ 35 public function connectTo($mode) 36 { 37 $this->Lexer->addSpecialPattern('{{simplenavi>[^}]*}}', $mode, 'plugin_simplenavi'); 38 } 39 40 /** @inheritdoc */ 41 public function handle($match, $state, $pos, Doku_Handler $handler) 42 { 43 return explode(' ', substr($match, 13, -2)); 44 } 45 46 /** @inheritdoc */ 47 public function render($format, Doku_Renderer $renderer, $data) 48 { 49 if ($format != 'xhtml') return false; 50 51 global $INFO; 52 $renderer->nocache(); 53 54 // first data is namespace, rest is options 55 $ns = array_shift($data); 56 if ($ns && $ns[0] === '.') { 57 // resolve relative to current page 58 $ns = getNS((new PageResolver($INFO['id']))->resolveId("$ns:xxx")); 59 } else { 60 $ns = cleanID($ns); 61 } 62 // convert to path 63 $ns = utf8_encodeFN(str_replace(':', '/', $ns)); 64 65 $items = $this->getSortedItems( 66 $ns, 67 $INFO['id'], 68 $this->getConf('usetitle'), 69 $this->getConf('natsort'), 70 $this->getConf('nsfirst') 71 ); 72 73 $class = 'plugin__simplenavi'; 74 if (in_array('filter', $data)) $class .= ' plugin__simplenavi_filter'; 75 76 $renderer->doc .= '<div class="' . $class . '">'; 77 $renderer->doc .= html_buildlist($items, 'idx', [$this, 'cbList'], [$this, 'cbListItem']); 78 $renderer->doc .= '</div>'; 79 80 return true; 81 } 82 83 /** 84 * Fetch the items to display 85 * 86 * This returns a flat list suitable for html_buildlist() 87 * 88 * @param string $ns the namespace to search in 89 * @param string $current the current page, the tree will be expanded to this 90 * @param bool $useTitle Sort by the title instead of the ID? 91 * @param bool $useNatSort Use natural sorting or just sort by ASCII? 92 * @return array 93 */ 94 public function getSortedItems($ns, $current, $useTitle, $useNatSort, $nsFirst) 95 { 96 global $conf; 97 98 // execute search using our own callback 99 $items = []; 100 search( 101 $items, 102 $conf['datadir'], 103 [$this, 'cbSearch'], 104 [ 105 'currentID' => $current, 106 'usetitle' => $useTitle, 107 ], 108 $ns, 109 1, 110 '' // no sorting, we do ourselves 111 ); 112 113 // split into separate levels 114 $current = 1; 115 $parents = []; 116 $levels = []; 117 foreach ($items as $idx => $item) { 118 if ($current < $item['level']) { 119 // previous item was the parent 120 $parents[] = array_key_last($levels[$current]); 121 } 122 $current = $item['level']; 123 $levels[$item['level']][$idx] = $item; 124 } 125 126 // sort each level separately 127 foreach ($levels as $level => $items) { 128 uasort($items, function ($a, $b) use ($useNatSort, $nsFirst) { 129 return $this->itemComparator($a, $b, $useNatSort, $nsFirst); 130 }); 131 $levels[$level] = $items; 132 } 133 134 // merge levels into a flat list again 135 $levels = array_reverse($levels, true); 136 foreach ($levels as $level => $items) { 137 if ($level == 1) break; 138 139 $parent = array_pop($parents); 140 $pos = array_search($parent, array_keys($levels[$level - 1])) + 1; 141 142 /** @noinspection PhpArrayAccessCanBeReplacedWithForeachValueInspection */ 143 $levels[$level - 1] = array_slice($levels[$level - 1], 0, $pos, true) + 144 $levels[$level] + 145 array_slice($levels[$level - 1], $pos, null, true); 146 } 147 148 return $levels[1]; 149 } 150 151 /** 152 * Compare two items 153 * 154 * @param array $a 155 * @param array $b 156 * @param bool $useNatSort 157 * @param bool $nsFirst 158 * @return int 159 */ 160 public function itemComparator($a, $b, $useNatSort, $nsFirst) 161 { 162 if ($nsFirst && $a['type'] != $b['type']) { 163 return $a['type'] == 'd' ? -1 : 1; 164 } 165 166 if ($useNatSort) { 167 return Sort::strcmp($a['title'], $b['title']); 168 } else { 169 return strcmp($a['title'], $b['title']); 170 } 171 } 172 173 174 /** 175 * Create a list openening 176 * 177 * @param array $item 178 * @return string 179 * @see html_buildlist() 180 */ 181 public function cbList($item) 182 { 183 global $INFO; 184 185 if (($item['type'] == 'd' && $item['open']) || $INFO['id'] == $item['id']) { 186 return '<strong>' . html_wikilink(':' . $item['id'], $item['title']) . '</strong>'; 187 } else { 188 return html_wikilink(':' . $item['id'], $item['title']); 189 } 190 191 } 192 193 /** 194 * Create a list item 195 * 196 * @param array $item 197 * @return string 198 * @see html_buildlist() 199 */ 200 public function cbListItem($item) 201 { 202 if ($item['type'] == "f") { 203 return '<li class="level' . $item['level'] . '">'; 204 } elseif ($item['open']) { 205 return '<li class="open">'; 206 } else { 207 return '<li class="closed">'; 208 } 209 } 210 211 /** 212 * Custom search callback 213 * 214 * @param $data 215 * @param $base 216 * @param $file 217 * @param $type 218 * @param $lvl 219 * @param array $opts - currentID is the currently shown page 220 * @return bool 221 */ 222 public function cbSearch(&$data, $base, $file, $type, $lvl, $opts) 223 { 224 global $conf; 225 $return = true; 226 227 $id = pathID($file); 228 229 if ($type == 'd' && !( 230 preg_match('#^' . $id . '(:|$)#', $opts['currentID']) || 231 preg_match('#^' . $id . '(:|$)#', getNS($opts['currentID'])) 232 233 )) { 234 //add but don't recurse 235 $return = false; 236 } elseif ($type == 'f' && (!empty($opts['nofiles']) || substr($file, -4) != '.txt')) { 237 //don't add 238 return false; 239 } 240 241 // for sneaky index, check access to the namespace's start page 242 if ($type == 'd' && $conf['sneaky_index']) { 243 $sp = (new PageResolver(''))->resolveId($id . ':'); 244 if (auth_quickaclcheck($sp) < AUTH_READ) { 245 return false; 246 } 247 } 248 249 if ($type == 'd') { 250 // link directories to their start pages 251 $original = $id; 252 $id = "$id:"; 253 $id = (new PageResolver(''))->resolveId($id); 254 $this->startpages[$id] = 1; 255 256 // if the resolve id is in the same namespace as the original it's a start page named like the dir 257 if (getNS($original) == getNS($id)) { 258 $useNS = $original; 259 } 260 261 } elseif (!empty($this->startpages[$id])) { 262 // skip already shown start pages 263 return false; 264 } elseif (noNS($id) == $conf['start']) { 265 // skip the main start page 266 return false; 267 } 268 269 //check hidden 270 if (isHiddenPage($id)) { 271 return false; 272 } 273 274 //check ACL 275 if ($type == 'f' && auth_quickaclcheck($id) < AUTH_READ) { 276 return false; 277 } 278 279 $data[$id] = [ 280 'id' => $id, 281 'type' => $type, 282 'level' => $lvl, 283 'open' => $return, 284 'title' => $this->getTitle($id, $opts['usetitle']), 285 'ns' => $useNS ?? (string)getNS($id), 286 ]; 287 288 return $return; 289 } 290 291 /** 292 * Get the title for the given page ID 293 * 294 * @param string $id 295 * @param bool $usetitle - use the first heading as title 296 * @return string 297 */ 298 protected function getTitle($id, $usetitle) 299 { 300 global $conf; 301 302 if ($usetitle) { 303 $p = p_get_first_heading($id); 304 if (!empty($p)) return $p; 305 } 306 307 $p = noNS($id); 308 if ($p == $conf['start'] || !$p) { 309 $p = noNS(getNS($id)); 310 if (!$p) { 311 return $conf['start']; 312 } 313 } 314 return $p; 315 } 316} 317