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 /** 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 * {{NEWPAGE>your:namespace#newtpl1|Title1,newtpl2|Title1#@HI@,Howdy}} 56 * 57 * @param string $match The text matched by the patterns 58 * @param int $state The lexer state for the match 59 * @param int $pos The character position of the matched text 60 * @param Doku_Handler $handler The Doku_Handler object 61 * @return array Return an array with all data you want to use in render 62 * @codingStandardsIgnoreStart 63 */ 64 public function handle($match, $state, $pos, Doku_Handler $handler) { 65 /* @codingStandardsIgnoreEnd */ 66 $options = substr($match, 9, -2); // strip markup 67 $options = explode('#', $options, 3); 68 69 $namespace = trim(ltrim($options[0], '>')); 70 $templates = explode(',', $options[1] ?? ''); 71 $templates = array_map('trim', $templates); 72 $newpagevars = trim($options[2] ?? ''); 73 return array( 74 'namespace' => $namespace, 75 'newpagetemplates' => $templates, 76 'newpagevars' => $newpagevars 77 ); 78 } 79 80 /** 81 * Create the new-page form. 82 * 83 * @param $format string output format being rendered 84 * @param $renderer Doku_Renderer the current renderer object 85 * @param $data array data created by handler() 86 * @return boolean rendered correctly? 87 */ 88 public function render($format, Doku_Renderer $renderer, $data) { 89 global $lang; 90 91 if($format == 'xhtml') { 92 $disablecache = true; 93 $renderer->info['cache'] = false; 94 $namespaceinput = $this->_htmlNamespaceInput($data['namespace'], $disablecache); 95 if($namespaceinput === false) { 96 if($this->getConf('addpage_hideACL')) { 97 $renderer->doc .= ''; 98 } else { 99 $renderer->doc .= $this->getLang('nooption'); 100 } 101 return true; 102 } 103 104 $newpagetemplateinput = $this->_htmlTemplateInput($data['newpagetemplates']); 105 106 $form = '<div class="addnewpage"><p>' 107 . '<form name="addnewpage" method="get" action="' . DOKU_BASE . DOKU_SCRIPT . '" accept-charset="' . $lang['encoding'] . '">' 108 . $namespaceinput 109 . '<input class="edit" type="text" name="title" size="20" maxlength="255" tabindex="2" />' 110 . $newpagetemplateinput 111 . '<input type="hidden" name="newpagevars" value="' . $data['newpagevars'] . '"/>' 112 . '<input type="hidden" name="do" value="edit" />' 113 . '<input type="hidden" name="id" />' 114 . '<input class="button" type="submit" value="' . $this->getLang('okbutton') . '" tabindex="4" />' 115 . '</form>' 116 . '</p></div>'; 117 118 $renderer->doc .= $form; 119 120 return true; 121 } 122 return false; 123 } 124 125 /** 126 * Parse namespace request 127 * 128 * @author Samuele Tognini <samuele@cli.di.unipi.it> 129 * @author Michael Braun <michael-dev@fami-braun.de> 130 */ 131 protected function _parseNS($ns) { 132 $ID=getID(); 133 if(strpos($ns, '@PAGE@') !== false) { 134 return cleanID(str_replace('@PAGE@', $ID, $ns)); 135 } 136 if($ns == "@NS@") return getNS($ID); 137 $ns = preg_replace("/^\.(:|$)/", dirname(str_replace(':', '/', $ID)) . "$1", $ns); 138 $ns = str_replace("/", ":", $ns); 139 140 return cleanID($ns); 141 } 142 143 /** 144 * Create the HTML Select element for namespace selection. 145 * 146 * @param string|false $dest_ns The destination namespace, or false if none provided. 147 * @param bool $disablecache reference indicates if caching need to be disabled 148 * @global string $ID The page ID 149 * @return string Select element with appropriate NS selected. 150 */ 151 protected function _htmlNamespaceInput($dest_ns, &$disablecache) { 152 global $ID; 153 $disablecache = false; 154 155 // If a NS has been provided: 156 // Whether to hide the NS selection (otherwise, show only subnamespaces). 157 $hide = $this->getConf('addpage_hide'); 158 159 $parsed_dest_ns = $this->_parseNS($dest_ns); 160 // Whether the user can create pages in the provided NS (or root, if no 161 // destination NS has been set. 162 $can_create = (auth_quickaclcheck($parsed_dest_ns . ":") >= AUTH_CREATE); 163 164 //namespace given, but hidden 165 if($hide && !empty($dest_ns)) { 166 if($can_create) { 167 return '<input type="hidden" name="np_cat" id="np_cat" value="' . $parsed_dest_ns . '"/>'; 168 } else { 169 return false; 170 } 171 } 172 173 //show select of given namespace 174 $currentns = getNS($ID); 175 176 $ret = '<select class="edit" id="np_cat" name="np_cat" tabindex="1">'; 177 178 // Whether the NS select element has any options 179 $someopt = false; 180 181 // Show root namespace if requested and allowed 182 if($this->getConf('addpage_showroot') && $can_create) { 183 if(empty($dest_ns)) { 184 // If no namespace has been provided, add an option for the root NS. 185 $ret .= '<option ' . (($currentns == '') ? 'selected ' : '') . ' value="">' . $this->getLang('namespaceRoot') . '</option>'; 186 } else { 187 // If a namespace has been provided, add an option for it. 188 $ret .= '<option ' . (($currentns == $dest_ns) ? 'selected ' : '') . ' value="' . formText($dest_ns) . '">' . formText($dest_ns) . '</option>'; 189 } 190 $someopt = true; 191 } 192 193 $subnamespaces = $this->_getNamespaceList($dest_ns); 194 195 // The top of this stack will always be the last printed ancestor namespace 196 $ancestor_stack = array(); 197 if (!empty($dest_ns)) { 198 $ancestor_stack[] = $dest_ns; 199 } 200 201 foreach($subnamespaces as $ns) { 202 203 if(auth_quickaclcheck($ns . ":") < AUTH_CREATE) continue; 204 205 // Pop any elements off the stack that are not ancestors of the current namespace 206 while(!empty($ancestor_stack) && strpos($ns, $ancestor_stack[count($ancestor_stack) - 1] . ':') !== 0) { 207 array_pop($ancestor_stack); 208 } 209 210 $nsparts = explode(':', $ns); 211 $first_unprinted_depth = empty($ancestor_stack)? 1 : (2 + substr_count($ancestor_stack[count($ancestor_stack) - 1], ':')); 212 for ($i = $first_unprinted_depth, $end = count($nsparts); $i <= $end; $i++) { 213 $namespace = implode(':', array_slice($nsparts, 0, $i)); 214 $ancestor_stack[] = $namespace; 215 $selectOptionText = str_repeat(' ', substr_count($namespace, ':')) . $nsparts[$i - 1]; 216 $ret .= '<option ' . 217 (($currentns == $namespace) ? 'selected ' : '') . 218 ($i == $end? ('value="' . $namespace . '">') : 'disabled>') . 219 $selectOptionText . 220 '</option>'; 221 } 222 $someopt = true; 223 $disablecache = true; 224 } 225 226 $ret .= '</select>'; 227 228 if($someopt) { 229 return $ret; 230 } else { 231 return false; 232 } 233 } 234 235 /** 236 * Get a list of namespaces below the given namespace. 237 * Recursively fetches subnamespaces. 238 * 239 * @param string $topns The top namespace 240 * @return array Multi-dimensional array of all namespaces below $tns 241 */ 242 protected function _getNamespaceList($topns = '') { 243 global $conf; 244 245 $topns = utf8_encodeFN(str_replace(':', '/', $topns)); 246 247 $excludes = $this->getConf('addpage_exclude'); 248 if($excludes == "") { 249 $excludes = array(); 250 } else { 251 $excludes = @explode(';', strtolower($excludes)); 252 } 253 $searchdata = array(); 254 search($searchdata, $conf['datadir'], 'search_namespaces', array(), $topns); 255 256 $namespaces = array(); 257 foreach($searchdata as $ns) { 258 foreach($excludes as $exclude) { 259 if( ! empty($exclude) && strpos($ns['id'], $exclude) === 0) { 260 continue 2; 261 } 262 } 263 $namespaces[] = $ns['id']; 264 } 265 266 return $namespaces; 267 } 268 269 /** 270 * Create html for selection of namespace templates 271 * 272 * @param array $newpagetemplates array of namespace templates 273 * @return string html of select or hidden input 274 */ 275 public function _htmlTemplateInput($newpagetemplates) { 276 $cnt = count($newpagetemplates); 277 if($cnt < 1 || $cnt == 1 && $newpagetemplates[0] == '') { 278 $input = ''; 279 280 } else { 281 if($cnt == 1) { 282 list($template, ) = $this->_parseNSTemplatePage($newpagetemplates[0]); 283 $input = '<input type="hidden" name="newpagetemplate" value="' . formText($template) . '" />'; 284 } else { 285 $first = true; 286 $input = '<select name="newpagetemplate" tabindex="3">'; 287 foreach($newpagetemplates as $template) { 288 $p = ($first ? ' selected="selected"' : ''); 289 $first = false; 290 291 list($template, $name) = $this->_parseNSTemplatePage($template); 292 $p .= ' value="'.formText($template).'"'; 293 $input .= "<option $p>".formText($name)."</option>"; 294 } 295 $input .= '</select>'; 296 } 297 $input = DOKU_TAB . DOKU_TAB . $input . DOKU_LF; 298 } 299 return $input; 300 } 301 302 /** 303 * Parses and resolves the namespace template page 304 * 305 * @param $nstemplate 306 * @return array 307 */ 308 protected function _parseNSTemplatePage($nstemplate) { 309 global $ID; 310 311 @list($template, $name) = explode('|', $nstemplate, 2); 312 313 $exist = null; 314 resolve_pageid(getNS($ID), $template, $exist); //get absolute id 315 316 if (is_null($name)) $name = $template; 317 318 return array($template, $name); 319 } 320 321} 322