xref: /plugin/include/helper.php (revision a58d2b0a015a970df79bd8a420ee0b237332ab52)
1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Esther Brunner <wikidesign@gmail.com>
5 */
6
7// must be run within Dokuwiki
8if (!defined('DOKU_INC')) die();
9
10if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
11if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
12if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
13
14class helper_plugin_include extends DokuWiki_Plugin { // DokuWiki_Helper_Plugin
15
16  var $pages     = array();   // filechain of included pages
17  var $page      = array();   // associative array with data about the page to include
18  var $ins       = array();   // instructions array
19  var $doc       = '';        // the final output XHTML string
20  var $mode      = 'section'; // inclusion mode: 'page' or 'section'
21  var $clevel    = 0;         // current section level
22  var $firstsec  = 0;         // show first section only
23  var $footer    = 1;         // show metaline below page
24  var $header    = array();   // included page / section header
25  var $renderer  = NULL;      // DokuWiki renderer object
26
27  // private variables
28  var $_offset   = NULL;
29
30  /**
31   * Constructor loads some config settings
32   */
33  function helper_plugin_include(){
34    $this->firstsec = $this->getConf('firstseconly');
35    $this->footer = $this->getConf('showfooter');
36  }
37
38  function getInfo(){
39    return array(
40      'author' => 'Esther Brunner',
41      'email'  => 'wikidesign@gmail.com',
42      'date'   => '2007-04-27',
43      'name'   => 'Include Plugin (helper class)',
44      'desc'   => 'Functions to include another page in a wiki page',
45      'url'    => 'http://www.wikidesign/en/plugin/include/start',
46    );
47  }
48
49  function getMethods(){
50    $result = array();
51    $result[] = array(
52      'name'   => 'setPage',
53      'desc'   => 'sets the page to include',
54      'params' => array("page attributes, 'id' required, 'section' for filtering" => 'array'),
55      'return' => array('success' => 'boolean'),
56    );
57    $result[] = array(
58      'name'   => 'setMode',
59      'desc'   => 'sets inclusion mode: should indention be merged?',
60      'params' => array("'page' (original) or 'section' (merged indention)" => 'string'),
61    );
62    $result[] = array(
63      'name'   => 'setLevel',
64      'desc'   => 'sets the indention for the current section level',
65      'params' => array('level: 0 to 5' => 'integer'),
66      'return' => array('success' => 'boolean'),
67    );
68    $result[] = array(
69      'name'   => 'setFlags',
70      'desc'   => 'overrides standard values for showfooter and firstseconly settings',
71      'params' => array('flags' => 'array'),
72    );
73    $result[] = array(
74      'name'   => 'renderXHTML',
75      'desc'   => 'renders the XHTML output of the included page',
76      'params' => array('DokuWiki renderer' => 'object'),
77      'return' => array('XHTML' => 'string'),
78    );
79    return $result;
80  }
81
82  /**
83   * Sets the page to include if it is not already included (prevent recursion)
84   */
85  function setPage($page){
86    global $ID;
87
88    $id     = $page['id'];
89    $fullid = $id.'#'.$page['section'];
90
91    if (!$id) return false;       // no page id given
92    if ($id == $ID) return false; // page can't include itself
93
94    // prevent include recursion
95    if ((isset($this->pages[$id.'#'])) || (isset($this->pages[$fullid]))) return false;
96
97    // add the page to the filechain
98    $this->pages[$fullid] = $page;
99    $this->page =& $this->pages[$fullid];
100    return true;
101  }
102
103  /**
104   * Sets the inclusion mode
105   */
106  function setMode($mode){
107    $this->mode = $mode;
108  }
109
110  /**
111   * Sets the right indention for a given section level
112   */
113  function setLevel($level){
114    if ((is_numeric($level)) && ($level >= 0) && ($level <= 5)){
115      $this->clevel = $level;
116      return true;
117    }
118    return false;
119  }
120
121  /**
122   * Overrides standard values for showfooter and firstseconly settings
123   */
124  function setFlags($flags){
125    foreach ($flags as $flag){
126      switch ($flag){
127      case 'footer':
128        $this->footer = 1;
129        break;
130      case 'nofooter':
131        $this->footer = 0;
132        break;
133      case 'firstseconly':
134        $this->firstsec = 1;
135        break;
136      case 'fullpage':
137        $this->firstsec = 0;
138        break;
139      }
140    }
141  }
142
143  /**
144   * Builds the XHTML to embed the page to include
145   */
146  function renderXHTML(&$renderer){
147    if (!$this->page['id']) return ''; // page must be set first
148    if (!$this->page['exists'] && ($this->page['perm'] < AUTH_CREATE)) return '';
149
150    // prepare variables
151    $this->doc      = '';
152    $this->renderer =& $renderer;
153
154    // get instructions and render them on the fly
155    $this->page['file'] = wikiFN($this->page['id']);
156    $this->ins = p_cached_instructions($this->page['file']);
157
158    // show only a given section?
159    if ($this->page['section'] && $this->page['exists']) $this->_getSection();
160
161    // convert relative links
162    $this->_convertInstructions();
163
164    // render the included page
165    if ($this->header) $content = '<h'.$this->header['level'].' class="entry-title">'.
166      '<a name="'.$this->header['hid'].'" id="'.$this->header['hid'].'">'.
167      $this->header['title'].'</a></h'.$this->header['level'].'>'.DOKU_LF;
168    else $content = '';
169    $content .= '<div class="entry-content">'.DOKU_LF.
170      $this->_cleanXHTML(p_render('xhtml', $this->ins, $info)).DOKU_LF.
171      '</div>'.DOKU_LF;
172
173    // embed the included page
174    $class = ($this->page['draft'] ? 'include draft' : 'include');
175    $renderer->doc .= '<div class="'.$class.' hentry"'.$this->_showTagLogos().'>'.DOKU_LF;
176    if (!$this->header && $this->clevel && ($this->mode == 'section'))
177      $renderer->doc .= '<div class="level'.$this->clevel.'">'.DOKU_LF;
178    if ((@file_exists(DOKU_PLUGIN.'editsections/action.php'))
179      && (!plugin_isdisabled('editsections'))){ // for Edit Section Reorganizer Plugin
180      $renderer->doc .= $this->_editButton().$content;
181    } else {
182      $renderer->doc .= $content.$this->_editButton();
183    }
184
185    if (!$this->header && $this->clevel && ($this->mode == 'section'))
186      $renderer->doc .= '</div>'.DOKU_LF; // class="level?"
187    $renderer->doc .= '</div>'.DOKU_LF; // class="include hentry"
188
189    // output meta line (if wanted) and remove page from filechain
190    $renderer->doc .= $this->_footer(array_pop($this->pages));
191    $this->helper_plugin_include();
192
193    return $this->doc;
194  }
195
196/* ---------- Private Methods ---------- */
197
198  /**
199   * Get a section including its subsections
200   */
201  function _getSection(){
202    foreach ($this->ins as $ins){
203      if ($ins[0] == 'header'){
204
205        // found the right header
206        if (cleanID($ins[1][0]) == $this->page['section']){
207          $level = $ins[1][1];
208          $i[] = $ins;
209
210        // next header of the same or higher level -> exit
211        } elseif ($ins[1][1] <= $level){
212          $this->ins = $i;
213          return true;
214        } elseif (isset($level)){
215          $i[] = $ins;
216        }
217
218      // add instructions from our section
219      } elseif (isset($level)){
220        $i[] = $ins;
221      }
222    }
223    $this->ins = $i;
224    return true;
225  }
226
227  /**
228   * Corrects relative internal links and media and
229   * converts headers of included pages to subheaders of the current page
230   */
231  function _convertInstructions(){
232    global $ID;
233    global $conf;
234
235    $this->header = array();
236    $offset = $this->clevel;
237
238    if (!$this->page['exists']) return false;
239
240    // check if included page is in same namespace
241    $inclNS = getNS($this->page['id']);
242    if (getNS($ID) == $inclNS) $convert = false;
243    else $convert = true;
244
245    $n = count($this->ins);
246    for ($i = 0; $i < $n; $i++){
247      $current = $this->ins[$i][0];
248
249      // convert internal links and media from relative to absolute
250      if ($convert && (substr($current, 0, 8) == 'internal')){
251        $this->ins[$i][1][0] = $this->_convertInternalLinks($i, $inclNS);
252
253      // set header level to current section level + header level
254      } elseif ($current == 'header'){
255        $this->_convertHeaders($i);
256
257      // the same for sections
258      } elseif ($current == 'section_open'){
259        $this->ins[$i][1][0] = $this->_convertSectionLevel($this->ins[$i][1][0]);
260
261      // show only the first section?
262      } elseif ($this->firstsec && ($current == 'section_close')
263        && ($this->ins[$i-1][0] != 'section_open')){
264        $this->_readMore($i);
265        return true;
266      }
267    }
268    $this->_finishConvert();
269    return true;
270  }
271
272  /**
273   * Convert relative internal links and media
274   *
275   * @param    integer $i: counter for current instruction
276   * @param    string  $ns: namespace of included page
277   * @return   string  $link: converted, now absolute link
278   */
279  function _convertInternalLinks($i, $ns){
280
281    // relative subnamespace
282    if ($this->ins[$i][1][0]{0} == '.'){
283
284      // parent namespace
285      if ($this->ins[$i][1][0]{1} == '.')
286        return getNS($ns).':'.substr($this->ins[$i][1][0], 2);
287
288      // current namespace
289      else
290        return $ns.':'.substr($this->ins[$i][1][0], 1);
291
292    // relative link
293    } elseif (strpos($this->ins[$i][1][0], ':') === false){
294      return $ns.':'.$this->ins[$i][1][0];
295    }
296  }
297
298  /**
299   * Convert header level and add header to TOC
300   *
301   * @param    integer $i: counter for current instruction
302   * @return   boolean true
303   */
304  function _convertHeaders($i){
305    $text   = $this->ins[$i][1][0];
306    $hid    = $this->renderer->_headerToLink($text, 'true');
307    if (empty($this->header)){
308      $this->_offset = $this->clevel - $this->ins[$i][1][1] + 1;
309      $level  = $this->clevel + 1;
310      $this->header = array(
311        'hid'   => $hid,
312        'title' => hsc($text),
313        'level' => $level
314      );
315      unset($this->ins[$i]);
316    } else {
317      $level = $this->_convertSectionLevel($this->ins[$i][1][1]);
318      $this->ins[$i][1][1] = $level;
319    }
320
321    // add TOC item
322    if (($level >= $conf['toptoclevel']) && ($level <= $conf['maxtoclevel'])){
323      $this->renderer->toc[] = array(
324        'hid'   => $hid,
325        'title' => $text,
326        'type'  => 'ul',
327        'level' => $level - $conf['toptoclevel'] + 1
328      );
329    }
330    return true;
331  }
332
333  /**
334   * Convert the level of headers and sections
335   *
336   * @param    integer $in: current level
337   * @return   integer $out: converted level
338   */
339  function _convertSectionLevel($in){
340    $out = $in + $this->_offset;
341    if ($out > 5) $out = 5;
342    return $out;
343  }
344
345  /**
346   * Adds a read more... link at the bottom of the first section
347   *
348   * @param    integer $i: counter for current instruction
349   * @return   boolean true
350   */
351  function _readMore($i){
352    $more = ((is_array($this->ins[$i+1])) && ($this->ins[$i+1][0] != 'document_end'));
353
354    if ($this->ins[0][0] == 'document_start') $this->ins = array_slice($this->ins, 1, $i);
355    else $this->ins = array_slice($this->ins, 0, $i);
356
357    if ($more){
358      array_unshift($this->ins, array('document_start', array(), 0));
359      $last = array_pop($this->ins);
360      $this->ins[] = array('p_open', array(), $last[2]);
361      $this->ins[] = array('internallink',array($this->page['id'], $this->getLang('readmore')),$last[2]);
362      $this->ins[] = array('p_close', array(), $last[2]);
363      $this->ins[] = $last;
364      $this->ins[] = array('document_end', array(), $last[2]);
365    } else {
366      $this->_finishConvert();
367    }
368    return true;
369  }
370
371  /**
372   * Adds 'document_start' and 'document_end' instructions if not already there
373   */
374  function _finishConvert(){
375    if ($this->ins[0][0] != 'document_start'){
376      array_unshift($this->ins, array('document_start', array(), 0));
377      $this->ins[] = array('document_end', array(), 0);
378    }
379  }
380
381  /**
382   * Remove TOC, section edit buttons and tags
383   */
384  function _cleanXHTML($xhtml){
385    preg_match('!<div class="tags">.*?</div>!s', $xhtml, $match);
386    $this->page['tags'] = $match[0];
387    $replace = array(
388      '!<div class="toc">.*?(</div>\n</div>)!s'   => '', // remove toc
389      '#<!-- SECTION "(.*?)" \[(\d+-\d*)\] -->#e' => '', // remove section edit buttons
390      '!<div class="tags">.*?(</div>)!s'          => '', // remove category tags
391    );
392    $xhtml  = preg_replace(array_keys($replace), array_values($replace), $xhtml);
393    return $xhtml;
394  }
395
396  /**
397   * Optionally display logo for the first tag found in the included page
398   */
399  function _showTagLogos(){
400    if (!$this->getConf('showtaglogos')) return '';
401
402    preg_match_all('/<a [^>]*title="(.*?)" rel="tag"[^>]*>([^<]*)</', $this->page['tags'], $tag);
403    $logoID  = getNS($tag[1][0]).':'.$tag[2][0];
404    $logosrc = mediaFN($logoID);
405    $types = array('.png', '.jpg', '.gif'); // auto-detect filetype
406    foreach ($types as $type){
407      if (!@file_exists($logosrc.$type)) continue;
408      $logoID  .= $type;
409      $logosrc .= $type;
410      list($w, $h, $t, $a) = getimagesize($logosrc);
411      return ' style="min-height: '.$h.'px">'.
412        '<img class="mediaright" src="'.ml($logoID).'" alt="'.$tag[2][0].'"/';
413    }
414    return '';
415  }
416
417  /**
418   * Display an edit button for the included page
419   */
420  function _editButton(){
421    if (!isset($this->page['perm']))
422      $this->page['perm'] = auth_quickaclcheck($this->page['id']);
423    if (@file_exists($this->page['file'])){
424      if (($this->page['perm'] >= AUTH_EDIT) && (is_writable($this->page['file'])))
425        $action = 'edit';
426      else return '';
427    } elseif ($this->page['perm'] >= AUTH_CREATE){
428      $action = 'create';
429    }
430    if ($this->getConf('showeditbtn')){
431      return '<div class="secedit">'.DOKU_LF.DOKU_TAB.
432        html_btn($action, $this->page['id'], '', array('do' => 'edit'), 'post').DOKU_LF.
433        '</div>'.DOKU_LF;
434    } else {
435      return '';
436    }
437  }
438
439  /**
440   * Returns the meta line below the included page
441   */
442  function _footer($page){
443    global $conf;
444
445    if (!$this->footer) return ''; // '<div class="inclmeta">&nbsp;</div>'.DOKU_LF;
446
447    $id   = $page['id'];
448    $meta = p_get_metadata($id);
449    $ret  = array();
450
451    // permalink
452    if ($this->getConf('showlink')){
453      $title = ($page['title'] ? $page['title'] : $meta['title']);
454      if (!$title) $title = str_replace('_', ' ', noNS($id));
455      $class = ($page['exists'] ? 'wikilink1' : 'wikilink2');
456      $link = array(
457        'url'    => wl($id),
458        'title'  => $id,
459        'name'   => hsc($title),
460        'target' => $conf['target']['wiki'],
461        'class'  => $class.' permalink',
462        'more'   => 'rel="bookmark"',
463      );
464      $ret[] = $this->renderer->_formatLink($link);
465    }
466
467    // date
468    if ($this->getConf('showdate')){
469      $date = ($page['date'] ? $page['date'] : $meta['date']['created']);
470      if ($date)
471        $ret[] = '<abbr class="published" title="'.gmdate('Y-m-d\TH:i:s\Z', $date).'">'.
472        date($conf['dformat'], $date).
473        '</abbr>';
474    }
475
476    // author
477    if ($this->getConf('showuser')){
478      $author   = ($page['user'] ? $page['user'] : $meta['creator']);
479      if ($author){
480        $userpage = cleanID($this->getConf('usernamespace').':'.$author);
481        resolve_pageid(getNS($ID), $id, $exists);
482        $class = ($exists ? 'wikilink1' : 'wikilink2');
483        $link = array(
484          'url'    => wl($userpage),
485          'title'  => $userpage,
486          'name'   => hsc($author),
487          'target' => $conf['target']['wiki'],
488          'class'  => $class.' url fn',
489          'pre'    => '<span class="vcard author">',
490          'suf'    => '</span>',
491        );
492        $ret[]    = $this->renderer->_formatLink($link);
493      }
494    }
495
496    // comments - let Discussion Plugin do the work for us
497    if (!$page['section'] && $this->getConf('showcomments')
498      && (!plugin_isdisabled('discussion'))
499      && ($discussion =& plugin_load('helper', 'discussion'))){
500      $disc = $discussion->td($id);
501      if ($disc) $ret[] = '<span class="comment">'.$disc.'</span>';
502    }
503
504    // linkbacks - let Linkback Plugin do the work for us
505    if (!$page['section'] && $this->getConf('showlinkbacks')
506      && (!plugin_isdisabled('linkback'))
507      && ($linkback =& plugin_load('helper', 'linkback'))){
508      $link = $linkback->td($id);
509      if ($link) $ret[] = '<span class="linkback">'.$link.'</span>';
510    }
511
512    $ret = implode(' &middot; ', $ret);
513
514    // tags
515    if (($this->getConf('showtags')) && ($page['tags'])){
516      $ret = $this->page['tags'].$ret;
517    }
518
519    if (!$ret) $ret = '&nbsp;';
520    return '<div class="inclmeta">'.DOKU_LF.$ret.DOKU_LF.'</div>'.DOKU_LF;
521  }
522
523}
524
525//Setup VIM: ex: et ts=4 enc=utf-8 :
526