1<?php 2if (!defined('DOKU_INC')) die(); 3 4class helper_plugin_newpagetemplate extends DokuWiki_Plugin 5{ 6 public $template; 7 private $screen; 8 private $nosave; 9 function init($opts, $options, $cli) 10 { 11 $page = $opts['page']; 12 $usrreplace = $opts['usrrepl']; 13 $template = $opts['tmpl']; 14 $overwrite = $opts['overwr']; 15 $ini = $opts['ini']; 16 $user = $opts['user']; 17 $screen = $opts['screen']; 18 $ini = $opts['ini']; 19 $nosave = $opts['nosave']; 20 21 if ($screen && $screen == 'cmdLine') { 22 $cli_opts = $options->getArgs(); 23 $cli->raw_commandLineOpts($cli_opts, $opts, 1); 24 return; 25 } 26 else if($screen) { 27 $this->screen = $screen; 28 } 29 30 if($nosave) { 31 $this->nosave = $nosave; 32 } 33 34 if (!empty($template)) { 35 if ($this->screen) { 36 echo "Template: $template \n"; 37 } 38 $tpl = $this->pagefromtemplate($opts['tmpl'], $opts['page'], $opts['usrrepl'], $opts['user']); 39 if ($this->screen) { 40 echo htmlentities($tpl) . "\n"; 41 } 42 } else if (isset($opts['ini'])) { 43 $this->output_ini($user, $ini,$usrreplace); 44 return; 45 } 46 echo $this->writePage($page,$tpl); 47 } 48 49 function writePage($page, $tpl) 50 { 51 if($this->nosave && $this->nosave == 'true') { 52 return "Not Saving $page\n"; 53 } 54 $file = wikiFn($page); 55 if($this->nosave && $this->nosave == 'existing') { 56 if(file_exists($file)) { 57 return "Not Saving existing page: $page\n"; 58 } 59 } 60 $summary = 'pagetemplate cli'; 61 saveWikiText($page, $tpl, $summary, $minor = false); 62 $this->setOwnership($file); 63 64 $ns = getNS($page); 65 $ns = preg_replace("/\.txt$/","",wikiFn($ns)); 66 $this->setOwnership($ns,true); 67 68 $meta = metaFN($page,'.meta'); 69 $this->setOwnership($meta); 70 71 $meta = metaFN($page,'.changes'); 72 $this->setOwnership($meta); 73 return "Saving $page\n"; 74 } 75 76 function setOwnership($page, $ns = false) { 77 $owner = $this->getConf('web_owner'); 78 $group = $this->getConf('web_group'); 79 chown($page, $owner); 80 chgrp($page, 'www-data'); 81 if($ns) return; 82 chmod($page, 0664); 83 } 84 85 function pagefromtemplate($template, $page, $newpagevars, $user = "") 86 { 87 88 global $conf; 89 global $INFO; 90 $ID = $page; 91 92 $tpl = io_readFile(wikiFN($template)); 93 94 if ($this->getConf('userreplace')) { 95 $stringvars = 96 array_map(function ($v) { 97 return explode(",", $v, 2); 98 }, explode(';', $newpagevars)); 99 foreach ($stringvars as $value) { 100 $tpl = str_replace(trim($value[0]), hsc(trim($value[1])), $tpl); 101 } 102 } 103 104 if ($this->getConf('standardreplace')) { 105 $file = noNS($ID); 106 $page = cleanID($file); 107 if ($this->getConf('prettytitles')) { 108 $title = str_replace('_', ' ', $page); 109 } else { 110 $title = $page; 111 } 112 if (class_exists('\dokuwiki\\Utf8\PhpString')) { 113 $ucfirst = '\dokuwiki\Utf8\PhpString::ucfirst'; 114 $ucwords = '\dokuwiki\\Utf8\PhpString::ucwords'; 115 $ucupper = '\dokuwiki\\Utf8\PhpString::strtoupper'; 116 117 } else { 118 $ucfirst = 'utf8_ucfirst'; 119 $ucwords = 'utf8_ucwords'; 120 $ucupper = 'utf8_strtoupper'; 121 122 } 123 124 $tpl = str_replace(array( 125 '@ID@', 126 '@NS@', 127 '@CURNS@', 128 '@!CURNS@', 129 '@!!CURNS@', 130 '@!CURNS!@', 131 '@FILE@', 132 '@!FILE@', 133 '@!FILE!@', 134 '@PAGE@', 135 '@!PAGE@', 136 '@!!PAGE@', 137 '@!PAGE!@', 138 '@USER@', 139 '@DATE@', 140 '@EVENT@' 141 ), 142 array( 143 $ID, 144 getNS($ID), 145 curNS($ID), 146 $ucfirst(curNS($ID)), 147 $ucwords(curNS($ID)), 148 $ucupper(curNS($ID)), 149 $file, 150 $ucfirst($file), 151 $ucupper($file), 152 $page, 153 $ucfirst($title), 154 $ucwords($title), 155 $ucupper($title), 156 $user, 157 $conf['dformat'], 158 $event->name, 159 ), $tpl); 160 161 // we need the callback to work around strftime's char limit 162 $tpl = preg_replace_callback('/%./',function ($m) {return strftime($m[0]); },$tpl); 163 } 164 if ($this->getConf('skip_unset_macros')) { 165 $tpl = preg_replace("/@.*?@/ms", "", $tpl); 166 } 167 return $tpl; 168 } 169 170 function output_ini($user="", $ini, $usrreplace) 171 { 172 if ($ini == 'config') { 173 $ini_file = $this->getConf('default_ini'); 174 $ini_file = DOKU_INC . "lib/plugins/newpagetemplate/$ini_file"; 175 } else { 176 $ini_file = DOKU_INC . "lib/plugins/newpagetemplate/$ini"; 177 } 178 179 $ini = parse_ini_file($ini_file, 1); 180 $templ = array_keys($ini); 181 foreach ($templ as $t) { 182 $pages = $ini[$t]['page']; 183 $newpagevars = $ini[$t]['newpagevars']; 184 if (is_array($newpagevars)) { 185 $this->process_array($pages, $newpagevars, $t, $user,$usrreplace); 186 } else { 187 $this->process_single($pages, $newpagevars, $t, $user,$usrreplace); 188 } 189 } 190 } 191 192 function process_array($pages, $newpagevars, $tpl, $user="",$usrreplace) 193 { 194 for ($i = 0; $i < count($pages); $i++) { 195 if(!empty($usrreplace)) { 196 $newpagevars[$i] = "$usrreplace;" . $newpagevars[$i]; 197 } 198 $res = $this->pagefromtemplate($tpl, $pages[$i], $newpagevars[$i], $user); 199 $result = $this->writePage($pages[$i], $res); 200 201 if ($this->screen) { 202 if($this->screen == 'admin') { 203 echo "<table class = 'newpagevars' style = 'width:90%;margin:auto;'>\n"; 204 echo "\n<tr><th>Template: $tpl</th></tr>\n"; 205 echo "<tr><td>Output: " . "\n" . htmlentities($res) . "</td></tr>\n"; 206 if($result) { 207 echo "\n<tr><td>$result</td></tr>\n"; 208 } 209 echo "</table>"; 210 } 211 else { 212 echo "\n===================\n"; 213 echo "Template: $tpl\n"; 214 echo "$res\n"; 215 if($result) { 216 echo "$result\n"; 217 } 218 } 219 } 220 } 221 222 } 223 224 function process_single($pages, $newpagevars, $tpl, $user="",$usrreplace) 225 { 226 /* handles mutiple pages but single instance of newpagevars */ 227 if(!empty($usrreplace)) { 228 $newpagevars = "$usrreplace;$newpagevars"; 229 } 230 for ($i = 0; $i < count($pages); $i++) { 231 $res = $this->pagefromtemplate($tpl, $pages[$i], $newpagevars, $user); 232 $result = $this->writePage($pages[$i], $res); 233 234 if ($this->screen) { 235 if($this->screen == 'admin') { 236 echo "<table class = 'newpagevars' style = 'width:90%;margin:auto;'>\n"; 237 echo "\n<tr><th>Template: $tpl</th></tr>\n"; 238 echo "<tr><td>Output: " . "\n" . htmlentities($res) . "</td></tr>\n"; 239 if($result) { 240 echo "\n<tr><td>$result</td></tr>\n"; 241 echo "</table>"; 242 } 243 } 244 else { 245 echo "\n===================\n"; 246 echo "Template: $tpl\n"; 247 echo "$res\n"; 248 if($result) { 249 echo "$result\n"; 250 } 251 } 252 } 253 } 254 } 255}