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