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 */ 133aa984aeSLarsGit223class Doku_Renderer_metadata extends Doku_Renderer 143aa984aeSLarsGit223{ 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 */ 503aa984aeSLarsGit223 public function getFormat() 513aa984aeSLarsGit223 { 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 */ 603aa984aeSLarsGit223 public function document_start() 613aa984aeSLarsGit223 { 62ef47e298SAndreas Gohr global $ID; 63620f4930SAndreas Gohr 64620f4930SAndreas Gohr $this->headers = array(); 65620f4930SAndreas Gohr 66ef47e298SAndreas Gohr // external pages are missing create date 670a444b5aSPhy if (!isset($this->persistent['date']['created']) || !$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 */ 853aa984aeSLarsGit223 public function document_end() 863aa984aeSLarsGit223 { 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) { 968cbc5ee8SAndreas Gohr $this->doc = \dokuwiki\Utf8\PhpString::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 */ 1163aa984aeSLarsGit223 public function cdata($text) 1173aa984aeSLarsGit223 { 1183aa984aeSLarsGit223 if (!$this->capture || !$this->capturing) { 1193aa984aeSLarsGit223 return; 1203aa984aeSLarsGit223 } 12117076c2aSAndreas Gohr 12217076c2aSAndreas Gohr $this->doc .= $text; 12317076c2aSAndreas Gohr 12417076c2aSAndreas Gohr $this->captured += strlen($text); 1253aa984aeSLarsGit223 if ($this->captured > self::ABSTRACT_LEN) { 1263aa984aeSLarsGit223 $this->capture = false; 1273aa984aeSLarsGit223 } 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 */ 1373aa984aeSLarsGit223 public function toc_additem($id, $text, $level) 1383aa984aeSLarsGit223 { 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 */ 1603aa984aeSLarsGit223 public function header($text, $level, $pos) 1613aa984aeSLarsGit223 { 1623aa984aeSLarsGit223 if (!isset($this->meta['title'])) { 1633aa984aeSLarsGit223 $this->meta['title'] = $text; 1643aa984aeSLarsGit223 } 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 */ 1773aa984aeSLarsGit223 public function p_open() 1783aa984aeSLarsGit223 { 17917076c2aSAndreas Gohr $this->cdata(DOKU_LF); 18039a89382SEsther Brunner } 18139a89382SEsther Brunner 18293075b01SAndreas Gohr /** 18393075b01SAndreas Gohr * Close a paragraph 18493075b01SAndreas Gohr */ 1853aa984aeSLarsGit223 public function p_close() 1863aa984aeSLarsGit223 { 18717076c2aSAndreas Gohr $this->cdata(DOKU_LF); 18839a89382SEsther Brunner } 18939a89382SEsther Brunner 19093075b01SAndreas Gohr /** 19193075b01SAndreas Gohr * Create a line break 19293075b01SAndreas Gohr */ 1933aa984aeSLarsGit223 public function linebreak() 1943aa984aeSLarsGit223 { 19517076c2aSAndreas Gohr $this->cdata(DOKU_LF); 19639a89382SEsther Brunner } 19739a89382SEsther Brunner 19893075b01SAndreas Gohr /** 19993075b01SAndreas Gohr * Create a horizontal line 20093075b01SAndreas Gohr */ 2013aa984aeSLarsGit223 public function hr() 2023aa984aeSLarsGit223 { 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 */ 2153aa984aeSLarsGit223 public function footnote_open() 2163aa984aeSLarsGit223 { 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 */ 2363aa984aeSLarsGit223 public function footnote_close() 2373aa984aeSLarsGit223 { 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 */ 2503aa984aeSLarsGit223 public function listu_open() 2513aa984aeSLarsGit223 { 25217076c2aSAndreas Gohr $this->cdata(DOKU_LF); 25339a89382SEsther Brunner } 25439a89382SEsther Brunner 25593075b01SAndreas Gohr /** 25693075b01SAndreas Gohr * Open an ordered list 25793075b01SAndreas Gohr */ 2583aa984aeSLarsGit223 public function listo_open() 2593aa984aeSLarsGit223 { 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 */ 2693aa984aeSLarsGit223 public function listitem_open($level, $node=false) 2703aa984aeSLarsGit223 { 27117076c2aSAndreas Gohr $this->cdata(str_repeat(DOKU_TAB, $level).'* '); 27239a89382SEsther Brunner } 27339a89382SEsther Brunner 27493075b01SAndreas Gohr /** 27593075b01SAndreas Gohr * Close a list item 27693075b01SAndreas Gohr */ 2773aa984aeSLarsGit223 public function listitem_close() 2783aa984aeSLarsGit223 { 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 */ 2873aa984aeSLarsGit223 public function preformatted($text) 2883aa984aeSLarsGit223 { 28917076c2aSAndreas Gohr $this->cdata($text); 29039a89382SEsther Brunner } 29139a89382SEsther Brunner 29293075b01SAndreas Gohr /** 29393075b01SAndreas Gohr * Start a block quote 29493075b01SAndreas Gohr */ 2953aa984aeSLarsGit223 public function quote_open() 2963aa984aeSLarsGit223 { 29717076c2aSAndreas Gohr $this->cdata(DOKU_LF.DOKU_TAB.'"'); 29839a89382SEsther Brunner } 29939a89382SEsther Brunner 30093075b01SAndreas Gohr /** 30193075b01SAndreas Gohr * Stop a block quote 30293075b01SAndreas Gohr */ 3033aa984aeSLarsGit223 public function quote_close() 3043aa984aeSLarsGit223 { 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 */ 3153aa984aeSLarsGit223 public function file($text, $lang = null, $file = null) 3163aa984aeSLarsGit223 { 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 */ 3273aa984aeSLarsGit223 public function code($text, $language = null, $file = null) 3283aa984aeSLarsGit223 { 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 */ 3393aa984aeSLarsGit223 public function acronym($acronym) 3403aa984aeSLarsGit223 { 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 */ 3513aa984aeSLarsGit223 public function smiley($smiley) 3523aa984aeSLarsGit223 { 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 */ 3653aa984aeSLarsGit223 public function entity($entity) 3663aa984aeSLarsGit223 { 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 */ 3783aa984aeSLarsGit223 public function multiplyentity($x, $y) 3793aa984aeSLarsGit223 { 38017076c2aSAndreas Gohr $this->cdata($x.'×'.$y); 38139a89382SEsther Brunner } 38239a89382SEsther Brunner 38393075b01SAndreas Gohr /** 38493075b01SAndreas Gohr * Render an opening single quote char (language specific) 38593075b01SAndreas Gohr */ 3863aa984aeSLarsGit223 public function singlequoteopening() 3873aa984aeSLarsGit223 { 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 */ 3953aa984aeSLarsGit223 public function singlequoteclosing() 3963aa984aeSLarsGit223 { 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 */ 4043aa984aeSLarsGit223 public function apostrophe() 4053aa984aeSLarsGit223 { 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 */ 4133aa984aeSLarsGit223 public function doublequoteopening() 4143aa984aeSLarsGit223 { 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 */ 4223aa984aeSLarsGit223 public function doublequoteclosing() 4233aa984aeSLarsGit223 { 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 */ 4343aa984aeSLarsGit223 public function camelcaselink($link) 4353aa984aeSLarsGit223 { 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 */ 4453aa984aeSLarsGit223 public function locallink($hash, $name = null) 4463aa984aeSLarsGit223 { 4479269d0b1SMichael Hamann if (is_array($name)) { 4489269d0b1SMichael Hamann $this->_firstimage($name['src']); 4493aa984aeSLarsGit223 if ($name['type'] == 'internalmedia') { 4503aa984aeSLarsGit223 $this->_recordMediaUsage($name['src']); 4513aa984aeSLarsGit223 } 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 */ 4613aa984aeSLarsGit223 public function internallink($id, $name = null) 4623aa984aeSLarsGit223 { 46339a89382SEsther Brunner global $ID; 46439a89382SEsther Brunner 465ffec1009SMichael Hamann if (is_array($name)) { 466aa5a2937SAndreas Gohr $this->_firstimage($name['src']); 4673aa984aeSLarsGit223 if ($name['type'] == 'internalmedia') { 4683aa984aeSLarsGit223 $this->_recordMediaUsage($name['src']); 4693aa984aeSLarsGit223 } 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 480*8c6be208SAndreas Gohr $resolver = new \dokuwiki\Utils\PageResolver($ID); 481*8c6be208SAndreas Gohr $id = $resolver->resolveId($id); 48293075b01SAndreas Gohr @list($page) = explode('#', $id, 2); 48339a89382SEsther Brunner 48439a89382SEsther Brunner // set metadata 485*8c6be208SAndreas Gohr $this->meta['relation']['references'][$page] = page_exists($page); 48639a89382SEsther Brunner // $data = array('relation' => array('isreferencedby' => array($ID => true))); 48739a89382SEsther Brunner // p_set_metadata($id, $data); 48839a89382SEsther Brunner 48939a89382SEsther Brunner // add link title to summary 49039a89382SEsther Brunner if ($this->capture) { 49139a89382SEsther Brunner $name = $this->_getLinkTitle($name, $default, $id); 49239a89382SEsther Brunner $this->doc .= $name; 49339a89382SEsther Brunner } 49439a89382SEsther Brunner } 49539a89382SEsther Brunner 49693075b01SAndreas Gohr /** 49793075b01SAndreas Gohr * Render an external link 49893075b01SAndreas Gohr * 49993075b01SAndreas Gohr * @param string $url full URL with scheme 50059bc3b48SGerrit Uitslag * @param string|array|null $name name for the link, array for media file 50193075b01SAndreas Gohr */ 5023aa984aeSLarsGit223 public function externallink($url, $name = null) 5033aa984aeSLarsGit223 { 504ffec1009SMichael Hamann if (is_array($name)) { 505aa5a2937SAndreas Gohr $this->_firstimage($name['src']); 5063aa984aeSLarsGit223 if ($name['type'] == 'internalmedia') { 5073aa984aeSLarsGit223 $this->_recordMediaUsage($name['src']); 5083aa984aeSLarsGit223 } 509ffec1009SMichael Hamann } 510aa5a2937SAndreas Gohr 51139a89382SEsther Brunner if ($this->capture) { 512a2ea2dc1SAdrian Lang $this->doc .= $this->_getLinkTitle($name, '<'.$url.'>'); 51339a89382SEsther Brunner } 51439a89382SEsther Brunner } 51539a89382SEsther Brunner 51693075b01SAndreas Gohr /** 51793075b01SAndreas Gohr * Render an interwiki link 51893075b01SAndreas Gohr * 51993075b01SAndreas Gohr * You may want to use $this->_resolveInterWiki() here 52093075b01SAndreas Gohr * 52193075b01SAndreas Gohr * @param string $match original link - probably not much use 52293075b01SAndreas Gohr * @param string|array $name name for the link, array for media file 52393075b01SAndreas Gohr * @param string $wikiName indentifier (shortcut) for the remote wiki 52493075b01SAndreas Gohr * @param string $wikiUri the fragment parsed from the original link 52593075b01SAndreas Gohr */ 5263aa984aeSLarsGit223 public function interwikilink($match, $name, $wikiName, $wikiUri) 5273aa984aeSLarsGit223 { 528ffec1009SMichael Hamann if (is_array($name)) { 529aa5a2937SAndreas Gohr $this->_firstimage($name['src']); 5303aa984aeSLarsGit223 if ($name['type'] == 'internalmedia') { 5313aa984aeSLarsGit223 $this->_recordMediaUsage($name['src']); 5323aa984aeSLarsGit223 } 533ffec1009SMichael Hamann } 534aa5a2937SAndreas Gohr 53539a89382SEsther Brunner if ($this->capture) { 53693075b01SAndreas Gohr list($wikiUri) = explode('#', $wikiUri, 2); 537a2ea2dc1SAdrian Lang $name = $this->_getLinkTitle($name, $wikiUri); 53839a89382SEsther Brunner $this->doc .= $name; 53939a89382SEsther Brunner } 54039a89382SEsther Brunner } 54139a89382SEsther Brunner 54293075b01SAndreas Gohr /** 54393075b01SAndreas Gohr * Link to windows share 54493075b01SAndreas Gohr * 54593075b01SAndreas Gohr * @param string $url the link 54693075b01SAndreas Gohr * @param string|array $name name for the link, array for media file 54793075b01SAndreas Gohr */ 5483aa984aeSLarsGit223 public function windowssharelink($url, $name = null) 5493aa984aeSLarsGit223 { 550ffec1009SMichael Hamann if (is_array($name)) { 551aa5a2937SAndreas Gohr $this->_firstimage($name['src']); 5523aa984aeSLarsGit223 if ($name['type'] == 'internalmedia') { 5533aa984aeSLarsGit223 $this->_recordMediaUsage($name['src']); 5543aa984aeSLarsGit223 } 555ffec1009SMichael Hamann } 556aa5a2937SAndreas Gohr 55739a89382SEsther Brunner if ($this->capture) { 5583aa984aeSLarsGit223 if ($name) { 5593aa984aeSLarsGit223 $this->doc .= $name; 5603aa984aeSLarsGit223 } else { 5613aa984aeSLarsGit223 $this->doc .= '<'.$url.'>'; 5623aa984aeSLarsGit223 } 56339a89382SEsther Brunner } 56439a89382SEsther Brunner } 56539a89382SEsther Brunner 56693075b01SAndreas Gohr /** 56793075b01SAndreas Gohr * Render a linked E-Mail Address 56893075b01SAndreas Gohr * 56993075b01SAndreas Gohr * Should honor $conf['mailguard'] setting 57093075b01SAndreas Gohr * 57193075b01SAndreas Gohr * @param string $address Email-Address 57293075b01SAndreas Gohr * @param string|array $name name for the link, array for media file 57393075b01SAndreas Gohr */ 5743aa984aeSLarsGit223 public function emaillink($address, $name = null) 5753aa984aeSLarsGit223 { 576ffec1009SMichael Hamann if (is_array($name)) { 577aa5a2937SAndreas Gohr $this->_firstimage($name['src']); 5783aa984aeSLarsGit223 if ($name['type'] == 'internalmedia') { 5793aa984aeSLarsGit223 $this->_recordMediaUsage($name['src']); 5803aa984aeSLarsGit223 } 581ffec1009SMichael Hamann } 582aa5a2937SAndreas Gohr 58339a89382SEsther Brunner if ($this->capture) { 5843aa984aeSLarsGit223 if ($name) { 5853aa984aeSLarsGit223 $this->doc .= $name; 5863aa984aeSLarsGit223 } else { 5873aa984aeSLarsGit223 $this->doc .= '<'.$address.'>'; 5883aa984aeSLarsGit223 } 58939a89382SEsther Brunner } 59039a89382SEsther Brunner } 59139a89382SEsther Brunner 59293075b01SAndreas Gohr /** 59393075b01SAndreas Gohr * Render an internal media file 59493075b01SAndreas Gohr * 59593075b01SAndreas Gohr * @param string $src media ID 59693075b01SAndreas Gohr * @param string $title descriptive text 59793075b01SAndreas Gohr * @param string $align left|center|right 59893075b01SAndreas Gohr * @param int $width width of media in pixel 59993075b01SAndreas Gohr * @param int $height height of media in pixel 60093075b01SAndreas Gohr * @param string $cache cache|recache|nocache 60193075b01SAndreas Gohr * @param string $linking linkonly|detail|nolink 60293075b01SAndreas Gohr */ 603de369923SAndreas Gohr public function internalmedia($src, $title = null, $align = null, $width = null, 6043aa984aeSLarsGit223 $height = null, $cache = null, $linking = null) 6053aa984aeSLarsGit223 { 6063aa984aeSLarsGit223 if ($this->capture && $title) { 6073aa984aeSLarsGit223 $this->doc .= '['.$title.']'; 6083aa984aeSLarsGit223 } 609aa5a2937SAndreas Gohr $this->_firstimage($src); 610ffec1009SMichael Hamann $this->_recordMediaUsage($src); 61139a89382SEsther Brunner } 61239a89382SEsther Brunner 61393075b01SAndreas Gohr /** 61493075b01SAndreas Gohr * Render an external media file 61593075b01SAndreas Gohr * 61693075b01SAndreas Gohr * @param string $src full media URL 61793075b01SAndreas Gohr * @param string $title descriptive text 61893075b01SAndreas Gohr * @param string $align left|center|right 61993075b01SAndreas Gohr * @param int $width width of media in pixel 62093075b01SAndreas Gohr * @param int $height height of media in pixel 62193075b01SAndreas Gohr * @param string $cache cache|recache|nocache 62293075b01SAndreas Gohr * @param string $linking linkonly|detail|nolink 62393075b01SAndreas Gohr */ 624de369923SAndreas Gohr public function externalmedia($src, $title = null, $align = null, $width = null, 6253aa984aeSLarsGit223 $height = null, $cache = null, $linking = null) 6263aa984aeSLarsGit223 { 6273aa984aeSLarsGit223 if ($this->capture && $title) { 6283aa984aeSLarsGit223 $this->doc .= '['.$title.']'; 6293aa984aeSLarsGit223 } 630aa5a2937SAndreas Gohr $this->_firstimage($src); 63139a89382SEsther Brunner } 63239a89382SEsther Brunner 63393075b01SAndreas Gohr /** 63493075b01SAndreas Gohr * Render the output of an RSS feed 63593075b01SAndreas Gohr * 63693075b01SAndreas Gohr * @param string $url URL of the feed 63793075b01SAndreas Gohr * @param array $params Finetuning of the output 63893075b01SAndreas Gohr */ 6393aa984aeSLarsGit223 public function rss($url, $params) 6403aa984aeSLarsGit223 { 641ce6b63d9Schris $this->meta['relation']['haspart'][$url] = true; 64264e9144aSchris 64364e9144aSchris $this->meta['date']['valid']['age'] = 64464e9144aSchris isset($this->meta['date']['valid']['age']) ? 64564e9144aSchris min($this->meta['date']['valid']['age'], $params['refresh']) : 64664e9144aSchris $params['refresh']; 647ce6b63d9Schris } 64839a89382SEsther Brunner 64993075b01SAndreas Gohr #region Utils 65039a89382SEsther Brunner 65139a89382SEsther Brunner /** 65239a89382SEsther Brunner * Removes any Namespace from the given name but keeps 65339a89382SEsther Brunner * casing and special chars 65439a89382SEsther Brunner * 65539a89382SEsther Brunner * @author Andreas Gohr <andi@splitbrain.org> 656f50a239bSTakamura * 657f50a239bSTakamura * @param string $name 658f50a239bSTakamura * 659f50a239bSTakamura * @return mixed|string 66039a89382SEsther Brunner */ 6613aa984aeSLarsGit223 public function _simpleTitle($name) 6623aa984aeSLarsGit223 { 66339a89382SEsther Brunner global $conf; 66439a89382SEsther Brunner 6653aa984aeSLarsGit223 if (is_array($name)) { 6663aa984aeSLarsGit223 return ''; 6673aa984aeSLarsGit223 } 6689bee852eSAndreas Gohr 66939a89382SEsther Brunner if ($conf['useslash']) { 67039a89382SEsther Brunner $nssep = '[:;/]'; 67139a89382SEsther Brunner } else { 67239a89382SEsther Brunner $nssep = '[:;]'; 67339a89382SEsther Brunner } 67439a89382SEsther Brunner $name = preg_replace('!.*'.$nssep.'!', '', $name); 6753be6e394Schris //if there is a hash we use the anchor name only 67639a89382SEsther Brunner $name = preg_replace('!.*#!', '', $name); 67739a89382SEsther Brunner return $name; 67839a89382SEsther Brunner } 67939a89382SEsther Brunner 68039a89382SEsther Brunner /** 68139a89382SEsther Brunner * Construct a title and handle images in titles 68239a89382SEsther Brunner * 68339a89382SEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com> 68459bc3b48SGerrit Uitslag * @param string|array|null $title either string title or media array 68593075b01SAndreas Gohr * @param string $default default title if nothing else is found 68693075b01SAndreas Gohr * @param null|string $id linked page id (used to extract title from first heading) 68793075b01SAndreas Gohr * @return string title text 68839a89382SEsther Brunner */ 6893aa984aeSLarsGit223 public function _getLinkTitle($title, $default, $id = null) 6903aa984aeSLarsGit223 { 69185038eabSDanny if (is_array($title)) { 69293075b01SAndreas Gohr if ($title['title']) { 69393075b01SAndreas Gohr return '['.$title['title'].']'; 69493075b01SAndreas Gohr } else { 69593075b01SAndreas Gohr return $default; 69693075b01SAndreas Gohr } 69785038eabSDanny } elseif (is_null($title) || trim($title) == '') { 698fe9ec250SChris Smith if (useHeading('content') && $id) { 69920f04039SDanny $heading = p_get_first_heading($id, METADATA_DONT_RENDER); 7003aa984aeSLarsGit223 if ($heading) { 7013aa984aeSLarsGit223 return $heading; 7023aa984aeSLarsGit223 } 70339a89382SEsther Brunner } 70439a89382SEsther Brunner return $default; 70585038eabSDanny } else { 70639a89382SEsther Brunner return $title; 70739a89382SEsther Brunner } 70839a89382SEsther Brunner } 70939a89382SEsther Brunner 71093075b01SAndreas Gohr /** 71193075b01SAndreas Gohr * Remember first image 71293075b01SAndreas Gohr * 71393075b01SAndreas Gohr * @param string $src image URL or ID 71493075b01SAndreas Gohr */ 7153aa984aeSLarsGit223 protected function _firstimage($src) 7163aa984aeSLarsGit223 { 717aa5a2937SAndreas Gohr global $ID; 718aa5a2937SAndreas Gohr 7193aa984aeSLarsGit223 if ($this->firstimage) { 7203aa984aeSLarsGit223 return; 7213aa984aeSLarsGit223 } 7223aa984aeSLarsGit223 72393075b01SAndreas Gohr list($src) = explode('#', $src, 2); 7243e7e6067SKlap-in if (!media_isexternal($src)) { 725*8c6be208SAndreas Gohr $src = (new \dokuwiki\Utils\MediaResolver($ID))->resolveId($src); 726aa5a2937SAndreas Gohr } 727aa5a2937SAndreas Gohr if (preg_match('/.(jpe?g|gif|png)$/i', $src)) { 728aa5a2937SAndreas Gohr $this->firstimage = $src; 729aa5a2937SAndreas Gohr } 730aa5a2937SAndreas Gohr } 731ffec1009SMichael Hamann 73293075b01SAndreas Gohr /** 73393075b01SAndreas Gohr * Store list of used media files in metadata 73493075b01SAndreas Gohr * 73593075b01SAndreas Gohr * @param string $src media ID 73693075b01SAndreas Gohr */ 7373aa984aeSLarsGit223 protected function _recordMediaUsage($src) 7383aa984aeSLarsGit223 { 739ffec1009SMichael Hamann global $ID; 740ffec1009SMichael Hamann 74193075b01SAndreas Gohr list ($src) = explode('#', $src, 2); 7423aa984aeSLarsGit223 if (media_isexternal($src)) { 7433aa984aeSLarsGit223 return; 7443aa984aeSLarsGit223 } 745*8c6be208SAndreas Gohr $src = (new \dokuwiki\Utils\MediaResolver($ID))->resolveId($src); 746*8c6be208SAndreas Gohr $file = mediaFN($src); 747*8c6be208SAndreas Gohr $this->meta['relation']['media'][$src] = file_exists($file); 748ffec1009SMichael Hamann } 74993075b01SAndreas Gohr 75093075b01SAndreas Gohr #endregion 75139a89382SEsther Brunner} 75239a89382SEsther Brunner 753e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 : 754