xref: /dokuwiki/inc/parser/metadata.php (revision b4f2363aa1360136c8a826f09aaebc6505211c73)
139a89382SEsther Brunner<?php
239a89382SEsther Brunner/**
393075b01SAndreas Gohr * The MetaData Renderer
493075b01SAndreas Gohr *
593075b01SAndreas Gohr * Metadata is additional information about a DokuWiki page that gets extracted mainly from the page's content
693075b01SAndreas Gohr * but also it's own filesystem data (like the creation time). All metadata is stored in the fields $meta and
793075b01SAndreas Gohr * $persistent.
893075b01SAndreas Gohr *
993075b01SAndreas Gohr * Some simplified rendering to $doc is done to gather the page's (text-only) abstract.
10*b4f2363aSAndreas Gohr *
11*b4f2363aSAndreas Gohr * @author Esther Brunner <wikidesign@gmail.com>
1239a89382SEsther Brunner */
1339a89382SEsther Brunnerclass Doku_Renderer_metadata extends Doku_Renderer {
1417076c2aSAndreas Gohr    /** the approximate byte lenght to capture for the abstract */
1517076c2aSAndreas Gohr    const ABSTRACT_LEN = 250;
1617076c2aSAndreas Gohr
1717076c2aSAndreas Gohr    /** the maximum UTF8 character length for the abstract */
1817076c2aSAndreas Gohr    const ABSTRACT_MAX = 500;
1939a89382SEsther Brunner
2093075b01SAndreas Gohr    /** @var array transient meta data, will be reset on each rendering */
2193075b01SAndreas Gohr    public $meta = array();
2239a89382SEsther Brunner
2393075b01SAndreas Gohr    /** @var array persistent meta data, will be kept until explicitly deleted */
2493075b01SAndreas Gohr    public $persistent = array();
2539a89382SEsther Brunner
2693075b01SAndreas Gohr    /** @var array the list of headers used to create unique link ids */
2793075b01SAndreas Gohr    protected $headers = array();
2893075b01SAndreas Gohr
2993075b01SAndreas Gohr    /** @var string temporary $doc store */
3093075b01SAndreas Gohr    protected $store = '';
3193075b01SAndreas Gohr
3293075b01SAndreas Gohr    /** @var string keeps the first image reference */
3393075b01SAndreas Gohr    protected $firstimage = '';
3493075b01SAndreas Gohr
3517076c2aSAndreas Gohr    /** @var bool determines if enough data for the abstract was collected, yet */
368f2758f6SAndreas Gohr    public $capture = true;
3717076c2aSAndreas Gohr
3817076c2aSAndreas Gohr    /** @var int number of bytes captured for abstract */
3917076c2aSAndreas Gohr    protected $captured = 0;
4017076c2aSAndreas Gohr
4193075b01SAndreas Gohr    /**
4293075b01SAndreas Gohr     * Returns the format produced by this renderer.
4393075b01SAndreas Gohr     *
4493075b01SAndreas Gohr     * @return string always 'metadata'
4593075b01SAndreas Gohr     */
465f70445dSAndreas Gohr    function getFormat() {
475f70445dSAndreas Gohr        return 'metadata';
485f70445dSAndreas Gohr    }
495f70445dSAndreas Gohr
5093075b01SAndreas Gohr    /**
5193075b01SAndreas Gohr     * Initialize the document
5293075b01SAndreas Gohr     *
5393075b01SAndreas Gohr     * Sets up some of the persistent info about the page if it doesn't exist, yet.
5493075b01SAndreas Gohr     */
5539a89382SEsther Brunner    function document_start() {
56ef47e298SAndreas Gohr        global $ID;
57620f4930SAndreas Gohr
58620f4930SAndreas Gohr        $this->headers = array();
59620f4930SAndreas Gohr
60ef47e298SAndreas Gohr        // external pages are missing create date
61ef47e298SAndreas Gohr        if(!$this->persistent['date']['created']) {
62ef47e298SAndreas Gohr            $this->persistent['date']['created'] = filectime(wikiFN($ID));
63ef47e298SAndreas Gohr        }
64a8e72133SAndreas Gohr        if(!isset($this->persistent['user'])) {
65a8e72133SAndreas Gohr            $this->persistent['user'] = '';
66a8e72133SAndreas Gohr        }
67ef47e298SAndreas Gohr        if(!isset($this->persistent['creator'])) {
68ef47e298SAndreas Gohr            $this->persistent['creator'] = '';
69ef47e298SAndreas Gohr        }
700a7e3bceSchris        // reset metadata to persistent values
710a7e3bceSchris        $this->meta = $this->persistent;
7239a89382SEsther Brunner    }
7339a89382SEsther Brunner
7493075b01SAndreas Gohr    /**
7593075b01SAndreas Gohr     * Finalize the document
7693075b01SAndreas Gohr     *
7793075b01SAndreas Gohr     * Stores collected data in the metadata
7893075b01SAndreas Gohr     */
7939a89382SEsther Brunner    function document_end() {
80482cff99SAndreas Gohr        global $ID;
81482cff99SAndreas Gohr
82b8595a66SAndreas Gohr        // store internal info in metadata (notoc,nocache)
83b8595a66SAndreas Gohr        $this->meta['internal'] = $this->info;
84b8595a66SAndreas Gohr
85c66972f2SAdrian Lang        if(!isset($this->meta['description']['abstract'])) {
8639a89382SEsther Brunner            // cut off too long abstracts
8739a89382SEsther Brunner            $this->doc = trim($this->doc);
8817076c2aSAndreas Gohr            if(strlen($this->doc) > self::ABSTRACT_MAX) {
8917076c2aSAndreas Gohr                $this->doc = utf8_substr($this->doc, 0, self::ABSTRACT_MAX).'…';
9017076c2aSAndreas Gohr            }
9139a89382SEsther Brunner            $this->meta['description']['abstract'] = $this->doc;
9239a89382SEsther Brunner        }
93aa5a2937SAndreas Gohr
94aa5a2937SAndreas Gohr        $this->meta['relation']['firstimage'] = $this->firstimage;
95482cff99SAndreas Gohr
96c0322273SAdrian Lang        if(!isset($this->meta['date']['modified'])) {
97482cff99SAndreas Gohr            $this->meta['date']['modified'] = filemtime(wikiFN($ID));
98482cff99SAndreas Gohr        }
99482cff99SAndreas Gohr
10039a89382SEsther Brunner    }
10139a89382SEsther Brunner
10293075b01SAndreas Gohr    /**
10317076c2aSAndreas Gohr     * Render plain text data
10417076c2aSAndreas Gohr     *
10517076c2aSAndreas Gohr     * This function takes care of the amount captured data and will stop capturing when
10617076c2aSAndreas Gohr     * enough abstract data is available
10717076c2aSAndreas Gohr     *
10817076c2aSAndreas Gohr     * @param $text
10917076c2aSAndreas Gohr     */
11017076c2aSAndreas Gohr    function cdata($text) {
11117076c2aSAndreas Gohr        if(!$this->capture) return;
11217076c2aSAndreas Gohr
11317076c2aSAndreas Gohr        $this->doc .= $text;
11417076c2aSAndreas Gohr
11517076c2aSAndreas Gohr        $this->captured += strlen($text);
11617076c2aSAndreas Gohr        if($this->captured > self::ABSTRACT_LEN) $this->capture = false;
11717076c2aSAndreas Gohr    }
11817076c2aSAndreas Gohr
11917076c2aSAndreas Gohr    /**
12093075b01SAndreas Gohr     * Add an item to the TOC
12193075b01SAndreas Gohr     *
12293075b01SAndreas Gohr     * @param string $id       the hash link
12393075b01SAndreas Gohr     * @param string $text     the text to display
12493075b01SAndreas Gohr     * @param int    $level    the nesting level
12593075b01SAndreas Gohr     */
126e7856beaSchris    function toc_additem($id, $text, $level) {
12739a89382SEsther Brunner        global $conf;
12839a89382SEsther Brunner
129e7856beaSchris        //only add items within configured levels
13039a89382SEsther Brunner        if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']) {
13139a89382SEsther Brunner            // the TOC is one of our standard ul list arrays ;-)
13239a89382SEsther Brunner            $this->meta['description']['tableofcontents'][] = array(
133e7856beaSchris                'hid'   => $id,
13439a89382SEsther Brunner                'title' => $text,
13539a89382SEsther Brunner                'type'  => 'ul',
13639a89382SEsther Brunner                'level' => $level - $conf['toptoclevel'] + 1
13739a89382SEsther Brunner            );
13839a89382SEsther Brunner        }
13939a89382SEsther Brunner
140e7856beaSchris    }
141e7856beaSchris
14293075b01SAndreas Gohr    /**
14393075b01SAndreas Gohr     * Render a heading
14493075b01SAndreas Gohr     *
14593075b01SAndreas Gohr     * @param string $text  the text to display
14693075b01SAndreas Gohr     * @param int    $level header level
14793075b01SAndreas Gohr     * @param int    $pos   byte position in the original source
14893075b01SAndreas Gohr     */
149e7856beaSchris    function header($text, $level, $pos) {
150c66972f2SAdrian Lang        if(!isset($this->meta['title'])) $this->meta['title'] = $text;
151e7856beaSchris
152e7856beaSchris        // add the header to the TOC
153e0c26282SGerrit Uitslag        $hid = $this->_headerToLink($text, true);
154e7856beaSchris        $this->toc_additem($hid, $text, $level);
155e7856beaSchris
15639a89382SEsther Brunner        // add to summary
15717076c2aSAndreas Gohr        $this->cdata(DOKU_LF.$text.DOKU_LF);
15839a89382SEsther Brunner    }
15939a89382SEsther Brunner
16093075b01SAndreas Gohr    /**
16193075b01SAndreas Gohr     * Open a paragraph
16293075b01SAndreas Gohr     */
16339a89382SEsther Brunner    function p_open() {
16417076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
16539a89382SEsther Brunner    }
16639a89382SEsther Brunner
16793075b01SAndreas Gohr    /**
16893075b01SAndreas Gohr     * Close a paragraph
16993075b01SAndreas Gohr     */
17039a89382SEsther Brunner    function p_close() {
17117076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
17239a89382SEsther Brunner    }
17339a89382SEsther Brunner
17493075b01SAndreas Gohr    /**
17593075b01SAndreas Gohr     * Create a line break
17693075b01SAndreas Gohr     */
17739a89382SEsther Brunner    function linebreak() {
17817076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
17939a89382SEsther Brunner    }
18039a89382SEsther Brunner
18193075b01SAndreas Gohr    /**
18293075b01SAndreas Gohr     * Create a horizontal line
18393075b01SAndreas Gohr     */
18439a89382SEsther Brunner    function hr() {
18517076c2aSAndreas Gohr        $this->cdata(DOKU_LF.'----------'.DOKU_LF);
18639a89382SEsther Brunner    }
18739a89382SEsther Brunner
18839a89382SEsther Brunner    /**
18939a89382SEsther Brunner     * Callback for footnote start syntax
19039a89382SEsther Brunner     *
19139a89382SEsther Brunner     * All following content will go to the footnote instead of
19239a89382SEsther Brunner     * the document. To achieve this the previous rendered content
19339a89382SEsther Brunner     * is moved to $store and $doc is cleared
19439a89382SEsther Brunner     *
19539a89382SEsther Brunner     * @author Andreas Gohr <andi@splitbrain.org>
19639a89382SEsther Brunner     */
19739a89382SEsther Brunner    function footnote_open() {
19839a89382SEsther Brunner        if($this->capture) {
19939a89382SEsther Brunner            // move current content to store and record footnote
20039a89382SEsther Brunner            $this->store = $this->doc;
20139a89382SEsther Brunner            $this->doc   = '';
20239a89382SEsther Brunner        }
20339a89382SEsther Brunner    }
20439a89382SEsther Brunner
20539a89382SEsther Brunner    /**
20639a89382SEsther Brunner     * Callback for footnote end syntax
20739a89382SEsther Brunner     *
20839a89382SEsther Brunner     * All rendered content is moved to the $footnotes array and the old
20939a89382SEsther Brunner     * content is restored from $store again
21039a89382SEsther Brunner     *
21139a89382SEsther Brunner     * @author Andreas Gohr
21239a89382SEsther Brunner     */
21339a89382SEsther Brunner    function footnote_close() {
21439a89382SEsther Brunner        if($this->capture) {
21539a89382SEsther Brunner            // restore old content
21639a89382SEsther Brunner            $this->doc   = $this->store;
21739a89382SEsther Brunner            $this->store = '';
21839a89382SEsther Brunner        }
21939a89382SEsther Brunner    }
22039a89382SEsther Brunner
22193075b01SAndreas Gohr    /**
22293075b01SAndreas Gohr     * Open an unordered list
22393075b01SAndreas Gohr     */
22439a89382SEsther Brunner    function listu_open() {
22517076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
22639a89382SEsther Brunner    }
22739a89382SEsther Brunner
22893075b01SAndreas Gohr    /**
22993075b01SAndreas Gohr     * Open an ordered list
23093075b01SAndreas Gohr     */
23139a89382SEsther Brunner    function listo_open() {
23217076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
23339a89382SEsther Brunner    }
23439a89382SEsther Brunner
23593075b01SAndreas Gohr    /**
23693075b01SAndreas Gohr     * Open a list item
23793075b01SAndreas Gohr     *
23893075b01SAndreas Gohr     * @param int $level the nesting level
239e3a24861SChristopher Smith     * @param bool $node true when a node; false when a leaf
24093075b01SAndreas Gohr     */
241e3a24861SChristopher Smith    function listitem_open($level,$node=false) {
24217076c2aSAndreas Gohr        $this->cdata(str_repeat(DOKU_TAB, $level).'* ');
24339a89382SEsther Brunner    }
24439a89382SEsther Brunner
24593075b01SAndreas Gohr    /**
24693075b01SAndreas Gohr     * Close a list item
24793075b01SAndreas Gohr     */
24839a89382SEsther Brunner    function listitem_close() {
24917076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
25039a89382SEsther Brunner    }
25139a89382SEsther Brunner
25293075b01SAndreas Gohr    /**
25393075b01SAndreas Gohr     * Output preformatted text
25493075b01SAndreas Gohr     *
25593075b01SAndreas Gohr     * @param string $text
25693075b01SAndreas Gohr     */
25739a89382SEsther Brunner    function preformatted($text) {
25817076c2aSAndreas Gohr        $this->cdata($text);
25939a89382SEsther Brunner    }
26039a89382SEsther Brunner
26193075b01SAndreas Gohr    /**
26293075b01SAndreas Gohr     * Start a block quote
26393075b01SAndreas Gohr     */
26439a89382SEsther Brunner    function quote_open() {
26517076c2aSAndreas Gohr        $this->cdata(DOKU_LF.DOKU_TAB.'"');
26639a89382SEsther Brunner    }
26739a89382SEsther Brunner
26893075b01SAndreas Gohr    /**
26993075b01SAndreas Gohr     * Stop a block quote
27093075b01SAndreas Gohr     */
27139a89382SEsther Brunner    function quote_close() {
27217076c2aSAndreas Gohr        $this->cdata('"'.DOKU_LF);
27339a89382SEsther Brunner    }
27439a89382SEsther Brunner
27593075b01SAndreas Gohr    /**
27693075b01SAndreas Gohr     * Display text as file content, optionally syntax highlighted
27793075b01SAndreas Gohr     *
27893075b01SAndreas Gohr     * @param string $text text to show
27993075b01SAndreas Gohr     * @param string $lang programming language to use for syntax highlighting
28093075b01SAndreas Gohr     * @param string $file file path label
28193075b01SAndreas Gohr     */
28293075b01SAndreas Gohr    function file($text, $lang = null, $file = null) {
28317076c2aSAndreas Gohr        $this->cdata(DOKU_LF.$text.DOKU_LF);
28493075b01SAndreas Gohr    }
28593075b01SAndreas Gohr
28693075b01SAndreas Gohr    /**
28793075b01SAndreas Gohr     * Display text as code content, optionally syntax highlighted
28893075b01SAndreas Gohr     *
28993075b01SAndreas Gohr     * @param string $text     text to show
29093075b01SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
29193075b01SAndreas Gohr     * @param string $file     file path label
29293075b01SAndreas Gohr     */
2930ea51e63SMatt Perry    function code($text, $language = null, $file = null) {
29417076c2aSAndreas Gohr        $this->cdata(DOKU_LF.$text.DOKU_LF);
29539a89382SEsther Brunner    }
29639a89382SEsther Brunner
29793075b01SAndreas Gohr    /**
29893075b01SAndreas Gohr     * Format an acronym
29993075b01SAndreas Gohr     *
30093075b01SAndreas Gohr     * Uses $this->acronyms
30193075b01SAndreas Gohr     *
30293075b01SAndreas Gohr     * @param string $acronym
30393075b01SAndreas Gohr     */
30439a89382SEsther Brunner    function acronym($acronym) {
30517076c2aSAndreas Gohr        $this->cdata($acronym);
30639a89382SEsther Brunner    }
30739a89382SEsther Brunner
30893075b01SAndreas Gohr    /**
30993075b01SAndreas Gohr     * Format a smiley
31093075b01SAndreas Gohr     *
31193075b01SAndreas Gohr     * Uses $this->smiley
31293075b01SAndreas Gohr     *
31393075b01SAndreas Gohr     * @param string $smiley
31493075b01SAndreas Gohr     */
31539a89382SEsther Brunner    function smiley($smiley) {
31617076c2aSAndreas Gohr        $this->cdata($smiley);
31739a89382SEsther Brunner    }
31839a89382SEsther Brunner
31993075b01SAndreas Gohr    /**
32093075b01SAndreas Gohr     * Format an entity
32193075b01SAndreas Gohr     *
32293075b01SAndreas Gohr     * Entities are basically small text replacements
32393075b01SAndreas Gohr     *
32493075b01SAndreas Gohr     * Uses $this->entities
32593075b01SAndreas Gohr     *
32693075b01SAndreas Gohr     * @param string $entity
32793075b01SAndreas Gohr     */
32839a89382SEsther Brunner    function entity($entity) {
32917076c2aSAndreas Gohr        $this->cdata($entity);
33039a89382SEsther Brunner    }
33139a89382SEsther Brunner
33293075b01SAndreas Gohr    /**
33393075b01SAndreas Gohr     * Typographically format a multiply sign
33493075b01SAndreas Gohr     *
33593075b01SAndreas Gohr     * Example: ($x=640, $y=480) should result in "640×480"
33693075b01SAndreas Gohr     *
33793075b01SAndreas Gohr     * @param string|int $x first value
33893075b01SAndreas Gohr     * @param string|int $y second value
33993075b01SAndreas Gohr     */
34039a89382SEsther Brunner    function multiplyentity($x, $y) {
34117076c2aSAndreas Gohr        $this->cdata($x.'×'.$y);
34239a89382SEsther Brunner    }
34339a89382SEsther Brunner
34493075b01SAndreas Gohr    /**
34593075b01SAndreas Gohr     * Render an opening single quote char (language specific)
34693075b01SAndreas Gohr     */
34739a89382SEsther Brunner    function singlequoteopening() {
34871b40da2SAnika Henke        global $lang;
34917076c2aSAndreas Gohr        $this->cdata($lang['singlequoteopening']);
35039a89382SEsther Brunner    }
35139a89382SEsther Brunner
35293075b01SAndreas Gohr    /**
35393075b01SAndreas Gohr     * Render a closing single quote char (language specific)
35493075b01SAndreas Gohr     */
35539a89382SEsther Brunner    function singlequoteclosing() {
35671b40da2SAnika Henke        global $lang;
35717076c2aSAndreas Gohr        $this->cdata($lang['singlequoteclosing']);
35839a89382SEsther Brunner    }
35939a89382SEsther Brunner
36093075b01SAndreas Gohr    /**
36193075b01SAndreas Gohr     * Render an apostrophe char (language specific)
36293075b01SAndreas Gohr     */
36357d757d1SAndreas Gohr    function apostrophe() {
36457d757d1SAndreas Gohr        global $lang;
36517076c2aSAndreas Gohr        $this->cdata($lang['apostrophe']);
36657d757d1SAndreas Gohr    }
36757d757d1SAndreas Gohr
36893075b01SAndreas Gohr    /**
36993075b01SAndreas Gohr     * Render an opening double quote char (language specific)
37093075b01SAndreas Gohr     */
37139a89382SEsther Brunner    function doublequoteopening() {
37271b40da2SAnika Henke        global $lang;
37317076c2aSAndreas Gohr        $this->cdata($lang['doublequoteopening']);
37439a89382SEsther Brunner    }
37539a89382SEsther Brunner
37693075b01SAndreas Gohr    /**
37793075b01SAndreas Gohr     * Render an closinging double quote char (language specific)
37893075b01SAndreas Gohr     */
37939a89382SEsther Brunner    function doublequoteclosing() {
38071b40da2SAnika Henke        global $lang;
38117076c2aSAndreas Gohr        $this->cdata($lang['doublequoteclosing']);
38239a89382SEsther Brunner    }
38339a89382SEsther Brunner
38493075b01SAndreas Gohr    /**
38593075b01SAndreas Gohr     * Render a CamelCase link
38693075b01SAndreas Gohr     *
38793075b01SAndreas Gohr     * @param string $link The link name
38893075b01SAndreas Gohr     * @see http://en.wikipedia.org/wiki/CamelCase
38993075b01SAndreas Gohr     */
39039a89382SEsther Brunner    function camelcaselink($link) {
39139a89382SEsther Brunner        $this->internallink($link, $link);
39239a89382SEsther Brunner    }
39339a89382SEsther Brunner
39493075b01SAndreas Gohr    /**
39593075b01SAndreas Gohr     * Render a page local link
39693075b01SAndreas Gohr     *
39793075b01SAndreas Gohr     * @param string $hash hash link identifier
39893075b01SAndreas Gohr     * @param string $name name for the link
39993075b01SAndreas Gohr     */
4009269d0b1SMichael Hamann    function locallink($hash, $name = null) {
4019269d0b1SMichael Hamann        if(is_array($name)) {
4029269d0b1SMichael Hamann            $this->_firstimage($name['src']);
4039269d0b1SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
4049269d0b1SMichael Hamann        }
4059269d0b1SMichael Hamann    }
40639a89382SEsther Brunner
40739a89382SEsther Brunner    /**
40839a89382SEsther Brunner     * keep track of internal links in $this->meta['relation']['references']
40993075b01SAndreas Gohr     *
41093075b01SAndreas Gohr     * @param string            $id   page ID to link to. eg. 'wiki:syntax'
41159bc3b48SGerrit Uitslag     * @param string|array|null $name name for the link, array for media file
41239a89382SEsther Brunner     */
4130ea51e63SMatt Perry    function internallink($id, $name = null) {
41439a89382SEsther Brunner        global $ID;
41539a89382SEsther Brunner
416ffec1009SMichael Hamann        if(is_array($name)) {
417aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
418ffec1009SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
419ffec1009SMichael Hamann        }
420aa5a2937SAndreas Gohr
42166d935e7SMichael Hamann        $parts = explode('?', $id, 2);
42266d935e7SMichael Hamann        if(count($parts) === 2) {
42366d935e7SMichael Hamann            $id = $parts[0];
42466d935e7SMichael Hamann        }
42566d935e7SMichael Hamann
42639a89382SEsther Brunner        $default = $this->_simpleTitle($id);
42739a89382SEsther Brunner
42839a89382SEsther Brunner        // first resolve and clean up the $id
42939a89382SEsther Brunner        resolve_pageid(getNS($ID), $id, $exists);
43093075b01SAndreas Gohr        @list($page) = explode('#', $id, 2);
43139a89382SEsther Brunner
43239a89382SEsther Brunner        // set metadata
4333be6e394Schris        $this->meta['relation']['references'][$page] = $exists;
43439a89382SEsther Brunner        // $data = array('relation' => array('isreferencedby' => array($ID => true)));
43539a89382SEsther Brunner        // p_set_metadata($id, $data);
43639a89382SEsther Brunner
43739a89382SEsther Brunner        // add link title to summary
43839a89382SEsther Brunner        if($this->capture) {
43939a89382SEsther Brunner            $name = $this->_getLinkTitle($name, $default, $id);
44039a89382SEsther Brunner            $this->doc .= $name;
44139a89382SEsther Brunner        }
44239a89382SEsther Brunner    }
44339a89382SEsther Brunner
44493075b01SAndreas Gohr    /**
44593075b01SAndreas Gohr     * Render an external link
44693075b01SAndreas Gohr     *
44793075b01SAndreas Gohr     * @param string            $url  full URL with scheme
44859bc3b48SGerrit Uitslag     * @param string|array|null $name name for the link, array for media file
44993075b01SAndreas Gohr     */
4500ea51e63SMatt Perry    function externallink($url, $name = null) {
451ffec1009SMichael Hamann        if(is_array($name)) {
452aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
453ffec1009SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
454ffec1009SMichael Hamann        }
455aa5a2937SAndreas Gohr
45639a89382SEsther Brunner        if($this->capture) {
457a2ea2dc1SAdrian Lang            $this->doc .= $this->_getLinkTitle($name, '<'.$url.'>');
45839a89382SEsther Brunner        }
45939a89382SEsther Brunner    }
46039a89382SEsther Brunner
46193075b01SAndreas Gohr    /**
46293075b01SAndreas Gohr     * Render an interwiki link
46393075b01SAndreas Gohr     *
46493075b01SAndreas Gohr     * You may want to use $this->_resolveInterWiki() here
46593075b01SAndreas Gohr     *
46693075b01SAndreas Gohr     * @param string       $match     original link - probably not much use
46793075b01SAndreas Gohr     * @param string|array $name      name for the link, array for media file
46893075b01SAndreas Gohr     * @param string       $wikiName  indentifier (shortcut) for the remote wiki
46993075b01SAndreas Gohr     * @param string       $wikiUri   the fragment parsed from the original link
47093075b01SAndreas Gohr     */
4710ea51e63SMatt Perry    function interwikilink($match, $name = null, $wikiName, $wikiUri) {
472ffec1009SMichael Hamann        if(is_array($name)) {
473aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
474ffec1009SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
475ffec1009SMichael Hamann        }
476aa5a2937SAndreas Gohr
47739a89382SEsther Brunner        if($this->capture) {
47893075b01SAndreas Gohr            list($wikiUri) = explode('#', $wikiUri, 2);
479a2ea2dc1SAdrian Lang            $name = $this->_getLinkTitle($name, $wikiUri);
48039a89382SEsther Brunner            $this->doc .= $name;
48139a89382SEsther Brunner        }
48239a89382SEsther Brunner    }
48339a89382SEsther Brunner
48493075b01SAndreas Gohr    /**
48593075b01SAndreas Gohr     * Link to windows share
48693075b01SAndreas Gohr     *
48793075b01SAndreas Gohr     * @param string       $url  the link
48893075b01SAndreas Gohr     * @param string|array $name name for the link, array for media file
48993075b01SAndreas Gohr     */
4900ea51e63SMatt Perry    function windowssharelink($url, $name = null) {
491ffec1009SMichael Hamann        if(is_array($name)) {
492aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
493ffec1009SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
494ffec1009SMichael Hamann        }
495aa5a2937SAndreas Gohr
49639a89382SEsther Brunner        if($this->capture) {
49739a89382SEsther Brunner            if($name) $this->doc .= $name;
49839a89382SEsther Brunner            else $this->doc .= '<'.$url.'>';
49939a89382SEsther Brunner        }
50039a89382SEsther Brunner    }
50139a89382SEsther Brunner
50293075b01SAndreas Gohr    /**
50393075b01SAndreas Gohr     * Render a linked E-Mail Address
50493075b01SAndreas Gohr     *
50593075b01SAndreas Gohr     * Should honor $conf['mailguard'] setting
50693075b01SAndreas Gohr     *
50793075b01SAndreas Gohr     * @param string       $address Email-Address
50893075b01SAndreas Gohr     * @param string|array $name    name for the link, array for media file
50993075b01SAndreas Gohr     */
5100ea51e63SMatt Perry    function emaillink($address, $name = null) {
511ffec1009SMichael Hamann        if(is_array($name)) {
512aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
513ffec1009SMichael Hamann            if($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']);
514ffec1009SMichael Hamann        }
515aa5a2937SAndreas Gohr
51639a89382SEsther Brunner        if($this->capture) {
51739a89382SEsther Brunner            if($name) $this->doc .= $name;
51839a89382SEsther Brunner            else $this->doc .= '<'.$address.'>';
51939a89382SEsther Brunner        }
52039a89382SEsther Brunner    }
52139a89382SEsther Brunner
52293075b01SAndreas Gohr    /**
52393075b01SAndreas Gohr     * Render an internal media file
52493075b01SAndreas Gohr     *
52593075b01SAndreas Gohr     * @param string $src     media ID
52693075b01SAndreas Gohr     * @param string $title   descriptive text
52793075b01SAndreas Gohr     * @param string $align   left|center|right
52893075b01SAndreas Gohr     * @param int    $width   width of media in pixel
52993075b01SAndreas Gohr     * @param int    $height  height of media in pixel
53093075b01SAndreas Gohr     * @param string $cache   cache|recache|nocache
53193075b01SAndreas Gohr     * @param string $linking linkonly|detail|nolink
53293075b01SAndreas Gohr     */
5330ea51e63SMatt Perry    function internalmedia($src, $title = null, $align = null, $width = null,
5340ea51e63SMatt Perry                           $height = null, $cache = null, $linking = null) {
53539a89382SEsther Brunner        if($this->capture && $title) $this->doc .= '['.$title.']';
536aa5a2937SAndreas Gohr        $this->_firstimage($src);
537ffec1009SMichael Hamann        $this->_recordMediaUsage($src);
53839a89382SEsther Brunner    }
53939a89382SEsther Brunner
54093075b01SAndreas Gohr    /**
54193075b01SAndreas Gohr     * Render an external media file
54293075b01SAndreas Gohr     *
54393075b01SAndreas Gohr     * @param string $src     full media URL
54493075b01SAndreas Gohr     * @param string $title   descriptive text
54593075b01SAndreas Gohr     * @param string $align   left|center|right
54693075b01SAndreas Gohr     * @param int    $width   width of media in pixel
54793075b01SAndreas Gohr     * @param int    $height  height of media in pixel
54893075b01SAndreas Gohr     * @param string $cache   cache|recache|nocache
54993075b01SAndreas Gohr     * @param string $linking linkonly|detail|nolink
55093075b01SAndreas Gohr     */
5510ea51e63SMatt Perry    function externalmedia($src, $title = null, $align = null, $width = null,
5520ea51e63SMatt Perry                           $height = null, $cache = null, $linking = null) {
55339a89382SEsther Brunner        if($this->capture && $title) $this->doc .= '['.$title.']';
554aa5a2937SAndreas Gohr        $this->_firstimage($src);
55539a89382SEsther Brunner    }
55639a89382SEsther Brunner
55793075b01SAndreas Gohr    /**
55893075b01SAndreas Gohr     * Render the output of an RSS feed
55993075b01SAndreas Gohr     *
56093075b01SAndreas Gohr     * @param string $url    URL of the feed
56193075b01SAndreas Gohr     * @param array  $params Finetuning of the output
56293075b01SAndreas Gohr     */
563ce6b63d9Schris    function rss($url, $params) {
564ce6b63d9Schris        $this->meta['relation']['haspart'][$url] = true;
56564e9144aSchris
56664e9144aSchris        $this->meta['date']['valid']['age'] =
56764e9144aSchris            isset($this->meta['date']['valid']['age']) ?
56864e9144aSchris                min($this->meta['date']['valid']['age'], $params['refresh']) :
56964e9144aSchris                $params['refresh'];
570ce6b63d9Schris    }
57139a89382SEsther Brunner
57293075b01SAndreas Gohr    #region Utils
57339a89382SEsther Brunner
57439a89382SEsther Brunner    /**
57539a89382SEsther Brunner     * Removes any Namespace from the given name but keeps
57639a89382SEsther Brunner     * casing and special chars
57739a89382SEsther Brunner     *
57839a89382SEsther Brunner     * @author Andreas Gohr <andi@splitbrain.org>
579f50a239bSTakamura     *
580f50a239bSTakamura     * @param string $name
581f50a239bSTakamura     *
582f50a239bSTakamura     * @return mixed|string
58339a89382SEsther Brunner     */
58439a89382SEsther Brunner    function _simpleTitle($name) {
58539a89382SEsther Brunner        global $conf;
58639a89382SEsther Brunner
5879bee852eSAndreas Gohr        if(is_array($name)) return '';
5889bee852eSAndreas Gohr
58939a89382SEsther Brunner        if($conf['useslash']) {
59039a89382SEsther Brunner            $nssep = '[:;/]';
59139a89382SEsther Brunner        } else {
59239a89382SEsther Brunner            $nssep = '[:;]';
59339a89382SEsther Brunner        }
59439a89382SEsther Brunner        $name = preg_replace('!.*'.$nssep.'!', '', $name);
5953be6e394Schris        //if there is a hash we use the anchor name only
59639a89382SEsther Brunner        $name = preg_replace('!.*#!', '', $name);
59739a89382SEsther Brunner        return $name;
59839a89382SEsther Brunner    }
59939a89382SEsther Brunner
60039a89382SEsther Brunner    /**
60139a89382SEsther Brunner     * Creates a linkid from a headline
60239a89382SEsther Brunner     *
60393075b01SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
60439a89382SEsther Brunner     * @param string  $title   The headline title
60539a89382SEsther Brunner     * @param boolean $create  Create a new unique ID?
60693075b01SAndreas Gohr     * @return string
60739a89382SEsther Brunner     */
60839a89382SEsther Brunner    function _headerToLink($title, $create = false) {
60939a89382SEsther Brunner        if($create) {
610620f4930SAndreas Gohr            return sectionID($title, $this->headers);
611620f4930SAndreas Gohr        } else {
612620f4930SAndreas Gohr            $check = false;
613620f4930SAndreas Gohr            return sectionID($title, $check);
61439a89382SEsther Brunner        }
61539a89382SEsther Brunner    }
61639a89382SEsther Brunner
61739a89382SEsther Brunner    /**
61839a89382SEsther Brunner     * Construct a title and handle images in titles
61939a89382SEsther Brunner     *
62039a89382SEsther Brunner     * @author Harry Fuecks <hfuecks@gmail.com>
62159bc3b48SGerrit Uitslag     * @param string|array|null $title    either string title or media array
62293075b01SAndreas Gohr     * @param string            $default  default title if nothing else is found
62393075b01SAndreas Gohr     * @param null|string       $id       linked page id (used to extract title from first heading)
62493075b01SAndreas Gohr     * @return string title text
62539a89382SEsther Brunner     */
6260ea51e63SMatt Perry    function _getLinkTitle($title, $default, $id = null) {
62785038eabSDanny        if(is_array($title)) {
62893075b01SAndreas Gohr            if($title['title']) {
62993075b01SAndreas Gohr                return '['.$title['title'].']';
63093075b01SAndreas Gohr            } else {
63193075b01SAndreas Gohr                return $default;
63293075b01SAndreas Gohr            }
63385038eabSDanny        } else if(is_null($title) || trim($title) == '') {
634fe9ec250SChris Smith            if(useHeading('content') && $id) {
63520f04039SDanny                $heading = p_get_first_heading($id, METADATA_DONT_RENDER);
63639a89382SEsther Brunner                if($heading) return $heading;
63739a89382SEsther Brunner            }
63839a89382SEsther Brunner            return $default;
63985038eabSDanny        } else {
64039a89382SEsther Brunner            return $title;
64139a89382SEsther Brunner        }
64239a89382SEsther Brunner    }
64339a89382SEsther Brunner
64493075b01SAndreas Gohr    /**
64593075b01SAndreas Gohr     * Remember first image
64693075b01SAndreas Gohr     *
64793075b01SAndreas Gohr     * @param string $src image URL or ID
64893075b01SAndreas Gohr     */
649aa5a2937SAndreas Gohr    function _firstimage($src) {
650aa5a2937SAndreas Gohr        if($this->firstimage) return;
651aa5a2937SAndreas Gohr        global $ID;
652aa5a2937SAndreas Gohr
65393075b01SAndreas Gohr        list($src) = explode('#', $src, 2);
6543e7e6067SKlap-in        if(!media_isexternal($src)) {
655aa5a2937SAndreas Gohr            resolve_mediaid(getNS($ID), $src, $exists);
656aa5a2937SAndreas Gohr        }
657aa5a2937SAndreas Gohr        if(preg_match('/.(jpe?g|gif|png)$/i', $src)) {
658aa5a2937SAndreas Gohr            $this->firstimage = $src;
659aa5a2937SAndreas Gohr        }
660aa5a2937SAndreas Gohr    }
661ffec1009SMichael Hamann
66293075b01SAndreas Gohr    /**
66393075b01SAndreas Gohr     * Store list of used media files in metadata
66493075b01SAndreas Gohr     *
66593075b01SAndreas Gohr     * @param string $src media ID
66693075b01SAndreas Gohr     */
667ffec1009SMichael Hamann    function _recordMediaUsage($src) {
668ffec1009SMichael Hamann        global $ID;
669ffec1009SMichael Hamann
67093075b01SAndreas Gohr        list ($src) = explode('#', $src, 2);
671ffec1009SMichael Hamann        if(media_isexternal($src)) return;
672ffec1009SMichael Hamann        resolve_mediaid(getNS($ID), $src, $exists);
673ffec1009SMichael Hamann        $this->meta['relation']['media'][$src] = $exists;
674ffec1009SMichael Hamann    }
67593075b01SAndreas Gohr
67693075b01SAndreas Gohr    #endregion
67739a89382SEsther Brunner}
67839a89382SEsther Brunner
679e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
680