xref: /dokuwiki/inc/parser/metadata.php (revision 3aa984aebffea8996fc8cf49fef36ab9e294e330)
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.
10b4f2363aSAndreas Gohr *
11b4f2363aSAndreas Gohr * @author Esther Brunner <wikidesign@gmail.com>
1239a89382SEsther Brunner */
13*3aa984aeSLarsGit223class Doku_Renderer_metadata extends Doku_Renderer
14*3aa984aeSLarsGit223{
1517076c2aSAndreas Gohr    /** the approximate byte lenght to capture for the abstract */
1617076c2aSAndreas Gohr    const ABSTRACT_LEN = 250;
1717076c2aSAndreas Gohr
1817076c2aSAndreas Gohr    /** the maximum UTF8 character length for the abstract */
1917076c2aSAndreas Gohr    const ABSTRACT_MAX = 500;
2039a89382SEsther Brunner
2193075b01SAndreas Gohr    /** @var array transient meta data, will be reset on each rendering */
2293075b01SAndreas Gohr    public $meta = array();
2339a89382SEsther Brunner
2493075b01SAndreas Gohr    /** @var array persistent meta data, will be kept until explicitly deleted */
2593075b01SAndreas Gohr    public $persistent = array();
2639a89382SEsther Brunner
2793075b01SAndreas Gohr    /** @var array the list of headers used to create unique link ids */
2893075b01SAndreas Gohr    protected $headers = array();
2993075b01SAndreas Gohr
3093075b01SAndreas Gohr    /** @var string temporary $doc store */
3193075b01SAndreas Gohr    protected $store = '';
3293075b01SAndreas Gohr
3393075b01SAndreas Gohr    /** @var string keeps the first image reference */
3493075b01SAndreas Gohr    protected $firstimage = '';
3593075b01SAndreas Gohr
36b8f47726SChristopher Smith    /** @var bool whether or not data is being captured for the abstract, public to be accessible by plugins */
37b8f47726SChristopher Smith    public $capturing = true;
38b8f47726SChristopher Smith
3917076c2aSAndreas Gohr    /** @var bool determines if enough data for the abstract was collected, yet */
408f2758f6SAndreas Gohr    public $capture = true;
4117076c2aSAndreas Gohr
4217076c2aSAndreas Gohr    /** @var int number of bytes captured for abstract */
4317076c2aSAndreas Gohr    protected $captured = 0;
4417076c2aSAndreas Gohr
4593075b01SAndreas Gohr    /**
4693075b01SAndreas Gohr     * Returns the format produced by this renderer.
4793075b01SAndreas Gohr     *
4893075b01SAndreas Gohr     * @return string always 'metadata'
4993075b01SAndreas Gohr     */
50*3aa984aeSLarsGit223    public function getFormat()
51*3aa984aeSLarsGit223    {
525f70445dSAndreas Gohr        return 'metadata';
535f70445dSAndreas Gohr    }
545f70445dSAndreas Gohr
5593075b01SAndreas Gohr    /**
5693075b01SAndreas Gohr     * Initialize the document
5793075b01SAndreas Gohr     *
5893075b01SAndreas Gohr     * Sets up some of the persistent info about the page if it doesn't exist, yet.
5993075b01SAndreas Gohr     */
60*3aa984aeSLarsGit223    public function document_start()
61*3aa984aeSLarsGit223    {
62ef47e298SAndreas Gohr        global $ID;
63620f4930SAndreas Gohr
64620f4930SAndreas Gohr        $this->headers = array();
65620f4930SAndreas Gohr
66ef47e298SAndreas Gohr        // external pages are missing create date
67ef47e298SAndreas Gohr        if (!$this->persistent['date']['created']) {
68ef47e298SAndreas Gohr            $this->persistent['date']['created'] = filectime(wikiFN($ID));
69ef47e298SAndreas Gohr        }
70a8e72133SAndreas Gohr        if (!isset($this->persistent['user'])) {
71a8e72133SAndreas Gohr            $this->persistent['user'] = '';
72a8e72133SAndreas Gohr        }
73ef47e298SAndreas Gohr        if (!isset($this->persistent['creator'])) {
74ef47e298SAndreas Gohr            $this->persistent['creator'] = '';
75ef47e298SAndreas Gohr        }
760a7e3bceSchris        // reset metadata to persistent values
770a7e3bceSchris        $this->meta = $this->persistent;
7839a89382SEsther Brunner    }
7939a89382SEsther Brunner
8093075b01SAndreas Gohr    /**
8193075b01SAndreas Gohr     * Finalize the document
8293075b01SAndreas Gohr     *
8393075b01SAndreas Gohr     * Stores collected data in the metadata
8493075b01SAndreas Gohr     */
85*3aa984aeSLarsGit223    public function document_end()
86*3aa984aeSLarsGit223    {
87482cff99SAndreas Gohr        global $ID;
88482cff99SAndreas Gohr
89b8595a66SAndreas Gohr        // store internal info in metadata (notoc,nocache)
90b8595a66SAndreas Gohr        $this->meta['internal'] = $this->info;
91b8595a66SAndreas Gohr
92c66972f2SAdrian Lang        if (!isset($this->meta['description']['abstract'])) {
9339a89382SEsther Brunner            // cut off too long abstracts
9439a89382SEsther Brunner            $this->doc = trim($this->doc);
9517076c2aSAndreas Gohr            if (strlen($this->doc) > self::ABSTRACT_MAX) {
9617076c2aSAndreas Gohr                $this->doc = utf8_substr($this->doc, 0, self::ABSTRACT_MAX).'…';
9717076c2aSAndreas Gohr            }
9839a89382SEsther Brunner            $this->meta['description']['abstract'] = $this->doc;
9939a89382SEsther Brunner        }
100aa5a2937SAndreas Gohr
101aa5a2937SAndreas Gohr        $this->meta['relation']['firstimage'] = $this->firstimage;
102482cff99SAndreas Gohr
103c0322273SAdrian Lang        if (!isset($this->meta['date']['modified'])) {
104482cff99SAndreas Gohr            $this->meta['date']['modified'] = filemtime(wikiFN($ID));
105482cff99SAndreas Gohr        }
10639a89382SEsther Brunner    }
10739a89382SEsther Brunner
10893075b01SAndreas Gohr    /**
10917076c2aSAndreas Gohr     * Render plain text data
11017076c2aSAndreas Gohr     *
11117076c2aSAndreas Gohr     * This function takes care of the amount captured data and will stop capturing when
11217076c2aSAndreas Gohr     * enough abstract data is available
11317076c2aSAndreas Gohr     *
11417076c2aSAndreas Gohr     * @param $text
11517076c2aSAndreas Gohr     */
116*3aa984aeSLarsGit223    public function cdata($text)
117*3aa984aeSLarsGit223    {
118*3aa984aeSLarsGit223        if (!$this->capture || !$this->capturing) {
119*3aa984aeSLarsGit223            return;
120*3aa984aeSLarsGit223        }
12117076c2aSAndreas Gohr
12217076c2aSAndreas Gohr        $this->doc .= $text;
12317076c2aSAndreas Gohr
12417076c2aSAndreas Gohr        $this->captured += strlen($text);
125*3aa984aeSLarsGit223        if ($this->captured > self::ABSTRACT_LEN) {
126*3aa984aeSLarsGit223            $this->capture = false;
127*3aa984aeSLarsGit223        }
12817076c2aSAndreas Gohr    }
12917076c2aSAndreas Gohr
13017076c2aSAndreas Gohr    /**
13193075b01SAndreas Gohr     * Add an item to the TOC
13293075b01SAndreas Gohr     *
13393075b01SAndreas Gohr     * @param string $id       the hash link
13493075b01SAndreas Gohr     * @param string $text     the text to display
13593075b01SAndreas Gohr     * @param int    $level    the nesting level
13693075b01SAndreas Gohr     */
137*3aa984aeSLarsGit223    public function toc_additem($id, $text, $level)
138*3aa984aeSLarsGit223    {
13939a89382SEsther Brunner        global $conf;
14039a89382SEsther Brunner
141e7856beaSchris        //only add items within configured levels
14239a89382SEsther Brunner        if ($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']) {
14339a89382SEsther Brunner            // the TOC is one of our standard ul list arrays ;-)
14439a89382SEsther Brunner            $this->meta['description']['tableofcontents'][] = array(
145e7856beaSchris                'hid'   => $id,
14639a89382SEsther Brunner                'title' => $text,
14739a89382SEsther Brunner                'type'  => 'ul',
14839a89382SEsther Brunner                'level' => $level - $conf['toptoclevel'] + 1
14939a89382SEsther Brunner            );
15039a89382SEsther Brunner        }
151e7856beaSchris    }
152e7856beaSchris
15393075b01SAndreas Gohr    /**
15493075b01SAndreas Gohr     * Render a heading
15593075b01SAndreas Gohr     *
15693075b01SAndreas Gohr     * @param string $text  the text to display
15793075b01SAndreas Gohr     * @param int    $level header level
15893075b01SAndreas Gohr     * @param int    $pos   byte position in the original source
15993075b01SAndreas Gohr     */
160*3aa984aeSLarsGit223    public function header($text, $level, $pos)
161*3aa984aeSLarsGit223    {
162*3aa984aeSLarsGit223        if (!isset($this->meta['title'])) {
163*3aa984aeSLarsGit223            $this->meta['title'] = $text;
164*3aa984aeSLarsGit223        }
165e7856beaSchris
166e7856beaSchris        // add the header to the TOC
167e0c26282SGerrit Uitslag        $hid = $this->_headerToLink($text, true);
168e7856beaSchris        $this->toc_additem($hid, $text, $level);
169e7856beaSchris
17039a89382SEsther Brunner        // add to summary
17117076c2aSAndreas Gohr        $this->cdata(DOKU_LF.$text.DOKU_LF);
17239a89382SEsther Brunner    }
17339a89382SEsther Brunner
17493075b01SAndreas Gohr    /**
17593075b01SAndreas Gohr     * Open a paragraph
17693075b01SAndreas Gohr     */
177*3aa984aeSLarsGit223    public function p_open()
178*3aa984aeSLarsGit223    {
17917076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
18039a89382SEsther Brunner    }
18139a89382SEsther Brunner
18293075b01SAndreas Gohr    /**
18393075b01SAndreas Gohr     * Close a paragraph
18493075b01SAndreas Gohr     */
185*3aa984aeSLarsGit223    public function p_close()
186*3aa984aeSLarsGit223    {
18717076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
18839a89382SEsther Brunner    }
18939a89382SEsther Brunner
19093075b01SAndreas Gohr    /**
19193075b01SAndreas Gohr     * Create a line break
19293075b01SAndreas Gohr     */
193*3aa984aeSLarsGit223    public function linebreak()
194*3aa984aeSLarsGit223    {
19517076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
19639a89382SEsther Brunner    }
19739a89382SEsther Brunner
19893075b01SAndreas Gohr    /**
19993075b01SAndreas Gohr     * Create a horizontal line
20093075b01SAndreas Gohr     */
201*3aa984aeSLarsGit223    public function hr()
202*3aa984aeSLarsGit223    {
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     */
215*3aa984aeSLarsGit223    public function footnote_open()
216*3aa984aeSLarsGit223    {
21739a89382SEsther Brunner        if ($this->capture) {
218b8f47726SChristopher Smith            // move current content to store
219b8f47726SChristopher Smith            // this is required to ensure safe behaviour of plugins accessed within footnotes
22039a89382SEsther Brunner            $this->store = $this->doc;
22139a89382SEsther Brunner            $this->doc   = '';
222b8f47726SChristopher Smith
223b8f47726SChristopher Smith            // disable capturing
224b8f47726SChristopher Smith            $this->capturing = false;
22539a89382SEsther Brunner        }
22639a89382SEsther Brunner    }
22739a89382SEsther Brunner
22839a89382SEsther Brunner    /**
22939a89382SEsther Brunner     * Callback for footnote end syntax
23039a89382SEsther Brunner     *
231b8f47726SChristopher Smith     * All content rendered whilst within footnote syntax mode is discarded,
232b8f47726SChristopher Smith     * the previously rendered content is restored and capturing is re-enabled.
23339a89382SEsther Brunner     *
23439a89382SEsther Brunner     * @author Andreas Gohr
23539a89382SEsther Brunner     */
236*3aa984aeSLarsGit223    public function footnote_close()
237*3aa984aeSLarsGit223    {
23839a89382SEsther Brunner        if ($this->capture) {
239b8f47726SChristopher Smith            // re-enable capturing
240b8f47726SChristopher Smith            $this->capturing = true;
241b8f47726SChristopher Smith            // restore previously rendered content
24239a89382SEsther Brunner            $this->doc   = $this->store;
24339a89382SEsther Brunner            $this->store = '';
24439a89382SEsther Brunner        }
24539a89382SEsther Brunner    }
24639a89382SEsther Brunner
24793075b01SAndreas Gohr    /**
24893075b01SAndreas Gohr     * Open an unordered list
24993075b01SAndreas Gohr     */
250*3aa984aeSLarsGit223    public function listu_open()
251*3aa984aeSLarsGit223    {
25217076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
25339a89382SEsther Brunner    }
25439a89382SEsther Brunner
25593075b01SAndreas Gohr    /**
25693075b01SAndreas Gohr     * Open an ordered list
25793075b01SAndreas Gohr     */
258*3aa984aeSLarsGit223    public function listo_open()
259*3aa984aeSLarsGit223    {
26017076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
26139a89382SEsther Brunner    }
26239a89382SEsther Brunner
26393075b01SAndreas Gohr    /**
26493075b01SAndreas Gohr     * Open a list item
26593075b01SAndreas Gohr     *
26693075b01SAndreas Gohr     * @param int $level the nesting level
267e3a24861SChristopher Smith     * @param bool $node true when a node; false when a leaf
26893075b01SAndreas Gohr     */
269*3aa984aeSLarsGit223    public function listitem_open($level, $node=false)
270*3aa984aeSLarsGit223    {
27117076c2aSAndreas Gohr        $this->cdata(str_repeat(DOKU_TAB, $level).'* ');
27239a89382SEsther Brunner    }
27339a89382SEsther Brunner
27493075b01SAndreas Gohr    /**
27593075b01SAndreas Gohr     * Close a list item
27693075b01SAndreas Gohr     */
277*3aa984aeSLarsGit223    public function listitem_close()
278*3aa984aeSLarsGit223    {
27917076c2aSAndreas Gohr        $this->cdata(DOKU_LF);
28039a89382SEsther Brunner    }
28139a89382SEsther Brunner
28293075b01SAndreas Gohr    /**
28393075b01SAndreas Gohr     * Output preformatted text
28493075b01SAndreas Gohr     *
28593075b01SAndreas Gohr     * @param string $text
28693075b01SAndreas Gohr     */
287*3aa984aeSLarsGit223    public function preformatted($text)
288*3aa984aeSLarsGit223    {
28917076c2aSAndreas Gohr        $this->cdata($text);
29039a89382SEsther Brunner    }
29139a89382SEsther Brunner
29293075b01SAndreas Gohr    /**
29393075b01SAndreas Gohr     * Start a block quote
29493075b01SAndreas Gohr     */
295*3aa984aeSLarsGit223    public function quote_open()
296*3aa984aeSLarsGit223    {
29717076c2aSAndreas Gohr        $this->cdata(DOKU_LF.DOKU_TAB.'"');
29839a89382SEsther Brunner    }
29939a89382SEsther Brunner
30093075b01SAndreas Gohr    /**
30193075b01SAndreas Gohr     * Stop a block quote
30293075b01SAndreas Gohr     */
303*3aa984aeSLarsGit223    public function quote_close()
304*3aa984aeSLarsGit223    {
30517076c2aSAndreas Gohr        $this->cdata('"'.DOKU_LF);
30639a89382SEsther Brunner    }
30739a89382SEsther Brunner
30893075b01SAndreas Gohr    /**
30993075b01SAndreas Gohr     * Display text as file content, optionally syntax highlighted
31093075b01SAndreas Gohr     *
31193075b01SAndreas Gohr     * @param string $text text to show
31293075b01SAndreas Gohr     * @param string $lang programming language to use for syntax highlighting
31393075b01SAndreas Gohr     * @param string $file file path label
31493075b01SAndreas Gohr     */
315*3aa984aeSLarsGit223    public function file($text, $lang = null, $file = null)
316*3aa984aeSLarsGit223    {
31717076c2aSAndreas Gohr        $this->cdata(DOKU_LF.$text.DOKU_LF);
31893075b01SAndreas Gohr    }
31993075b01SAndreas Gohr
32093075b01SAndreas Gohr    /**
32193075b01SAndreas Gohr     * Display text as code content, optionally syntax highlighted
32293075b01SAndreas Gohr     *
32393075b01SAndreas Gohr     * @param string $text     text to show
32493075b01SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
32593075b01SAndreas Gohr     * @param string $file     file path label
32693075b01SAndreas Gohr     */
327*3aa984aeSLarsGit223    public function code($text, $language = null, $file = null)
328*3aa984aeSLarsGit223    {
32917076c2aSAndreas Gohr        $this->cdata(DOKU_LF.$text.DOKU_LF);
33039a89382SEsther Brunner    }
33139a89382SEsther Brunner
33293075b01SAndreas Gohr    /**
33393075b01SAndreas Gohr     * Format an acronym
33493075b01SAndreas Gohr     *
33593075b01SAndreas Gohr     * Uses $this->acronyms
33693075b01SAndreas Gohr     *
33793075b01SAndreas Gohr     * @param string $acronym
33893075b01SAndreas Gohr     */
339*3aa984aeSLarsGit223    public function acronym($acronym)
340*3aa984aeSLarsGit223    {
34117076c2aSAndreas Gohr        $this->cdata($acronym);
34239a89382SEsther Brunner    }
34339a89382SEsther Brunner
34493075b01SAndreas Gohr    /**
34593075b01SAndreas Gohr     * Format a smiley
34693075b01SAndreas Gohr     *
34793075b01SAndreas Gohr     * Uses $this->smiley
34893075b01SAndreas Gohr     *
34993075b01SAndreas Gohr     * @param string $smiley
35093075b01SAndreas Gohr     */
351*3aa984aeSLarsGit223    public function smiley($smiley)
352*3aa984aeSLarsGit223    {
35317076c2aSAndreas Gohr        $this->cdata($smiley);
35439a89382SEsther Brunner    }
35539a89382SEsther Brunner
35693075b01SAndreas Gohr    /**
35793075b01SAndreas Gohr     * Format an entity
35893075b01SAndreas Gohr     *
35993075b01SAndreas Gohr     * Entities are basically small text replacements
36093075b01SAndreas Gohr     *
36193075b01SAndreas Gohr     * Uses $this->entities
36293075b01SAndreas Gohr     *
36393075b01SAndreas Gohr     * @param string $entity
36493075b01SAndreas Gohr     */
365*3aa984aeSLarsGit223    public function entity($entity)
366*3aa984aeSLarsGit223    {
36717076c2aSAndreas Gohr        $this->cdata($entity);
36839a89382SEsther Brunner    }
36939a89382SEsther Brunner
37093075b01SAndreas Gohr    /**
37193075b01SAndreas Gohr     * Typographically format a multiply sign
37293075b01SAndreas Gohr     *
37393075b01SAndreas Gohr     * Example: ($x=640, $y=480) should result in "640×480"
37493075b01SAndreas Gohr     *
37593075b01SAndreas Gohr     * @param string|int $x first value
37693075b01SAndreas Gohr     * @param string|int $y second value
37793075b01SAndreas Gohr     */
378*3aa984aeSLarsGit223    public function multiplyentity($x, $y)
379*3aa984aeSLarsGit223    {
38017076c2aSAndreas Gohr        $this->cdata($x.'×'.$y);
38139a89382SEsther Brunner    }
38239a89382SEsther Brunner
38393075b01SAndreas Gohr    /**
38493075b01SAndreas Gohr     * Render an opening single quote char (language specific)
38593075b01SAndreas Gohr     */
386*3aa984aeSLarsGit223    public function singlequoteopening()
387*3aa984aeSLarsGit223    {
38871b40da2SAnika Henke        global $lang;
38917076c2aSAndreas Gohr        $this->cdata($lang['singlequoteopening']);
39039a89382SEsther Brunner    }
39139a89382SEsther Brunner
39293075b01SAndreas Gohr    /**
39393075b01SAndreas Gohr     * Render a closing single quote char (language specific)
39493075b01SAndreas Gohr     */
395*3aa984aeSLarsGit223    public function singlequoteclosing()
396*3aa984aeSLarsGit223    {
39771b40da2SAnika Henke        global $lang;
39817076c2aSAndreas Gohr        $this->cdata($lang['singlequoteclosing']);
39939a89382SEsther Brunner    }
40039a89382SEsther Brunner
40193075b01SAndreas Gohr    /**
40293075b01SAndreas Gohr     * Render an apostrophe char (language specific)
40393075b01SAndreas Gohr     */
404*3aa984aeSLarsGit223    public function apostrophe()
405*3aa984aeSLarsGit223    {
40657d757d1SAndreas Gohr        global $lang;
40717076c2aSAndreas Gohr        $this->cdata($lang['apostrophe']);
40857d757d1SAndreas Gohr    }
40957d757d1SAndreas Gohr
41093075b01SAndreas Gohr    /**
41193075b01SAndreas Gohr     * Render an opening double quote char (language specific)
41293075b01SAndreas Gohr     */
413*3aa984aeSLarsGit223    public function doublequoteopening()
414*3aa984aeSLarsGit223    {
41571b40da2SAnika Henke        global $lang;
41617076c2aSAndreas Gohr        $this->cdata($lang['doublequoteopening']);
41739a89382SEsther Brunner    }
41839a89382SEsther Brunner
41993075b01SAndreas Gohr    /**
42093075b01SAndreas Gohr     * Render an closinging double quote char (language specific)
42193075b01SAndreas Gohr     */
422*3aa984aeSLarsGit223    public function doublequoteclosing()
423*3aa984aeSLarsGit223    {
42471b40da2SAnika Henke        global $lang;
42517076c2aSAndreas Gohr        $this->cdata($lang['doublequoteclosing']);
42639a89382SEsther Brunner    }
42739a89382SEsther Brunner
42893075b01SAndreas Gohr    /**
42993075b01SAndreas Gohr     * Render a CamelCase link
43093075b01SAndreas Gohr     *
43193075b01SAndreas Gohr     * @param string $link The link name
43293075b01SAndreas Gohr     * @see http://en.wikipedia.org/wiki/CamelCase
43393075b01SAndreas Gohr     */
434*3aa984aeSLarsGit223    public function camelcaselink($link)
435*3aa984aeSLarsGit223    {
43639a89382SEsther Brunner        $this->internallink($link, $link);
43739a89382SEsther Brunner    }
43839a89382SEsther Brunner
43993075b01SAndreas Gohr    /**
44093075b01SAndreas Gohr     * Render a page local link
44193075b01SAndreas Gohr     *
44293075b01SAndreas Gohr     * @param string $hash hash link identifier
44393075b01SAndreas Gohr     * @param string $name name for the link
44493075b01SAndreas Gohr     */
445*3aa984aeSLarsGit223    public function locallink($hash, $name = null)
446*3aa984aeSLarsGit223    {
4479269d0b1SMichael Hamann        if (is_array($name)) {
4489269d0b1SMichael Hamann            $this->_firstimage($name['src']);
449*3aa984aeSLarsGit223            if ($name['type'] == 'internalmedia') {
450*3aa984aeSLarsGit223                $this->_recordMediaUsage($name['src']);
451*3aa984aeSLarsGit223            }
4529269d0b1SMichael Hamann        }
4539269d0b1SMichael Hamann    }
45439a89382SEsther Brunner
45539a89382SEsther Brunner    /**
45639a89382SEsther Brunner     * keep track of internal links in $this->meta['relation']['references']
45793075b01SAndreas Gohr     *
45893075b01SAndreas Gohr     * @param string            $id   page ID to link to. eg. 'wiki:syntax'
45959bc3b48SGerrit Uitslag     * @param string|array|null $name name for the link, array for media file
46039a89382SEsther Brunner     */
461*3aa984aeSLarsGit223    public function internallink($id, $name = null)
462*3aa984aeSLarsGit223    {
46339a89382SEsther Brunner        global $ID;
46439a89382SEsther Brunner
465ffec1009SMichael Hamann        if (is_array($name)) {
466aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
467*3aa984aeSLarsGit223            if ($name['type'] == 'internalmedia') {
468*3aa984aeSLarsGit223                $this->_recordMediaUsage($name['src']);
469*3aa984aeSLarsGit223            }
470ffec1009SMichael Hamann        }
471aa5a2937SAndreas Gohr
47266d935e7SMichael Hamann        $parts = explode('?', $id, 2);
47366d935e7SMichael Hamann        if (count($parts) === 2) {
47466d935e7SMichael Hamann            $id = $parts[0];
47566d935e7SMichael Hamann        }
47666d935e7SMichael Hamann
47739a89382SEsther Brunner        $default = $this->_simpleTitle($id);
47839a89382SEsther Brunner
47939a89382SEsther Brunner        // first resolve and clean up the $id
48039a89382SEsther Brunner        resolve_pageid(getNS($ID), $id, $exists);
48193075b01SAndreas Gohr        @list($page) = explode('#', $id, 2);
48239a89382SEsther Brunner
48339a89382SEsther Brunner        // set metadata
4843be6e394Schris        $this->meta['relation']['references'][$page] = $exists;
48539a89382SEsther Brunner        // $data = array('relation' => array('isreferencedby' => array($ID => true)));
48639a89382SEsther Brunner        // p_set_metadata($id, $data);
48739a89382SEsther Brunner
48839a89382SEsther Brunner        // add link title to summary
48939a89382SEsther Brunner        if ($this->capture) {
49039a89382SEsther Brunner            $name = $this->_getLinkTitle($name, $default, $id);
49139a89382SEsther Brunner            $this->doc .= $name;
49239a89382SEsther Brunner        }
49339a89382SEsther Brunner    }
49439a89382SEsther Brunner
49593075b01SAndreas Gohr    /**
49693075b01SAndreas Gohr     * Render an external link
49793075b01SAndreas Gohr     *
49893075b01SAndreas Gohr     * @param string            $url  full URL with scheme
49959bc3b48SGerrit Uitslag     * @param string|array|null $name name for the link, array for media file
50093075b01SAndreas Gohr     */
501*3aa984aeSLarsGit223    public function externallink($url, $name = null)
502*3aa984aeSLarsGit223    {
503ffec1009SMichael Hamann        if (is_array($name)) {
504aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
505*3aa984aeSLarsGit223            if ($name['type'] == 'internalmedia') {
506*3aa984aeSLarsGit223                $this->_recordMediaUsage($name['src']);
507*3aa984aeSLarsGit223            }
508ffec1009SMichael Hamann        }
509aa5a2937SAndreas Gohr
51039a89382SEsther Brunner        if ($this->capture) {
511a2ea2dc1SAdrian Lang            $this->doc .= $this->_getLinkTitle($name, '<'.$url.'>');
51239a89382SEsther Brunner        }
51339a89382SEsther Brunner    }
51439a89382SEsther Brunner
51593075b01SAndreas Gohr    /**
51693075b01SAndreas Gohr     * Render an interwiki link
51793075b01SAndreas Gohr     *
51893075b01SAndreas Gohr     * You may want to use $this->_resolveInterWiki() here
51993075b01SAndreas Gohr     *
52093075b01SAndreas Gohr     * @param string       $match     original link - probably not much use
52193075b01SAndreas Gohr     * @param string|array $name      name for the link, array for media file
52293075b01SAndreas Gohr     * @param string       $wikiName  indentifier (shortcut) for the remote wiki
52393075b01SAndreas Gohr     * @param string       $wikiUri   the fragment parsed from the original link
52493075b01SAndreas Gohr     */
525*3aa984aeSLarsGit223    public function interwikilink($match, $name, $wikiName, $wikiUri)
526*3aa984aeSLarsGit223    {
527ffec1009SMichael Hamann        if (is_array($name)) {
528aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
529*3aa984aeSLarsGit223            if ($name['type'] == 'internalmedia') {
530*3aa984aeSLarsGit223                $this->_recordMediaUsage($name['src']);
531*3aa984aeSLarsGit223            }
532ffec1009SMichael Hamann        }
533aa5a2937SAndreas Gohr
53439a89382SEsther Brunner        if ($this->capture) {
53593075b01SAndreas Gohr            list($wikiUri) = explode('#', $wikiUri, 2);
536a2ea2dc1SAdrian Lang            $name = $this->_getLinkTitle($name, $wikiUri);
53739a89382SEsther Brunner            $this->doc .= $name;
53839a89382SEsther Brunner        }
53939a89382SEsther Brunner    }
54039a89382SEsther Brunner
54193075b01SAndreas Gohr    /**
54293075b01SAndreas Gohr     * Link to windows share
54393075b01SAndreas Gohr     *
54493075b01SAndreas Gohr     * @param string       $url  the link
54593075b01SAndreas Gohr     * @param string|array $name name for the link, array for media file
54693075b01SAndreas Gohr     */
547*3aa984aeSLarsGit223    public function windowssharelink($url, $name = null)
548*3aa984aeSLarsGit223    {
549ffec1009SMichael Hamann        if (is_array($name)) {
550aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
551*3aa984aeSLarsGit223            if ($name['type'] == 'internalmedia') {
552*3aa984aeSLarsGit223                $this->_recordMediaUsage($name['src']);
553*3aa984aeSLarsGit223            }
554ffec1009SMichael Hamann        }
555aa5a2937SAndreas Gohr
55639a89382SEsther Brunner        if ($this->capture) {
557*3aa984aeSLarsGit223            if ($name) {
558*3aa984aeSLarsGit223                $this->doc .= $name;
559*3aa984aeSLarsGit223            } else {
560*3aa984aeSLarsGit223                $this->doc .= '<'.$url.'>';
561*3aa984aeSLarsGit223            }
56239a89382SEsther Brunner        }
56339a89382SEsther Brunner    }
56439a89382SEsther Brunner
56593075b01SAndreas Gohr    /**
56693075b01SAndreas Gohr     * Render a linked E-Mail Address
56793075b01SAndreas Gohr     *
56893075b01SAndreas Gohr     * Should honor $conf['mailguard'] setting
56993075b01SAndreas Gohr     *
57093075b01SAndreas Gohr     * @param string       $address Email-Address
57193075b01SAndreas Gohr     * @param string|array $name    name for the link, array for media file
57293075b01SAndreas Gohr     */
573*3aa984aeSLarsGit223    public function emaillink($address, $name = null)
574*3aa984aeSLarsGit223    {
575ffec1009SMichael Hamann        if (is_array($name)) {
576aa5a2937SAndreas Gohr            $this->_firstimage($name['src']);
577*3aa984aeSLarsGit223            if ($name['type'] == 'internalmedia') {
578*3aa984aeSLarsGit223                $this->_recordMediaUsage($name['src']);
579*3aa984aeSLarsGit223            }
580ffec1009SMichael Hamann        }
581aa5a2937SAndreas Gohr
58239a89382SEsther Brunner        if ($this->capture) {
583*3aa984aeSLarsGit223            if ($name) {
584*3aa984aeSLarsGit223                $this->doc .= $name;
585*3aa984aeSLarsGit223            } else {
586*3aa984aeSLarsGit223                $this->doc .= '<'.$address.'>';
587*3aa984aeSLarsGit223            }
58839a89382SEsther Brunner        }
58939a89382SEsther Brunner    }
59039a89382SEsther Brunner
59193075b01SAndreas Gohr    /**
59293075b01SAndreas Gohr     * Render an internal media file
59393075b01SAndreas Gohr     *
59493075b01SAndreas Gohr     * @param string $src     media ID
59593075b01SAndreas Gohr     * @param string $title   descriptive text
59693075b01SAndreas Gohr     * @param string $align   left|center|right
59793075b01SAndreas Gohr     * @param int    $width   width of media in pixel
59893075b01SAndreas Gohr     * @param int    $height  height of media in pixel
59993075b01SAndreas Gohr     * @param string $cache   cache|recache|nocache
60093075b01SAndreas Gohr     * @param string $linking linkonly|detail|nolink
60193075b01SAndreas Gohr     */
602de369923SAndreas Gohr    public function internalmedia($src, $title = null, $align = null, $width = null,
603*3aa984aeSLarsGit223                           $height = null, $cache = null, $linking = null)
604*3aa984aeSLarsGit223    {
605*3aa984aeSLarsGit223        if ($this->capture && $title) {
606*3aa984aeSLarsGit223            $this->doc .= '['.$title.']';
607*3aa984aeSLarsGit223        }
608aa5a2937SAndreas Gohr        $this->_firstimage($src);
609ffec1009SMichael Hamann        $this->_recordMediaUsage($src);
61039a89382SEsther Brunner    }
61139a89382SEsther Brunner
61293075b01SAndreas Gohr    /**
61393075b01SAndreas Gohr     * Render an external media file
61493075b01SAndreas Gohr     *
61593075b01SAndreas Gohr     * @param string $src     full media URL
61693075b01SAndreas Gohr     * @param string $title   descriptive text
61793075b01SAndreas Gohr     * @param string $align   left|center|right
61893075b01SAndreas Gohr     * @param int    $width   width of media in pixel
61993075b01SAndreas Gohr     * @param int    $height  height of media in pixel
62093075b01SAndreas Gohr     * @param string $cache   cache|recache|nocache
62193075b01SAndreas Gohr     * @param string $linking linkonly|detail|nolink
62293075b01SAndreas Gohr     */
623de369923SAndreas Gohr    public function externalmedia($src, $title = null, $align = null, $width = null,
624*3aa984aeSLarsGit223                           $height = null, $cache = null, $linking = null)
625*3aa984aeSLarsGit223    {
626*3aa984aeSLarsGit223        if ($this->capture && $title) {
627*3aa984aeSLarsGit223            $this->doc .= '['.$title.']';
628*3aa984aeSLarsGit223        }
629aa5a2937SAndreas Gohr        $this->_firstimage($src);
63039a89382SEsther Brunner    }
63139a89382SEsther Brunner
63293075b01SAndreas Gohr    /**
63393075b01SAndreas Gohr     * Render the output of an RSS feed
63493075b01SAndreas Gohr     *
63593075b01SAndreas Gohr     * @param string $url    URL of the feed
63693075b01SAndreas Gohr     * @param array  $params Finetuning of the output
63793075b01SAndreas Gohr     */
638*3aa984aeSLarsGit223    public function rss($url, $params)
639*3aa984aeSLarsGit223    {
640ce6b63d9Schris        $this->meta['relation']['haspart'][$url] = true;
64164e9144aSchris
64264e9144aSchris        $this->meta['date']['valid']['age'] =
64364e9144aSchris            isset($this->meta['date']['valid']['age']) ?
64464e9144aSchris                min($this->meta['date']['valid']['age'], $params['refresh']) :
64564e9144aSchris                $params['refresh'];
646ce6b63d9Schris    }
64739a89382SEsther Brunner
64893075b01SAndreas Gohr    #region Utils
64939a89382SEsther Brunner
65039a89382SEsther Brunner    /**
65139a89382SEsther Brunner     * Removes any Namespace from the given name but keeps
65239a89382SEsther Brunner     * casing and special chars
65339a89382SEsther Brunner     *
65439a89382SEsther Brunner     * @author Andreas Gohr <andi@splitbrain.org>
655f50a239bSTakamura     *
656f50a239bSTakamura     * @param string $name
657f50a239bSTakamura     *
658f50a239bSTakamura     * @return mixed|string
65939a89382SEsther Brunner     */
660*3aa984aeSLarsGit223    public function _simpleTitle($name)
661*3aa984aeSLarsGit223    {
66239a89382SEsther Brunner        global $conf;
66339a89382SEsther Brunner
664*3aa984aeSLarsGit223        if (is_array($name)) {
665*3aa984aeSLarsGit223            return '';
666*3aa984aeSLarsGit223        }
6679bee852eSAndreas Gohr
66839a89382SEsther Brunner        if ($conf['useslash']) {
66939a89382SEsther Brunner            $nssep = '[:;/]';
67039a89382SEsther Brunner        } else {
67139a89382SEsther Brunner            $nssep = '[:;]';
67239a89382SEsther Brunner        }
67339a89382SEsther Brunner        $name = preg_replace('!.*'.$nssep.'!', '', $name);
6743be6e394Schris        //if there is a hash we use the anchor name only
67539a89382SEsther Brunner        $name = preg_replace('!.*#!', '', $name);
67639a89382SEsther Brunner        return $name;
67739a89382SEsther Brunner    }
67839a89382SEsther Brunner
67939a89382SEsther Brunner    /**
68039a89382SEsther Brunner     * Construct a title and handle images in titles
68139a89382SEsther Brunner     *
68239a89382SEsther Brunner     * @author Harry Fuecks <hfuecks@gmail.com>
68359bc3b48SGerrit Uitslag     * @param string|array|null $title    either string title or media array
68493075b01SAndreas Gohr     * @param string            $default  default title if nothing else is found
68593075b01SAndreas Gohr     * @param null|string       $id       linked page id (used to extract title from first heading)
68693075b01SAndreas Gohr     * @return string title text
68739a89382SEsther Brunner     */
688*3aa984aeSLarsGit223    public function _getLinkTitle($title, $default, $id = null)
689*3aa984aeSLarsGit223    {
69085038eabSDanny        if (is_array($title)) {
69193075b01SAndreas Gohr            if ($title['title']) {
69293075b01SAndreas Gohr                return '['.$title['title'].']';
69393075b01SAndreas Gohr            } else {
69493075b01SAndreas Gohr                return $default;
69593075b01SAndreas Gohr            }
69685038eabSDanny        } elseif (is_null($title) || trim($title) == '') {
697fe9ec250SChris Smith            if (useHeading('content') && $id) {
69820f04039SDanny                $heading = p_get_first_heading($id, METADATA_DONT_RENDER);
699*3aa984aeSLarsGit223                if ($heading) {
700*3aa984aeSLarsGit223                    return $heading;
701*3aa984aeSLarsGit223                }
70239a89382SEsther Brunner            }
70339a89382SEsther Brunner            return $default;
70485038eabSDanny        } else {
70539a89382SEsther Brunner            return $title;
70639a89382SEsther Brunner        }
70739a89382SEsther Brunner    }
70839a89382SEsther Brunner
70993075b01SAndreas Gohr    /**
71093075b01SAndreas Gohr     * Remember first image
71193075b01SAndreas Gohr     *
71293075b01SAndreas Gohr     * @param string $src image URL or ID
71393075b01SAndreas Gohr     */
714*3aa984aeSLarsGit223    protected function _firstimage($src)
715*3aa984aeSLarsGit223    {
716aa5a2937SAndreas Gohr        global $ID;
717aa5a2937SAndreas Gohr
718*3aa984aeSLarsGit223        if ($this->firstimage) {
719*3aa984aeSLarsGit223            return;
720*3aa984aeSLarsGit223        }
721*3aa984aeSLarsGit223
72293075b01SAndreas Gohr        list($src) = explode('#', $src, 2);
7233e7e6067SKlap-in        if (!media_isexternal($src)) {
724aa5a2937SAndreas Gohr            resolve_mediaid(getNS($ID), $src, $exists);
725aa5a2937SAndreas Gohr        }
726aa5a2937SAndreas Gohr        if (preg_match('/.(jpe?g|gif|png)$/i', $src)) {
727aa5a2937SAndreas Gohr            $this->firstimage = $src;
728aa5a2937SAndreas Gohr        }
729aa5a2937SAndreas Gohr    }
730ffec1009SMichael Hamann
73193075b01SAndreas Gohr    /**
73293075b01SAndreas Gohr     * Store list of used media files in metadata
73393075b01SAndreas Gohr     *
73493075b01SAndreas Gohr     * @param string $src media ID
73593075b01SAndreas Gohr     */
736*3aa984aeSLarsGit223    protected function _recordMediaUsage($src)
737*3aa984aeSLarsGit223    {
738ffec1009SMichael Hamann        global $ID;
739ffec1009SMichael Hamann
74093075b01SAndreas Gohr        list ($src) = explode('#', $src, 2);
741*3aa984aeSLarsGit223        if (media_isexternal($src)) {
742*3aa984aeSLarsGit223            return;
743*3aa984aeSLarsGit223        }
744ffec1009SMichael Hamann        resolve_mediaid(getNS($ID), $src, $exists);
745ffec1009SMichael Hamann        $this->meta['relation']['media'][$src] = $exists;
746ffec1009SMichael Hamann    }
74793075b01SAndreas Gohr
74893075b01SAndreas Gohr    #endregion
74939a89382SEsther Brunner}
75039a89382SEsther Brunner
751e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
752