xref: /dokuwiki/inc/parser/metadata.php (revision e0c26282a603881e8d2f839d94c28dbbfc57d71b)
139a89382SEsther Brunner<?php
239a89382SEsther Brunner/**
339a89382SEsther Brunner * Renderer for metadata
439a89382SEsther Brunner *
539a89382SEsther Brunner * @author Esther Brunner <wikidesign@gmail.com>
639a89382SEsther Brunner */
7fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.');
839a89382SEsther Brunner
939a89382SEsther Brunnerif(!defined('DOKU_LF')) {
1039a89382SEsther Brunner    // Some whitespace to help View > Source
1139a89382SEsther Brunner    define ('DOKU_LF', "\n");
1239a89382SEsther Brunner}
1339a89382SEsther Brunner
1439a89382SEsther Brunnerif(!defined('DOKU_TAB')) {
1539a89382SEsther Brunner    // Some whitespace to help View > Source
1639a89382SEsther Brunner    define ('DOKU_TAB', "\t");
1739a89382SEsther Brunner}
1839a89382SEsther Brunner
1939a89382SEsther Brunner/**
2093075b01SAndreas Gohr * The MetaData Renderer
2193075b01SAndreas Gohr *
2293075b01SAndreas Gohr * Metadata is additional information about a DokuWiki page that gets extracted mainly from the page's content
2393075b01SAndreas Gohr * but also it's own filesystem data (like the creation time). All metadata is stored in the fields $meta and
2493075b01SAndreas Gohr * $persistent.
2593075b01SAndreas Gohr *
2693075b01SAndreas Gohr * Some simplified rendering to $doc is done to gather the page's (text-only) abstract.
2739a89382SEsther Brunner */
2839a89382SEsther Brunnerclass Doku_Renderer_metadata extends Doku_Renderer {
2917076c2aSAndreas Gohr    /** the approximate byte lenght to capture for the abstract */
3017076c2aSAndreas Gohr    const ABSTRACT_LEN = 250;
3117076c2aSAndreas Gohr
3217076c2aSAndreas Gohr    /** the maximum UTF8 character length for the abstract */
3317076c2aSAndreas Gohr    const ABSTRACT_MAX = 500;
3439a89382SEsther Brunner
3593075b01SAndreas Gohr    /** @var array transient meta data, will be reset on each rendering */
3693075b01SAndreas Gohr    public $meta = array();
3739a89382SEsther Brunner
3893075b01SAndreas Gohr    /** @var array persistent meta data, will be kept until explicitly deleted */
3993075b01SAndreas Gohr    public $persistent = array();
4039a89382SEsther Brunner
4193075b01SAndreas Gohr    /** @var array the list of headers used to create unique link ids */
4293075b01SAndreas Gohr    protected $headers = array();
4393075b01SAndreas Gohr
4493075b01SAndreas Gohr    /** @var string temporary $doc store */
4593075b01SAndreas Gohr    protected $store = '';
4693075b01SAndreas Gohr
4793075b01SAndreas Gohr    /** @var string keeps the first image reference */
4893075b01SAndreas Gohr    protected $firstimage = '';
4993075b01SAndreas Gohr
5017076c2aSAndreas Gohr    /** @var bool determines if enough data for the abstract was collected, yet */
518f2758f6SAndreas Gohr    public $capture = true;
5217076c2aSAndreas Gohr
5317076c2aSAndreas Gohr    /** @var int number of bytes captured for abstract */
5417076c2aSAndreas Gohr    protected $captured = 0;
5517076c2aSAndreas Gohr
5693075b01SAndreas Gohr    /**
5793075b01SAndreas Gohr     * Returns the format produced by this renderer.
5893075b01SAndreas Gohr     *
5993075b01SAndreas Gohr     * @return string always 'metadata'
6093075b01SAndreas Gohr     */
615f70445dSAndreas Gohr    function getFormat() {
625f70445dSAndreas Gohr        return 'metadata';
635f70445dSAndreas Gohr    }
645f70445dSAndreas Gohr
6593075b01SAndreas Gohr    /**
6693075b01SAndreas Gohr     * Initialize the document
6793075b01SAndreas Gohr     *
6893075b01SAndreas Gohr     * Sets up some of the persistent info about the page if it doesn't exist, yet.
6993075b01SAndreas Gohr     */
7039a89382SEsther Brunner    function document_start() {
71ef47e298SAndreas Gohr        global $ID;
72620f4930SAndreas Gohr
73620f4930SAndreas Gohr        $this->headers = array();
74620f4930SAndreas Gohr
75ef47e298SAndreas Gohr        // external pages are missing create date
76ef47e298SAndreas Gohr        if(!$this->persistent['date']['created']) {
77ef47e298SAndreas Gohr            $this->persistent['date']['created'] = filectime(wikiFN($ID));
78ef47e298SAndreas Gohr        }
79a8e72133SAndreas Gohr        if(!isset($this->persistent['user'])) {
80a8e72133SAndreas Gohr            $this->persistent['user'] = '';
81a8e72133SAndreas Gohr        }
82ef47e298SAndreas Gohr        if(!isset($this->persistent['creator'])) {
83ef47e298SAndreas Gohr            $this->persistent['creator'] = '';
84ef47e298SAndreas Gohr        }
850a7e3bceSchris        // reset metadata to persistent values
860a7e3bceSchris        $this->meta = $this->persistent;
8739a89382SEsther Brunner    }
8839a89382SEsther Brunner
8993075b01SAndreas Gohr    /**
9093075b01SAndreas Gohr     * Finalize the document
9193075b01SAndreas Gohr     *
9293075b01SAndreas Gohr     * Stores collected data in the metadata
9393075b01SAndreas Gohr     */
9439a89382SEsther Brunner    function document_end() {
95482cff99SAndreas Gohr        global $ID;
96482cff99SAndreas Gohr
97b8595a66SAndreas Gohr        // store internal info in metadata (notoc,nocache)
98b8595a66SAndreas Gohr        $this->meta['internal'] = $this->info;
99b8595a66SAndreas Gohr
100c66972f2SAdrian Lang        if(!isset($this->meta['description']['abstract'])) {
10139a89382SEsther Brunner            // cut off too long abstracts
10239a89382SEsther Brunner            $this->doc = trim($this->doc);
10317076c2aSAndreas Gohr            if(strlen($this->doc) > self::ABSTRACT_MAX) {
10417076c2aSAndreas Gohr                $this->doc = utf8_substr($this->doc, 0, self::ABSTRACT_MAX).'…';
10517076c2aSAndreas Gohr            }
10639a89382SEsther Brunner            $this->meta['description']['abstract'] = $this->doc;
10739a89382SEsther Brunner        }
108aa5a2937SAndreas Gohr
109aa5a2937SAndreas Gohr        $this->meta['relation']['firstimage'] = $this->firstimage;
110482cff99SAndreas Gohr
111c0322273SAdrian Lang        if(!isset($this->meta['date']['modified'])) {
112482cff99SAndreas Gohr            $this->meta['date']['modified'] = filemtime(wikiFN($ID));
113482cff99SAndreas Gohr        }
114482cff99SAndreas Gohr
11539a89382SEsther Brunner    }
11639a89382SEsther Brunner
11793075b01SAndreas Gohr    /**
11817076c2aSAndreas Gohr     * Render plain text data
11917076c2aSAndreas Gohr     *
12017076c2aSAndreas Gohr     * This function takes care of the amount captured data and will stop capturing when
12117076c2aSAndreas Gohr     * enough abstract data is available
12217076c2aSAndreas Gohr     *
12317076c2aSAndreas Gohr     * @param $text
12417076c2aSAndreas Gohr     */
12517076c2aSAndreas Gohr    function cdata($text) {
12617076c2aSAndreas Gohr        if(!$this->capture) return;
12717076c2aSAndreas Gohr
12817076c2aSAndreas Gohr        $this->doc .= $text;
12917076c2aSAndreas Gohr
13017076c2aSAndreas Gohr        $this->captured += strlen($text);
13117076c2aSAndreas Gohr        if($this->captured > self::ABSTRACT_LEN) $this->capture = false;
13217076c2aSAndreas Gohr    }
13317076c2aSAndreas Gohr
13417076c2aSAndreas Gohr    /**
13593075b01SAndreas Gohr     * Add an item to the TOC
13693075b01SAndreas Gohr     *
13793075b01SAndreas Gohr     * @param string $id       the hash link
13893075b01SAndreas Gohr     * @param string $text     the text to display
13993075b01SAndreas Gohr     * @param int    $level    the nesting level
14093075b01SAndreas Gohr     */
141e7856beaSchris    function toc_additem($id, $text, $level) {
14239a89382SEsther Brunner        global $conf;
14339a89382SEsther Brunner
144e7856beaSchris        //only add items within configured levels
14539a89382SEsther Brunner        if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']) {
14639a89382SEsther Brunner            // the TOC is one of our standard ul list arrays ;-)
14739a89382SEsther Brunner            $this->meta['description']['tableofcontents'][] = array(
148e7856beaSchris                'hid'   => $id,
14939a89382SEsther Brunner                'title' => $text,
15039a89382SEsther Brunner                'type'  => 'ul',
15139a89382SEsther Brunner                'level' => $level - $conf['toptoclevel'] + 1
15239a89382SEsther Brunner            );
15339a89382SEsther Brunner        }
15439a89382SEsther Brunner
155e7856beaSchris    }
156e7856beaSchris
15793075b01SAndreas Gohr    /**
15893075b01SAndreas Gohr     * Render a heading
15993075b01SAndreas Gohr     *
16093075b01SAndreas Gohr     * @param string $text  the text to display
16193075b01SAndreas Gohr     * @param int    $level header level
16293075b01SAndreas Gohr     * @param int    $pos   byte position in the original source
16393075b01SAndreas Gohr     */
164e7856beaSchris    function header($text, $level, $pos) {
165c66972f2SAdrian Lang        if(!isset($this->meta['title'])) $this->meta['title'] = $text;
166e7856beaSchris
167e7856beaSchris        // add the header to the TOC
168*e0c26282SGerrit Uitslag        $hid = $this->_headerToLink($text, true);
169e7856beaSchris        $this->toc_additem($hid, $text, $level);
170e7856beaSchris
17139a89382SEsther Brunner        // add to summary
17217076c2aSAndreas Gohr        $this->cdata(DOKU_LF.$text.DOKU_LF);
17339a89382SEsther Brunner    }
17439a89382SEsther Brunner
17593075b01SAndreas Gohr    /**
17693075b01SAndreas Gohr     * Open a paragraph
17793075b01SAndreas Gohr     */
17839a89382SEsther Brunner    function p_open() {
17917076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
18039a89382SEsther Brunner    }
18139a89382SEsther Brunner
18293075b01SAndreas Gohr    /**
18393075b01SAndreas Gohr     * Close a paragraph
18493075b01SAndreas Gohr     */
18539a89382SEsther Brunner    function p_close() {
18617076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
18739a89382SEsther Brunner    }
18839a89382SEsther Brunner
18993075b01SAndreas Gohr    /**
19093075b01SAndreas Gohr     * Create a line break
19193075b01SAndreas Gohr     */
19239a89382SEsther Brunner    function linebreak() {
19317076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
19439a89382SEsther Brunner    }
19539a89382SEsther Brunner
19693075b01SAndreas Gohr    /**
19793075b01SAndreas Gohr     * Create a horizontal line
19893075b01SAndreas Gohr     */
19939a89382SEsther Brunner    function hr() {
20017076c2aSAndreas Gohr        $this->cdata(DOKU_LF.'----------'.DOKU_LF);
20139a89382SEsther Brunner    }
20239a89382SEsther Brunner
20339a89382SEsther Brunner    /**
20439a89382SEsther Brunner     * Callback for footnote start syntax
20539a89382SEsther Brunner     *
20639a89382SEsther Brunner     * All following content will go to the footnote instead of
20739a89382SEsther Brunner     * the document. To achieve this the previous rendered content
20839a89382SEsther Brunner     * is moved to $store and $doc is cleared
20939a89382SEsther Brunner     *
21039a89382SEsther Brunner     * @author Andreas Gohr <andi@splitbrain.org>
21139a89382SEsther Brunner     */
21239a89382SEsther Brunner    function footnote_open() {
21339a89382SEsther Brunner        if($this->capture) {
21439a89382SEsther Brunner            // move current content to store and record footnote
21539a89382SEsther Brunner            $this->store = $this->doc;
21639a89382SEsther Brunner            $this->doc   = '';
21739a89382SEsther Brunner        }
21839a89382SEsther Brunner    }
21939a89382SEsther Brunner
22039a89382SEsther Brunner    /**
22139a89382SEsther Brunner     * Callback for footnote end syntax
22239a89382SEsther Brunner     *
22339a89382SEsther Brunner     * All rendered content is moved to the $footnotes array and the old
22439a89382SEsther Brunner     * content is restored from $store again
22539a89382SEsther Brunner     *
22639a89382SEsther Brunner     * @author Andreas Gohr
22739a89382SEsther Brunner     */
22839a89382SEsther Brunner    function footnote_close() {
22939a89382SEsther Brunner        if($this->capture) {
23039a89382SEsther Brunner            // restore old content
23139a89382SEsther Brunner            $this->doc   = $this->store;
23239a89382SEsther Brunner            $this->store = '';
23339a89382SEsther Brunner        }
23439a89382SEsther Brunner    }
23539a89382SEsther Brunner
23693075b01SAndreas Gohr    /**
23793075b01SAndreas Gohr     * Open an unordered list
23893075b01SAndreas Gohr     */
23939a89382SEsther Brunner    function listu_open() {
24017076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
24139a89382SEsther Brunner    }
24239a89382SEsther Brunner
24393075b01SAndreas Gohr    /**
24493075b01SAndreas Gohr     * Open an ordered list
24593075b01SAndreas Gohr     */
24639a89382SEsther Brunner    function listo_open() {
24717076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
24839a89382SEsther Brunner    }
24939a89382SEsther Brunner
25093075b01SAndreas Gohr    /**
25193075b01SAndreas Gohr     * Open a list item
25293075b01SAndreas Gohr     *
25393075b01SAndreas Gohr     * @param int $level the nesting level
25493075b01SAndreas Gohr     */
25539a89382SEsther Brunner    function listitem_open($level) {
25617076c2aSAndreas Gohr        $this->cdata(str_repeat(DOKU_TAB, $level).'* ');
25739a89382SEsther Brunner    }
25839a89382SEsther Brunner
25993075b01SAndreas Gohr    /**
26093075b01SAndreas Gohr     * Close a list item
26193075b01SAndreas Gohr     */
26239a89382SEsther Brunner    function listitem_close() {
26317076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
26439a89382SEsther Brunner    }
26539a89382SEsther Brunner
26693075b01SAndreas Gohr    /**
26793075b01SAndreas Gohr     * Output preformatted text
26893075b01SAndreas Gohr     *
26993075b01SAndreas Gohr     * @param string $text
27093075b01SAndreas Gohr     */
27139a89382SEsther Brunner    function preformatted($text) {
27217076c2aSAndreas Gohr        $this->cdata($text);
27339a89382SEsther Brunner    }
27439a89382SEsther Brunner
27593075b01SAndreas Gohr    /**
27693075b01SAndreas Gohr     * Start a block quote
27793075b01SAndreas Gohr     */
27839a89382SEsther Brunner    function quote_open() {
27917076c2aSAndreas Gohr        $this->cdata(DOKU_LF.DOKU_TAB.'"');
28039a89382SEsther Brunner    }
28139a89382SEsther Brunner
28293075b01SAndreas Gohr    /**
28393075b01SAndreas Gohr     * Stop a block quote
28493075b01SAndreas Gohr     */
28539a89382SEsther Brunner    function quote_close() {
28617076c2aSAndreas Gohr        $this->cdata('"'.DOKU_LF);
28739a89382SEsther Brunner    }
28839a89382SEsther Brunner
28993075b01SAndreas Gohr    /**
29093075b01SAndreas Gohr     * Display text as file content, optionally syntax highlighted
29193075b01SAndreas Gohr     *
29293075b01SAndreas Gohr     * @param string $text text to show
29393075b01SAndreas Gohr     * @param string $lang programming language to use for syntax highlighting
29493075b01SAndreas Gohr     * @param string $file file path label
29593075b01SAndreas Gohr     */
29693075b01SAndreas Gohr    function file($text, $lang = null, $file = null) {
29717076c2aSAndreas Gohr        $this->cdata(DOKU_LF.$text.DOKU_LF);
29893075b01SAndreas Gohr    }
29993075b01SAndreas Gohr
30093075b01SAndreas Gohr    /**
30193075b01SAndreas Gohr     * Display text as code content, optionally syntax highlighted
30293075b01SAndreas Gohr     *
30393075b01SAndreas Gohr     * @param string $text     text to show
30493075b01SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
30593075b01SAndreas Gohr     * @param string $file     file path label
30693075b01SAndreas Gohr     */
3070ea51e63SMatt Perry    function code($text, $language = null, $file = null) {
30817076c2aSAndreas Gohr        $this->cdata(DOKU_LF.$text.DOKU_LF);
30939a89382SEsther Brunner    }
31039a89382SEsther Brunner
31193075b01SAndreas Gohr    /**
31293075b01SAndreas Gohr     * Format an acronym
31393075b01SAndreas Gohr     *
31493075b01SAndreas Gohr     * Uses $this->acronyms
31593075b01SAndreas Gohr     *
31693075b01SAndreas Gohr     * @param string $acronym
31793075b01SAndreas Gohr     */
31839a89382SEsther Brunner    function acronym($acronym) {
31917076c2aSAndreas Gohr        $this->cdata($acronym);
32039a89382SEsther Brunner    }
32139a89382SEsther Brunner
32293075b01SAndreas Gohr    /**
32393075b01SAndreas Gohr     * Format a smiley
32493075b01SAndreas Gohr     *
32593075b01SAndreas Gohr     * Uses $this->smiley
32693075b01SAndreas Gohr     *
32793075b01SAndreas Gohr     * @param string $smiley
32893075b01SAndreas Gohr     */
32939a89382SEsther Brunner    function smiley($smiley) {
33017076c2aSAndreas Gohr        $this->cdata($smiley);
33139a89382SEsther Brunner    }
33239a89382SEsther Brunner
33393075b01SAndreas Gohr    /**
33493075b01SAndreas Gohr     * Format an entity
33593075b01SAndreas Gohr     *
33693075b01SAndreas Gohr     * Entities are basically small text replacements
33793075b01SAndreas Gohr     *
33893075b01SAndreas Gohr     * Uses $this->entities
33993075b01SAndreas Gohr     *
34093075b01SAndreas Gohr     * @param string $entity
34193075b01SAndreas Gohr     */
34239a89382SEsther Brunner    function entity($entity) {
34317076c2aSAndreas Gohr        $this->cdata($entity);
34439a89382SEsther Brunner    }
34539a89382SEsther Brunner
34693075b01SAndreas Gohr    /**
34793075b01SAndreas Gohr     * Typographically format a multiply sign
34893075b01SAndreas Gohr     *
34993075b01SAndreas Gohr     * Example: ($x=640, $y=480) should result in "640×480"
35093075b01SAndreas Gohr     *
35193075b01SAndreas Gohr     * @param string|int $x first value
35293075b01SAndreas Gohr     * @param string|int $y second value
35393075b01SAndreas Gohr     */
35439a89382SEsther Brunner    function multiplyentity($x, $y) {
35517076c2aSAndreas Gohr        $this->cdata($x.'×'.$y);
35639a89382SEsther Brunner    }
35739a89382SEsther Brunner
35893075b01SAndreas Gohr    /**
35993075b01SAndreas Gohr     * Render an opening single quote char (language specific)
36093075b01SAndreas Gohr     */
36139a89382SEsther Brunner    function singlequoteopening() {
36271b40da2SAnika Henke        global $lang;
36317076c2aSAndreas Gohr        $this->cdata($lang['singlequoteopening']);
36439a89382SEsther Brunner    }
36539a89382SEsther Brunner
36693075b01SAndreas Gohr    /**
36793075b01SAndreas Gohr     * Render a closing single quote char (language specific)
36893075b01SAndreas Gohr     */
36939a89382SEsther Brunner    function singlequoteclosing() {
37071b40da2SAnika Henke        global $lang;
37117076c2aSAndreas Gohr        $this->cdata($lang['singlequoteclosing']);
37239a89382SEsther Brunner    }
37339a89382SEsther Brunner
37493075b01SAndreas Gohr    /**
37593075b01SAndreas Gohr     * Render an apostrophe char (language specific)
37693075b01SAndreas Gohr     */
37757d757d1SAndreas Gohr    function apostrophe() {
37857d757d1SAndreas Gohr        global $lang;
37917076c2aSAndreas Gohr        $this->cdata($lang['apostrophe']);
38057d757d1SAndreas Gohr    }
38157d757d1SAndreas Gohr
38293075b01SAndreas Gohr    /**
38393075b01SAndreas Gohr     * Render an opening double quote char (language specific)
38493075b01SAndreas Gohr     */
38539a89382SEsther Brunner    function doublequoteopening() {
38671b40da2SAnika Henke        global $lang;
38717076c2aSAndreas Gohr        $this->cdata($lang['doublequoteopening']);
38839a89382SEsther Brunner    }
38939a89382SEsther Brunner
39093075b01SAndreas Gohr    /**
39193075b01SAndreas Gohr     * Render an closinging double quote char (language specific)
39293075b01SAndreas Gohr     */
39339a89382SEsther Brunner    function doublequoteclosing() {
39471b40da2SAnika Henke        global $lang;
39517076c2aSAndreas Gohr        $this->cdata($lang['doublequoteclosing']);
39639a89382SEsther Brunner    }
39739a89382SEsther Brunner
39893075b01SAndreas Gohr    /**
39993075b01SAndreas Gohr     * Render a CamelCase link
40093075b01SAndreas Gohr     *
40193075b01SAndreas Gohr     * @param string $link The link name
40293075b01SAndreas Gohr     * @see http://en.wikipedia.org/wiki/CamelCase
40393075b01SAndreas Gohr     */
40439a89382SEsther Brunner    function camelcaselink($link) {
40539a89382SEsther Brunner        $this->internallink($link, $link);
40639a89382SEsther Brunner    }
40739a89382SEsther Brunner
40893075b01SAndreas Gohr    /**
40993075b01SAndreas Gohr     * Render a page local link
41093075b01SAndreas Gohr     *
41193075b01SAndreas Gohr     * @param string $hash hash link identifier
41293075b01SAndreas Gohr     * @param string $name name for the link
41393075b01SAndreas Gohr     */
4149269d0b1SMichael Hamann    function locallink($hash, $name = null) {
4159269d0b1SMichael Hamann        if(is_array($name)) {
4169269d0b1SMichael Hamann            $this->_firstimage($name['src']);
4179269d0b1SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
4189269d0b1SMichael Hamann        }
4199269d0b1SMichael Hamann    }
42039a89382SEsther Brunner
42139a89382SEsther Brunner    /**
42239a89382SEsther Brunner     * keep track of internal links in $this->meta['relation']['references']
42393075b01SAndreas Gohr     *
42493075b01SAndreas Gohr     * @param string       $id   page ID to link to. eg. 'wiki:syntax'
42593075b01SAndreas Gohr     * @param string|array $name name for the link, array for media file
42639a89382SEsther Brunner     */
4270ea51e63SMatt Perry    function internallink($id, $name = null) {
42839a89382SEsther Brunner        global $ID;
42939a89382SEsther Brunner
430ffec1009SMichael Hamann        if(is_array($name)) {
431aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
432ffec1009SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
433ffec1009SMichael Hamann        }
434aa5a2937SAndreas Gohr
43566d935e7SMichael Hamann        $parts = explode('?', $id, 2);
43666d935e7SMichael Hamann        if(count($parts) === 2) {
43766d935e7SMichael Hamann            $id = $parts[0];
43866d935e7SMichael Hamann        }
43966d935e7SMichael Hamann
44039a89382SEsther Brunner        $default = $this->_simpleTitle($id);
44139a89382SEsther Brunner
44239a89382SEsther Brunner        // first resolve and clean up the $id
44339a89382SEsther Brunner        resolve_pageid(getNS($ID), $id, $exists);
44493075b01SAndreas Gohr        @list($page) = explode('#', $id, 2);
44539a89382SEsther Brunner
44639a89382SEsther Brunner        // set metadata
4473be6e394Schris        $this->meta['relation']['references'][$page] = $exists;
44839a89382SEsther Brunner        // $data = array('relation' => array('isreferencedby' => array($ID => true)));
44939a89382SEsther Brunner        // p_set_metadata($id, $data);
45039a89382SEsther Brunner
45139a89382SEsther Brunner        // add link title to summary
45239a89382SEsther Brunner        if($this->capture) {
45339a89382SEsther Brunner            $name = $this->_getLinkTitle($name, $default, $id);
45439a89382SEsther Brunner            $this->doc .= $name;
45539a89382SEsther Brunner        }
45639a89382SEsther Brunner    }
45739a89382SEsther Brunner
45893075b01SAndreas Gohr    /**
45993075b01SAndreas Gohr     * Render an external link
46093075b01SAndreas Gohr     *
46193075b01SAndreas Gohr     * @param string       $url  full URL with scheme
46293075b01SAndreas Gohr     * @param string|array $name name for the link, array for media file
46393075b01SAndreas Gohr     */
4640ea51e63SMatt Perry    function externallink($url, $name = null) {
465ffec1009SMichael Hamann        if(is_array($name)) {
466aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
467ffec1009SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
468ffec1009SMichael Hamann        }
469aa5a2937SAndreas Gohr
47039a89382SEsther Brunner        if($this->capture) {
471a2ea2dc1SAdrian Lang            $this->doc .= $this->_getLinkTitle($name, '<'.$url.'>');
47239a89382SEsther Brunner        }
47339a89382SEsther Brunner    }
47439a89382SEsther Brunner
47593075b01SAndreas Gohr    /**
47693075b01SAndreas Gohr     * Render an interwiki link
47793075b01SAndreas Gohr     *
47893075b01SAndreas Gohr     * You may want to use $this->_resolveInterWiki() here
47993075b01SAndreas Gohr     *
48093075b01SAndreas Gohr     * @param string       $match     original link - probably not much use
48193075b01SAndreas Gohr     * @param string|array $name      name for the link, array for media file
48293075b01SAndreas Gohr     * @param string       $wikiName  indentifier (shortcut) for the remote wiki
48393075b01SAndreas Gohr     * @param string       $wikiUri   the fragment parsed from the original link
48493075b01SAndreas Gohr     */
4850ea51e63SMatt Perry    function interwikilink($match, $name = null, $wikiName, $wikiUri) {
486ffec1009SMichael Hamann        if(is_array($name)) {
487aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
488ffec1009SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
489ffec1009SMichael Hamann        }
490aa5a2937SAndreas Gohr
49139a89382SEsther Brunner        if($this->capture) {
49293075b01SAndreas Gohr            list($wikiUri) = explode('#', $wikiUri, 2);
493a2ea2dc1SAdrian Lang            $name = $this->_getLinkTitle($name, $wikiUri);
49439a89382SEsther Brunner            $this->doc .= $name;
49539a89382SEsther Brunner        }
49639a89382SEsther Brunner    }
49739a89382SEsther Brunner
49893075b01SAndreas Gohr    /**
49993075b01SAndreas Gohr     * Link to windows share
50093075b01SAndreas Gohr     *
50193075b01SAndreas Gohr     * @param string       $url  the link
50293075b01SAndreas Gohr     * @param string|array $name name for the link, array for media file
50393075b01SAndreas Gohr     */
5040ea51e63SMatt Perry    function windowssharelink($url, $name = null) {
505ffec1009SMichael Hamann        if(is_array($name)) {
506aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
507ffec1009SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
508ffec1009SMichael Hamann        }
509aa5a2937SAndreas Gohr
51039a89382SEsther Brunner        if($this->capture) {
51139a89382SEsther Brunner            if($name) $this->doc .= $name;
51239a89382SEsther Brunner            else $this->doc .= '<'.$url.'>';
51339a89382SEsther Brunner        }
51439a89382SEsther Brunner    }
51539a89382SEsther Brunner
51693075b01SAndreas Gohr    /**
51793075b01SAndreas Gohr     * Render a linked E-Mail Address
51893075b01SAndreas Gohr     *
51993075b01SAndreas Gohr     * Should honor $conf['mailguard'] setting
52093075b01SAndreas Gohr     *
52193075b01SAndreas Gohr     * @param string       $address Email-Address
52293075b01SAndreas Gohr     * @param string|array $name    name for the link, array for media file
52393075b01SAndreas Gohr     */
5240ea51e63SMatt Perry    function emaillink($address, $name = null) {
525ffec1009SMichael Hamann        if(is_array($name)) {
526aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
527ffec1009SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
528ffec1009SMichael Hamann        }
529aa5a2937SAndreas Gohr
53039a89382SEsther Brunner        if($this->capture) {
53139a89382SEsther Brunner            if($name) $this->doc .= $name;
53239a89382SEsther Brunner            else $this->doc .= '<'.$address.'>';
53339a89382SEsther Brunner        }
53439a89382SEsther Brunner    }
53539a89382SEsther Brunner
53693075b01SAndreas Gohr    /**
53793075b01SAndreas Gohr     * Render an internal media file
53893075b01SAndreas Gohr     *
53993075b01SAndreas Gohr     * @param string $src     media ID
54093075b01SAndreas Gohr     * @param string $title   descriptive text
54193075b01SAndreas Gohr     * @param string $align   left|center|right
54293075b01SAndreas Gohr     * @param int    $width   width of media in pixel
54393075b01SAndreas Gohr     * @param int    $height  height of media in pixel
54493075b01SAndreas Gohr     * @param string $cache   cache|recache|nocache
54593075b01SAndreas Gohr     * @param string $linking linkonly|detail|nolink
54693075b01SAndreas Gohr     */
5470ea51e63SMatt Perry    function internalmedia($src, $title = null, $align = null, $width = null,
5480ea51e63SMatt Perry                           $height = null, $cache = null, $linking = null) {
54939a89382SEsther Brunner        if($this->capture && $title) $this->doc .= '['.$title.']';
550aa5a2937SAndreas Gohr        $this->_firstimage($src);
551ffec1009SMichael Hamann        $this->_recordMediaUsage($src);
55239a89382SEsther Brunner    }
55339a89382SEsther Brunner
55493075b01SAndreas Gohr    /**
55593075b01SAndreas Gohr     * Render an external media file
55693075b01SAndreas Gohr     *
55793075b01SAndreas Gohr     * @param string $src     full media URL
55893075b01SAndreas Gohr     * @param string $title   descriptive text
55993075b01SAndreas Gohr     * @param string $align   left|center|right
56093075b01SAndreas Gohr     * @param int    $width   width of media in pixel
56193075b01SAndreas Gohr     * @param int    $height  height of media in pixel
56293075b01SAndreas Gohr     * @param string $cache   cache|recache|nocache
56393075b01SAndreas Gohr     * @param string $linking linkonly|detail|nolink
56493075b01SAndreas Gohr     */
5650ea51e63SMatt Perry    function externalmedia($src, $title = null, $align = null, $width = null,
5660ea51e63SMatt Perry                           $height = null, $cache = null, $linking = null) {
56739a89382SEsther Brunner        if($this->capture && $title) $this->doc .= '['.$title.']';
568aa5a2937SAndreas Gohr        $this->_firstimage($src);
56939a89382SEsther Brunner    }
57039a89382SEsther Brunner
57193075b01SAndreas Gohr    /**
57293075b01SAndreas Gohr     * Render the output of an RSS feed
57393075b01SAndreas Gohr     *
57493075b01SAndreas Gohr     * @param string $url    URL of the feed
57593075b01SAndreas Gohr     * @param array  $params Finetuning of the output
57693075b01SAndreas Gohr     */
577ce6b63d9Schris    function rss($url, $params) {
578ce6b63d9Schris        $this->meta['relation']['haspart'][$url] = true;
57964e9144aSchris
58064e9144aSchris        $this->meta['date']['valid']['age'] =
58164e9144aSchris            isset($this->meta['date']['valid']['age']) ?
58264e9144aSchris                min($this->meta['date']['valid']['age'], $params['refresh']) :
58364e9144aSchris                $params['refresh'];
584ce6b63d9Schris    }
58539a89382SEsther Brunner
58693075b01SAndreas Gohr    #region Utils
58739a89382SEsther Brunner
58839a89382SEsther Brunner    /**
58939a89382SEsther Brunner     * Removes any Namespace from the given name but keeps
59039a89382SEsther Brunner     * casing and special chars
59139a89382SEsther Brunner     *
59239a89382SEsther Brunner     * @author Andreas Gohr <andi@splitbrain.org>
59339a89382SEsther Brunner     */
59439a89382SEsther Brunner    function _simpleTitle($name) {
59539a89382SEsther Brunner        global $conf;
59639a89382SEsther Brunner
5979bee852eSAndreas Gohr        if(is_array($name)) return '';
5989bee852eSAndreas Gohr
59939a89382SEsther Brunner        if($conf['useslash']) {
60039a89382SEsther Brunner            $nssep = '[:;/]';
60139a89382SEsther Brunner        } else {
60239a89382SEsther Brunner            $nssep = '[:;]';
60339a89382SEsther Brunner        }
60439a89382SEsther Brunner        $name = preg_replace('!.*'.$nssep.'!', '', $name);
6053be6e394Schris        //if there is a hash we use the anchor name only
60639a89382SEsther Brunner        $name = preg_replace('!.*#!', '', $name);
60739a89382SEsther Brunner        return $name;
60839a89382SEsther Brunner    }
60939a89382SEsther Brunner
61039a89382SEsther Brunner    /**
61139a89382SEsther Brunner     * Creates a linkid from a headline
61239a89382SEsther Brunner     *
61393075b01SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
61439a89382SEsther Brunner     * @param string  $title   The headline title
61539a89382SEsther Brunner     * @param boolean $create  Create a new unique ID?
61693075b01SAndreas Gohr     * @return string
61739a89382SEsther Brunner     */
61839a89382SEsther Brunner    function _headerToLink($title, $create = false) {
61939a89382SEsther Brunner        if($create) {
620620f4930SAndreas Gohr            return sectionID($title, $this->headers);
621620f4930SAndreas Gohr        } else {
622620f4930SAndreas Gohr            $check = false;
623620f4930SAndreas Gohr            return sectionID($title, $check);
62439a89382SEsther Brunner        }
62539a89382SEsther Brunner    }
62639a89382SEsther Brunner
62739a89382SEsther Brunner    /**
62839a89382SEsther Brunner     * Construct a title and handle images in titles
62939a89382SEsther Brunner     *
63039a89382SEsther Brunner     * @author Harry Fuecks <hfuecks@gmail.com>
63193075b01SAndreas Gohr     * @param string|array $title    either string title or media array
63293075b01SAndreas Gohr     * @param string       $default  default title if nothing else is found
63393075b01SAndreas Gohr     * @param null|string  $id       linked page id (used to extract title from first heading)
63493075b01SAndreas Gohr     * @return string title text
63539a89382SEsther Brunner     */
6360ea51e63SMatt Perry    function _getLinkTitle($title, $default, $id = null) {
63785038eabSDanny        if(is_array($title)) {
63893075b01SAndreas Gohr            if($title['title']) {
63993075b01SAndreas Gohr                return '['.$title['title'].']';
64093075b01SAndreas Gohr            } else {
64193075b01SAndreas Gohr                return $default;
64293075b01SAndreas Gohr            }
64385038eabSDanny        } else if(is_null($title) || trim($title) == '') {
644fe9ec250SChris Smith            if(useHeading('content') && $id) {
64520f04039SDanny                $heading = p_get_first_heading($id, METADATA_DONT_RENDER);
64639a89382SEsther Brunner                if($heading) return $heading;
64739a89382SEsther Brunner            }
64839a89382SEsther Brunner            return $default;
64985038eabSDanny        } else {
65039a89382SEsther Brunner            return $title;
65139a89382SEsther Brunner        }
65239a89382SEsther Brunner    }
65339a89382SEsther Brunner
65493075b01SAndreas Gohr    /**
65593075b01SAndreas Gohr     * Remember first image
65693075b01SAndreas Gohr     *
65793075b01SAndreas Gohr     * @param string $src image URL or ID
65893075b01SAndreas Gohr     */
659aa5a2937SAndreas Gohr    function _firstimage($src) {
660aa5a2937SAndreas Gohr        if($this->firstimage) return;
661aa5a2937SAndreas Gohr        global $ID;
662aa5a2937SAndreas Gohr
66393075b01SAndreas Gohr        list($src) = explode('#', $src, 2);
6643e7e6067SKlap-in        if(!media_isexternal($src)) {
665aa5a2937SAndreas Gohr            resolve_mediaid(getNS($ID), $src, $exists);
666aa5a2937SAndreas Gohr        }
667aa5a2937SAndreas Gohr        if(preg_match('/.(jpe?g|gif|png)$/i', $src)) {
668aa5a2937SAndreas Gohr            $this->firstimage = $src;
669aa5a2937SAndreas Gohr        }
670aa5a2937SAndreas Gohr    }
671ffec1009SMichael Hamann
67293075b01SAndreas Gohr    /**
67393075b01SAndreas Gohr     * Store list of used media files in metadata
67493075b01SAndreas Gohr     *
67593075b01SAndreas Gohr     * @param string $src media ID
67693075b01SAndreas Gohr     */
677ffec1009SMichael Hamann    function _recordMediaUsage($src) {
678ffec1009SMichael Hamann        global $ID;
679ffec1009SMichael Hamann
68093075b01SAndreas Gohr        list ($src) = explode('#', $src, 2);
681ffec1009SMichael Hamann        if(media_isexternal($src)) return;
682ffec1009SMichael Hamann        resolve_mediaid(getNS($ID), $src, $exists);
683ffec1009SMichael Hamann        $this->meta['relation']['media'][$src] = $exists;
684ffec1009SMichael Hamann    }
68593075b01SAndreas Gohr
68693075b01SAndreas Gohr    #endregion
68739a89382SEsther Brunner}
68839a89382SEsther Brunner
689e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
690