xref: /dokuwiki/inc/parser/metadata.php (revision b8f47726234de627bf87d33458e0d312c91f2da7)
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
50*b8f47726SChristopher Smith    /** @var bool whether or not data is being captured for the abstract, public to be accessible by plugins */
51*b8f47726SChristopher Smith    public $capturing = true;
52*b8f47726SChristopher Smith
5317076c2aSAndreas Gohr    /** @var bool determines if enough data for the abstract was collected, yet */
548f2758f6SAndreas Gohr    public $capture = true;
5517076c2aSAndreas Gohr
5617076c2aSAndreas Gohr    /** @var int number of bytes captured for abstract */
5717076c2aSAndreas Gohr    protected $captured = 0;
5817076c2aSAndreas Gohr
5993075b01SAndreas Gohr    /**
6093075b01SAndreas Gohr     * Returns the format produced by this renderer.
6193075b01SAndreas Gohr     *
6293075b01SAndreas Gohr     * @return string always 'metadata'
6393075b01SAndreas Gohr     */
645f70445dSAndreas Gohr    function getFormat() {
655f70445dSAndreas Gohr        return 'metadata';
665f70445dSAndreas Gohr    }
675f70445dSAndreas Gohr
6893075b01SAndreas Gohr    /**
6993075b01SAndreas Gohr     * Initialize the document
7093075b01SAndreas Gohr     *
7193075b01SAndreas Gohr     * Sets up some of the persistent info about the page if it doesn't exist, yet.
7293075b01SAndreas Gohr     */
7339a89382SEsther Brunner    function document_start() {
74ef47e298SAndreas Gohr        global $ID;
75620f4930SAndreas Gohr
76620f4930SAndreas Gohr        $this->headers = array();
77620f4930SAndreas Gohr
78ef47e298SAndreas Gohr        // external pages are missing create date
79ef47e298SAndreas Gohr        if(!$this->persistent['date']['created']) {
80ef47e298SAndreas Gohr            $this->persistent['date']['created'] = filectime(wikiFN($ID));
81ef47e298SAndreas Gohr        }
82a8e72133SAndreas Gohr        if(!isset($this->persistent['user'])) {
83a8e72133SAndreas Gohr            $this->persistent['user'] = '';
84a8e72133SAndreas Gohr        }
85ef47e298SAndreas Gohr        if(!isset($this->persistent['creator'])) {
86ef47e298SAndreas Gohr            $this->persistent['creator'] = '';
87ef47e298SAndreas Gohr        }
880a7e3bceSchris        // reset metadata to persistent values
890a7e3bceSchris        $this->meta = $this->persistent;
9039a89382SEsther Brunner    }
9139a89382SEsther Brunner
9293075b01SAndreas Gohr    /**
9393075b01SAndreas Gohr     * Finalize the document
9493075b01SAndreas Gohr     *
9593075b01SAndreas Gohr     * Stores collected data in the metadata
9693075b01SAndreas Gohr     */
9739a89382SEsther Brunner    function document_end() {
98482cff99SAndreas Gohr        global $ID;
99482cff99SAndreas Gohr
100b8595a66SAndreas Gohr        // store internal info in metadata (notoc,nocache)
101b8595a66SAndreas Gohr        $this->meta['internal'] = $this->info;
102b8595a66SAndreas Gohr
103c66972f2SAdrian Lang        if(!isset($this->meta['description']['abstract'])) {
10439a89382SEsther Brunner            // cut off too long abstracts
10539a89382SEsther Brunner            $this->doc = trim($this->doc);
10617076c2aSAndreas Gohr            if(strlen($this->doc) > self::ABSTRACT_MAX) {
10717076c2aSAndreas Gohr                $this->doc = utf8_substr($this->doc, 0, self::ABSTRACT_MAX).'…';
10817076c2aSAndreas Gohr            }
10939a89382SEsther Brunner            $this->meta['description']['abstract'] = $this->doc;
11039a89382SEsther Brunner        }
111aa5a2937SAndreas Gohr
112aa5a2937SAndreas Gohr        $this->meta['relation']['firstimage'] = $this->firstimage;
113482cff99SAndreas Gohr
114c0322273SAdrian Lang        if(!isset($this->meta['date']['modified'])) {
115482cff99SAndreas Gohr            $this->meta['date']['modified'] = filemtime(wikiFN($ID));
116482cff99SAndreas Gohr        }
117482cff99SAndreas Gohr
11839a89382SEsther Brunner    }
11939a89382SEsther Brunner
12093075b01SAndreas Gohr    /**
12117076c2aSAndreas Gohr     * Render plain text data
12217076c2aSAndreas Gohr     *
12317076c2aSAndreas Gohr     * This function takes care of the amount captured data and will stop capturing when
12417076c2aSAndreas Gohr     * enough abstract data is available
12517076c2aSAndreas Gohr     *
12617076c2aSAndreas Gohr     * @param $text
12717076c2aSAndreas Gohr     */
12817076c2aSAndreas Gohr    function cdata($text) {
129*b8f47726SChristopher Smith        if(!$this->capture || !$this->capturing) return;
13017076c2aSAndreas Gohr
13117076c2aSAndreas Gohr        $this->doc .= $text;
13217076c2aSAndreas Gohr
13317076c2aSAndreas Gohr        $this->captured += strlen($text);
13417076c2aSAndreas Gohr        if($this->captured > self::ABSTRACT_LEN) $this->capture = false;
13517076c2aSAndreas Gohr    }
13617076c2aSAndreas Gohr
13717076c2aSAndreas Gohr    /**
13893075b01SAndreas Gohr     * Add an item to the TOC
13993075b01SAndreas Gohr     *
14093075b01SAndreas Gohr     * @param string $id       the hash link
14193075b01SAndreas Gohr     * @param string $text     the text to display
14293075b01SAndreas Gohr     * @param int    $level    the nesting level
14393075b01SAndreas Gohr     */
144e7856beaSchris    function toc_additem($id, $text, $level) {
14539a89382SEsther Brunner        global $conf;
14639a89382SEsther Brunner
147e7856beaSchris        //only add items within configured levels
14839a89382SEsther Brunner        if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']) {
14939a89382SEsther Brunner            // the TOC is one of our standard ul list arrays ;-)
15039a89382SEsther Brunner            $this->meta['description']['tableofcontents'][] = array(
151e7856beaSchris                'hid'   => $id,
15239a89382SEsther Brunner                'title' => $text,
15339a89382SEsther Brunner                'type'  => 'ul',
15439a89382SEsther Brunner                'level' => $level - $conf['toptoclevel'] + 1
15539a89382SEsther Brunner            );
15639a89382SEsther Brunner        }
15739a89382SEsther Brunner
158e7856beaSchris    }
159e7856beaSchris
16093075b01SAndreas Gohr    /**
16193075b01SAndreas Gohr     * Render a heading
16293075b01SAndreas Gohr     *
16393075b01SAndreas Gohr     * @param string $text  the text to display
16493075b01SAndreas Gohr     * @param int    $level header level
16593075b01SAndreas Gohr     * @param int    $pos   byte position in the original source
16693075b01SAndreas Gohr     */
167e7856beaSchris    function header($text, $level, $pos) {
168c66972f2SAdrian Lang        if(!isset($this->meta['title'])) $this->meta['title'] = $text;
169e7856beaSchris
170e7856beaSchris        // add the header to the TOC
171e0c26282SGerrit Uitslag        $hid = $this->_headerToLink($text, true);
172e7856beaSchris        $this->toc_additem($hid, $text, $level);
173e7856beaSchris
17439a89382SEsther Brunner        // add to summary
17517076c2aSAndreas Gohr        $this->cdata(DOKU_LF.$text.DOKU_LF);
17639a89382SEsther Brunner    }
17739a89382SEsther Brunner
17893075b01SAndreas Gohr    /**
17993075b01SAndreas Gohr     * Open a paragraph
18093075b01SAndreas Gohr     */
18139a89382SEsther Brunner    function p_open() {
18217076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
18339a89382SEsther Brunner    }
18439a89382SEsther Brunner
18593075b01SAndreas Gohr    /**
18693075b01SAndreas Gohr     * Close a paragraph
18793075b01SAndreas Gohr     */
18839a89382SEsther Brunner    function p_close() {
18917076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
19039a89382SEsther Brunner    }
19139a89382SEsther Brunner
19293075b01SAndreas Gohr    /**
19393075b01SAndreas Gohr     * Create a line break
19493075b01SAndreas Gohr     */
19539a89382SEsther Brunner    function linebreak() {
19617076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
19739a89382SEsther Brunner    }
19839a89382SEsther Brunner
19993075b01SAndreas Gohr    /**
20093075b01SAndreas Gohr     * Create a horizontal line
20193075b01SAndreas Gohr     */
20239a89382SEsther Brunner    function hr() {
20317076c2aSAndreas Gohr        $this->cdata(DOKU_LF.'----------'.DOKU_LF);
20439a89382SEsther Brunner    }
20539a89382SEsther Brunner
20639a89382SEsther Brunner    /**
20739a89382SEsther Brunner     * Callback for footnote start syntax
20839a89382SEsther Brunner     *
20939a89382SEsther Brunner     * All following content will go to the footnote instead of
21039a89382SEsther Brunner     * the document. To achieve this the previous rendered content
21139a89382SEsther Brunner     * is moved to $store and $doc is cleared
21239a89382SEsther Brunner     *
21339a89382SEsther Brunner     * @author Andreas Gohr <andi@splitbrain.org>
21439a89382SEsther Brunner     */
21539a89382SEsther Brunner    function footnote_open() {
21639a89382SEsther Brunner        if($this->capture) {
217*b8f47726SChristopher Smith            // move current content to store
218*b8f47726SChristopher Smith            // this is required to ensure safe behaviour of plugins accessed within footnotes
21939a89382SEsther Brunner            $this->store = $this->doc;
22039a89382SEsther Brunner            $this->doc   = '';
221*b8f47726SChristopher Smith
222*b8f47726SChristopher Smith            // disable capturing
223*b8f47726SChristopher Smith            $this->capturing = false;
22439a89382SEsther Brunner        }
22539a89382SEsther Brunner    }
22639a89382SEsther Brunner
22739a89382SEsther Brunner    /**
22839a89382SEsther Brunner     * Callback for footnote end syntax
22939a89382SEsther Brunner     *
230*b8f47726SChristopher Smith     * All content rendered whilst within footnote syntax mode is discarded,
231*b8f47726SChristopher Smith     * the previously rendered content is restored and capturing is re-enabled.
23239a89382SEsther Brunner     *
23339a89382SEsther Brunner     * @author Andreas Gohr
23439a89382SEsther Brunner     */
23539a89382SEsther Brunner    function footnote_close() {
23639a89382SEsther Brunner        if($this->capture) {
237*b8f47726SChristopher Smith            // re-enable capturing
238*b8f47726SChristopher Smith            $this->capturing = true;
239*b8f47726SChristopher Smith            // restore previously rendered content
24039a89382SEsther Brunner            $this->doc   = $this->store;
24139a89382SEsther Brunner            $this->store = '';
24239a89382SEsther Brunner        }
24339a89382SEsther Brunner    }
24439a89382SEsther Brunner
24593075b01SAndreas Gohr    /**
24693075b01SAndreas Gohr     * Open an unordered list
24793075b01SAndreas Gohr     */
24839a89382SEsther Brunner    function listu_open() {
24917076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
25039a89382SEsther Brunner    }
25139a89382SEsther Brunner
25293075b01SAndreas Gohr    /**
25393075b01SAndreas Gohr     * Open an ordered list
25493075b01SAndreas Gohr     */
25539a89382SEsther Brunner    function listo_open() {
25617076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
25739a89382SEsther Brunner    }
25839a89382SEsther Brunner
25993075b01SAndreas Gohr    /**
26093075b01SAndreas Gohr     * Open a list item
26193075b01SAndreas Gohr     *
26293075b01SAndreas Gohr     * @param int $level the nesting level
263e3a24861SChristopher Smith     * @param bool $node true when a node; false when a leaf
26493075b01SAndreas Gohr     */
265e3a24861SChristopher Smith    function listitem_open($level,$node=false) {
26617076c2aSAndreas Gohr        $this->cdata(str_repeat(DOKU_TAB, $level).'* ');
26739a89382SEsther Brunner    }
26839a89382SEsther Brunner
26993075b01SAndreas Gohr    /**
27093075b01SAndreas Gohr     * Close a list item
27193075b01SAndreas Gohr     */
27239a89382SEsther Brunner    function listitem_close() {
27317076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
27439a89382SEsther Brunner    }
27539a89382SEsther Brunner
27693075b01SAndreas Gohr    /**
27793075b01SAndreas Gohr     * Output preformatted text
27893075b01SAndreas Gohr     *
27993075b01SAndreas Gohr     * @param string $text
28093075b01SAndreas Gohr     */
28139a89382SEsther Brunner    function preformatted($text) {
28217076c2aSAndreas Gohr        $this->cdata($text);
28339a89382SEsther Brunner    }
28439a89382SEsther Brunner
28593075b01SAndreas Gohr    /**
28693075b01SAndreas Gohr     * Start a block quote
28793075b01SAndreas Gohr     */
28839a89382SEsther Brunner    function quote_open() {
28917076c2aSAndreas Gohr        $this->cdata(DOKU_LF.DOKU_TAB.'"');
29039a89382SEsther Brunner    }
29139a89382SEsther Brunner
29293075b01SAndreas Gohr    /**
29393075b01SAndreas Gohr     * Stop a block quote
29493075b01SAndreas Gohr     */
29539a89382SEsther Brunner    function quote_close() {
29617076c2aSAndreas Gohr        $this->cdata('"'.DOKU_LF);
29739a89382SEsther Brunner    }
29839a89382SEsther Brunner
29993075b01SAndreas Gohr    /**
30093075b01SAndreas Gohr     * Display text as file content, optionally syntax highlighted
30193075b01SAndreas Gohr     *
30293075b01SAndreas Gohr     * @param string $text text to show
30393075b01SAndreas Gohr     * @param string $lang programming language to use for syntax highlighting
30493075b01SAndreas Gohr     * @param string $file file path label
30593075b01SAndreas Gohr     */
30693075b01SAndreas Gohr    function file($text, $lang = null, $file = null) {
30717076c2aSAndreas Gohr        $this->cdata(DOKU_LF.$text.DOKU_LF);
30893075b01SAndreas Gohr    }
30993075b01SAndreas Gohr
31093075b01SAndreas Gohr    /**
31193075b01SAndreas Gohr     * Display text as code content, optionally syntax highlighted
31293075b01SAndreas Gohr     *
31393075b01SAndreas Gohr     * @param string $text     text to show
31493075b01SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
31593075b01SAndreas Gohr     * @param string $file     file path label
31693075b01SAndreas Gohr     */
3170ea51e63SMatt Perry    function code($text, $language = null, $file = null) {
31817076c2aSAndreas Gohr        $this->cdata(DOKU_LF.$text.DOKU_LF);
31939a89382SEsther Brunner    }
32039a89382SEsther Brunner
32193075b01SAndreas Gohr    /**
32293075b01SAndreas Gohr     * Format an acronym
32393075b01SAndreas Gohr     *
32493075b01SAndreas Gohr     * Uses $this->acronyms
32593075b01SAndreas Gohr     *
32693075b01SAndreas Gohr     * @param string $acronym
32793075b01SAndreas Gohr     */
32839a89382SEsther Brunner    function acronym($acronym) {
32917076c2aSAndreas Gohr        $this->cdata($acronym);
33039a89382SEsther Brunner    }
33139a89382SEsther Brunner
33293075b01SAndreas Gohr    /**
33393075b01SAndreas Gohr     * Format a smiley
33493075b01SAndreas Gohr     *
33593075b01SAndreas Gohr     * Uses $this->smiley
33693075b01SAndreas Gohr     *
33793075b01SAndreas Gohr     * @param string $smiley
33893075b01SAndreas Gohr     */
33939a89382SEsther Brunner    function smiley($smiley) {
34017076c2aSAndreas Gohr        $this->cdata($smiley);
34139a89382SEsther Brunner    }
34239a89382SEsther Brunner
34393075b01SAndreas Gohr    /**
34493075b01SAndreas Gohr     * Format an entity
34593075b01SAndreas Gohr     *
34693075b01SAndreas Gohr     * Entities are basically small text replacements
34793075b01SAndreas Gohr     *
34893075b01SAndreas Gohr     * Uses $this->entities
34993075b01SAndreas Gohr     *
35093075b01SAndreas Gohr     * @param string $entity
35193075b01SAndreas Gohr     */
35239a89382SEsther Brunner    function entity($entity) {
35317076c2aSAndreas Gohr        $this->cdata($entity);
35439a89382SEsther Brunner    }
35539a89382SEsther Brunner
35693075b01SAndreas Gohr    /**
35793075b01SAndreas Gohr     * Typographically format a multiply sign
35893075b01SAndreas Gohr     *
35993075b01SAndreas Gohr     * Example: ($x=640, $y=480) should result in "640×480"
36093075b01SAndreas Gohr     *
36193075b01SAndreas Gohr     * @param string|int $x first value
36293075b01SAndreas Gohr     * @param string|int $y second value
36393075b01SAndreas Gohr     */
36439a89382SEsther Brunner    function multiplyentity($x, $y) {
36517076c2aSAndreas Gohr        $this->cdata($x.'×'.$y);
36639a89382SEsther Brunner    }
36739a89382SEsther Brunner
36893075b01SAndreas Gohr    /**
36993075b01SAndreas Gohr     * Render an opening single quote char (language specific)
37093075b01SAndreas Gohr     */
37139a89382SEsther Brunner    function singlequoteopening() {
37271b40da2SAnika Henke        global $lang;
37317076c2aSAndreas Gohr        $this->cdata($lang['singlequoteopening']);
37439a89382SEsther Brunner    }
37539a89382SEsther Brunner
37693075b01SAndreas Gohr    /**
37793075b01SAndreas Gohr     * Render a closing single quote char (language specific)
37893075b01SAndreas Gohr     */
37939a89382SEsther Brunner    function singlequoteclosing() {
38071b40da2SAnika Henke        global $lang;
38117076c2aSAndreas Gohr        $this->cdata($lang['singlequoteclosing']);
38239a89382SEsther Brunner    }
38339a89382SEsther Brunner
38493075b01SAndreas Gohr    /**
38593075b01SAndreas Gohr     * Render an apostrophe char (language specific)
38693075b01SAndreas Gohr     */
38757d757d1SAndreas Gohr    function apostrophe() {
38857d757d1SAndreas Gohr        global $lang;
38917076c2aSAndreas Gohr        $this->cdata($lang['apostrophe']);
39057d757d1SAndreas Gohr    }
39157d757d1SAndreas Gohr
39293075b01SAndreas Gohr    /**
39393075b01SAndreas Gohr     * Render an opening double quote char (language specific)
39493075b01SAndreas Gohr     */
39539a89382SEsther Brunner    function doublequoteopening() {
39671b40da2SAnika Henke        global $lang;
39717076c2aSAndreas Gohr        $this->cdata($lang['doublequoteopening']);
39839a89382SEsther Brunner    }
39939a89382SEsther Brunner
40093075b01SAndreas Gohr    /**
40193075b01SAndreas Gohr     * Render an closinging double quote char (language specific)
40293075b01SAndreas Gohr     */
40339a89382SEsther Brunner    function doublequoteclosing() {
40471b40da2SAnika Henke        global $lang;
40517076c2aSAndreas Gohr        $this->cdata($lang['doublequoteclosing']);
40639a89382SEsther Brunner    }
40739a89382SEsther Brunner
40893075b01SAndreas Gohr    /**
40993075b01SAndreas Gohr     * Render a CamelCase link
41093075b01SAndreas Gohr     *
41193075b01SAndreas Gohr     * @param string $link The link name
41293075b01SAndreas Gohr     * @see http://en.wikipedia.org/wiki/CamelCase
41393075b01SAndreas Gohr     */
41439a89382SEsther Brunner    function camelcaselink($link) {
41539a89382SEsther Brunner        $this->internallink($link, $link);
41639a89382SEsther Brunner    }
41739a89382SEsther Brunner
41893075b01SAndreas Gohr    /**
41993075b01SAndreas Gohr     * Render a page local link
42093075b01SAndreas Gohr     *
42193075b01SAndreas Gohr     * @param string $hash hash link identifier
42293075b01SAndreas Gohr     * @param string $name name for the link
42393075b01SAndreas Gohr     */
4249269d0b1SMichael Hamann    function locallink($hash, $name = null) {
4259269d0b1SMichael Hamann        if(is_array($name)) {
4269269d0b1SMichael Hamann            $this->_firstimage($name['src']);
4279269d0b1SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
4289269d0b1SMichael Hamann        }
4299269d0b1SMichael Hamann    }
43039a89382SEsther Brunner
43139a89382SEsther Brunner    /**
43239a89382SEsther Brunner     * keep track of internal links in $this->meta['relation']['references']
43393075b01SAndreas Gohr     *
43493075b01SAndreas Gohr     * @param string            $id   page ID to link to. eg. 'wiki:syntax'
43559bc3b48SGerrit Uitslag     * @param string|array|null $name name for the link, array for media file
43639a89382SEsther Brunner     */
4370ea51e63SMatt Perry    function internallink($id, $name = null) {
43839a89382SEsther Brunner        global $ID;
43939a89382SEsther Brunner
440ffec1009SMichael Hamann        if(is_array($name)) {
441aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
442ffec1009SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
443ffec1009SMichael Hamann        }
444aa5a2937SAndreas Gohr
44566d935e7SMichael Hamann        $parts = explode('?', $id, 2);
44666d935e7SMichael Hamann        if(count($parts) === 2) {
44766d935e7SMichael Hamann            $id = $parts[0];
44866d935e7SMichael Hamann        }
44966d935e7SMichael Hamann
45039a89382SEsther Brunner        $default = $this->_simpleTitle($id);
45139a89382SEsther Brunner
45239a89382SEsther Brunner        // first resolve and clean up the $id
45339a89382SEsther Brunner        resolve_pageid(getNS($ID), $id, $exists);
45493075b01SAndreas Gohr        @list($page) = explode('#', $id, 2);
45539a89382SEsther Brunner
45639a89382SEsther Brunner        // set metadata
4573be6e394Schris        $this->meta['relation']['references'][$page] = $exists;
45839a89382SEsther Brunner        // $data = array('relation' => array('isreferencedby' => array($ID => true)));
45939a89382SEsther Brunner        // p_set_metadata($id, $data);
46039a89382SEsther Brunner
46139a89382SEsther Brunner        // add link title to summary
46239a89382SEsther Brunner        if($this->capture) {
46339a89382SEsther Brunner            $name = $this->_getLinkTitle($name, $default, $id);
46439a89382SEsther Brunner            $this->doc .= $name;
46539a89382SEsther Brunner        }
46639a89382SEsther Brunner    }
46739a89382SEsther Brunner
46893075b01SAndreas Gohr    /**
46993075b01SAndreas Gohr     * Render an external link
47093075b01SAndreas Gohr     *
47193075b01SAndreas Gohr     * @param string            $url  full URL with scheme
47259bc3b48SGerrit Uitslag     * @param string|array|null $name name for the link, array for media file
47393075b01SAndreas Gohr     */
4740ea51e63SMatt Perry    function externallink($url, $name = null) {
475ffec1009SMichael Hamann        if(is_array($name)) {
476aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
477ffec1009SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
478ffec1009SMichael Hamann        }
479aa5a2937SAndreas Gohr
48039a89382SEsther Brunner        if($this->capture) {
481a2ea2dc1SAdrian Lang            $this->doc .= $this->_getLinkTitle($name, '<'.$url.'>');
48239a89382SEsther Brunner        }
48339a89382SEsther Brunner    }
48439a89382SEsther Brunner
48593075b01SAndreas Gohr    /**
48693075b01SAndreas Gohr     * Render an interwiki link
48793075b01SAndreas Gohr     *
48893075b01SAndreas Gohr     * You may want to use $this->_resolveInterWiki() here
48993075b01SAndreas Gohr     *
49093075b01SAndreas Gohr     * @param string       $match     original link - probably not much use
49193075b01SAndreas Gohr     * @param string|array $name      name for the link, array for media file
49293075b01SAndreas Gohr     * @param string       $wikiName  indentifier (shortcut) for the remote wiki
49393075b01SAndreas Gohr     * @param string       $wikiUri   the fragment parsed from the original link
49493075b01SAndreas Gohr     */
4950ea51e63SMatt Perry    function interwikilink($match, $name = null, $wikiName, $wikiUri) {
496ffec1009SMichael Hamann        if(is_array($name)) {
497aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
498ffec1009SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
499ffec1009SMichael Hamann        }
500aa5a2937SAndreas Gohr
50139a89382SEsther Brunner        if($this->capture) {
50293075b01SAndreas Gohr            list($wikiUri) = explode('#', $wikiUri, 2);
503a2ea2dc1SAdrian Lang            $name = $this->_getLinkTitle($name, $wikiUri);
50439a89382SEsther Brunner            $this->doc .= $name;
50539a89382SEsther Brunner        }
50639a89382SEsther Brunner    }
50739a89382SEsther Brunner
50893075b01SAndreas Gohr    /**
50993075b01SAndreas Gohr     * Link to windows share
51093075b01SAndreas Gohr     *
51193075b01SAndreas Gohr     * @param string       $url  the link
51293075b01SAndreas Gohr     * @param string|array $name name for the link, array for media file
51393075b01SAndreas Gohr     */
5140ea51e63SMatt Perry    function windowssharelink($url, $name = null) {
515ffec1009SMichael Hamann        if(is_array($name)) {
516aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
517ffec1009SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
518ffec1009SMichael Hamann        }
519aa5a2937SAndreas Gohr
52039a89382SEsther Brunner        if($this->capture) {
52139a89382SEsther Brunner            if($name) $this->doc .= $name;
52239a89382SEsther Brunner            else $this->doc .= '<'.$url.'>';
52339a89382SEsther Brunner        }
52439a89382SEsther Brunner    }
52539a89382SEsther Brunner
52693075b01SAndreas Gohr    /**
52793075b01SAndreas Gohr     * Render a linked E-Mail Address
52893075b01SAndreas Gohr     *
52993075b01SAndreas Gohr     * Should honor $conf['mailguard'] setting
53093075b01SAndreas Gohr     *
53193075b01SAndreas Gohr     * @param string       $address Email-Address
53293075b01SAndreas Gohr     * @param string|array $name    name for the link, array for media file
53393075b01SAndreas Gohr     */
5340ea51e63SMatt Perry    function emaillink($address, $name = null) {
535ffec1009SMichael Hamann        if(is_array($name)) {
536aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
537ffec1009SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
538ffec1009SMichael Hamann        }
539aa5a2937SAndreas Gohr
54039a89382SEsther Brunner        if($this->capture) {
54139a89382SEsther Brunner            if($name) $this->doc .= $name;
54239a89382SEsther Brunner            else $this->doc .= '<'.$address.'>';
54339a89382SEsther Brunner        }
54439a89382SEsther Brunner    }
54539a89382SEsther Brunner
54693075b01SAndreas Gohr    /**
54793075b01SAndreas Gohr     * Render an internal media file
54893075b01SAndreas Gohr     *
54993075b01SAndreas Gohr     * @param string $src     media ID
55093075b01SAndreas Gohr     * @param string $title   descriptive text
55193075b01SAndreas Gohr     * @param string $align   left|center|right
55293075b01SAndreas Gohr     * @param int    $width   width of media in pixel
55393075b01SAndreas Gohr     * @param int    $height  height of media in pixel
55493075b01SAndreas Gohr     * @param string $cache   cache|recache|nocache
55593075b01SAndreas Gohr     * @param string $linking linkonly|detail|nolink
55693075b01SAndreas Gohr     */
5570ea51e63SMatt Perry    function internalmedia($src, $title = null, $align = null, $width = null,
5580ea51e63SMatt Perry                           $height = null, $cache = null, $linking = null) {
55939a89382SEsther Brunner        if($this->capture && $title) $this->doc .= '['.$title.']';
560aa5a2937SAndreas Gohr        $this->_firstimage($src);
561ffec1009SMichael Hamann        $this->_recordMediaUsage($src);
56239a89382SEsther Brunner    }
56339a89382SEsther Brunner
56493075b01SAndreas Gohr    /**
56593075b01SAndreas Gohr     * Render an external media file
56693075b01SAndreas Gohr     *
56793075b01SAndreas Gohr     * @param string $src     full media URL
56893075b01SAndreas Gohr     * @param string $title   descriptive text
56993075b01SAndreas Gohr     * @param string $align   left|center|right
57093075b01SAndreas Gohr     * @param int    $width   width of media in pixel
57193075b01SAndreas Gohr     * @param int    $height  height of media in pixel
57293075b01SAndreas Gohr     * @param string $cache   cache|recache|nocache
57393075b01SAndreas Gohr     * @param string $linking linkonly|detail|nolink
57493075b01SAndreas Gohr     */
5750ea51e63SMatt Perry    function externalmedia($src, $title = null, $align = null, $width = null,
5760ea51e63SMatt Perry                           $height = null, $cache = null, $linking = null) {
57739a89382SEsther Brunner        if($this->capture && $title) $this->doc .= '['.$title.']';
578aa5a2937SAndreas Gohr        $this->_firstimage($src);
57939a89382SEsther Brunner    }
58039a89382SEsther Brunner
58193075b01SAndreas Gohr    /**
58293075b01SAndreas Gohr     * Render the output of an RSS feed
58393075b01SAndreas Gohr     *
58493075b01SAndreas Gohr     * @param string $url    URL of the feed
58593075b01SAndreas Gohr     * @param array  $params Finetuning of the output
58693075b01SAndreas Gohr     */
587ce6b63d9Schris    function rss($url, $params) {
588ce6b63d9Schris        $this->meta['relation']['haspart'][$url] = true;
58964e9144aSchris
59064e9144aSchris        $this->meta['date']['valid']['age'] =
59164e9144aSchris            isset($this->meta['date']['valid']['age']) ?
59264e9144aSchris                min($this->meta['date']['valid']['age'], $params['refresh']) :
59364e9144aSchris                $params['refresh'];
594ce6b63d9Schris    }
59539a89382SEsther Brunner
59693075b01SAndreas Gohr    #region Utils
59739a89382SEsther Brunner
59839a89382SEsther Brunner    /**
59939a89382SEsther Brunner     * Removes any Namespace from the given name but keeps
60039a89382SEsther Brunner     * casing and special chars
60139a89382SEsther Brunner     *
60239a89382SEsther Brunner     * @author Andreas Gohr <andi@splitbrain.org>
603f50a239bSTakamura     *
604f50a239bSTakamura     * @param string $name
605f50a239bSTakamura     *
606f50a239bSTakamura     * @return mixed|string
60739a89382SEsther Brunner     */
60839a89382SEsther Brunner    function _simpleTitle($name) {
60939a89382SEsther Brunner        global $conf;
61039a89382SEsther Brunner
6119bee852eSAndreas Gohr        if(is_array($name)) return '';
6129bee852eSAndreas Gohr
61339a89382SEsther Brunner        if($conf['useslash']) {
61439a89382SEsther Brunner            $nssep = '[:;/]';
61539a89382SEsther Brunner        } else {
61639a89382SEsther Brunner            $nssep = '[:;]';
61739a89382SEsther Brunner        }
61839a89382SEsther Brunner        $name = preg_replace('!.*'.$nssep.'!', '', $name);
6193be6e394Schris        //if there is a hash we use the anchor name only
62039a89382SEsther Brunner        $name = preg_replace('!.*#!', '', $name);
62139a89382SEsther Brunner        return $name;
62239a89382SEsther Brunner    }
62339a89382SEsther Brunner
62439a89382SEsther Brunner    /**
62539a89382SEsther Brunner     * Creates a linkid from a headline
62639a89382SEsther Brunner     *
62793075b01SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
62839a89382SEsther Brunner     * @param string  $title   The headline title
62939a89382SEsther Brunner     * @param boolean $create  Create a new unique ID?
63093075b01SAndreas Gohr     * @return string
63139a89382SEsther Brunner     */
63239a89382SEsther Brunner    function _headerToLink($title, $create = false) {
63339a89382SEsther Brunner        if($create) {
634620f4930SAndreas Gohr            return sectionID($title, $this->headers);
635620f4930SAndreas Gohr        } else {
636620f4930SAndreas Gohr            $check = false;
637620f4930SAndreas Gohr            return sectionID($title, $check);
63839a89382SEsther Brunner        }
63939a89382SEsther Brunner    }
64039a89382SEsther Brunner
64139a89382SEsther Brunner    /**
64239a89382SEsther Brunner     * Construct a title and handle images in titles
64339a89382SEsther Brunner     *
64439a89382SEsther Brunner     * @author Harry Fuecks <hfuecks@gmail.com>
64559bc3b48SGerrit Uitslag     * @param string|array|null $title    either string title or media array
64693075b01SAndreas Gohr     * @param string            $default  default title if nothing else is found
64793075b01SAndreas Gohr     * @param null|string       $id       linked page id (used to extract title from first heading)
64893075b01SAndreas Gohr     * @return string title text
64939a89382SEsther Brunner     */
6500ea51e63SMatt Perry    function _getLinkTitle($title, $default, $id = null) {
65185038eabSDanny        if(is_array($title)) {
65293075b01SAndreas Gohr            if($title['title']) {
65393075b01SAndreas Gohr                return '['.$title['title'].']';
65493075b01SAndreas Gohr            } else {
65593075b01SAndreas Gohr                return $default;
65693075b01SAndreas Gohr            }
65785038eabSDanny        } else if(is_null($title) || trim($title) == '') {
658fe9ec250SChris Smith            if(useHeading('content') && $id) {
65920f04039SDanny                $heading = p_get_first_heading($id, METADATA_DONT_RENDER);
66039a89382SEsther Brunner                if($heading) return $heading;
66139a89382SEsther Brunner            }
66239a89382SEsther Brunner            return $default;
66385038eabSDanny        } else {
66439a89382SEsther Brunner            return $title;
66539a89382SEsther Brunner        }
66639a89382SEsther Brunner    }
66739a89382SEsther Brunner
66893075b01SAndreas Gohr    /**
66993075b01SAndreas Gohr     * Remember first image
67093075b01SAndreas Gohr     *
67193075b01SAndreas Gohr     * @param string $src image URL or ID
67293075b01SAndreas Gohr     */
673aa5a2937SAndreas Gohr    function _firstimage($src) {
674aa5a2937SAndreas Gohr        if($this->firstimage) return;
675aa5a2937SAndreas Gohr        global $ID;
676aa5a2937SAndreas Gohr
67793075b01SAndreas Gohr        list($src) = explode('#', $src, 2);
6783e7e6067SKlap-in        if(!media_isexternal($src)) {
679aa5a2937SAndreas Gohr            resolve_mediaid(getNS($ID), $src, $exists);
680aa5a2937SAndreas Gohr        }
681aa5a2937SAndreas Gohr        if(preg_match('/.(jpe?g|gif|png)$/i', $src)) {
682aa5a2937SAndreas Gohr            $this->firstimage = $src;
683aa5a2937SAndreas Gohr        }
684aa5a2937SAndreas Gohr    }
685ffec1009SMichael Hamann
68693075b01SAndreas Gohr    /**
68793075b01SAndreas Gohr     * Store list of used media files in metadata
68893075b01SAndreas Gohr     *
68993075b01SAndreas Gohr     * @param string $src media ID
69093075b01SAndreas Gohr     */
691ffec1009SMichael Hamann    function _recordMediaUsage($src) {
692ffec1009SMichael Hamann        global $ID;
693ffec1009SMichael Hamann
69493075b01SAndreas Gohr        list ($src) = explode('#', $src, 2);
695ffec1009SMichael Hamann        if(media_isexternal($src)) return;
696ffec1009SMichael Hamann        resolve_mediaid(getNS($ID), $src, $exists);
697ffec1009SMichael Hamann        $this->meta['relation']['media'][$src] = $exists;
698ffec1009SMichael Hamann    }
69993075b01SAndreas Gohr
70093075b01SAndreas Gohr    #endregion
70139a89382SEsther Brunner}
70239a89382SEsther Brunner
703e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
704