xref: /dokuwiki/inc/parser/metadata.php (revision f50a239b3b819527445d240746b09a05fb76d103)
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
168e0c26282SGerrit 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
254e3a24861SChristopher Smith     * @param bool $node true when a node; false when a leaf
25593075b01SAndreas Gohr     */
256e3a24861SChristopher Smith    function listitem_open($level,$node=false) {
25717076c2aSAndreas Gohr        $this->cdata(str_repeat(DOKU_TAB, $level).'* ');
25839a89382SEsther Brunner    }
25939a89382SEsther Brunner
26093075b01SAndreas Gohr    /**
26193075b01SAndreas Gohr     * Close a list item
26293075b01SAndreas Gohr     */
26339a89382SEsther Brunner    function listitem_close() {
26417076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
26539a89382SEsther Brunner    }
26639a89382SEsther Brunner
26793075b01SAndreas Gohr    /**
26893075b01SAndreas Gohr     * Output preformatted text
26993075b01SAndreas Gohr     *
27093075b01SAndreas Gohr     * @param string $text
27193075b01SAndreas Gohr     */
27239a89382SEsther Brunner    function preformatted($text) {
27317076c2aSAndreas Gohr        $this->cdata($text);
27439a89382SEsther Brunner    }
27539a89382SEsther Brunner
27693075b01SAndreas Gohr    /**
27793075b01SAndreas Gohr     * Start a block quote
27893075b01SAndreas Gohr     */
27939a89382SEsther Brunner    function quote_open() {
28017076c2aSAndreas Gohr        $this->cdata(DOKU_LF.DOKU_TAB.'"');
28139a89382SEsther Brunner    }
28239a89382SEsther Brunner
28393075b01SAndreas Gohr    /**
28493075b01SAndreas Gohr     * Stop a block quote
28593075b01SAndreas Gohr     */
28639a89382SEsther Brunner    function quote_close() {
28717076c2aSAndreas Gohr        $this->cdata('"'.DOKU_LF);
28839a89382SEsther Brunner    }
28939a89382SEsther Brunner
29093075b01SAndreas Gohr    /**
29193075b01SAndreas Gohr     * Display text as file content, optionally syntax highlighted
29293075b01SAndreas Gohr     *
29393075b01SAndreas Gohr     * @param string $text text to show
29493075b01SAndreas Gohr     * @param string $lang programming language to use for syntax highlighting
29593075b01SAndreas Gohr     * @param string $file file path label
29693075b01SAndreas Gohr     */
29793075b01SAndreas Gohr    function file($text, $lang = null, $file = null) {
29817076c2aSAndreas Gohr        $this->cdata(DOKU_LF.$text.DOKU_LF);
29993075b01SAndreas Gohr    }
30093075b01SAndreas Gohr
30193075b01SAndreas Gohr    /**
30293075b01SAndreas Gohr     * Display text as code content, optionally syntax highlighted
30393075b01SAndreas Gohr     *
30493075b01SAndreas Gohr     * @param string $text     text to show
30593075b01SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
30693075b01SAndreas Gohr     * @param string $file     file path label
30793075b01SAndreas Gohr     */
3080ea51e63SMatt Perry    function code($text, $language = null, $file = null) {
30917076c2aSAndreas Gohr        $this->cdata(DOKU_LF.$text.DOKU_LF);
31039a89382SEsther Brunner    }
31139a89382SEsther Brunner
31293075b01SAndreas Gohr    /**
31393075b01SAndreas Gohr     * Format an acronym
31493075b01SAndreas Gohr     *
31593075b01SAndreas Gohr     * Uses $this->acronyms
31693075b01SAndreas Gohr     *
31793075b01SAndreas Gohr     * @param string $acronym
31893075b01SAndreas Gohr     */
31939a89382SEsther Brunner    function acronym($acronym) {
32017076c2aSAndreas Gohr        $this->cdata($acronym);
32139a89382SEsther Brunner    }
32239a89382SEsther Brunner
32393075b01SAndreas Gohr    /**
32493075b01SAndreas Gohr     * Format a smiley
32593075b01SAndreas Gohr     *
32693075b01SAndreas Gohr     * Uses $this->smiley
32793075b01SAndreas Gohr     *
32893075b01SAndreas Gohr     * @param string $smiley
32993075b01SAndreas Gohr     */
33039a89382SEsther Brunner    function smiley($smiley) {
33117076c2aSAndreas Gohr        $this->cdata($smiley);
33239a89382SEsther Brunner    }
33339a89382SEsther Brunner
33493075b01SAndreas Gohr    /**
33593075b01SAndreas Gohr     * Format an entity
33693075b01SAndreas Gohr     *
33793075b01SAndreas Gohr     * Entities are basically small text replacements
33893075b01SAndreas Gohr     *
33993075b01SAndreas Gohr     * Uses $this->entities
34093075b01SAndreas Gohr     *
34193075b01SAndreas Gohr     * @param string $entity
34293075b01SAndreas Gohr     */
34339a89382SEsther Brunner    function entity($entity) {
34417076c2aSAndreas Gohr        $this->cdata($entity);
34539a89382SEsther Brunner    }
34639a89382SEsther Brunner
34793075b01SAndreas Gohr    /**
34893075b01SAndreas Gohr     * Typographically format a multiply sign
34993075b01SAndreas Gohr     *
35093075b01SAndreas Gohr     * Example: ($x=640, $y=480) should result in "640×480"
35193075b01SAndreas Gohr     *
35293075b01SAndreas Gohr     * @param string|int $x first value
35393075b01SAndreas Gohr     * @param string|int $y second value
35493075b01SAndreas Gohr     */
35539a89382SEsther Brunner    function multiplyentity($x, $y) {
35617076c2aSAndreas Gohr        $this->cdata($x.'×'.$y);
35739a89382SEsther Brunner    }
35839a89382SEsther Brunner
35993075b01SAndreas Gohr    /**
36093075b01SAndreas Gohr     * Render an opening single quote char (language specific)
36193075b01SAndreas Gohr     */
36239a89382SEsther Brunner    function singlequoteopening() {
36371b40da2SAnika Henke        global $lang;
36417076c2aSAndreas Gohr        $this->cdata($lang['singlequoteopening']);
36539a89382SEsther Brunner    }
36639a89382SEsther Brunner
36793075b01SAndreas Gohr    /**
36893075b01SAndreas Gohr     * Render a closing single quote char (language specific)
36993075b01SAndreas Gohr     */
37039a89382SEsther Brunner    function singlequoteclosing() {
37171b40da2SAnika Henke        global $lang;
37217076c2aSAndreas Gohr        $this->cdata($lang['singlequoteclosing']);
37339a89382SEsther Brunner    }
37439a89382SEsther Brunner
37593075b01SAndreas Gohr    /**
37693075b01SAndreas Gohr     * Render an apostrophe char (language specific)
37793075b01SAndreas Gohr     */
37857d757d1SAndreas Gohr    function apostrophe() {
37957d757d1SAndreas Gohr        global $lang;
38017076c2aSAndreas Gohr        $this->cdata($lang['apostrophe']);
38157d757d1SAndreas Gohr    }
38257d757d1SAndreas Gohr
38393075b01SAndreas Gohr    /**
38493075b01SAndreas Gohr     * Render an opening double quote char (language specific)
38593075b01SAndreas Gohr     */
38639a89382SEsther Brunner    function doublequoteopening() {
38771b40da2SAnika Henke        global $lang;
38817076c2aSAndreas Gohr        $this->cdata($lang['doublequoteopening']);
38939a89382SEsther Brunner    }
39039a89382SEsther Brunner
39193075b01SAndreas Gohr    /**
39293075b01SAndreas Gohr     * Render an closinging double quote char (language specific)
39393075b01SAndreas Gohr     */
39439a89382SEsther Brunner    function doublequoteclosing() {
39571b40da2SAnika Henke        global $lang;
39617076c2aSAndreas Gohr        $this->cdata($lang['doublequoteclosing']);
39739a89382SEsther Brunner    }
39839a89382SEsther Brunner
39993075b01SAndreas Gohr    /**
40093075b01SAndreas Gohr     * Render a CamelCase link
40193075b01SAndreas Gohr     *
40293075b01SAndreas Gohr     * @param string $link The link name
40393075b01SAndreas Gohr     * @see http://en.wikipedia.org/wiki/CamelCase
40493075b01SAndreas Gohr     */
40539a89382SEsther Brunner    function camelcaselink($link) {
40639a89382SEsther Brunner        $this->internallink($link, $link);
40739a89382SEsther Brunner    }
40839a89382SEsther Brunner
40993075b01SAndreas Gohr    /**
41093075b01SAndreas Gohr     * Render a page local link
41193075b01SAndreas Gohr     *
41293075b01SAndreas Gohr     * @param string $hash hash link identifier
41393075b01SAndreas Gohr     * @param string $name name for the link
41493075b01SAndreas Gohr     */
4159269d0b1SMichael Hamann    function locallink($hash, $name = null) {
4169269d0b1SMichael Hamann        if(is_array($name)) {
4179269d0b1SMichael Hamann            $this->_firstimage($name['src']);
4189269d0b1SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
4199269d0b1SMichael Hamann        }
4209269d0b1SMichael Hamann    }
42139a89382SEsther Brunner
42239a89382SEsther Brunner    /**
42339a89382SEsther Brunner     * keep track of internal links in $this->meta['relation']['references']
42493075b01SAndreas Gohr     *
42593075b01SAndreas Gohr     * @param string            $id   page ID to link to. eg. 'wiki:syntax'
42659bc3b48SGerrit Uitslag     * @param string|array|null $name name for the link, array for media file
42739a89382SEsther Brunner     */
4280ea51e63SMatt Perry    function internallink($id, $name = null) {
42939a89382SEsther Brunner        global $ID;
43039a89382SEsther Brunner
431ffec1009SMichael Hamann        if(is_array($name)) {
432aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
433ffec1009SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
434ffec1009SMichael Hamann        }
435aa5a2937SAndreas Gohr
43666d935e7SMichael Hamann        $parts = explode('?', $id, 2);
43766d935e7SMichael Hamann        if(count($parts) === 2) {
43866d935e7SMichael Hamann            $id = $parts[0];
43966d935e7SMichael Hamann        }
44066d935e7SMichael Hamann
44139a89382SEsther Brunner        $default = $this->_simpleTitle($id);
44239a89382SEsther Brunner
44339a89382SEsther Brunner        // first resolve and clean up the $id
44439a89382SEsther Brunner        resolve_pageid(getNS($ID), $id, $exists);
44593075b01SAndreas Gohr        @list($page) = explode('#', $id, 2);
44639a89382SEsther Brunner
44739a89382SEsther Brunner        // set metadata
4483be6e394Schris        $this->meta['relation']['references'][$page] = $exists;
44939a89382SEsther Brunner        // $data = array('relation' => array('isreferencedby' => array($ID => true)));
45039a89382SEsther Brunner        // p_set_metadata($id, $data);
45139a89382SEsther Brunner
45239a89382SEsther Brunner        // add link title to summary
45339a89382SEsther Brunner        if($this->capture) {
45439a89382SEsther Brunner            $name = $this->_getLinkTitle($name, $default, $id);
45539a89382SEsther Brunner            $this->doc .= $name;
45639a89382SEsther Brunner        }
45739a89382SEsther Brunner    }
45839a89382SEsther Brunner
45993075b01SAndreas Gohr    /**
46093075b01SAndreas Gohr     * Render an external link
46193075b01SAndreas Gohr     *
46293075b01SAndreas Gohr     * @param string            $url  full URL with scheme
46359bc3b48SGerrit Uitslag     * @param string|array|null $name name for the link, array for media file
46493075b01SAndreas Gohr     */
4650ea51e63SMatt Perry    function externallink($url, $name = null) {
466ffec1009SMichael Hamann        if(is_array($name)) {
467aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
468ffec1009SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
469ffec1009SMichael Hamann        }
470aa5a2937SAndreas Gohr
47139a89382SEsther Brunner        if($this->capture) {
472a2ea2dc1SAdrian Lang            $this->doc .= $this->_getLinkTitle($name, '<'.$url.'>');
47339a89382SEsther Brunner        }
47439a89382SEsther Brunner    }
47539a89382SEsther Brunner
47693075b01SAndreas Gohr    /**
47793075b01SAndreas Gohr     * Render an interwiki link
47893075b01SAndreas Gohr     *
47993075b01SAndreas Gohr     * You may want to use $this->_resolveInterWiki() here
48093075b01SAndreas Gohr     *
48193075b01SAndreas Gohr     * @param string       $match     original link - probably not much use
48293075b01SAndreas Gohr     * @param string|array $name      name for the link, array for media file
48393075b01SAndreas Gohr     * @param string       $wikiName  indentifier (shortcut) for the remote wiki
48493075b01SAndreas Gohr     * @param string       $wikiUri   the fragment parsed from the original link
48593075b01SAndreas Gohr     */
4860ea51e63SMatt Perry    function interwikilink($match, $name = null, $wikiName, $wikiUri) {
487ffec1009SMichael Hamann        if(is_array($name)) {
488aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
489ffec1009SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
490ffec1009SMichael Hamann        }
491aa5a2937SAndreas Gohr
49239a89382SEsther Brunner        if($this->capture) {
49393075b01SAndreas Gohr            list($wikiUri) = explode('#', $wikiUri, 2);
494a2ea2dc1SAdrian Lang            $name = $this->_getLinkTitle($name, $wikiUri);
49539a89382SEsther Brunner            $this->doc .= $name;
49639a89382SEsther Brunner        }
49739a89382SEsther Brunner    }
49839a89382SEsther Brunner
49993075b01SAndreas Gohr    /**
50093075b01SAndreas Gohr     * Link to windows share
50193075b01SAndreas Gohr     *
50293075b01SAndreas Gohr     * @param string       $url  the link
50393075b01SAndreas Gohr     * @param string|array $name name for the link, array for media file
50493075b01SAndreas Gohr     */
5050ea51e63SMatt Perry    function windowssharelink($url, $name = null) {
506ffec1009SMichael Hamann        if(is_array($name)) {
507aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
508ffec1009SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
509ffec1009SMichael Hamann        }
510aa5a2937SAndreas Gohr
51139a89382SEsther Brunner        if($this->capture) {
51239a89382SEsther Brunner            if($name) $this->doc .= $name;
51339a89382SEsther Brunner            else $this->doc .= '<'.$url.'>';
51439a89382SEsther Brunner        }
51539a89382SEsther Brunner    }
51639a89382SEsther Brunner
51793075b01SAndreas Gohr    /**
51893075b01SAndreas Gohr     * Render a linked E-Mail Address
51993075b01SAndreas Gohr     *
52093075b01SAndreas Gohr     * Should honor $conf['mailguard'] setting
52193075b01SAndreas Gohr     *
52293075b01SAndreas Gohr     * @param string       $address Email-Address
52393075b01SAndreas Gohr     * @param string|array $name    name for the link, array for media file
52493075b01SAndreas Gohr     */
5250ea51e63SMatt Perry    function emaillink($address, $name = null) {
526ffec1009SMichael Hamann        if(is_array($name)) {
527aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
528ffec1009SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
529ffec1009SMichael Hamann        }
530aa5a2937SAndreas Gohr
53139a89382SEsther Brunner        if($this->capture) {
53239a89382SEsther Brunner            if($name) $this->doc .= $name;
53339a89382SEsther Brunner            else $this->doc .= '<'.$address.'>';
53439a89382SEsther Brunner        }
53539a89382SEsther Brunner    }
53639a89382SEsther Brunner
53793075b01SAndreas Gohr    /**
53893075b01SAndreas Gohr     * Render an internal media file
53993075b01SAndreas Gohr     *
54093075b01SAndreas Gohr     * @param string $src     media ID
54193075b01SAndreas Gohr     * @param string $title   descriptive text
54293075b01SAndreas Gohr     * @param string $align   left|center|right
54393075b01SAndreas Gohr     * @param int    $width   width of media in pixel
54493075b01SAndreas Gohr     * @param int    $height  height of media in pixel
54593075b01SAndreas Gohr     * @param string $cache   cache|recache|nocache
54693075b01SAndreas Gohr     * @param string $linking linkonly|detail|nolink
54793075b01SAndreas Gohr     */
5480ea51e63SMatt Perry    function internalmedia($src, $title = null, $align = null, $width = null,
5490ea51e63SMatt Perry                           $height = null, $cache = null, $linking = null) {
55039a89382SEsther Brunner        if($this->capture && $title) $this->doc .= '['.$title.']';
551aa5a2937SAndreas Gohr        $this->_firstimage($src);
552ffec1009SMichael Hamann        $this->_recordMediaUsage($src);
55339a89382SEsther Brunner    }
55439a89382SEsther Brunner
55593075b01SAndreas Gohr    /**
55693075b01SAndreas Gohr     * Render an external media file
55793075b01SAndreas Gohr     *
55893075b01SAndreas Gohr     * @param string $src     full media URL
55993075b01SAndreas Gohr     * @param string $title   descriptive text
56093075b01SAndreas Gohr     * @param string $align   left|center|right
56193075b01SAndreas Gohr     * @param int    $width   width of media in pixel
56293075b01SAndreas Gohr     * @param int    $height  height of media in pixel
56393075b01SAndreas Gohr     * @param string $cache   cache|recache|nocache
56493075b01SAndreas Gohr     * @param string $linking linkonly|detail|nolink
56593075b01SAndreas Gohr     */
5660ea51e63SMatt Perry    function externalmedia($src, $title = null, $align = null, $width = null,
5670ea51e63SMatt Perry                           $height = null, $cache = null, $linking = null) {
56839a89382SEsther Brunner        if($this->capture && $title) $this->doc .= '['.$title.']';
569aa5a2937SAndreas Gohr        $this->_firstimage($src);
57039a89382SEsther Brunner    }
57139a89382SEsther Brunner
57293075b01SAndreas Gohr    /**
57393075b01SAndreas Gohr     * Render the output of an RSS feed
57493075b01SAndreas Gohr     *
57593075b01SAndreas Gohr     * @param string $url    URL of the feed
57693075b01SAndreas Gohr     * @param array  $params Finetuning of the output
57793075b01SAndreas Gohr     */
578ce6b63d9Schris    function rss($url, $params) {
579ce6b63d9Schris        $this->meta['relation']['haspart'][$url] = true;
58064e9144aSchris
58164e9144aSchris        $this->meta['date']['valid']['age'] =
58264e9144aSchris            isset($this->meta['date']['valid']['age']) ?
58364e9144aSchris                min($this->meta['date']['valid']['age'], $params['refresh']) :
58464e9144aSchris                $params['refresh'];
585ce6b63d9Schris    }
58639a89382SEsther Brunner
58793075b01SAndreas Gohr    #region Utils
58839a89382SEsther Brunner
58939a89382SEsther Brunner    /**
59039a89382SEsther Brunner     * Removes any Namespace from the given name but keeps
59139a89382SEsther Brunner     * casing and special chars
59239a89382SEsther Brunner     *
59339a89382SEsther Brunner     * @author Andreas Gohr <andi@splitbrain.org>
594*f50a239bSTakamura     *
595*f50a239bSTakamura     * @param string $name
596*f50a239bSTakamura     *
597*f50a239bSTakamura     * @return mixed|string
59839a89382SEsther Brunner     */
59939a89382SEsther Brunner    function _simpleTitle($name) {
60039a89382SEsther Brunner        global $conf;
60139a89382SEsther Brunner
6029bee852eSAndreas Gohr        if(is_array($name)) return '';
6039bee852eSAndreas Gohr
60439a89382SEsther Brunner        if($conf['useslash']) {
60539a89382SEsther Brunner            $nssep = '[:;/]';
60639a89382SEsther Brunner        } else {
60739a89382SEsther Brunner            $nssep = '[:;]';
60839a89382SEsther Brunner        }
60939a89382SEsther Brunner        $name = preg_replace('!.*'.$nssep.'!', '', $name);
6103be6e394Schris        //if there is a hash we use the anchor name only
61139a89382SEsther Brunner        $name = preg_replace('!.*#!', '', $name);
61239a89382SEsther Brunner        return $name;
61339a89382SEsther Brunner    }
61439a89382SEsther Brunner
61539a89382SEsther Brunner    /**
61639a89382SEsther Brunner     * Creates a linkid from a headline
61739a89382SEsther Brunner     *
61893075b01SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
61939a89382SEsther Brunner     * @param string  $title   The headline title
62039a89382SEsther Brunner     * @param boolean $create  Create a new unique ID?
62193075b01SAndreas Gohr     * @return string
62239a89382SEsther Brunner     */
62339a89382SEsther Brunner    function _headerToLink($title, $create = false) {
62439a89382SEsther Brunner        if($create) {
625620f4930SAndreas Gohr            return sectionID($title, $this->headers);
626620f4930SAndreas Gohr        } else {
627620f4930SAndreas Gohr            $check = false;
628620f4930SAndreas Gohr            return sectionID($title, $check);
62939a89382SEsther Brunner        }
63039a89382SEsther Brunner    }
63139a89382SEsther Brunner
63239a89382SEsther Brunner    /**
63339a89382SEsther Brunner     * Construct a title and handle images in titles
63439a89382SEsther Brunner     *
63539a89382SEsther Brunner     * @author Harry Fuecks <hfuecks@gmail.com>
63659bc3b48SGerrit Uitslag     * @param string|array|null $title    either string title or media array
63793075b01SAndreas Gohr     * @param string            $default  default title if nothing else is found
63893075b01SAndreas Gohr     * @param null|string       $id       linked page id (used to extract title from first heading)
63993075b01SAndreas Gohr     * @return string title text
64039a89382SEsther Brunner     */
6410ea51e63SMatt Perry    function _getLinkTitle($title, $default, $id = null) {
64285038eabSDanny        if(is_array($title)) {
64393075b01SAndreas Gohr            if($title['title']) {
64493075b01SAndreas Gohr                return '['.$title['title'].']';
64593075b01SAndreas Gohr            } else {
64693075b01SAndreas Gohr                return $default;
64793075b01SAndreas Gohr            }
64885038eabSDanny        } else if(is_null($title) || trim($title) == '') {
649fe9ec250SChris Smith            if(useHeading('content') && $id) {
65020f04039SDanny                $heading = p_get_first_heading($id, METADATA_DONT_RENDER);
65139a89382SEsther Brunner                if($heading) return $heading;
65239a89382SEsther Brunner            }
65339a89382SEsther Brunner            return $default;
65485038eabSDanny        } else {
65539a89382SEsther Brunner            return $title;
65639a89382SEsther Brunner        }
65739a89382SEsther Brunner    }
65839a89382SEsther Brunner
65993075b01SAndreas Gohr    /**
66093075b01SAndreas Gohr     * Remember first image
66193075b01SAndreas Gohr     *
66293075b01SAndreas Gohr     * @param string $src image URL or ID
66393075b01SAndreas Gohr     */
664aa5a2937SAndreas Gohr    function _firstimage($src) {
665aa5a2937SAndreas Gohr        if($this->firstimage) return;
666aa5a2937SAndreas Gohr        global $ID;
667aa5a2937SAndreas Gohr
66893075b01SAndreas Gohr        list($src) = explode('#', $src, 2);
6693e7e6067SKlap-in        if(!media_isexternal($src)) {
670aa5a2937SAndreas Gohr            resolve_mediaid(getNS($ID), $src, $exists);
671aa5a2937SAndreas Gohr        }
672aa5a2937SAndreas Gohr        if(preg_match('/.(jpe?g|gif|png)$/i', $src)) {
673aa5a2937SAndreas Gohr            $this->firstimage = $src;
674aa5a2937SAndreas Gohr        }
675aa5a2937SAndreas Gohr    }
676ffec1009SMichael Hamann
67793075b01SAndreas Gohr    /**
67893075b01SAndreas Gohr     * Store list of used media files in metadata
67993075b01SAndreas Gohr     *
68093075b01SAndreas Gohr     * @param string $src media ID
68193075b01SAndreas Gohr     */
682ffec1009SMichael Hamann    function _recordMediaUsage($src) {
683ffec1009SMichael Hamann        global $ID;
684ffec1009SMichael Hamann
68593075b01SAndreas Gohr        list ($src) = explode('#', $src, 2);
686ffec1009SMichael Hamann        if(media_isexternal($src)) return;
687ffec1009SMichael Hamann        resolve_mediaid(getNS($ID), $src, $exists);
688ffec1009SMichael Hamann        $this->meta['relation']['media'][$src] = $exists;
689ffec1009SMichael Hamann    }
69093075b01SAndreas Gohr
69193075b01SAndreas Gohr    #endregion
69239a89382SEsther Brunner}
69339a89382SEsther Brunner
694e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
695