1<?php 2/** 3 * Add-New-Page Plugin: a simple form for adding new pages. 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author iDO <ido@idotech.info> 7 * @author Sam Wilson <sam@samwilson.id.au> 8 * 9 * @noinspection PhpUnused, 10 * PhpMissingParamTypeInspection, PhpMissingReturnTypeInspection 11 */ 12 13// must be run within Dokuwiki 14if(!defined('DOKU_INC')) die(); 15 16class syntax_plugin_addnewpage extends DokuWiki_Syntax_Plugin { 17 18 /** @var array the parsed options */ 19 protected $options; 20 21 /** 22 * Syntax Type 23 */ 24 public function getType() { 25 return 'substition'; 26 } 27 28 /** 29 * Paragraph Type 30 */ 31 public function getPType() { 32 return 'block'; 33 } 34 35 /** 36 * @return int 37 */ 38 public function getSort() { 39 return 199; 40 } 41 42 /** 43 * @param string $mode 44 */ 45 public function connectTo($mode) { 46 $this->Lexer->addSpecialPattern('\{\{NEWPAGE[^\}]*\}\}', $mode, 'plugin_addnewpage'); 47 } 48 49 /** 50 * Handler to prepare matched data for the rendering process 51 * 52 * Handled syntax options: 53 * {{NEWPAGE}} 54 * {{NEWPAGE>your:namespace}} 55 * {{NEWPAGE#newtpl1,newtpl2}} 56 * {{NEWPAGE#newtpl1|Title1,newtpl2|Title1}} 57 * {{NEWPAGE>your:namespace#newtpl1|Title1,newtpl2|Title1}} 58 * {{NEWPAGE>your:namespace#newtpl1|Title1,newtpl2|Title1#@HI@,Howdy}} 59 * 60 * @param string $match The text matched by the patterns 61 * @param int $state The lexer state for the match 62 * @param int $pos The character position of the matched text 63 * @param Doku_Handler $handler The Doku_Handler object 64 * @return array Return an array with all data you want to use in render 65 * @codingStandardsIgnoreStart 66 */ 67 public function handle($match, $state, $pos, Doku_Handler $handler) { 68 /* @codingStandardsIgnoreEnd */ 69 $match = substr($match, 9, -2); // strip markup 70 71 $data = array( 72 'namespace' => '', 73 'newpagetemplates' => array(), 74 'newpagevars' => array(), 75 'options' => array( 76 'exclude' => $this->getConf('addpage_exclude'), 77 'showroot' => $this->getConf('addpage_showroot'), 78 'hide' => $this->getConf('addpage_hide'), 79 'hideacl' => $this->getConf('addpage_hideACL'), 80 'autopage' => $this->getConf('addpage_autopage'), 81 ) 82 ); 83 84 if(preg_match('/>(.*?)(#|\?|$)/', $match, $m)) { 85 $data['namespace'] = trim($m[1]); 86 } 87 88 if(preg_match('/#(.*?)(#.*?)?(\?|$)/', $match, $m)) { 89 $data['newpagetemplates'] = array_map('trim', explode(',', $m[1])); 90 $data['newpagevars'] = array_map('trim', explode(',', $m[2])); 91 } 92 93 if(preg_match('/\?(.*?)(#|$)/', $match, $m)) { 94 $this->_parseOptions($m[1], $data['options']); 95 } 96 97 return $data; 98 } 99 100 /** 101 * Create the new-page form. 102 * 103 * @param $format string output format being rendered 104 * @param $renderer Doku_Renderer the current renderer object 105 * @param $data array data created by handler() 106 * @return boolean rendered correctly? 107 */ 108 public function render($format, Doku_Renderer $renderer, $data) { 109 global $lang; 110 111 // make options available in class 112 $this->options = $data['options']; 113 114 if($format == 'xhtml') { 115 $disablecache = true; 116 $renderer->info['cache'] = false; 117 $namespaceinput = $this->_htmlNamespaceInput($data['namespace'], $disablecache); 118 if($namespaceinput === false) { 119 if($this->options['hideacl']) { 120 $renderer->doc .= ''; 121 } else { 122 $renderer->doc .= $this->getLang('nooption'); 123 } 124 return true; 125 } 126 127 $newpagetemplateinput = $this->_htmlTemplateInput($data['newpagetemplates']); 128 129 $input = 'text'; 130 if($this->options['autopage']) $input = 'hidden'; 131 132 $form = '<div class="addnewpage"><p>' 133 . '<form name="addnewpage" method="get" action="' . DOKU_BASE . DOKU_SCRIPT . '" accept-charset="' . $lang['encoding'] . '">' 134 . $namespaceinput 135 . '<input class="edit" type="text" name="title" size="20" maxlength="255" tabindex="2" />' 136 . $newpagetemplateinput 137 . '<input type="hidden" name="newpagevars" value="' . $data['newpagevars'] . '"/>' 138 . '<input type="hidden" name="do" value="edit" />' 139 . '<input type="hidden" name="id" />' 140 . '<input class="button" type="submit" value="' . $this->getLang('okbutton') . '" tabindex="4" />' 141 . '</form>' 142 . '</p></div>'; 143 144 $renderer->doc .= $form; 145 146 return true; 147 } 148 return false; 149 } 150 151 /** 152 * Overwrites the $options with the ones parsed from $optstr 153 * 154 * @param string $optstr 155 * @param array $options 156 * @author Andreas Gohr <gohr@cosmocode.de> 157 */ 158 protected function _parseOptions($optstr, &$options) { 159 $opts = preg_split('/[,&]/', $optstr); 160 161 foreach($opts as $opt) { 162 $opt = strtolower(trim($opt)); 163 $val = true; 164 // booleans can be negated with a no prefix 165 if(substr($opt, 0, 2) == 'no') { 166 $opt = substr($opt, 2); 167 $val = false; 168 } 169 170 // not a known option? might be a key=value pair 171 if(!isset($options[$opt])) { 172 list($opt, $val) = array_map('trim', explode('=', $opt, 2)); 173 } 174 175 // still unknown? skip it 176 if(!isset($options[$opt])) continue; 177 178 // overwrite the current value 179 $options[$opt] = $val; 180 } 181 } 182 183 /** 184 * Parse namespace request 185 * 186 * This creates the final ID to be created (still having an @INPUT@ variable 187 * which is filled in via JavaScript) 188 * 189 * @author Samuele Tognini <samuele@cli.di.unipi.it> 190 * @author Michael Braun <michael-dev@fami-braun.de> 191 * @author Andreas Gohr <gohr@cosmocode.de> 192 * @param string $ns The namespace as given in the syntax 193 * @return string 194 */ 195 protected function _parseNS($ns) { 196 global $INFO; 197 198 $selfid = $INFO['id']; 199 $selfns = getNS($selfid); 200 // replace the input variable with something unique that survives cleanID 201 $keep = sha1(time()); 202 203 // by default append the input to the namespace (except on autopage) 204 if(strpos($ns, '@INPUT@') === false && !$this->options['autopage']) $ns .= ":@INPUT@"; 205 206 // date replacements 207 $ns = dformat(null, $ns); 208 209 // placeholders 210 $replacements = array( 211 '/\//' => ':', // forward slashes to colons 212 '/@PAGE@/' => $selfid, 213 '/@NS@/' => $selfns, 214 '/^\.(:|\/|$)/' => "$selfns:", 215 '/@INPUT@/' => $keep, 216 ); 217 $ns = preg_replace(array_keys($replacements), array_values($replacements), $ns); 218 219 // clean up, then reinsert the input variable 220 $ns = cleanID($ns); 221 $ns = str_replace($keep, '@INPUT@', $ns); 222 223 return $ns; 224 } 225 226 /** 227 * Create the HTML Select element for namespace selection. 228 * 229 * @param string|false $dest_ns The destination namespace, or false if none provided. 230 * @param bool $disablecache reference indicates if caching need to be disabled 231 * @global string $ID The page ID 232 * @return string Select element with appropriate NS selected. 233 */ 234 protected function _htmlNamespaceInput($dest_ns, &$disablecache) { 235 global $ID; 236 $disablecache = false; 237 238 // If a NS has been provided: 239 // Whether to hide the NS selection (otherwise, show only subnamespaces). 240 $hide = $this->options['hide']; 241 242 $parsed_dest_ns = $this->_parseNS($dest_ns); 243 // Whether the user can create pages in the provided NS (or root, if no 244 // destination NS has been set. 245 $can_create = (auth_quickaclcheck($parsed_dest_ns . ":") >= AUTH_CREATE); 246 247 //namespace given, but hidden 248 if($hide && !empty($dest_ns)) { 249 if($can_create) { 250 return '<input type="hidden" name="np_cat" id="np_cat" value="' . $parsed_dest_ns . '"/>'; 251 } else { 252 return false; 253 } 254 } 255 256 //show select of given namespace 257 $currentns = getNS($ID); 258 259 $ret = '<select class="edit" id="np_cat" name="np_cat" tabindex="1">'; 260 261 // Whether the NS select element has any options 262 $someopt = false; 263 264 // Show root namespace if requested and allowed 265 if($this->options['showroot'] && $can_create) { 266 if(empty($dest_ns)) { 267 // If no namespace has been provided, add an option for the root NS. 268 $ret .= '<option ' . (($currentns == '') ? 'selected ' : '') . ' value="">' . $this->getLang('namespaceRoot') . '</option>'; 269 } else { 270 // If a namespace has been provided, add an option for it. 271 $ret .= '<option ' . (($currentns == $dest_ns) ? 'selected ' : '') . ' value="' . formText($dest_ns) . '">' . formText($dest_ns) . '</option>'; 272 } 273 $someopt = true; 274 } 275 276 $subnamespaces = $this->_getNamespaceList($dest_ns); 277 278 // The top of this stack will always be the last printed ancestor namespace 279 $ancestor_stack = array(); 280 if (!empty($dest_ns)) { 281 $ancestor_stack[] = $dest_ns; 282 } 283 284 foreach($subnamespaces as $ns) { 285 286 if(auth_quickaclcheck($ns . ":") < AUTH_CREATE) continue; 287 288 // Pop any elements off the stack that are not ancestors of the current namespace 289 while(!empty($ancestor_stack) && strpos($ns, $ancestor_stack[count($ancestor_stack) - 1] . ':') !== 0) { 290 array_pop($ancestor_stack); 291 } 292 293 $nsparts = explode(':', $ns); 294 $first_unprinted_depth = empty($ancestor_stack) ? 1 : (2 + substr_count($ancestor_stack[count($ancestor_stack) - 1], ':')); 295 for($i = $first_unprinted_depth, $end = count($nsparts); $i <= $end; $i++) { 296 $namespace = implode(':', array_slice($nsparts, 0, $i)); 297 $ancestor_stack[] = $namespace; 298 $selectOptionText = str_repeat(' ', substr_count($namespace, ':')) . $nsparts[$i - 1]; 299 $ret .= '<option ' . 300 (($currentns == $namespace) ? 'selected ' : '') . 301 ($i == $end ? ('value="' . $namespace . '">') : 'disabled>') . 302 $selectOptionText . 303 '</option>'; 304 } 305 $someopt = true; 306 $disablecache = true; 307 } 308 309 $ret .= '</select>'; 310 311 if($someopt) { 312 return $ret; 313 } else { 314 return false; 315 } 316 } 317 318 /** 319 * Get a list of namespaces below the given namespace. 320 * Recursively fetches subnamespaces. 321 * 322 * @param string $topns The top namespace 323 * @return array Multi-dimensional array of all namespaces below $tns 324 */ 325 protected function _getNamespaceList($topns = '') { 326 global $conf; 327 328 $topns = utf8_encodeFN(str_replace(':', '/', $topns)); 329 330 $excludes = $this->options['exclude']; 331 if($excludes == "") { 332 $excludes = array(); 333 } else { 334 $excludes = @explode(';', strtolower($excludes)); 335 } 336 $searchdata = array(); 337 search($searchdata, $conf['datadir'], 'search_namespaces', array(), $topns); 338 339 $namespaces = array(); 340 foreach($searchdata as $ns) { 341 foreach($excludes as $exclude) { 342 if(!empty($exclude) && strpos($ns['id'], $exclude) === 0) { 343 continue 2; 344 } 345 } 346 $namespaces[] = $ns['id']; 347 } 348 349 return $namespaces; 350 } 351 352 /** 353 * Create html for selection of namespace templates 354 * 355 * @param array $newpagetemplates array of namespace templates 356 * @return string html of select or hidden input 357 */ 358 public function _htmlTemplateInput($newpagetemplates) { 359 $cnt = count($newpagetemplates); 360 if($cnt < 1 || $cnt == 1 && $newpagetemplates[0] == '') { 361 $input = ''; 362 363 } else { 364 if($cnt == 1) { 365 list($template,) = $this->_parseNSTemplatePage($newpagetemplates[0]); 366 $input = '<input type="hidden" name="newpagetemplate" value="' . formText($template) . '" />'; 367 } else { 368 $first = true; 369 $input = '<select name="newpagetemplate" tabindex="3">'; 370 foreach($newpagetemplates as $template) { 371 $p = ($first ? ' selected="selected"' : ''); 372 $first = false; 373 374 list($template, $name) = $this->_parseNSTemplatePage($template); 375 $p .= ' value="' . formText($template) . '"'; 376 $input .= "<option $p>" . formText($name) . "</option>"; 377 } 378 $input .= '</select>'; 379 } 380 $input = DOKU_TAB . DOKU_TAB . $input . DOKU_LF; 381 } 382 return $input; 383 } 384 385 /** 386 * Parses and resolves the namespace template page 387 * 388 * @param $nstemplate 389 * @return array 390 */ 391 protected function _parseNSTemplatePage($nstemplate) { 392 global $ID; 393 394 @list($template, $name) = explode('|', $nstemplate, 2); 395 396 $exist = null; 397 resolve_pageid(getNS($ID), $template, $exist); //get absolute id 398 399 if(is_null($name)) $name = $template; 400 401 return array($template, $name); 402 } 403 404} 405