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