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