xref: /dokuwiki/inc/parser/metadata.php (revision 08b9e7cabaff74983b2acc2a6bdbf526b17db9dd)
1<?php
2/**
3 * Renderer for metadata
4 *
5 * @author Esther Brunner <wikidesign@gmail.com>
6 */
7if(!defined('DOKU_INC')) die('meh.');
8
9if ( !defined('DOKU_LF') ) {
10    // Some whitespace to help View > Source
11    define ('DOKU_LF',"\n");
12}
13
14if ( !defined('DOKU_TAB') ) {
15    // Some whitespace to help View > Source
16    define ('DOKU_TAB',"\t");
17}
18
19require_once DOKU_INC . 'inc/parser/renderer.php';
20
21/**
22 * The Renderer
23 */
24class Doku_Renderer_metadata extends Doku_Renderer {
25
26  var $doc  = '';
27  var $meta = array();
28  var $persistent = array();
29
30  var $headers = array();
31  var $capture = true;
32  var $store   = '';
33  var $firstimage = '';
34
35  function getFormat(){
36    return 'metadata';
37  }
38
39  function document_start(){
40    global $ID;
41
42    $this->headers = array();
43
44    // external pages are missing create date
45    if(!$this->persistent['date']['created']){
46        $this->persistent['date']['created'] = filectime(wikiFN($ID));
47    }
48    if(!isset($this->persistent['user'])){
49        $this->persistent['user'] = '';
50    }
51    if(!isset($this->persistent['creator'])){
52        $this->persistent['creator'] = '';
53    }
54    // reset metadata to persistent values
55    $this->meta = $this->persistent;
56  }
57
58  function document_end(){
59    global $ID;
60
61    // store internal info in metadata (notoc,nocache)
62    $this->meta['internal'] = $this->info;
63
64    if (!isset($this->meta['description']['abstract'])){
65      // cut off too long abstracts
66      $this->doc = trim($this->doc);
67      if (strlen($this->doc) > 500)
68        $this->doc = utf8_substr($this->doc, 0, 500).'…';
69      $this->meta['description']['abstract'] = $this->doc;
70    }
71
72    $this->meta['relation']['firstimage'] = $this->firstimage;
73
74    if(!isset($this->meta['date']['modified'])){
75        $this->meta['date']['modified'] = filemtime(wikiFN($ID));
76    }
77
78  }
79
80  function toc_additem($id, $text, $level) {
81    global $conf;
82
83    //only add items within configured levels
84    if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']){
85      // the TOC is one of our standard ul list arrays ;-)
86      $this->meta['description']['tableofcontents'][] = array(
87        'hid'   => $id,
88        'title' => $text,
89        'type'  => 'ul',
90        'level' => $level-$conf['toptoclevel']+1
91      );
92    }
93
94  }
95
96  function header($text, $level, $pos) {
97    if (!isset($this->meta['title'])) $this->meta['title'] = $text;
98
99    // add the header to the TOC
100    $hid = $this->_headerToLink($text,'true');
101    $this->toc_additem($hid, $text, $level);
102
103    // add to summary
104    if ($this->capture && ($level > 1)) $this->doc .= DOKU_LF.$text.DOKU_LF;
105  }
106
107  function section_open($level){}
108  function section_close(){}
109
110  function cdata($text){
111    if ($this->capture) $this->doc .= $text;
112  }
113
114  function p_open(){
115    if ($this->capture) $this->doc .= DOKU_LF;
116  }
117
118  function p_close(){
119    if ($this->capture){
120      if (strlen($this->doc) > 250) $this->capture = false;
121      else $this->doc .= DOKU_LF;
122    }
123  }
124
125  function linebreak(){
126    if ($this->capture) $this->doc .= DOKU_LF;
127  }
128
129  function hr(){
130    if ($this->capture){
131      if (strlen($this->doc) > 250) $this->capture = false;
132      else $this->doc .= DOKU_LF.'----------'.DOKU_LF;
133    }
134  }
135
136  /**
137   * Callback for footnote start syntax
138   *
139   * All following content will go to the footnote instead of
140   * the document. To achieve this the previous rendered content
141   * is moved to $store and $doc is cleared
142   *
143   * @author Andreas Gohr <andi@splitbrain.org>
144   */
145  function footnote_open() {
146    if ($this->capture){
147      // move current content to store and record footnote
148      $this->store = $this->doc;
149      $this->doc   = '';
150    }
151  }
152
153  /**
154   * Callback for footnote end syntax
155   *
156   * All rendered content is moved to the $footnotes array and the old
157   * content is restored from $store again
158   *
159   * @author Andreas Gohr
160   */
161  function footnote_close() {
162    if ($this->capture){
163      // restore old content
164      $this->doc = $this->store;
165      $this->store = '';
166    }
167  }
168
169  function listu_open(){
170    if ($this->capture) $this->doc .= DOKU_LF;
171  }
172
173  function listu_close(){
174    if ($this->capture && (strlen($this->doc) > 250)) $this->capture = false;
175  }
176
177  function listo_open(){
178    if ($this->capture) $this->doc .= DOKU_LF;
179  }
180
181  function listo_close(){
182    if ($this->capture && (strlen($this->doc) > 250)) $this->capture = false;
183  }
184
185  function listitem_open($level){
186    if ($this->capture) $this->doc .= str_repeat(DOKU_TAB, $level).'* ';
187  }
188
189  function listitem_close(){
190    if ($this->capture) $this->doc .= DOKU_LF;
191  }
192
193  function listcontent_open(){}
194  function listcontent_close(){}
195
196  function unformatted($text){
197    if ($this->capture) $this->doc .= $text;
198  }
199
200  function preformatted($text){
201    if ($this->capture) $this->doc .= $text;
202  }
203
204  function file($text, $lang = null, $file = null){
205    if ($this->capture){
206      $this->doc .= DOKU_LF.$text;
207      if (strlen($this->doc) > 250) $this->capture = false;
208      else $this->doc .= DOKU_LF;
209    }
210  }
211
212  function quote_open(){
213    if ($this->capture) $this->doc .= DOKU_LF.DOKU_TAB.'"';
214  }
215
216  function quote_close(){
217    if ($this->capture){
218      $this->doc .= '"';
219      if (strlen($this->doc) > 250) $this->capture = false;
220      else $this->doc .= DOKU_LF;
221    }
222  }
223
224  function code($text, $language = NULL, $file = null){
225    if ($this->capture){
226      $this->doc .= DOKU_LF.$text;
227      if (strlen($this->doc) > 250) $this->capture = false;
228      else $this->doc .= DOKU_LF;
229    }
230  }
231
232  function acronym($acronym){
233    if ($this->capture) $this->doc .= $acronym;
234  }
235
236  function smiley($smiley){
237    if ($this->capture) $this->doc .= $smiley;
238  }
239
240  function entity($entity){
241    if ($this->capture) $this->doc .= $entity;
242  }
243
244  function multiplyentity($x, $y){
245    if ($this->capture) $this->doc .= $x.'×'.$y;
246  }
247
248  function singlequoteopening(){
249    global $lang;
250    if ($this->capture) $this->doc .= $lang['singlequoteopening'];
251  }
252
253  function singlequoteclosing(){
254    global $lang;
255    if ($this->capture) $this->doc .= $lang['singlequoteclosing'];
256  }
257
258  function apostrophe() {
259    global $lang;
260    if ($this->capture) $this->doc .= $lang['apostrophe'];
261  }
262
263  function doublequoteopening(){
264    global $lang;
265    if ($this->capture) $this->doc .= $lang['doublequoteopening'];
266  }
267
268  function doublequoteclosing(){
269    global $lang;
270    if ($this->capture) $this->doc .= $lang['doublequoteclosing'];
271  }
272
273  function camelcaselink($link) {
274    $this->internallink($link, $link);
275  }
276
277  function locallink($hash, $name = NULL){}
278
279  /**
280   * keep track of internal links in $this->meta['relation']['references']
281   */
282  function internallink($id, $name = NULL){
283    global $ID;
284
285    if(is_array($name))
286        $this->_firstimage($name['src']);
287
288    $default = $this->_simpleTitle($id);
289
290    // first resolve and clean up the $id
291    resolve_pageid(getNS($ID), $id, $exists);
292    list($page, $hash) = explode('#', $id, 2);
293
294    // set metadata
295    $this->meta['relation']['references'][$page] = $exists;
296    // $data = array('relation' => array('isreferencedby' => array($ID => true)));
297    // p_set_metadata($id, $data);
298
299    // add link title to summary
300    if ($this->capture){
301      $name = $this->_getLinkTitle($name, $default, $id);
302      $this->doc .= $name;
303    }
304  }
305
306  function externallink($url, $name = NULL){
307    if(is_array($name))
308        $this->_firstimage($name['src']);
309
310    if ($this->capture){
311      $this->doc .= $this->_getLinkTitle($name, '<' . $url . '>');
312    }
313  }
314
315  function interwikilink($match, $name = NULL, $wikiName, $wikiUri){
316    if(is_array($name))
317        $this->_firstimage($name['src']);
318
319    if ($this->capture){
320      list($wikiUri, $hash) = explode('#', $wikiUri, 2);
321      $name = $this->_getLinkTitle($name, $wikiUri);
322      $this->doc .= $name;
323    }
324  }
325
326  function windowssharelink($url, $name = NULL){
327    if(is_array($name))
328        $this->_firstimage($name['src']);
329
330    if ($this->capture){
331      if ($name) $this->doc .= $name;
332      else $this->doc .= '<'.$url.'>';
333    }
334  }
335
336  function emaillink($address, $name = NULL){
337    if(is_array($name))
338        $this->_firstimage($name['src']);
339
340    if ($this->capture){
341      if ($name) $this->doc .= $name;
342      else $this->doc .= '<'.$address.'>';
343    }
344  }
345
346  function internalmedia($src, $title=NULL, $align=NULL, $width=NULL,
347                         $height=NULL, $cache=NULL, $linking=NULL){
348    if ($this->capture && $title) $this->doc .= '['.$title.']';
349    $this->_firstimage($src);
350  }
351
352  function externalmedia($src, $title=NULL, $align=NULL, $width=NULL,
353                         $height=NULL, $cache=NULL, $linking=NULL){
354    if ($this->capture && $title) $this->doc .= '['.$title.']';
355    $this->_firstimage($src);
356  }
357
358  function rss($url,$params) {
359    $this->meta['relation']['haspart'][$url] = true;
360
361    $this->meta['date']['valid']['age'] =
362            isset($this->meta['date']['valid']['age']) ?
363                min($this->meta['date']['valid']['age'],$params['refresh']) :
364                $params['refresh'];
365  }
366
367  //----------------------------------------------------------
368  // Utils
369
370  /**
371   * Removes any Namespace from the given name but keeps
372   * casing and special chars
373   *
374   * @author Andreas Gohr <andi@splitbrain.org>
375   */
376  function _simpleTitle($name){
377    global $conf;
378
379    if(is_array($name)) return '';
380
381    if($conf['useslash']){
382        $nssep = '[:;/]';
383    }else{
384        $nssep = '[:;]';
385    }
386    $name = preg_replace('!.*'.$nssep.'!','',$name);
387    //if there is a hash we use the anchor name only
388    $name = preg_replace('!.*#!','',$name);
389    return $name;
390  }
391
392  /**
393   * Creates a linkid from a headline
394   *
395   * @param string  $title   The headline title
396   * @param boolean $create  Create a new unique ID?
397   * @author Andreas Gohr <andi@splitbrain.org>
398   */
399  function _headerToLink($title, $create=false) {
400      if($create){
401          return sectionID($title,$this->headers);
402      }else{
403          $check = false;
404          return sectionID($title,$check);
405      }
406  }
407
408  /**
409   * Construct a title and handle images in titles
410   *
411   * @author Harry Fuecks <hfuecks@gmail.com>
412   */
413  function _getLinkTitle($title, $default, $id=NULL) {
414    global $conf;
415
416    $isImage = false;
417    if (is_array($title)){
418      if($title['title']) return '['.$title['title'].']';
419    } else if (is_null($title) || trim($title)==''){
420      if (useHeading('content') && $id){
421        $heading = p_get_first_heading($id,METADATA_DONT_RENDER);
422        if ($heading) return $heading;
423      }
424      return $default;
425    } else {
426      return $title;
427    }
428  }
429
430  function _firstimage($src){
431    if($this->firstimage) return;
432    global $ID;
433
434    list($src,$hash) = explode('#',$src,2);
435    if(!preg_match('/^https?:\/\//i',$src)){
436        resolve_mediaid(getNS($ID),$src, $exists);
437    }
438    if(preg_match('/.(jpe?g|gif|png)$/i',$src)){
439        $this->firstimage = $src;
440    }
441  }
442}
443
444//Setup VIM: ex: et ts=4 :
445