1<?php 2/** 3 * Random Include Plugin: displays a wiki page within another 4 * Usage: 5 * {{randominc>namespace}} to random include a page from "namespace" 6 * {{randomincsec>namespace}} see Include plugin 7 * 8 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 9 * @author Esther Brunner <wikidesign@gmail.com> 10 * @author Christopher Smith <chris@jalakai.co.uk> 11 * @author Vittorio Rigamonti <rigazilla@gmail.com> 12 */ 13 14if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); 15if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 16 17/** 18 * All DokuWiki plugins to extend the parser/rendering mechanism 19 * need to inherit from this class 20 */ 21class syntax_plugin_randominc extends DokuWiki_Syntax_Plugin { 22 23 function getType(){ return 'substition'; } 24 function getSort(){ return 303; } 25 function getPType(){ return 'block'; } 26 27 function connectTo($mode) { 28 $this->Lexer->addSpecialPattern("{{randominc>.+?}}", $mode, 'plugin_randominc'); 29 $this->Lexer->addSpecialPattern("{{randomincsec>.+?}}", $mode, 'plugin_randominc'); 30 } 31 32 function handle($match, $state, $pos, Doku_Handler $handler) { 33 // strip markup 34 $match = substr($match, 2, -2); 35 list($match, $flags) = explode('&', $match, 2); 36 37 // break the pattern up into its constituent parts 38 list($include, $id, $section) = preg_split('/>|#/u', $match, 3); 39 return array($include, $id, cleanID($section), explode('&', $flags), $pos); 40 } 41 42 function _randompage($ns, $depth=0) { 43 require_once(DOKU_INC.'inc/search.php'); 44 global $conf; 45 global $ID; 46 47 $dir = $conf['datadir']; 48 $ns = cleanID($ns); 49 50 #fixme use appropriate function 51 if(empty($ns)) { 52 $ns = dirname(str_replace(':','/',$ID)); 53 if($ns == '.') $ns =''; 54 } 55 56 $dir = $conf['datadir'].'/'.str_replace(':','/',$ns); 57 $ns = str_replace('/',':',$ns); 58 59 $data = array(); 60 search($data,$dir,'search_allpages',array('ns' => $ns, 'depth' => $depth)); 61 62 if (count($data) == 0) { 63 return ''; 64 } 65 $page = $data[array_rand($data, 1)][id]; 66 return $page; 67 } 68 69 //Function from Php manual to get a random number in a Array 70 function array_rand($array, $lim=1) { 71 mt_srand((double) microtime() * 1000000); 72 for($a=0; $a<=$lim; $a++){ 73 $num[] = mt_srand(0, count($array)-1); 74 } 75 return @$num; 76 } 77 78 /** 79 * Renders the included page(s) 80 * 81 * @author Michael Hamann <michael@content-space.de> 82 */ 83 function render($format, Doku_Renderer $renderer, $data) { 84 global $ID; 85 86 // static stack that records all ancestors of the child pages 87 static $page_stack = array(); 88 89 // when there is no id just assume the global $ID is the current id 90 if (empty($page_stack)) $page_stack[] = $ID; 91 92 $parent_id = $page_stack[count($page_stack)-1]; 93 $root_id = $page_stack[0]; 94 95 list($type, $raw_id, $section, $flags, $pos) = $data; 96 $id = $this->_applyMacro($raw_id); 97 resolve_pageid(getNS($ID), $id, $exists); // resolve shortcuts 98 $ns=getNS($id.':dummy'); 99 100 // Get randominc specific flags, the rest will be passed to include plugin 101 $flagsarray = array(); 102 $this->getFlags($flags, $flagsarray); 103 104 $page = $this->_randompage($ns, $flagsarray['depth']); 105 if (empty($page)) { 106 msg($this->getLang('nopagemsg')); 107 return true; 108 } 109 $an_id=$ns.':'.$page; 110 resolve_pageid(getNS($ID), $an_id, $exists); // resolve shortcuts 111 $page = $an_id; 112 113 // Map randominc syntax to include plugin mode 114 switch ($type) 115 { 116 case 'randomincsec': 117 $mode = 'section'; 118 $sect = $section; 119 break; 120 default: 121 $mode = 'page'; 122 $sect = NULL; 123 break; 124 } 125 126 if (!$this->helper) 127 { 128 $this->helper = plugin_load('helper', 'include'); 129 if (!$this->helper) 130 { 131 msg($this->getLang('plugin_include_failure'), -1); 132 return true; 133 } 134 } 135 $flags = $this->helper->get_flags($flags); 136 137 $pages = $this->helper->_get_included_pages($mode, $page, $sect, $parent_id, $flags); 138 139 if ($format == 'metadata') { 140 /** @var Doku_Renderer_metadata $renderer */ 141 142 // remove old persistent metadata of previous versions of the include plugin 143 if (isset($renderer->persistent['plugin_include'])) { 144 unset($renderer->persistent['plugin_include']); 145 unset($renderer->meta['plugin_include']); 146 } 147 148 $renderer->meta['plugin_include']['instructions'][] = compact('mode', 'page', 'sect', 'parent_id', $flags); 149 if (!isset($renderer->meta['plugin_include']['pages'])) 150 $renderer->meta['plugin_include']['pages'] = array(); // add an array for array_merge 151 $renderer->meta['plugin_include']['pages'] = array_merge($renderer->meta['plugin_include']['pages'], $pages); 152 $renderer->meta['plugin_include']['include_content'] = isset($_REQUEST['include_content']); 153 } 154 155 $secids = array(); 156 if ($format == 'xhtml' || $format == 'odt') { 157 $secids = p_get_metadata($ID, 'plugin_include secids'); 158 } 159 160 // Embed page into an extra div for randominc's own max-width and max-height flags 161 if ($format == 'xhtml') { 162 $renderer->doc .= '<div class="entry-content" style="overflow: hidden;'.$flagsarray['max-width'].';'.$flagsarray['max-height'].'">'.DOKU_LF; 163 } 164 165 foreach ($pages as $page) { 166 extract($page); 167 $id = $page['id']; 168 $exists = $page['exists']; 169 170 if (in_array($id, $page_stack)) continue; 171 array_push($page_stack, $id); 172 173 // add references for backlink 174 if ($format == 'metadata') { 175 $renderer->meta['relation']['references'][$id] = $exists; 176 $renderer->meta['relation']['haspart'][$id] = $exists; 177 if (!$sect && !$flags['firstsec'] && !$flags['linkonly'] && !isset($renderer->meta['plugin_include']['secids'][$id])) { 178 $renderer->meta['plugin_include']['secids'][$id] = array('hid' => 'plugin_include__'.str_replace(':', '__', $id), 'pos' => $pos); 179 } 180 } 181 182 if (isset($secids[$id]) && $pos === $secids[$id]['pos']) { 183 $flags['include_secid'] = $secids[$id]['hid']; 184 } else { 185 unset($flags['include_secid']); 186 } 187 188 $instructions = $this->helper->_get_instructions($id, $sect, $mode, $level, $flags, $root_id, $secids); 189 190 if (!$flags['editbtn']) { 191 global $conf; 192 $maxseclevel_org = $conf['maxseclevel']; 193 $conf['maxseclevel'] = 0; 194 } 195 $renderer->nest($instructions); 196 if (isset($maxseclevel_org)) { 197 $conf['maxseclevel'] = $maxseclevel_org; 198 unset($maxseclevel_org); 199 } 200 201 array_pop($page_stack); 202 } 203 204 // When all includes have been handled remove the current id 205 // in order to allow the rendering of other pages 206 if (count($page_stack) == 1) array_pop($page_stack); 207 208 // Close our div 209 if ($format == 'xhtml') { 210 $renderer->doc .= '</div>'.DOKU_LF; 211 } 212 213 return true; 214 } 215 216/* ---------- Util Functions ---------- */ 217 218 /** 219 * Makes user or date dependent includes possible 220 */ 221 protected function _applyMacro($id) { 222 global $INFO, $auth; 223 224 $user = $_SERVER['REMOTE_USER']; 225 $userdata = $auth->getUserData($user); 226 $group = $userdata['grps'][0]; 227 228 $replace = array( 229 '@USER@' => cleanID($user), 230 '@NAME@' => cleanID($INFO['userinfo']['name']), 231 '@GROUP@' => cleanID($group), 232 '@YEAR@' => date('Y'), 233 '@MONTH@' => date('m'), 234 '@DAY@' => date('d'), 235 ); 236 return str_replace(array_keys($replace), array_values($replace), $id); 237 } 238 239 /** 240 * Get randominc specific flags 241 */ 242 protected function getFlags($flags, &$flagsarray) { 243 $flagsarray['depth'] = 0; 244 foreach ($flags as $flag) { 245 if (!(strncmp($flag,'max-width',9) || strpos($flag,';'))) 246 { 247 $flagsarray['max-width'] = $flag; 248 } 249 if (!(strncmp($flag,'max-height',10) || strpos($flag,';'))) 250 { 251 $flagsarray['max-height'] = $flag; 252 } 253 if (!(strncmp($flag,'depth',5) || strpos($flag,';'))) 254 { 255 $equal = strpos($flag,'='); 256 if ($equal !== false) 257 { 258 $flagsarray['depth'] = substr($flag, $equal+1); 259 } 260 } 261 } 262 } 263} 264 265//Setup VIM: ex: et ts=4 enc=utf-8 : 266