function test_none() { global $conf; $conf['sepchar'] = '-'; $data = array('id' => 'page-id-long', 'tpl' => '"@PAGE@" "@!PAGE@" "@!!PAGE@" "@!PAGE!@"'); $this->assertEquals(parsePageTemplate($data), '"page id long" "Page id long" "Page Id Long" "PAGE ID LONG"'); }
function test_none() { global $conf; $conf['sepchar'] = '-'; $data = array('id' => 'page-id-long', 'tpl' => '"@PAGE@" "@!PAGE@" "@!!PAGE@" "@!PAGE!@"'); $old = error_reporting(E_ALL & ~E_NOTICE); $this->assertEqual(parsePageTemplate($data), '"page id long" "Page id long" "Page Id Long" "PAGE ID LONG"'); error_reporting($old); }
function pagefromtemplate(&$event, $param) { if (strlen(trim($_REQUEST['newpagetemplate'])) > 0) { global $conf; global $INFO; global $ID; $tpl = io_readFile(wikiFN($_REQUEST['newpagetemplate'])); if ($this->getConf('userreplace')) { $stringvars = array_map(create_function('$v', 'return explode(",",$v,2);'), explode(';', $_REQUEST['newpagevars'])); foreach ($stringvars as $value) { $tpl = str_replace(trim($value[0]), trim($value[1]), $tpl); } } if ($this->getConf('standardreplace')) { // replace placeholders $tpl = parsePageTemplate($tpl, $ID); } $event->result = $tpl; $event->preventDefault(); } }
/** * Returns the pagetemplate contents for the ID's namespace * * @triggers COMMON_PAGETPL_LOAD * @author Andreas Gohr <*****@*****.**> */ function pageTemplate($id) { global $conf; if (is_array($id)) { $id = $id[0]; } // prepare initial event data $data = array('id' => $id, 'tpl' => '', 'tplfile' => '', 'doreplace' => true); $evt = new Doku_Event('COMMON_PAGETPL_LOAD', $data); if ($evt->advise_before(true)) { // the before event might have loaded the content already if (empty($data['tpl'])) { // if the before event did not set a template file, try to find one if (empty($data['tplfile'])) { $path = dirname(wikiFN($id)); if (@file_exists($path . '/_template.txt')) { $data['tplfile'] = $path . '/_template.txt'; } else { // search upper namespaces for templates $len = strlen(rtrim($conf['datadir'], '/')); while (strlen($path) >= $len) { if (@file_exists($path . '/__template.txt')) { $data['tplfile'] = $path . '/__template.txt'; break; } $path = substr($path, 0, strrpos($path, '/')); } } } // load the content $data['tpl'] = io_readFile($data['tplfile']); } if ($data['doreplace']) { parsePageTemplate($data); } } $evt->advise_after(); unset($evt); return $data['tpl']; }
/** * Load page data and do default pattern replacements like namespace templates do * and add it to list of targetpages * * Note: for runas the values of the real user are used for the placeholders * @NAME@ => $USERINFO['name'] * @MAIL@ => $USERINFO['mail'] * and the replaced value: * @USER@ => $INPUT->server->str('REMOTE_USER') * * @param string $targetpageid pageid of destination * @param string $templatepageid pageid of template for this targetpage */ protected function addParsedTargetpage($targetpageid, $templatepageid) { $data = array('id' => $targetpageid, 'tpl' => rawWiki($templatepageid), 'doreplace' => true); parsePageTemplate($data); //collect and apply some other replacements $patterns = array(); $values = array(); $keys = array('__lang__', '__trans__', '__year__', '__month__', '__day__', '__time__'); foreach ($keys as $key) { $patterns[$key] = $this->patterns[$key]; $values[$key] = $this->values[$key]; } $this->targetpages[$targetpageid] = preg_replace($patterns, $values, $data['tpl']); }
function run($data, $thanks, $argv) { global $ID; global $conf; global $USERINFO; list($tpl, $pagename, $sep) = $argv; if (is_null($sep)) { $sep = $conf['sepchar']; } $runas = $this->getConf('runas'); $patterns = array(); $values = array(); $templates = array(); // run through fields foreach ($data as $opt) { $label = $opt->getParam('label'); $value = $opt->getParam('value'); // prepare replacements if (!is_null($label)) { $patterns[$label] = '/(@@|##)' . preg_quote($label, '/') . '(?:\\|([^|]*?))' . (is_null($value) ? '' : '?') . '\\1/si'; $values[$label] = is_null($value) ? '$2' : $value; $patterns[$label . '|'] = '/(@@|##)' . preg_quote($label, '/') . '(?:\\|(.*?))(?:\\|(.*?))\\1/si'; $values[$label . '|'] = is_null($value) ? '$2' : '$3'; } // handle pagenames $pname = $opt->getParam('pagename'); if (!is_null($pname)) { $pagename .= $sep . $pname; } if (!is_null($opt->getParam('page_tpl')) && !is_null($opt->getParam('page_tgt'))) { $page_tpl = $this->replace($patterns, $values, $opt->getParam('page_tpl')); if (auth_aclcheck($page_tpl, $runas ? $runas : $_SERVER['REMOTE_USER'], $USERINFO['grps']) >= AUTH_READ) { $templates[$opt->getParam('page_tgt')] = rawWiki($page_tpl); } } } $pagename = $this->replace($patterns, $values, $pagename); // check pagename $pagename = cleanID($pagename); if ($pagename === '') { throw new Exception($this->getLang('e_pagename')); } $_templates = array(); foreach ($templates as $k => $v) { $_templates[cleanID("{$pagename}:{$k}")] = $v; } $templates = $_templates; // get templates if ($tpl == '_') { // use namespace template if (!isset($templates[$pagename])) { $templates[$pagename] = pageTemplate(array($pagename)); } } elseif ($tpl !== '!') { // Namespace link require_once DOKU_INC . 'inc/search.php'; if ($runas) { // Hack user credentials. global $USERINFO; $backup = array($_SERVER['REMOTE_USER'], $USERINFO['grps']); $_SERVER['REMOTE_USER'] = $runas; $USERINFO['grps'] = array(); } $t_pages = array(); search($t_pages, $conf['datadir'], 'search_universal', array('depth' => 0, 'listfiles' => true), str_replace(':', '/', getNS($tpl))); foreach ($t_pages as $t_page) { $t_name = cleanID($t_page['id']); $p_name = preg_replace('/^' . preg_quote_cb(cleanID($tpl)) . '($|:)/', $pagename . '$1', $t_name); if ($p_name === $t_name) { // When using a single-page template, ignore other pages // in the same namespace. continue; } if (!isset($templates[$p_name])) { // load page data and do default pattern replacements like // namespace templates do $data = array('id' => $p_name, 'tpl' => rawWiki($t_name), 'doreplace' => true); parsePageTemplate($data); $templates[$p_name] = $data['tpl']; } } if ($runas) { /* Restore user credentials. */ global $USERINFO; list($_SERVER['REMOTE_USER'], $USERINFO['grps']) = $backup; } } if (empty($templates)) { throw new Exception(sprintf($this->getLang('e_template'), $tpl)); } // check all target pagenames foreach (array_keys($templates) as $pname) { // prevent overriding already existing pages if (page_exists($pname)) { throw new Exception(sprintf($this->getLang('e_pageexists'), html_wikilink($pname))); } // check auth if ($runas) { $auth = auth_aclcheck($pname, $runas, array()); } else { $auth = auth_quickaclcheck($pname); } if ($auth < AUTH_CREATE) { throw new Exception($this->getLang('e_denied')); } } foreach ($templates as $pname => $template) { // set NSBASE var to make certain dataplugin constructs easier $patterns['__nsbase__'] = '/@NSBASE@/'; $values['__nsbase__'] = noNS(getNS($pname)); // save page saveWikiText($pname, $this->replace($patterns, $values, $template, false), sprintf($this->getLang('summary'), $ID)); } $ret = "<p>{$thanks}</p>"; // Build result tree $pages = array_keys($templates); usort($pages, array($this, '_sort')); $oldid = $ID; $data = array(); $last_folder = array(); foreach ($pages as $ID) { $lvl = substr_count($ID, ':'); for ($n = 0; $n < $lvl; ++$n) { if (!isset($last_folder[$n]) || strpos($ID, $last_folder[$n]['id']) !== 0) { $last_folder[$n] = array('id' => substr($ID, 0, strpos($ID, ':', ($n > 0 ? strlen($last_folder[$n - 1]['id']) : 0) + 1) + 1), 'level' => $n + 1, 'open' => 1); $data[] = $last_folder[$n]; } } $data[] = array('id' => $ID, 'level' => 1 + substr_count($ID, ':'), 'type' => 'f'); } $ret .= html_buildlist($data, 'idx', array($this, 'html_list_index'), 'html_li_index'); // Add indexer bugs for every just-created page $ret .= '<div class="no">'; ob_start(); foreach ($pages as $ID) { // indexerWebBug uses ID and INFO[exists], but the bureaucracy form // page always exists, as does the just-saved page, so INFO[exists] // is correct in any case tpl_indexerWebBug(); // the iframe will trigger real rendering of the pages to make sure // any used plugins are initialized (eg. the do plugin) echo '<iframe src="' . wl($ID, array('do' => 'export_html')) . '" width="1" height="1" style="visibility:hidden"></iframe>'; } $ret .= ob_get_contents(); ob_end_clean(); $ID = $oldid; $ret .= '</div>'; return $ret; }
/** * Returns the pagetemplate contents for the ID's namespace * * @author Andreas Gohr <*****@*****.**> */ function pageTemplate($data) { $id = $data[0]; global $conf; $path = dirname(wikiFN($id)); if (@file_exists($path . '/_template.txt')) { $tpl = io_readFile($path . '/_template.txt'); } else { // search upper namespaces for templates $len = strlen(rtrim($conf['datadir'], '/')); while (strlen($path) >= $len) { if (@file_exists($path . '/__template.txt')) { $tpl = io_readFile($path . '/__template.txt'); break; } $path = substr($path, 0, strrpos($path, '/')); } } return isset($tpl) ? parsePageTemplate($tpl, $id) : ''; }
/** * @param $fields * @param $tpl * @param $runas * @return string template */ function getTemplates($fields, $tpl, $runas) { global $USERINFO; global $conf; if ($tpl == '_') { // use namespace template if (!isset($this->templates[$this->pagename])) { $this->templates[$this->pagename] = pageTemplate(array($this->pagename)); } } elseif ($tpl !== '!') { $tpl = $this->replaceDefault($tpl); // Namespace link if ($runas) { // Hack user credentials. $backup = array($_SERVER['REMOTE_USER'], $USERINFO['grps']); $_SERVER['REMOTE_USER'] = $runas; $USERINFO['grps'] = array(); } $t_pages = array(); search($t_pages, $conf['datadir'], 'search_universal', array('depth' => 0, 'listfiles' => true, 'showhidden' => true), str_replace(':', '/', getNS($tpl))); foreach ($t_pages as $t_page) { $t_name = cleanID($t_page['id']); $p_name = preg_replace('/^' . preg_quote_cb(cleanID($tpl)) . '($|:)/', $this->pagename . '$1', $t_name); if ($p_name === $t_name) { // When using a single-page template, ignore other pages // in the same namespace. continue; } if (!isset($this->templates[$p_name])) { // load page data and do default pattern replacements like // namespace templates do $data = array('id' => $p_name, 'tpl' => rawWiki($t_name), 'doreplace' => true); parsePageTemplate($data); $this->templates[$p_name] = $this->replace(array('__lang__' => $this->patterns['__lang__'], '__trans__' => $this->patterns['__trans__']), array('__lang__' => $this->values['__lang__'], '__trans__' => $this->values['__trans__']), $data['tpl'], false); } } if ($runas) { /* Restore user credentials. */ list($_SERVER['REMOTE_USER'], $USERINFO['grps']) = $backup; } } return $tpl; }