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 $option_text = ((@$this->getLang('namespaceRoot')) ? $this->getLang('namespaceRoot') : 'top'); 173 $ret .= '<option ' . (($currentns == '') ? 'selected ' : '') . 'value="">' . $option_text . '</option>'; 174 $someopt = true; 175 } else { 176 // If a namespace has been provided, add an option for it. 177 $ret .= '<option ' . (($currentns == $dest_ns) ? 'selected ' : '') . 'value="' . $dest_ns . '">' . $dest_ns . '</option>'; 178 $someopt = true; 179 } 180 } 181 182 $subnamespaces = $this->_getnslist($dest_ns); 183 foreach($subnamespaces as $ns) { 184 if(auth_quickaclcheck($ns . ":") < AUTH_CREATE) continue; 185 $nsparts = explode(':', $ns); 186 $nsparts = str_repeat(' ', substr_count($ns, ':')) . $nsparts[count($nsparts) - 1]; 187 $ret .= '<option ' . (($currentns == $ns) ? 'selected ' : '') . 'value="' . $ns . '">' . $nsparts . '</option>'; 188 $someopt = true; 189 $disablecache = true; 190 } 191 $ret .= '</select>'; 192 193 if($someopt) { 194 return $ret; 195 } else { 196 return false; 197 } 198 } 199 200 /** 201 * Get a list of namespaces below the given namespace. 202 * Recursively fetches subnamespaces. 203 * 204 * @param string $topns The top namespace 205 * @return array Multi-dimensional array of all namespaces below $tns 206 */ 207 protected function _getnslist($topns = '') { 208 global $conf; 209 210 $topns = utf8_encodeFN(str_replace(':', '/', $topns)); 211 212 $excludes = $this->getConf('addpage_exclude'); 213 if($excludes == "") { 214 $excludes = array(); 215 } else { 216 $excludes = @explode(';', strtolower($excludes)); 217 } 218 $searchdata = array(); 219 search($searchdata, $conf['datadir'], 'search_namespaces', array(), $topns); 220 221 $namespaces = array(); 222 foreach($searchdata as $ns) { 223 foreach($excludes as $exclude) { 224 if(strpos($ns['id'], $exclude) === 0) { 225 continue 2; 226 } 227 } 228 $namespaces[] = $ns['id']; 229 } 230 231 return $namespaces; 232 } 233 234 /** 235 * Create html for selection of namespace templates 236 * 237 * @param array $newpagetemplates array of namespace templates 238 * @return string html of select or hidden input 239 */ 240 public function _htmlTemplateInput($newpagetemplates) { 241 $cnt = count($newpagetemplates); 242 if($cnt < 1 || $cnt == 1 && $newpagetemplates[0] == '') { 243 $input = ''; 244 245 } else { 246 if($cnt == 1) { 247 list($template, ) = $this->_parseNStemplatepage($newpagetemplates[0]); 248 $input = '<input type="hidden" name="newpagetemplate" value="' . $template . '" />'; 249 } else { 250 $first = true; 251 $input = '<select name="newpagetemplate" tabindex="3">'; 252 foreach($newpagetemplates as $template) { 253 $p = ($first ? ' selected="selected"' : ''); 254 $first = false; 255 256 list($template, $name) = $this->_parseNStemplatepage($template); 257 $p .= ' value="'.formText($template).'"'; 258 $input .= "<option $p>".formText($name)."</option>"; 259 } 260 $input .= '</select>'; 261 } 262 $input = DOKU_TAB . DOKU_TAB . $input . DOKU_LF; 263 } 264 return $input; 265 } 266 267 /** 268 * Parses and resolves the namespace template page 269 * 270 * @param $nstemplate 271 * @return array 272 */ 273 protected function _parseNStemplatepage($nstemplate) { 274 global $ID; 275 276 @list($template, $name) = explode('|', $nstemplate, 2); 277 278 $exist = null; 279 resolve_pageid(getNS($ID), $template, $exist); //get absolute id 280 281 if (is_null($name)) $name = $template; 282 283 return array($template, $name); 284 } 285 286} 287