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 } 90 91 return $data; 92 } 93 94 /** 95 * Create the new-page form. 96 * 97 * @param $mode string output format being rendered 98 * @param $renderer Doku_Renderer the current renderer object 99 * @param $data array data created by handler() 100 * @return boolean rendered correctly? 101 */ 102 public function render($mode, Doku_Renderer $renderer, $data) { 103 global $lang; 104 105 // make options available in class 106 $this->options = $data['options']; 107 108 if($mode == 'xhtml') { 109 $disablecache = null; 110 $namespaceinput = $this->_htmlNamespaceInput($data['namespace'], $disablecache); 111 if($namespaceinput === false) { 112 if($this->options['hideacl']) { 113 $renderer->doc .= ''; 114 } else { 115 $renderer->doc .= $this->getLang('nooption'); 116 } 117 return true; 118 } 119 if($disablecache) $renderer->info['cache'] = false; 120 121 $newpagetemplateinput = $this->_htmlTemplateInput($data['newpagetemplates']); 122 123 $input = 'text'; 124 if($this->options['autopage']) $input = 'hidden'; 125 126 $form = '<div class="addnewpage">' . DOKU_LF 127 . DOKU_TAB . '<form name="addnewpage" method="get" action="' . DOKU_BASE . DOKU_SCRIPT . '" accept-charset="' . $lang['encoding'] . '">' . DOKU_LF 128 . DOKU_TAB . DOKU_TAB . $namespaceinput . DOKU_LF 129 . DOKU_TAB . DOKU_TAB . '<input class="edit" type="'.$input.'" name="title" size="20" maxlength="255" tabindex="2" />' . DOKU_LF 130 . $newpagetemplateinput 131 . DOKU_TAB . DOKU_TAB . '<input type="hidden" name="do" value="edit" />' . DOKU_LF 132 . DOKU_TAB . DOKU_TAB . '<input type="hidden" name="id" />' . DOKU_LF 133 . DOKU_TAB . DOKU_TAB . '<input class="button" type="submit" value="' . $this->getLang('okbutton') . '" tabindex="4" />' . DOKU_LF 134 . DOKU_TAB . '</form>' . DOKU_LF 135 . '</div>'; 136 $renderer->doc .= $form; 137 138 return true; 139 } 140 return false; 141 } 142 143 /** 144 * Overwrites the $options with the ones parsed from $optstr 145 * 146 * @param string $optstr 147 * @param array $options 148 * @author Andreas Gohr <gohr@cosmocode.de> 149 */ 150 protected function _parseOptions($optstr, &$options) { 151 $opts = preg_split('/[,&]/', $optstr); 152 153 foreach($opts as $opt) { 154 $opt = strtolower(trim($opt)); 155 $val = true; 156 // booleans can be negated with a no prefix 157 if(substr($opt, 0, 2) == 'no') { 158 $opt = substr($opt, 2); 159 $val = false; 160 } 161 162 // not a known option? might be a key=value pair 163 if(!isset($options[$opt])) { 164 list($opt, $val) = array_map('trim', explode('=', $opt, 2)); 165 } 166 167 // still unknown? skip it 168 if(!isset($options[$opt])) continue; 169 170 // overwrite the current value 171 $options[$opt] = $val; 172 } 173 } 174 175 /** 176 * Parse namespace request 177 * 178 * This creates the final ID to be created (still having an @INPUT@ variable 179 * which is filled in via JavaScript) 180 * 181 * @author Samuele Tognini <samuele@cli.di.unipi.it> 182 * @author Michael Braun <michael-dev@fami-braun.de> 183 * @author Andreas Gohr <gohr@cosmocode.de> 184 * @param string $ns The namespace as given in the syntax 185 * @return string 186 */ 187 protected function _parseNS($ns) { 188 global $INFO; 189 190 $selfid = $INFO['id']; 191 $selfns = getNS($INFO['id']); 192 // replace the input variable with something unique that survives cleanID 193 $keep = sha1(time()); 194 195 // by default append the input to the namespace (except on autopage) 196 if(strpos($ns, '@INPUT@') === false && !$this->options['autopage']) $ns .= ":@INPUT@"; 197 198 // date replacements 199 $ns = dformat(null, $ns); 200 201 // placeholders 202 $replacements = array( 203 '/\//' => ':', // forward slashes to colons 204 '/@PAGE@/' => $selfid, 205 '/@NS@/' => $selfns, 206 '/^\.(:|\/|$)/' => "$selfns:", 207 '/@INPUT@/' => $keep, 208 ); 209 $ns = preg_replace(array_keys($replacements), array_values($replacements), $ns); 210 211 // clean up, then reinsert the input variable 212 $ns = cleanID($ns); 213 $ns = str_replace($keep, '@INPUT@', $ns); 214 215 return $ns; 216 } 217 218 /** 219 * Create the HTML Select element for namespace selection. 220 * 221 * @param string|false $dest_ns The destination namespace, or false if none provided. 222 * @param bool $disablecache reference indicates if caching need to be disabled 223 * @global string $ID The page ID 224 * @return string Select element with appropriate NS selected. 225 */ 226 protected function _htmlNamespaceInput($dest_ns, &$disablecache) { 227 global $ID; 228 $disablecache = false; 229 230 // If a NS has been provided: 231 // Whether to hide the NS selection (otherwise, show only subnamespaces). 232 $hide = $this->options['hide']; 233 234 $parsed_dest_ns = $this->_parseNS($dest_ns); 235 // Whether the user can create pages in the provided NS (or root, if no 236 // destination NS has been set. 237 $can_create = (auth_quickaclcheck($parsed_dest_ns . ":") >= AUTH_CREATE); 238 239 //namespace given, but hidden 240 if($hide && !empty($dest_ns)) { 241 if($can_create) { 242 return '<input type="hidden" name="np_cat" id="np_cat" value="' . $parsed_dest_ns . '"/>'; 243 } else { 244 return false; 245 } 246 } 247 248 //show select of given namespace 249 $currentns = getNS($ID); 250 251 $ret = '<select class="edit" id="np_cat" name="np_cat" tabindex="1">'; 252 253 // Whether the NS select element has any options 254 $someopt = false; 255 256 // Show root namespace if requested and allowed 257 if($this->options['showroot'] && $can_create) { 258 if(empty($dest_ns)) { 259 // If no namespace has been provided, add an option for the root NS. 260 $ret .= '<option ' . (($currentns == '') ? 'selected ' : '') . 'value="">' . $this->getLang('namespaceRoot') . '</option>'; 261 $someopt = true; 262 } else { 263 // If a namespace has been provided, add an option for it. 264 $ret .= '<option ' . (($currentns == $dest_ns) ? 'selected ' : '') . 'value="' . formText($dest_ns) . '">' . formText($dest_ns) . '</option>'; 265 $someopt = true; 266 } 267 } 268 269 $subnamespaces = $this->_getNamespaceList($dest_ns); 270 271 // The top of this stack will always be the last printed ancestor namespace 272 $ancestor_stack = array(); 273 if(!empty($dest_ns)) { 274 array_push($ancestor_stack, $dest_ns); 275 } 276 277 foreach($subnamespaces as $ns) { 278 279 if(auth_quickaclcheck($ns . ":") < AUTH_CREATE) continue; 280 281 // Pop any elements off the stack that are not ancestors of the current namespace 282 while(!empty($ancestor_stack) && strpos($ns, $ancestor_stack[count($ancestor_stack) - 1] . ':') !== 0) { 283 array_pop($ancestor_stack); 284 } 285 286 $nsparts = explode(':', $ns); 287 $first_unprinted_depth = empty($ancestor_stack) ? 1 : (2 + substr_count($ancestor_stack[count($ancestor_stack) - 1], ':')); 288 for($i = $first_unprinted_depth, $end = count($nsparts); $i <= $end; $i++) { 289 $namespace = implode(':', array_slice($nsparts, 0, $i)); 290 array_push($ancestor_stack, $namespace); 291 $selectOptionText = str_repeat(' ', substr_count($namespace, ':')) . $nsparts[$i - 1]; 292 $ret .= '<option ' . 293 (($currentns == $namespace) ? 'selected ' : '') . 294 ($i == $end ? ('value="' . $namespace . '">') : 'disabled>') . 295 $selectOptionText . 296 '</option>'; 297 } 298 $someopt = true; 299 $disablecache = true; 300 } 301 302 $ret .= '</select>'; 303 304 if($someopt) { 305 return $ret; 306 } else { 307 return false; 308 } 309 } 310 311 /** 312 * Get a list of namespaces below the given namespace. 313 * Recursively fetches subnamespaces. 314 * 315 * @param string $topns The top namespace 316 * @return array Multi-dimensional array of all namespaces below $tns 317 */ 318 protected function _getNamespaceList($topns = '') { 319 global $conf; 320 321 $topns = utf8_encodeFN(str_replace(':', '/', $topns)); 322 323 $excludes = $this->options['exclude']; 324 if($excludes == "") { 325 $excludes = array(); 326 } else { 327 $excludes = @explode(';', strtolower($excludes)); 328 } 329 $searchdata = array(); 330 search($searchdata, $conf['datadir'], 'search_namespaces', array(), $topns); 331 332 $namespaces = array(); 333 foreach($searchdata as $ns) { 334 foreach($excludes as $exclude) { 335 if(!empty($exclude) && strpos($ns['id'], $exclude) === 0) { 336 continue 2; 337 } 338 } 339 $namespaces[] = $ns['id']; 340 } 341 342 return $namespaces; 343 } 344 345 /** 346 * Create html for selection of namespace templates 347 * 348 * @param array $newpagetemplates array of namespace templates 349 * @return string html of select or hidden input 350 */ 351 public function _htmlTemplateInput($newpagetemplates) { 352 $cnt = count($newpagetemplates); 353 if($cnt < 1 || $cnt == 1 && $newpagetemplates[0] == '') { 354 $input = ''; 355 356 } else { 357 if($cnt == 1) { 358 list($template,) = $this->_parseNSTemplatePage($newpagetemplates[0]); 359 $input = '<input type="hidden" name="newpagetemplate" value="' . formText($template) . '" />'; 360 } else { 361 $first = true; 362 $input = '<select name="newpagetemplate" tabindex="3">'; 363 foreach($newpagetemplates as $template) { 364 $p = ($first ? ' selected="selected"' : ''); 365 $first = false; 366 367 list($template, $name) = $this->_parseNSTemplatePage($template); 368 $p .= ' value="' . formText($template) . '"'; 369 $input .= "<option $p>" . formText($name) . "</option>"; 370 } 371 $input .= '</select>'; 372 } 373 $input = DOKU_TAB . DOKU_TAB . $input . DOKU_LF; 374 } 375 return $input; 376 } 377 378 /** 379 * Parses and resolves the namespace template page 380 * 381 * @param $nstemplate 382 * @return array 383 */ 384 protected function _parseNSTemplatePage($nstemplate) { 385 global $ID; 386 387 @list($template, $name) = explode('|', $nstemplate, 2); 388 389 $exist = null; 390 resolve_pageid(getNS($ID), $template, $exist); //get absolute id 391 392 if(is_null($name)) $name = $template; 393 394 return array($template, $name); 395 } 396 397} 398