3 use dokuwiki\ChangeLog\MediaChangeLog
;
6 * Renderer for XHTML output
8 * This is DokuWiki's main renderer used to display page content in the wiki
10 * @author Harry Fuecks <hfuecks@gmail.com>
11 * @author Andreas Gohr <andi@splitbrain.org>
14 class Doku_Renderer_xhtml
extends Doku_Renderer
{
15 /** @var array store the table of contents */
16 public $toc = array();
18 /** @var array A stack of section edit data */
19 protected $sectionedits = array();
21 /** @var string|int link pages and media against this revision */
24 /** @var int last section edit id, used by startSectionEdit */
25 protected $lastsecid = 0;
27 /** @var array a list of footnotes, list starts at 1! */
28 protected $footnotes = array();
30 /** @var int current section level */
31 protected $lastlevel = 0;
32 /** @var array section node tracker */
33 protected $node = array(0, 0, 0, 0, 0);
35 /** @var string temporary $doc store */
36 protected $store = '';
38 /** @var array global counter, for table classes etc. */
39 protected $_counter = array(); //
41 /** @var int counts the code and file blocks, used to provide download links */
42 protected $_codeblock = 0;
44 /** @var array list of allowed URL schemes */
45 protected $schemes = null;
48 * Register a new edit section range
50 * @param int $start The byte position for the edit start
51 * @param array $data Associative array with section data:
52 * Key 'name': the section name/title
53 * Key 'target': the target for the section edit,
54 * e.g. 'section' or 'table'
55 * Key 'hid': header id
56 * Key 'codeblockOffset': actual code block index
57 * Key 'start': set in startSectionEdit(),
59 * Key 'range': calculated from 'start' and
60 * $key in finishSectionEdit(),
62 * @return string A marker class for the starting HTML element
64 * @author Adrian Lang <lang@cosmocode.de>
66 public function startSectionEdit($start, $data) {
67 if (!is_array($data)) {
70 'startSectionEdit: $data "%s" is NOT an array! One of your plugins needs an update.',
75 // @deprecated 2018-04-14, backward compatibility
76 $args = func_get_args();
78 if(isset($args[1])) $data['target'] = $args[1];
79 if(isset($args[2])) $data['name'] = $args[2];
80 if(isset($args[3])) $data['hid'] = $args[3];
82 $data['secid'] = ++
$this->lastsecid
;
83 $data['start'] = $start;
84 $this->sectionedits
[] = $data;
85 return 'sectionedit'.$data['secid'];
89 * Finish an edit section range
91 * @param int $end The byte position for the edit end; null for the rest of the page
93 * @author Adrian Lang <lang@cosmocode.de>
95 public function finishSectionEdit($end = null, $hid = null) {
96 $data = array_pop($this->sectionedits
);
97 if(!is_null($end) && $end <= $data['start']) {
101 $data['hid'] .= $hid;
103 $data['range'] = $data['start'].'-'.(is_null($end) ?
'' : $end);
104 unset($data['start']);
105 $this->doc
.= '<!-- EDIT'.hsc(json_encode ($data)).' -->';
109 * Returns the format produced by this renderer.
111 * @return string always 'xhtml'
113 public function getFormat() {
118 * Initialize the document
120 public function document_start() {
121 //reset some internals
122 $this->toc
= array();
126 * Finalize the document
128 public function document_end() {
129 // Finish open section edits.
130 while(count($this->sectionedits
) > 0) {
131 if($this->sectionedits
[count($this->sectionedits
) - 1]['start'] <= 1) {
132 // If there is only one section, do not write a section edit
134 array_pop($this->sectionedits
);
136 $this->finishSectionEdit();
140 if(count($this->footnotes
) > 0) {
141 $this->doc
.= '<div class="footnotes">'.DOKU_LF
;
143 foreach($this->footnotes
as $id => $footnote) {
144 // check its not a placeholder that indicates actual footnote text is elsewhere
145 if(substr($footnote, 0, 5) != "@@FNT") {
147 // open the footnote and set the anchor and backlink
148 $this->doc
.= '<div class="fn">';
149 $this->doc
.= '<sup><a href="#fnt__'.$id.'" id="fn__'.$id.'" class="fn_bot">';
150 $this->doc
.= $id.')</a></sup> '.DOKU_LF
;
152 // get any other footnotes that use the same markup
153 $alt = array_keys($this->footnotes
, "@@FNT$id");
156 foreach($alt as $ref) {
157 // set anchor and backlink for the other footnotes
158 $this->doc
.= ', <sup><a href="#fnt__'.($ref).'" id="fn__'.($ref).'" class="fn_bot">';
159 $this->doc
.= ($ref).')</a></sup> '.DOKU_LF
;
163 // add footnote markup and close this footnote
164 $this->doc
.= '<div class="content">'.$footnote.'</div>';
165 $this->doc
.= '</div>'.DOKU_LF
;
168 $this->doc
.= '</div>'.DOKU_LF
;
174 $this->info
['toc'] &&
175 is_array($this->toc
) &&
176 $conf['tocminheads'] && count($this->toc
) >= $conf['tocminheads']
182 // make sure there are no empty paragraphs
183 $this->doc
= preg_replace('#<p>\s*</p>#', '', $this->doc
);
187 * Add an item to the TOC
189 * @param string $id the hash link
190 * @param string $text the text to display
191 * @param int $level the nesting level
193 public function toc_additem($id, $text, $level) {
197 if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']) {
198 $this->toc
[] = html_mktocitem($id, $text, $level - $conf['toptoclevel'] +
1);
205 * @param string $text the text to display
206 * @param int $level header level
207 * @param int $pos byte position in the original source
209 public function header($text, $level, $pos) {
212 if(blank($text)) return; //skip empty headlines
214 $hid = $this->_headerToLink($text, true);
216 //only add items within configured levels
217 $this->toc_additem($hid, $text, $level);
219 // adjust $node to reflect hierarchy of levels
220 $this->node
[$level - 1]++
;
221 if($level < $this->lastlevel
) {
222 for($i = 0; $i < $this->lastlevel
- $level; $i++
) {
223 $this->node
[$this->lastlevel
- $i - 1] = 0;
226 $this->lastlevel
= $level;
228 if($level <= $conf['maxseclevel'] &&
229 count($this->sectionedits
) > 0 &&
230 $this->sectionedits
[count($this->sectionedits
) - 1]['target'] === 'section'
232 $this->finishSectionEdit($pos - 1);
236 $this->doc
.= DOKU_LF
.'<h'.$level;
237 if($level <= $conf['maxseclevel']) {
239 $data['target'] = 'section';
240 $data['name'] = $text;
242 $data['codeblockOffset'] = $this->_codeblock
;
243 $this->doc
.= ' class="'.$this->startSectionEdit($pos, $data).'"';
245 $this->doc
.= ' id="'.$hid.'">';
246 $this->doc
.= $this->_xmlEntities($text);
247 $this->doc
.= "</h$level>".DOKU_LF
;
253 * @param int $level section level (as determined by the previous header)
255 public function section_open($level) {
256 $this->doc
.= '<div class="level'.$level.'">'.DOKU_LF
;
260 * Close the current section
262 public function section_close() {
263 $this->doc
.= DOKU_LF
.'</div>'.DOKU_LF
;
267 * Render plain text data
271 public function cdata($text) {
272 $this->doc
.= $this->_xmlEntities($text);
278 public function p_open() {
279 $this->doc
.= DOKU_LF
.'<p>'.DOKU_LF
;
285 public function p_close() {
286 $this->doc
.= DOKU_LF
.'</p>'.DOKU_LF
;
290 * Create a line break
292 public function linebreak() {
293 $this->doc
.= '<br/>'.DOKU_LF
;
297 * Create a horizontal line
299 public function hr() {
300 $this->doc
.= '<hr />'.DOKU_LF
;
304 * Start strong (bold) formatting
306 public function strong_open() {
307 $this->doc
.= '<strong>';
311 * Stop strong (bold) formatting
313 public function strong_close() {
314 $this->doc
.= '</strong>';
318 * Start emphasis (italics) formatting
320 public function emphasis_open() {
321 $this->doc
.= '<em>';
325 * Stop emphasis (italics) formatting
327 public function emphasis_close() {
328 $this->doc
.= '</em>';
332 * Start underline formatting
334 public function underline_open() {
335 $this->doc
.= '<em class="u">';
339 * Stop underline formatting
341 public function underline_close() {
342 $this->doc
.= '</em>';
346 * Start monospace formatting
348 public function monospace_open() {
349 $this->doc
.= '<code>';
353 * Stop monospace formatting
355 public function monospace_close() {
356 $this->doc
.= '</code>';
362 public function subscript_open() {
363 $this->doc
.= '<sub>';
369 public function subscript_close() {
370 $this->doc
.= '</sub>';
374 * Start a superscript
376 public function superscript_open() {
377 $this->doc
.= '<sup>';
383 public function superscript_close() {
384 $this->doc
.= '</sup>';
388 * Start deleted (strike-through) formatting
390 public function deleted_open() {
391 $this->doc
.= '<del>';
395 * Stop deleted (strike-through) formatting
397 public function deleted_close() {
398 $this->doc
.= '</del>';
402 * Callback for footnote start syntax
404 * All following content will go to the footnote instead of
405 * the document. To achieve this the previous rendered content
406 * is moved to $store and $doc is cleared
408 * @author Andreas Gohr <andi@splitbrain.org>
410 public function footnote_open() {
412 // move current content to store and record footnote
413 $this->store
= $this->doc
;
418 * Callback for footnote end syntax
420 * All rendered content is moved to the $footnotes array and the old
421 * content is restored from $store again
423 * @author Andreas Gohr
425 public function footnote_close() {
426 /** @var $fnid int takes track of seen footnotes, assures they are unique even across multiple docs FS#2841 */
428 // assign new footnote id (we start at 1)
431 // recover footnote into the stack and restore old content
432 $footnote = $this->doc
;
433 $this->doc
= $this->store
;
436 // check to see if this footnote has been seen before
437 $i = array_search($footnote, $this->footnotes
);
440 // its a new footnote, add it to the $footnotes array
441 $this->footnotes
[$fnid] = $footnote;
443 // seen this one before, save a placeholder
444 $this->footnotes
[$fnid] = "@@FNT".($i);
447 // output the footnote reference and link
448 $this->doc
.= '<sup><a href="#fn__'.$fnid.'" id="fnt__'.$fnid.'" class="fn_top">'.$fnid.')</a></sup>';
452 * Open an unordered list
454 * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
456 public function listu_open($classes = null) {
458 if($classes !== null) {
459 if(is_array($classes)) $classes = join(' ', $classes);
460 $class = " class=\"$classes\"";
462 $this->doc
.= "<ul$class>".DOKU_LF
;
466 * Close an unordered list
468 public function listu_close() {
469 $this->doc
.= '</ul>'.DOKU_LF
;
473 * Open an ordered list
475 * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
477 public function listo_open($classes = null) {
479 if($classes !== null) {
480 if(is_array($classes)) $classes = join(' ', $classes);
481 $class = " class=\"$classes\"";
483 $this->doc
.= "<ol$class>".DOKU_LF
;
487 * Close an ordered list
489 public function listo_close() {
490 $this->doc
.= '</ol>'.DOKU_LF
;
496 * @param int $level the nesting level
497 * @param bool $node true when a node; false when a leaf
499 public function listitem_open($level, $node=false) {
500 $branching = $node ?
' node' : '';
501 $this->doc
.= '<li class="level'.$level.$branching.'">';
507 public function listitem_close() {
508 $this->doc
.= '</li>'.DOKU_LF
;
512 * Start the content of a list item
514 public function listcontent_open() {
515 $this->doc
.= '<div class="li">';
519 * Stop the content of a list item
521 public function listcontent_close() {
522 $this->doc
.= '</div>'.DOKU_LF
;
526 * Output unformatted $text
528 * Defaults to $this->cdata()
530 * @param string $text
532 public function unformatted($text) {
533 $this->doc
.= $this->_xmlEntities($text);
537 * Execute PHP code if allowed
539 * @param string $text PHP code that is either executed or printed
540 * @param string $wrapper html element to wrap result if $conf['phpok'] is okff
542 * @author Andreas Gohr <andi@splitbrain.org>
544 public function php($text, $wrapper = 'code') {
550 $this->doc
.= ob_get_contents();
553 $this->doc
.= p_xhtml_cached_geshi($text, 'php', $wrapper);
558 * Output block level PHP code
560 * If $conf['phpok'] is true this should evaluate the given code and append the result
563 * @param string $text The PHP code
565 public function phpblock($text) {
566 $this->php($text, 'pre');
570 * Insert HTML if allowed
572 * @param string $text html text
573 * @param string $wrapper html element to wrap result if $conf['htmlok'] is okff
575 * @author Andreas Gohr <andi@splitbrain.org>
577 public function html($text, $wrapper = 'code') {
580 if($conf['htmlok']) {
583 $this->doc
.= p_xhtml_cached_geshi($text, 'html4strict', $wrapper);
588 * Output raw block-level HTML
590 * If $conf['htmlok'] is true this should add the code as is to $doc
592 * @param string $text The HTML
594 public function htmlblock($text) {
595 $this->html($text, 'pre');
599 * Start a block quote
601 public function quote_open() {
602 $this->doc
.= '<blockquote><div class="no">'.DOKU_LF
;
608 public function quote_close() {
609 $this->doc
.= '</div></blockquote>'.DOKU_LF
;
613 * Output preformatted text
615 * @param string $text
617 public function preformatted($text) {
618 $this->doc
.= '<pre class="code">'.trim($this->_xmlEntities($text), "\n\r").'</pre>'.DOKU_LF
;
622 * Display text as file content, optionally syntax highlighted
624 * @param string $text text to show
625 * @param string $language programming language to use for syntax highlighting
626 * @param string $filename file path label
627 * @param array $options assoziative array with additional geshi options
629 public function file($text, $language = null, $filename = null, $options=null) {
630 $this->_highlight('file', $text, $language, $filename, $options);
634 * Display text as code content, optionally syntax highlighted
636 * @param string $text text to show
637 * @param string $language programming language to use for syntax highlighting
638 * @param string $filename file path label
639 * @param array $options assoziative array with additional geshi options
641 public function code($text, $language = null, $filename = null, $options=null) {
642 $this->_highlight('code', $text, $language, $filename, $options);
646 * Use GeSHi to highlight language syntax in code and file blocks
648 * @author Andreas Gohr <andi@splitbrain.org>
649 * @param string $type code|file
650 * @param string $text text to show
651 * @param string $language programming language to use for syntax highlighting
652 * @param string $filename file path label
653 * @param array $options assoziative array with additional geshi options
655 public function _highlight($type, $text, $language = null, $filename = null, $options = null) {
660 $language = preg_replace(PREG_PATTERN_VALID_LANGUAGE
, '', $language);
664 list($ext) = mimetype($filename, false);
665 $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
666 $class = 'mediafile mf_'.$class;
669 if ($INPUT->has('codeblockOffset')) {
670 $offset = $INPUT->str('codeblockOffset');
672 $this->doc
.= '<dl class="'.$type.'">'.DOKU_LF
;
673 $this->doc
.= '<dt><a href="' .
677 array('codeblock' => $offset +
$this->_codeblock
)
678 ) . '" title="' . $lang['download'] . '" class="' . $class . '">';
679 $this->doc
.= hsc($filename);
680 $this->doc
.= '</a></dt>'.DOKU_LF
.'<dd>';
683 if($text[0] == "\n") {
684 $text = substr($text, 1);
686 if(substr($text, -1) == "\n") {
687 $text = substr($text, 0, -1);
690 if(empty($language)) { // empty is faster than is_null and can prevent '' string
691 $this->doc
.= '<pre class="'.$type.'">'.$this->_xmlEntities($text).'</pre>'.DOKU_LF
;
693 $class = 'code'; //we always need the code class to make the syntax highlighting apply
694 if($type != 'code') $class .= ' '.$type;
696 $this->doc
.= "<pre class=\"$class $language\">" .
697 p_xhtml_cached_geshi($text, $language, '', $options) .
702 $this->doc
.= '</dd></dl>'.DOKU_LF
;
711 * Uses $this->acronyms
713 * @param string $acronym
715 public function acronym($acronym) {
717 if(array_key_exists($acronym, $this->acronyms
)) {
719 $title = $this->_xmlEntities($this->acronyms
[$acronym]);
721 $this->doc
.= '<abbr title="'.$title
722 .'">'.$this->_xmlEntities($acronym).'</abbr>';
725 $this->doc
.= $this->_xmlEntities($acronym);
734 * @param string $smiley
736 public function smiley($smiley) {
737 if(array_key_exists($smiley, $this->smileys
)) {
738 $this->doc
.= '<img src="'.DOKU_BASE
.'lib/images/smileys/'.$this->smileys
[$smiley].
739 '" class="icon" alt="'.
740 $this->_xmlEntities($smiley).'" />';
742 $this->doc
.= $this->_xmlEntities($smiley);
749 * Entities are basically small text replacements
751 * Uses $this->entities
753 * @param string $entity
755 public function entity($entity) {
756 if(array_key_exists($entity, $this->entities
)) {
757 $this->doc
.= $this->entities
[$entity];
759 $this->doc
.= $this->_xmlEntities($entity);
764 * Typographically format a multiply sign
766 * Example: ($x=640, $y=480) should result in "640×480"
768 * @param string|int $x first value
769 * @param string|int $y second value
771 public function multiplyentity($x, $y) {
772 $this->doc
.= "$x×$y";
776 * Render an opening single quote char (language specific)
778 public function singlequoteopening() {
780 $this->doc
.= $lang['singlequoteopening'];
784 * Render a closing single quote char (language specific)
786 public function singlequoteclosing() {
788 $this->doc
.= $lang['singlequoteclosing'];
792 * Render an apostrophe char (language specific)
794 public function apostrophe() {
796 $this->doc
.= $lang['apostrophe'];
800 * Render an opening double quote char (language specific)
802 public function doublequoteopening() {
804 $this->doc
.= $lang['doublequoteopening'];
808 * Render an closinging double quote char (language specific)
810 public function doublequoteclosing() {
812 $this->doc
.= $lang['doublequoteclosing'];
816 * Render a CamelCase link
818 * @param string $link The link name
819 * @param bool $returnonly whether to return html or write to doc attribute
820 * @return void|string writes to doc attribute or returns html depends on $returnonly
822 * @see http://en.wikipedia.org/wiki/CamelCase
824 public function camelcaselink($link, $returnonly = false) {
826 return $this->internallink($link, $link, null, true);
828 $this->internallink($link, $link);
833 * Render a page local link
835 * @param string $hash hash link identifier
836 * @param string $name name for the link
837 * @param bool $returnonly whether to return html or write to doc attribute
838 * @return void|string writes to doc attribute or returns html depends on $returnonly
840 public function locallink($hash, $name = null, $returnonly = false) {
842 $name = $this->_getLinkTitle($name, $hash, $isImage);
843 $hash = $this->_headerToLink($hash);
846 $doc = '<a href="#'.$hash.'" title="'.$title.'" class="wikilink1">';
858 * Render an internal Wiki Link
860 * $search,$returnonly & $linktype are not for the renderer but are used
861 * elsewhere - no need to implement them in other renderers
863 * @author Andreas Gohr <andi@splitbrain.org>
864 * @param string $id pageid
865 * @param string|null $name link name
866 * @param string|null $search adds search url param
867 * @param bool $returnonly whether to return html or write to doc attribute
868 * @param string $linktype type to set use of headings
869 * @return void|string writes to doc attribute or returns html depends on $returnonly
871 public function internallink($id, $name = null, $search = null, $returnonly = false, $linktype = 'content') {
877 $parts = explode('?', $id, 2);
878 if(count($parts) === 2) {
883 // For empty $id we need to know the current $ID
884 // We need this check because _simpleTitle needs
885 // correct $id and resolve_pageid() use cleanID($id)
886 // (some things could be lost)
891 // default name is based on $id as given
892 $default = $this->_simpleTitle($id);
894 // now first resolve and clean up the $id
895 resolve_pageid(getNS($ID), $id, $exists, $this->date_at
, true);
898 $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype);
901 $class = 'wikilink1';
903 $class = 'wikilink2';
904 $link['rel'] = 'nofollow';
911 @list
($id, $hash) = explode('#', $id, 2);
912 if(!empty($hash)) $hash = $this->_headerToLink($hash);
914 //prepare for formating
915 $link['target'] = $conf['target']['wiki'];
919 // highlight link to current page
920 if(isset($INFO) && $id == $INFO['id']) {
921 $link['pre'] = '<span class="curid">';
922 $link['suf'] = '</span>';
925 $link['class'] = $class;
927 $params = $params.'&at='.rawurlencode($this->date_at
);
929 $link['url'] = wl($id, $params);
930 $link['name'] = $name;
931 $link['title'] = $id;
934 ($conf['userewrite']) ?
$link['url'] .= '?' : $link['url'] .= '&';
935 if(is_array($search)) {
936 $search = array_map('rawurlencode', $search);
937 $link['url'] .= 's[]='.join('&s[]=', $search);
939 $link['url'] .= 's='.rawurlencode($search);
944 if($hash) $link['url'] .= '#'.$hash;
948 return $this->_formatLink($link);
950 $this->doc
.= $this->_formatLink($link);
955 * Render an external link
957 * @param string $url full URL with scheme
958 * @param string|array $name name for the link, array for media file
959 * @param bool $returnonly whether to return html or write to doc attribute
960 * @return void|string writes to doc attribute or returns html depends on $returnonly
962 public function externallink($url, $name = null, $returnonly = false) {
965 $name = $this->_getLinkTitle($name, $url, $isImage);
967 // url might be an attack vector, only allow registered protocols
968 if(is_null($this->schemes
)) $this->schemes
= getSchemes();
969 list($scheme) = explode('://', $url);
970 $scheme = strtolower($scheme);
971 if(!in_array($scheme, $this->schemes
)) $url = '';
973 // is there still an URL?
985 $class = 'urlextern';
990 //prepare for formating
992 $link['target'] = $conf['target']['extern'];
997 $link['class'] = $class;
1001 $link['name'] = $name;
1002 $link['title'] = $this->_xmlEntities($url);
1003 if($conf['relnofollow']) $link['rel'] .= ' ugc nofollow';
1004 if($conf['target']['extern']) $link['rel'] .= ' noopener';
1008 return $this->_formatLink($link);
1010 $this->doc
.= $this->_formatLink($link);
1015 * Render an interwiki link
1017 * You may want to use $this->_resolveInterWiki() here
1019 * @param string $match original link - probably not much use
1020 * @param string|array $name name for the link, array for media file
1021 * @param string $wikiName indentifier (shortcut) for the remote wiki
1022 * @param string $wikiUri the fragment parsed from the original link
1023 * @param bool $returnonly whether to return html or write to doc attribute
1024 * @return void|string writes to doc attribute or returns html depends on $returnonly
1026 public function interwikilink($match, $name, $wikiName, $wikiUri, $returnonly = false) {
1030 $link['target'] = $conf['target']['interwiki'];
1034 $link['name'] = $this->_getLinkTitle($name, $wikiUri, $isImage);
1039 $url = $this->_resolveInterWiki($wikiName, $wikiUri, $exists);
1042 $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $wikiName);
1043 $link['class'] = "interwiki iw_$class";
1045 $link['class'] = 'media';
1048 //do we stay at the same server? Use local target
1049 if(strpos($url, DOKU_URL
) === 0 OR strpos($url, DOKU_BASE
) === 0) {
1050 $link['target'] = $conf['target']['wiki'];
1052 if($exists !== null && !$isImage) {
1054 $link['class'] .= ' wikilink1';
1056 $link['class'] .= ' wikilink2';
1057 $link['rel'] .= ' nofollow';
1060 if($conf['target']['interwiki']) $link['rel'] .= ' noopener';
1062 $link['url'] = $url;
1063 $link['title'] = htmlspecialchars($link['url']);
1067 if($url == '') return $link['name'];
1068 return $this->_formatLink($link);
1070 if($url == '') $this->doc
.= $link['name'];
1071 else $this->doc
.= $this->_formatLink($link);
1076 * Link to windows share
1078 * @param string $url the link
1079 * @param string|array $name name for the link, array for media file
1080 * @param bool $returnonly whether to return html or write to doc attribute
1081 * @return void|string writes to doc attribute or returns html depends on $returnonly
1083 public function windowssharelink($url, $name = null, $returnonly = false) {
1088 $link['target'] = $conf['target']['windows'];
1091 $link['style'] = '';
1093 $link['name'] = $this->_getLinkTitle($name, $url, $isImage);
1095 $link['class'] = 'windows';
1097 $link['class'] = 'media';
1100 $link['title'] = $this->_xmlEntities($url);
1101 $url = str_replace('\\', '/', $url);
1102 $url = 'file:///'.$url;
1103 $link['url'] = $url;
1107 return $this->_formatLink($link);
1109 $this->doc
.= $this->_formatLink($link);
1114 * Render a linked E-Mail Address
1116 * Honors $conf['mailguard'] setting
1118 * @param string $address Email-Address
1119 * @param string|array $name name for the link, array for media file
1120 * @param bool $returnonly whether to return html or write to doc attribute
1121 * @return void|string writes to doc attribute or returns html depends on $returnonly
1123 public function emaillink($address, $name = null, $returnonly = false) {
1127 $link['target'] = '';
1130 $link['style'] = '';
1133 $name = $this->_getLinkTitle($name, '', $isImage);
1135 $link['class'] = 'mail';
1137 $link['class'] = 'media';
1140 $address = $this->_xmlEntities($address);
1141 $address = obfuscate($address);
1148 if($conf['mailguard'] == 'visible') $address = rawurlencode($address);
1150 $link['url'] = 'mailto:'.$address;
1151 $link['name'] = $name;
1152 $link['title'] = $title;
1156 return $this->_formatLink($link);
1158 $this->doc
.= $this->_formatLink($link);
1163 * Render an internal media file
1165 * @param string $src media ID
1166 * @param string $title descriptive text
1167 * @param string $align left|center|right
1168 * @param int $width width of media in pixel
1169 * @param int $height height of media in pixel
1170 * @param string $cache cache|recache|nocache
1171 * @param string $linking linkonly|detail|nolink
1172 * @param bool $return return HTML instead of adding to $doc
1173 * @return void|string writes to doc attribute or returns html depends on $return
1175 public function internalmedia($src, $title = null, $align = null, $width = null,
1176 $height = null, $cache = null, $linking = null, $return = false) {
1178 if (strpos($src, '#') !== false) {
1179 list($src, $hash) = explode('#', $src, 2);
1181 resolve_mediaid(getNS($ID), $src, $exists, $this->date_at
, true);
1184 $render = ($linking == 'linkonly') ?
false : true;
1185 $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
1187 list($ext, $mime) = mimetype($src, false);
1188 if(substr($mime, 0, 5) == 'image' && $render) {
1194 'rev' => $this->_getLastMediaRevisionAt($src)
1196 ($linking == 'direct')
1198 } elseif(($mime == 'application/x-shockwave-flash' ||
media_supportedav($mime)) && $render) {
1199 // don't link movies
1203 $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
1204 $link['class'] .= ' mediafile mf_'.$class;
1210 'rev' => $this->_getLastMediaRevisionAt($src)
1214 if($exists) $link['title'] .= ' ('.filesize_h(filesize(mediaFN($src))).')';
1217 if (!empty($hash)) $link['url'] .= '#'.$hash;
1219 //markup non existing files
1221 $link['class'] .= ' wikilink2';
1226 if($linking == 'nolink' ||
$noLink) return $link['name'];
1227 else return $this->_formatLink($link);
1229 if($linking == 'nolink' ||
$noLink) $this->doc
.= $link['name'];
1230 else $this->doc
.= $this->_formatLink($link);
1235 * Render an external media file
1237 * @param string $src full media URL
1238 * @param string $title descriptive text
1239 * @param string $align left|center|right
1240 * @param int $width width of media in pixel
1241 * @param int $height height of media in pixel
1242 * @param string $cache cache|recache|nocache
1243 * @param string $linking linkonly|detail|nolink
1244 * @param bool $return return HTML instead of adding to $doc
1245 * @return void|string writes to doc attribute or returns html depends on $return
1247 public function externalmedia($src, $title = null, $align = null, $width = null,
1248 $height = null, $cache = null, $linking = null, $return = false) {
1249 if(link_isinterwiki($src)){
1250 list($shortcut, $reference) = explode('>', $src, 2);
1252 $src = $this->_resolveInterWiki($shortcut, $reference, $exists);
1253 if($src == '' && empty($title)){
1254 // make sure at least something will be shown in this case
1255 $title = $reference;
1258 list($src, $hash) = explode('#', $src, 2);
1261 // only output plaintext without link if there is no src
1264 $render = ($linking == 'linkonly') ?
false : true;
1265 $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
1267 $link['url'] = ml($src, array('cache' => $cache));
1269 list($ext, $mime) = mimetype($src, false);
1270 if(substr($mime, 0, 5) == 'image' && $render) {
1271 // link only jpeg images
1272 // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = true;
1273 } elseif(($mime == 'application/x-shockwave-flash' ||
media_supportedav($mime)) && $render) {
1274 // don't link movies
1278 $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
1279 $link['class'] .= ' mediafile mf_'.$class;
1282 if($hash) $link['url'] .= '#'.$hash;
1286 if($linking == 'nolink' ||
$noLink) return $link['name'];
1287 else return $this->_formatLink($link);
1289 if($linking == 'nolink' ||
$noLink) $this->doc
.= $link['name'];
1290 else $this->doc
.= $this->_formatLink($link);
1295 * Renders an RSS feed
1297 * @param string $url URL of the feed
1298 * @param array $params Finetuning of the output
1300 * @author Andreas Gohr <andi@splitbrain.org>
1302 public function rss($url, $params) {
1306 require_once(DOKU_INC
.'inc/FeedParser.php');
1307 $feed = new FeedParser();
1308 $feed->set_feed_url($url);
1310 //disable warning while fetching
1311 if(!defined('DOKU_E_LEVEL')) {
1312 $elvl = error_reporting(E_ERROR
);
1314 $rc = $feed->init();
1316 error_reporting($elvl);
1319 if($params['nosort']) $feed->enable_order_by_date(false);
1321 //decide on start and end
1322 if($params['reverse']) {
1324 $start = $feed->get_item_quantity() - 1;
1325 $end = $start - ($params['max']);
1326 $end = ($end < -1) ?
-1 : $end;
1330 $end = $feed->get_item_quantity();
1331 $end = ($end > $params['max']) ?
$params['max'] : $end;
1334 $this->doc
.= '<ul class="rss">';
1336 for($x = $start; $x != $end; $x +
= $mod) {
1337 $item = $feed->get_item($x);
1338 $this->doc
.= '<li><div class="li">';
1339 // support feeds without links
1340 $lnkurl = $item->get_permalink();
1342 // title is escaped by SimplePie, we unescape here because it
1343 // is escaped again in externallink() FS#1705
1344 $this->externallink(
1345 $item->get_permalink(),
1346 html_entity_decode($item->get_title(), ENT_QUOTES
, 'UTF-8')
1349 $this->doc
.= ' '.$item->get_title();
1351 if($params['author']) {
1352 $author = $item->get_author(0);
1354 $name = $author->get_name();
1355 if(!$name) $name = $author->get_email();
1356 if($name) $this->doc
.= ' '.$lang['by'].' '.hsc($name);
1359 if($params['date']) {
1360 $this->doc
.= ' ('.$item->get_local_date($conf['dformat']).')';
1362 if($params['details']) {
1363 $this->doc
.= '<div class="detail">';
1364 if($conf['htmlok']) {
1365 $this->doc
.= $item->get_description();
1367 $this->doc
.= strip_tags($item->get_description());
1369 $this->doc
.= '</div>';
1372 $this->doc
.= '</div></li>';
1375 $this->doc
.= '<li><div class="li">';
1376 $this->doc
.= '<em>'.$lang['rssfailed'].'</em>';
1377 $this->externallink($url);
1378 if($conf['allowdebug']) {
1379 $this->doc
.= '<!--'.hsc($feed->error
).'-->';
1381 $this->doc
.= '</div></li>';
1383 $this->doc
.= '</ul>';
1389 * @param int $maxcols maximum number of columns
1390 * @param int $numrows NOT IMPLEMENTED
1391 * @param int $pos byte position in the original source
1392 * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
1394 public function table_open($maxcols = null, $numrows = null, $pos = null, $classes = null) {
1395 // initialize the row counter used for classes
1396 $this->_counter
['row_counter'] = 0;
1398 if($classes !== null) {
1399 if(is_array($classes)) $classes = join(' ', $classes);
1400 $class .= ' ' . $classes;
1403 $hid = $this->_headerToLink($class, true);
1405 $data['target'] = 'table';
1407 $data['hid'] = $hid;
1408 $class .= ' '.$this->startSectionEdit($pos, $data);
1410 $this->doc
.= '<div class="'.$class.'"><table class="inline">'.
1417 * @param int $pos byte position in the original source
1419 public function table_close($pos = null) {
1420 $this->doc
.= '</table></div>'.DOKU_LF
;
1422 $this->finishSectionEdit($pos);
1427 * Open a table header
1429 public function tablethead_open() {
1430 $this->doc
.= DOKU_TAB
.'<thead>'.DOKU_LF
;
1434 * Close a table header
1436 public function tablethead_close() {
1437 $this->doc
.= DOKU_TAB
.'</thead>'.DOKU_LF
;
1443 public function tabletbody_open() {
1444 $this->doc
.= DOKU_TAB
.'<tbody>'.DOKU_LF
;
1448 * Close a table body
1450 public function tabletbody_close() {
1451 $this->doc
.= DOKU_TAB
.'</tbody>'.DOKU_LF
;
1455 * Open a table footer
1457 public function tabletfoot_open() {
1458 $this->doc
.= DOKU_TAB
.'<tfoot>'.DOKU_LF
;
1462 * Close a table footer
1464 public function tabletfoot_close() {
1465 $this->doc
.= DOKU_TAB
.'</tfoot>'.DOKU_LF
;
1471 * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
1473 public function tablerow_open($classes = null) {
1474 // initialize the cell counter used for classes
1475 $this->_counter
['cell_counter'] = 0;
1476 $class = 'row'.$this->_counter
['row_counter']++
;
1477 if($classes !== null) {
1478 if(is_array($classes)) $classes = join(' ', $classes);
1479 $class .= ' ' . $classes;
1481 $this->doc
.= DOKU_TAB
.'<tr class="'.$class.'">'.DOKU_LF
.DOKU_TAB
.DOKU_TAB
;
1487 public function tablerow_close() {
1488 $this->doc
.= DOKU_LF
.DOKU_TAB
.'</tr>'.DOKU_LF
;
1492 * Open a table header cell
1494 * @param int $colspan
1495 * @param string $align left|center|right
1496 * @param int $rowspan
1497 * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
1499 public function tableheader_open($colspan = 1, $align = null, $rowspan = 1, $classes = null) {
1500 $class = 'class="col'.$this->_counter
['cell_counter']++
;
1501 if(!is_null($align)) {
1502 $class .= ' '.$align.'align';
1504 if($classes !== null) {
1505 if(is_array($classes)) $classes = join(' ', $classes);
1506 $class .= ' ' . $classes;
1509 $this->doc
.= '<th '.$class;
1511 $this->_counter
['cell_counter'] +
= $colspan - 1;
1512 $this->doc
.= ' colspan="'.$colspan.'"';
1515 $this->doc
.= ' rowspan="'.$rowspan.'"';
1521 * Close a table header cell
1523 public function tableheader_close() {
1524 $this->doc
.= '</th>';
1530 * @param int $colspan
1531 * @param string $align left|center|right
1532 * @param int $rowspan
1533 * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
1535 public function tablecell_open($colspan = 1, $align = null, $rowspan = 1, $classes = null) {
1536 $class = 'class="col'.$this->_counter
['cell_counter']++
;
1537 if(!is_null($align)) {
1538 $class .= ' '.$align.'align';
1540 if($classes !== null) {
1541 if(is_array($classes)) $classes = join(' ', $classes);
1542 $class .= ' ' . $classes;
1545 $this->doc
.= '<td '.$class;
1547 $this->_counter
['cell_counter'] +
= $colspan - 1;
1548 $this->doc
.= ' colspan="'.$colspan.'"';
1551 $this->doc
.= ' rowspan="'.$rowspan.'"';
1557 * Close a table cell
1559 public function tablecell_close() {
1560 $this->doc
.= '</td>';
1564 * Returns the current header level.
1565 * (required e.g. by the filelist plugin)
1567 * @return int The current header level
1569 public function getLastlevel() {
1570 return $this->lastlevel
;
1573 #region Utility functions
1578 * Assembles all parts defined in $link returns HTML for the link
1580 * @param array $link attributes of a link
1583 * @author Andreas Gohr <andi@splitbrain.org>
1585 public function _formatLink($link) {
1586 //make sure the url is XHTML compliant (skip mailto)
1587 if(substr($link['url'], 0, 7) != 'mailto:') {
1588 $link['url'] = str_replace('&', '&', $link['url']);
1589 $link['url'] = str_replace('&amp;', '&', $link['url']);
1591 //remove double encodings in titles
1592 $link['title'] = str_replace('&amp;', '&', $link['title']);
1594 // be sure there are no bad chars in url or title
1595 // (we can't do this for name because it can contain an img tag)
1596 $link['url'] = strtr($link['url'], array('>' => '%3E', '<' => '%3C', '"' => '%22'));
1597 $link['title'] = strtr($link['title'], array('>' => '>', '<' => '<', '"' => '"'));
1600 $ret .= $link['pre'];
1601 $ret .= '<a href="'.$link['url'].'"';
1602 if(!empty($link['class'])) $ret .= ' class="'.$link['class'].'"';
1603 if(!empty($link['target'])) $ret .= ' target="'.$link['target'].'"';
1604 if(!empty($link['title'])) $ret .= ' title="'.$link['title'].'"';
1605 if(!empty($link['style'])) $ret .= ' style="'.$link['style'].'"';
1606 if(!empty($link['rel'])) $ret .= ' rel="'.trim($link['rel']).'"';
1607 if(!empty($link['more'])) $ret .= ' '.$link['more'];
1609 $ret .= $link['name'];
1611 $ret .= $link['suf'];
1616 * Renders internal and external media
1618 * @author Andreas Gohr <andi@splitbrain.org>
1619 * @param string $src media ID
1620 * @param string $title descriptive text
1621 * @param string $align left|center|right
1622 * @param int $width width of media in pixel
1623 * @param int $height height of media in pixel
1624 * @param string $cache cache|recache|nocache
1625 * @param bool $render should the media be embedded inline or just linked
1628 public function _media($src, $title = null, $align = null, $width = null,
1629 $height = null, $cache = null, $render = true) {
1633 list($ext, $mime) = mimetype($src);
1634 if(substr($mime, 0, 5) == 'image') {
1635 // first get the $title
1636 if(!is_null($title)) {
1637 $title = $this->_xmlEntities($title);
1638 } elseif($ext == 'jpg' ||
$ext == 'jpeg') {
1639 //try to use the caption from IPTC/EXIF
1640 require_once(DOKU_INC
.'inc/JpegMeta.php');
1641 $jpeg = new JpegMeta(mediaFN($src));
1642 if($jpeg !== false) $cap = $jpeg->getTitle();
1644 $title = $this->_xmlEntities($cap);
1648 // if the picture is not supposed to be rendered
1649 // return the title of the picture
1650 if($title === null ||
$title === "") {
1651 // just show the sourcename
1652 $title = $this->_xmlEntities(\dokuwiki\Utf8\PhpString
::basename(noNS($src)));
1657 $ret .= '<img src="' . ml(
1660 'w' => $width, 'h' => $height,
1662 'rev' => $this->_getLastMediaRevisionAt($src)
1665 $ret .= ' class="media'.$align.'"';
1668 $ret .= ' title="'.$title.'"';
1669 $ret .= ' alt="'.$title.'"';
1674 if(!is_null($width))
1675 $ret .= ' width="'.$this->_xmlEntities($width).'"';
1677 if(!is_null($height))
1678 $ret .= ' height="'.$this->_xmlEntities($height).'"';
1682 } elseif(media_supportedav($mime, 'video') ||
media_supportedav($mime, 'audio')) {
1683 // first get the $title
1684 $title = !is_null($title) ?
$title : false;
1686 // if the file is not supposed to be rendered
1687 // return the title of the file (just the sourcename if there is no title)
1688 return $this->_xmlEntities($title ?
$title : \dokuwiki\Utf8\PhpString
::basename(noNS($src)));
1692 $att['class'] = "media$align";
1694 $att['title'] = $title;
1697 if(media_supportedav($mime, 'video')) {
1699 $ret .= $this->_video($src, $width, $height, $att);
1701 if(media_supportedav($mime, 'audio')) {
1703 $ret .= $this->_audio($src, $att);
1706 } elseif($mime == 'application/x-shockwave-flash') {
1708 // if the flash is not supposed to be rendered
1709 // return the title of the flash
1711 // just show the sourcename
1712 $title = \dokuwiki\Utf8\PhpString
::basename(noNS($src));
1714 return $this->_xmlEntities($title);
1718 $att['class'] = "media$align";
1719 if($align == 'right') $att['align'] = 'right';
1720 if($align == 'left') $att['align'] = 'left';
1721 $ret .= html_flashobject(
1722 ml($src, array('cache' => $cache), true, '&'), $width, $height,
1723 array('quality' => 'high'),
1726 $this->_xmlEntities($title)
1729 // well at least we have a title to display
1730 $ret .= $this->_xmlEntities($title);
1732 // just show the sourcename
1733 $ret .= $this->_xmlEntities(\dokuwiki\Utf8\PhpString
::basename(noNS($src)));
1740 * Escape string for output
1745 public function _xmlEntities($string) {
1746 return htmlspecialchars($string, ENT_QUOTES
, 'UTF-8');
1752 * Construct a title and handle images in titles
1754 * @author Harry Fuecks <hfuecks@gmail.com>
1755 * @param string|array $title either string title or media array
1756 * @param string $default default title if nothing else is found
1757 * @param bool $isImage will be set to true if it's a media file
1758 * @param null|string $id linked page id (used to extract title from first heading)
1759 * @param string $linktype content|navigation
1760 * @return string HTML of the title, might be full image tag or just escaped text
1762 public function _getLinkTitle($title, $default, &$isImage, $id = null, $linktype = 'content') {
1764 if(is_array($title)) {
1766 return $this->_imageTitle($title);
1767 } elseif(is_null($title) ||
trim($title) == '') {
1768 if(useHeading($linktype) && $id) {
1769 $heading = p_get_first_heading($id);
1770 if(!blank($heading)) {
1771 return $this->_xmlEntities($heading);
1774 return $this->_xmlEntities($default);
1776 return $this->_xmlEntities($title);
1781 * Returns HTML code for images used in link titles
1783 * @author Andreas Gohr <andi@splitbrain.org>
1785 * @return string HTML img tag or similar
1787 public function _imageTitle($img) {
1790 // some fixes on $img['src']
1791 // see internalmedia() and externalmedia()
1792 list($img['src']) = explode('#', $img['src'], 2);
1793 if($img['type'] == 'internalmedia') {
1794 resolve_mediaid(getNS($ID), $img['src'], $exists ,$this->date_at
, true);
1797 return $this->_media(
1808 * helperfunction to return a basic link to a media
1810 * used in internalmedia() and externalmedia()
1812 * @author Pierre Spring <pierre.spring@liip.ch>
1813 * @param string $src media ID
1814 * @param string $title descriptive text
1815 * @param string $align left|center|right
1816 * @param int $width width of media in pixel
1817 * @param int $height height of media in pixel
1818 * @param string $cache cache|recache|nocache
1819 * @param bool $render should the media be embedded inline or just linked
1820 * @return array associative array with link config
1822 public function _getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render) {
1826 $link['class'] = 'media';
1827 $link['style'] = '';
1831 $link['target'] = $conf['target']['media'];
1832 if($conf['target']['media']) $link['rel'] = 'noopener';
1833 $link['title'] = $this->_xmlEntities($src);
1834 $link['name'] = $this->_media($src, $title, $align, $width, $height, $cache, $render);
1840 * Embed video(s) in HTML
1842 * @author Anika Henke <anika@selfthinker.org>
1843 * @author Schplurtz le Déboulonné <Schplurtz@laposte.net>
1845 * @param string $src - ID of video to embed
1846 * @param int $width - width of the video in pixels
1847 * @param int $height - height of the video in pixels
1848 * @param array $atts - additional attributes for the <video> tag
1851 public function _video($src, $width, $height, $atts = null) {
1852 // prepare width and height
1853 if(is_null($atts)) $atts = array();
1854 $atts['width'] = (int) $width;
1855 $atts['height'] = (int) $height;
1856 if(!$atts['width']) $atts['width'] = 320;
1857 if(!$atts['height']) $atts['height'] = 240;
1862 $isExternal = media_isexternal($src);
1865 // take direct source for external files
1866 list(/*ext*/, $srcMime) = mimetype($src);
1867 $files[$srcMime] = $src;
1869 // prepare alternative formats
1870 $extensions = array('webm', 'ogv', 'mp4');
1871 $files = media_alternativefiles($src, $extensions);
1872 $poster = media_alternativefiles($src, array('jpg', 'png'));
1873 $tracks = media_trackfiles($src);
1874 if(!empty($poster)) {
1875 $posterUrl = ml(reset($poster), '', true, '&');
1881 $out .= '<video '.buildAttributes($atts).' controls="controls"';
1882 if($posterUrl) $out .= ' poster="'.hsc($posterUrl).'"';
1886 // output source for each alternative video format
1887 foreach($files as $mime => $file) {
1890 $linkType = 'externalmedia';
1892 $url = ml($file, '', true, '&');
1893 $linkType = 'internalmedia';
1895 $title = $atts['title'] ?
$atts['title'] : $this->_xmlEntities(\dokuwiki\Utf8\PhpString
::basename(noNS($file)));
1897 $out .= '<source src="'.hsc($url).'" type="'.$mime.'" />'.NL
;
1898 // alternative content (just a link to the file)
1899 $fallback .= $this->$linkType(
1906 $linking = 'linkonly',
1911 // output each track if any
1912 foreach( $tracks as $trackid => $info ) {
1913 list( $kind, $srclang ) = array_map( 'hsc', $info );
1914 $out .= "<track kind=\"$kind\" srclang=\"$srclang\" ";
1915 $out .= "label=\"$srclang\" ";
1916 $out .= 'src="'.ml($trackid, '', true).'">'.NL
;
1921 $out .= '</video>'.NL
;
1926 * Embed audio in HTML
1928 * @author Anika Henke <anika@selfthinker.org>
1930 * @param string $src - ID of audio to embed
1931 * @param array $atts - additional attributes for the <audio> tag
1934 public function _audio($src, $atts = array()) {
1936 $isExternal = media_isexternal($src);
1939 // take direct source for external files
1940 list(/*ext*/, $srcMime) = mimetype($src);
1941 $files[$srcMime] = $src;
1943 // prepare alternative formats
1944 $extensions = array('ogg', 'mp3', 'wav');
1945 $files = media_alternativefiles($src, $extensions);
1950 $out .= '<audio '.buildAttributes($atts).' controls="controls">'.NL
;
1953 // output source for each alternative audio format
1954 foreach($files as $mime => $file) {
1957 $linkType = 'externalmedia';
1959 $url = ml($file, '', true, '&');
1960 $linkType = 'internalmedia';
1962 $title = $atts['title'] ?
$atts['title'] : $this->_xmlEntities(\dokuwiki\Utf8\PhpString
::basename(noNS($file)));
1964 $out .= '<source src="'.hsc($url).'" type="'.$mime.'" />'.NL
;
1965 // alternative content (just a link to the file)
1966 $fallback .= $this->$linkType(
1973 $linking = 'linkonly',
1980 $out .= '</audio>'.NL
;
1985 * _getLastMediaRevisionAt is a helperfunction to internalmedia() and _media()
1986 * which returns an existing media revision less or equal to rev or date_at
1989 * @param string $media_id
1991 * @return string revision ('' for current)
1993 protected function _getLastMediaRevisionAt($media_id){
1994 if(!$this->date_at ||
media_isexternal($media_id)) return '';
1995 $pagelog = new MediaChangeLog($media_id);
1996 return $pagelog->getLastRevisionAt($this->date_at
);
2002 //Setup VIM: ex: et ts=4 :