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