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