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