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