xref: /plugin/nodetailsxhtml/renderer.php (revision 4a5dbf65826a4aeaf0c2535a8a937614ffbc0829)
1b07cf47aSGerry Weißbach<?php
2b07cf47aSGerry Weißbach/**
3b07cf47aSGerry Weißbach * Render Plugin for XHTML  without details link for internal images.
4b07cf47aSGerry Weißbach *
5b07cf47aSGerry Weißbach * @author i-net software <tools@inetsoftware.de>
6b07cf47aSGerry Weißbach */
7b07cf47aSGerry Weißbach
8b07cf47aSGerry Weißbachif(!defined('DOKU_INC')) die();
9b07cf47aSGerry Weißbachif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
10b07cf47aSGerry Weißbach
11b07cf47aSGerry Weißbachrequire_once DOKU_INC . 'inc/parser/xhtml.php';
12b07cf47aSGerry Weißbach
13b07cf47aSGerry Weißbach/**
14b07cf47aSGerry Weißbach * The Renderer
15b07cf47aSGerry Weißbach */
16b07cf47aSGerry Weißbachclass renderer_plugin_nodetailsxhtml extends Doku_Renderer_xhtml {
17b07cf47aSGerry Weißbach
18a6f86da8SGerry Weißbach    private $acronymsExchanged = null;
19a6f86da8SGerry Weißbach    private $hasSeenHeader = false;
20a6f86da8SGerry Weißbach    private $scriptmode = false;
21b07cf47aSGerry Weißbach
22a6f86da8SGerry Weißbach    private $startlevel = 0; // level to start with numbered headings (default = 2)
23a6f86da8SGerry Weißbach    private $levels = array(        '======'=>1,
24b07cf47aSGerry Weißbach                                    '====='=>2,
25b07cf47aSGerry Weißbach                                    '===='=>3,
26b07cf47aSGerry Weißbach                                    '==='=>4,
27a6f86da8SGerry Weißbach                                    '=='=>5
28a6f86da8SGerry Weißbach    );
29b07cf47aSGerry Weißbach
30a6f86da8SGerry Weißbach    public $sectionLevel = 0;
31a6f86da8SGerry Weißbach    public $info = array(
32b07cf47aSGerry Weißbach                                    'cache'      => true, // may the rendered result cached?
33b07cf47aSGerry Weißbach                                    'toc'        => true, // render the TOC?
34b07cf47aSGerry Weißbach                                    'forceTOC'   => false, // shall I force the TOC?
35b07cf47aSGerry Weißbach                                    'scriptmode' => false, // In scriptmode, some tags will not be encoded => '<%', '%>'
36b07cf47aSGerry Weißbach    );
37b07cf47aSGerry Weißbach
38a6f86da8SGerry Weißbach    public $headingCount = array(   1=>0,
39b07cf47aSGerry Weißbach                                    2=>0,
40b07cf47aSGerry Weißbach                                    3=>0,
41b07cf47aSGerry Weißbach                                    4=>0,
42a6f86da8SGerry Weißbach                                    5=>0
43a6f86da8SGerry Weißbach    );
44b07cf47aSGerry Weißbach
45b07cf47aSGerry Weißbach    /**
46b07cf47aSGerry Weißbach     * return some info
47b07cf47aSGerry Weißbach     */
48b07cf47aSGerry Weißbach    function getInfo(){
49b5315745SGerry Weißbach        return confToHash(dirname(__FILE__).'/plugin.info.txt');
50b07cf47aSGerry Weißbach    }
51b07cf47aSGerry Weißbach
52b07cf47aSGerry Weißbach    function canRender($format) {
53b07cf47aSGerry Weißbach        return ($format=='xhtml');
54b07cf47aSGerry Weißbach    }
55b07cf47aSGerry Weißbach
56b07cf47aSGerry Weißbach    function document_start() {
570313ced8SGerry Weißbach        global $TOC, $ID, $INFO, $conf;
58b07cf47aSGerry Weißbach
59b07cf47aSGerry Weißbach        parent::document_start();
60b07cf47aSGerry Weißbach
61b07cf47aSGerry Weißbach        // Cheating in again
620313ced8SGerry Weißbach        $meta = p_get_metadata($ID, null, false); // 2010-10-23 This should be save to use
630313ced8SGerry Weißbach
640313ced8SGerry Weißbach        if (isset($meta['toc']['toptoclevel'])) {
650313ced8SGerry Weißbach            $conf['toptoclevel'] = $meta['toc']['toptoclevel'];
660313ced8SGerry Weißbach        }
670313ced8SGerry Weißbach        if (isset($meta['toc']['maxtoclevel'])) {
680313ced8SGerry Weißbach            $conf['maxtoclevel'] = $meta['toc']['maxtoclevel'];
690313ced8SGerry Weißbach        }
700313ced8SGerry Weißbach        if (isset($meta['toc']['toptoclevel'])||isset($INFO['meta']['toc']['maxtoclevel'])) {
710313ced8SGerry Weißbach            $conf['tocminheads'] = 1;
720313ced8SGerry Weißbach        }
730313ced8SGerry Weißbach
74f92ca477SGerry Weißbach        $newMeta = $meta['description'];
75f92ca477SGerry Weißbach        if ( is_array($newMeta) && !empty( $newMeta['tableofcontents'] ) && count($newMeta['tableofcontents']) > 1 ) {
76b07cf47aSGerry Weißbach            // $TOC = $this->toc = $newMeta; // 2010-08-23 doubled the TOC
77f92ca477SGerry Weißbach            $TOC = $newMeta['tableofcontents'];
78b07cf47aSGerry Weißbach        }
79b07cf47aSGerry Weißbach    }
80b07cf47aSGerry Weißbach
81b07cf47aSGerry Weißbach    function document_end() {
82b07cf47aSGerry Weißbach
83b07cf47aSGerry Weißbach        parent::document_end();
84b07cf47aSGerry Weißbach
85b07cf47aSGerry Weißbach        // Prepare the TOC
86b07cf47aSGerry Weißbach        global $TOC, $ID;
87b07cf47aSGerry Weißbach        $meta = array();
880313ced8SGerry Weißbach
890313ced8SGerry Weißbach        $forceToc = $this->info['forceTOC'] || p_get_metadata($ID, 'internal forceTOC', false);
90b07cf47aSGerry Weißbach
91b07cf47aSGerry Weißbach        // NOTOC, and no forceTOC
92b84893f2SGerry Weißbach        if ( $this->info['toc'] === false && !$forceToc ) {
93b07cf47aSGerry Weißbach            $TOC = $this->toc = array();
94b07cf47aSGerry Weißbach            $meta['internal']['toc'] = false;
95b07cf47aSGerry Weißbach            $meta['description']['tableofcontents'] = array();
960313ced8SGerry Weißbach            $meta['internal']['forceTOC'] = false;
97b07cf47aSGerry Weißbach
98*4a5dbf65SGerry Weißbach        } else if ( $forceToc || ($this->utf8_strlen(strip_tags($this->doc)) >= $this->getConf('documentlengthfortoc') && count($this->toc) > 1 ) ) {
99b07cf47aSGerry Weißbach            $TOC = $this->toc;
100b07cf47aSGerry Weißbach            // This is a little bit like cheating ... but this will force the TOC into the metadata
101b07cf47aSGerry Weißbach            $meta = array();
102b07cf47aSGerry Weißbach            $meta['internal']['toc'] = true;
1030313ced8SGerry Weißbach            $meta['internal']['forceTOC'] = $forceToc;
104b07cf47aSGerry Weißbach            $meta['description']['tableofcontents'] = $TOC;
105b07cf47aSGerry Weißbach        }
106b07cf47aSGerry Weißbach
107b07cf47aSGerry Weißbach        // allways write new metadata
108b07cf47aSGerry Weißbach        p_set_metadata($ID, $meta);
109b07cf47aSGerry Weißbach
110b07cf47aSGerry Weißbach        // make sure there are no empty blocks
111b279fde7SGerry Weißbach        $this->doc = preg_replace('#<(div|section|article) class="[^"]*?level\d[^"]*?">\s*</\1>#','',$this->doc);
112b07cf47aSGerry Weißbach    }
113b07cf47aSGerry Weißbach
114*4a5dbf65SGerry Weißbach    private function utf8_strlen( $input ) {
115*4a5dbf65SGerry Weißbach        if ( class_exists('dokuwiki\Utf8\PhpString') ) {
116*4a5dbf65SGerry Weißbach            return dokuwiki\Utf8\PhpString::strlen( $input );
117*4a5dbf65SGerry Weißbach        } else {
118*4a5dbf65SGerry Weißbach            return utf8_strlen( $input );
119*4a5dbf65SGerry Weißbach        }
120*4a5dbf65SGerry Weißbach    }
121*4a5dbf65SGerry Weißbach
1222a003d8aSGerry Weißbach    function header($text, $level, $pos, $returnonly = false) {
123b07cf47aSGerry Weißbach        global $conf;
124b07cf47aSGerry Weißbach        global $ID;
125b07cf47aSGerry Weißbach        global $INFO;
126b07cf47aSGerry Weißbach
127b07cf47aSGerry Weißbach        if($text) {
12865523c28SGerry Weißbach
12965523c28SGerry Weißbach            // Check Text for hint about a CSS style class
13065523c28SGerry Weißbach            $class = "";
13165523c28SGerry Weißbach            if ( preg_match("/^class:(.*?)>(.*?)$/", $text, $matches) ) {
13265523c28SGerry Weißbach                $class = ' ' . $this->_xmlEntities($matches[1]);
13365523c28SGerry Weißbach                $text = $matches[2];
13465523c28SGerry Weißbach            }
13565523c28SGerry Weißbach
136b07cf47aSGerry Weißbach            /* There should be no class for "sectioneditX" if there is no edit perm */
137b07cf47aSGerry Weißbach            $maxLevel = $conf['maxseclevel'];
138b07cf47aSGerry Weißbach            if ( $INFO['perm'] <= AUTH_READ )
139b07cf47aSGerry Weißbach            {
140b07cf47aSGerry Weißbach                $conf['maxseclevel'] = 0;
141b07cf47aSGerry Weißbach            }
142b07cf47aSGerry Weißbach
143b07cf47aSGerry Weißbach            $headingNumber = '';
144b07cf47aSGerry Weißbach            $useNumbered = p_get_metadata($ID, 'usenumberedheading', true); // 2011-02-07 This should be save to use
145b07cf47aSGerry Weißbach            if ( $this->getConf('usenumberedheading') || !empty($useNumbered) || !empty($INFO['meta']['usenumberedheading']) || isset($_REQUEST['usenumberedheading'])) {
146b07cf47aSGerry Weißbach
147b07cf47aSGerry Weißbach                // increment the number of the heading
148b07cf47aSGerry Weißbach                $this->headingCount[$level]++;
149b07cf47aSGerry Weißbach
150b07cf47aSGerry Weißbach                // build the actual number
151b07cf47aSGerry Weißbach                for ($i=1;$i<=5;$i++) {
152b07cf47aSGerry Weißbach
153b07cf47aSGerry Weißbach                    // reset the number of the subheadings
154b07cf47aSGerry Weißbach                    if ($i>$level) {
155b07cf47aSGerry Weißbach                        $this->headingCount[$i] = 0;
156b07cf47aSGerry Weißbach                    }
157b07cf47aSGerry Weißbach
158b07cf47aSGerry Weißbach                    // build the number of the heading
159b07cf47aSGerry Weißbach                    $headingNumber .= $this->headingCount[$i] . '.';
160b07cf47aSGerry Weißbach                }
161b07cf47aSGerry Weißbach
162b07cf47aSGerry Weißbach                $headingNumber = preg_replace("/(\.0)+\.?$/", '', $headingNumber) . ' ';
163b07cf47aSGerry Weißbach            }
164b07cf47aSGerry Weißbach
16565523c28SGerry Weißbach            $doc = $this->doc;
16665523c28SGerry Weißbach            $this->doc = "";
1670313ced8SGerry Weißbach
168f350675fSGerry Weißbach            parent::header($headingNumber . $text, $level, $pos);
16965523c28SGerry Weißbach
170854239fdSGerry Weißbach            if ( $this->getConf('useHeadAnchorInsteadOfHeaderID') ) {
171a3de832aSGerry Weißbach                $matches = [];
172a3de832aSGerry Weißbach                preg_match("/id=\"(.*?)\"/", $this->doc, $matches);
173a3de832aSGerry Weißbach                if ( count($matches) > 1 ) {
174a3de832aSGerry Weißbach                    $this->doc = preg_replace("/id=\".*?\"/", '', $this->doc);
175a3de832aSGerry Weißbach                    $this->doc = DOKU_LF.'<a id="'. $matches[1] .'" class="head-anchor" style="visibility:hidden"></a>'.DOKU_LF . $this->doc;
176a3de832aSGerry Weißbach                }
177854239fdSGerry Weißbach            }
178a3de832aSGerry Weißbach
17915337681SGerry Weißbach            if ( $this->getConf('useSectionArticle') ) {
18065523c28SGerry Weißbach                $this->doc = $doc . preg_replace("/(<h([1-9]))/", "<".($this->sectionLevel<1?'section':'article')." class=\"level\\2{$class}\">\\1", $this->doc);
18115337681SGerry Weißbach            } else {
18215337681SGerry Weißbach                $this->doc = $doc . $this->doc;
18315337681SGerry Weißbach            }
18465523c28SGerry Weißbach
185f350675fSGerry Weißbach            $conf['maxseclevel'] = $maxLevel;
186b07cf47aSGerry Weißbach
187b07cf47aSGerry Weißbach        } else if ( $INFO['perm'] > AUTH_READ ) {
188b07cf47aSGerry Weißbach
189b07cf47aSGerry Weißbach            if ( $hasSeenHeader ) $this->finishSectionEdit($pos);
190b07cf47aSGerry Weißbach
191b07cf47aSGerry Weißbach            // write the header
192c569f51dSGerry Weißbach            $name = $this->startSectionEdit($pos, array( 'target' => 'section_empty', 'name' => rand() . $level));
19315337681SGerry Weißbach            if ( $this->getConf('useSectionArticle') ) {
19465523c28SGerry Weißbach                $this->doc .= '<'.($this->sectionLevel<1?'section':'article').' class="'.$name.'">';
19515337681SGerry Weißbach            }
19665523c28SGerry Weißbach
19765523c28SGerry Weißbach            $this->doc .= DOKU_LF.'<a name="'. $name .'" class="' . $name . '" ></a>'.DOKU_LF;
198b07cf47aSGerry Weißbach        }
199b07cf47aSGerry Weißbach
200b07cf47aSGerry Weißbach        $hasSeenHeader = true;
201b07cf47aSGerry Weißbach    }
202b07cf47aSGerry Weißbach
203e3af976dSGerry Weißbach    public function finishSectionEdit($end = null, $hid = null) {
204b07cf47aSGerry Weißbach        global $INFO;
205b07cf47aSGerry Weißbach        if ( $INFO['perm'] > AUTH_READ )
206b07cf47aSGerry Weißbach        {
2070ad3e6f7Si-net /// software            return parent::finishSectionEdit($end, $hid);
208b07cf47aSGerry Weißbach        }
209b07cf47aSGerry Weißbach    }
210b07cf47aSGerry Weißbach
211b5315745SGerry Weißbach    public function startSectionEdit($start, $data) {
212b07cf47aSGerry Weißbach        global $INFO;
213b07cf47aSGerry Weißbach        if ( $INFO['perm'] > AUTH_READ )
214b07cf47aSGerry Weißbach        {
215b5315745SGerry Weißbach            return parent::startSectionEdit($start, $data);
216b07cf47aSGerry Weißbach        }
217b07cf47aSGerry Weißbach
218b07cf47aSGerry Weißbach        return "";
219b07cf47aSGerry Weißbach    }
220b07cf47aSGerry Weißbach
22165523c28SGerry Weißbach    function section_close() {
22265523c28SGerry Weißbach        $this->sectionLevel--;
22315337681SGerry Weißbach        $this->doc .= DOKU_LF.'</div>'.DOKU_LF;
22415337681SGerry Weißbach        if ( $this->getConf('useSectionArticle') ) {
22515337681SGerry Weißbach            $this->doc .= '</'.($this->sectionLevel<1?'section':'article').'>'.DOKU_LF;
22615337681SGerry Weißbach        }
22765523c28SGerry Weißbach    }
22865523c28SGerry Weißbach
22965523c28SGerry Weißbach    function section_open($level) {
23065523c28SGerry Weißbach        $this->sectionLevel++;
23165523c28SGerry Weißbach        return parent::section_open($level);
23265523c28SGerry Weißbach    }
23365523c28SGerry Weißbach
234b84893f2SGerry Weißbach    function internalmedia ($src, $title=null, $align=null, $width=null,
235b84893f2SGerry Weißbach                            $height=null, $cache=null, $linking=null, $return=NULL) {
236b07cf47aSGerry Weißbach        global $ID;
237b07cf47aSGerry Weißbach        list($src,$hash) = explode('#',$src,2);
2389d34047fSGerry Weißbach
2399d34047fSGerry Weißbach        if ( class_exists('dokuwiki\File\MediaResolver') ) {
2409d34047fSGerry Weißbach            $src = (new dokuwiki\File\MediaResolver($ID))->resolveId($src);
2419d34047fSGerry Weißbach        } else {
242b07cf47aSGerry Weißbach            resolve_mediaid(getNS($ID),$src, $exists);
2439d34047fSGerry Weißbach        }
244b07cf47aSGerry Weißbach
245b07cf47aSGerry Weißbach        $noLink = false;
246b07cf47aSGerry Weißbach        $render = ($linking == 'linkonly') ? false : true;
247b07cf47aSGerry Weißbach        $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
248b07cf47aSGerry Weißbach
249b07cf47aSGerry Weißbach        list($ext,$mime,$dl) = mimetype($src);
250b07cf47aSGerry Weißbach        if(substr($mime,0,5) == 'image' && $render){
251b07cf47aSGerry Weißbach            $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),($linking=='direct'));
252b07cf47aSGerry Weißbach            if ( substr($mime,0,5) == 'image' && $linking='details' ) { $noLink = true;}
253b07cf47aSGerry Weißbach        }elseif($mime == 'application/x-shockwave-flash' && $render){
254b07cf47aSGerry Weißbach            // don't link flash movies
255b07cf47aSGerry Weißbach            $noLink = true;
256b07cf47aSGerry Weißbach        }else{
257b07cf47aSGerry Weißbach            // add file icons
258b07cf47aSGerry Weißbach            $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
259b07cf47aSGerry Weißbach            $link['class'] .= ' mediafile mf_'.$class;
260b07cf47aSGerry Weißbach            $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),true);
261b07cf47aSGerry Weißbach        }
262b07cf47aSGerry Weißbach
263b07cf47aSGerry Weißbach        if($hash) $link['url'] .= '#'.$hash;
264b07cf47aSGerry Weißbach
265b07cf47aSGerry Weißbach        //markup non existing files
266b07cf47aSGerry Weißbach        if (!$exists)
267b07cf47aSGerry Weißbach        $link['class'] .= ' wikilink2';
268b07cf47aSGerry Weißbach
269b07cf47aSGerry Weißbach        //output formatted
270b07cf47aSGerry Weißbach        if ($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
271b07cf47aSGerry Weißbach        else $this->doc .= $this->_formatLink($link);
272b07cf47aSGerry Weißbach    }
273b07cf47aSGerry Weißbach
274b07cf47aSGerry Weißbach    /**
275b07cf47aSGerry Weißbach     * Render an internal Wiki Link
276b07cf47aSGerry Weißbach     *
277b07cf47aSGerry Weißbach     * $search,$returnonly & $linktype are not for the renderer but are used
278b07cf47aSGerry Weißbach     * elsewhere - no need to implement them in other renderers
279b07cf47aSGerry Weißbach     *
280b07cf47aSGerry Weißbach     * @author Andreas Gohr <andi@splitbrain.org>
281b07cf47aSGerry Weißbach     */
282b07cf47aSGerry Weißbach    function internallink($id, $name = null, $search=null,$returnonly=false,$linktype='content') {
283b07cf47aSGerry Weißbach        global $conf;
284b07cf47aSGerry Weißbach        global $ID;
285b07cf47aSGerry Weißbach        global $INFO;
286b07cf47aSGerry Weißbach
287b07cf47aSGerry Weißbach        $params = '';
288b07cf47aSGerry Weißbach        $parts = explode('?', $id, 2);
289b07cf47aSGerry Weißbach        if (count($parts) === 2) {
290b07cf47aSGerry Weißbach            $id = $parts[0];
291b07cf47aSGerry Weißbach            $params = $parts[1];
292b07cf47aSGerry Weißbach        }
293b07cf47aSGerry Weißbach
294b07cf47aSGerry Weißbach        // For empty $id we need to know the current $ID
295b07cf47aSGerry Weißbach        // We need this check because _simpleTitle needs
296b07cf47aSGerry Weißbach        // correct $id and resolve_pageid() use cleanID($id)
297b07cf47aSGerry Weißbach        // (some things could be lost)
298b07cf47aSGerry Weißbach        if ($id === '') {
299b07cf47aSGerry Weißbach            $id = $ID;
300b07cf47aSGerry Weißbach        }
301b07cf47aSGerry Weißbach
302b07cf47aSGerry Weißbach        // default name is based on $id as given
303b07cf47aSGerry Weißbach        $default = $this->_simpleTitle($id);
304b07cf47aSGerry Weißbach
305b07cf47aSGerry Weißbach        // now first resolve and clean up the $id
3069d34047fSGerry Weißbach        if ( class_exists('dokuwiki\File\PageResolver') ) {
3079d34047fSGerry Weißbach            $id = (new dokuwiki\File\PageResolver($ID))->resolveId($id);
3089d34047fSGerry Weißbach        } else {
309b07cf47aSGerry Weißbach            resolve_pageid(getNS($ID),$id,$exists);
3109d34047fSGerry Weißbach        }
311b07cf47aSGerry Weißbach
312b07cf47aSGerry Weißbach        $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype);
313b07cf47aSGerry Weißbach        if ( !$isImage ) {
314b07cf47aSGerry Weißbach            if ( $exists ) {
315b07cf47aSGerry Weißbach                $class='wikilink1';
316b07cf47aSGerry Weißbach            } else {
317b07cf47aSGerry Weißbach                $class='wikilink2';
318b07cf47aSGerry Weißbach                $link['rel']='nofollow';
319b07cf47aSGerry Weißbach            }
320b07cf47aSGerry Weißbach        } else {
321b07cf47aSGerry Weißbach            $class='media';
322b07cf47aSGerry Weißbach        }
323b07cf47aSGerry Weißbach
324b07cf47aSGerry Weißbach        //keep hash anchor
325b07cf47aSGerry Weißbach        list($id,$hash) = explode('#',$id,2);
326b07cf47aSGerry Weißbach        if(!empty($hash)) $hash = $this->_headerToLink($hash);
327b07cf47aSGerry Weißbach
328b07cf47aSGerry Weißbach        //prepare for formating
329b07cf47aSGerry Weißbach        $link['target'] = $conf['target']['wiki'];
330b07cf47aSGerry Weißbach        $link['style']  = '';
331b07cf47aSGerry Weißbach        $link['pre']    = '';
332b07cf47aSGerry Weißbach        $link['suf']    = '';
333b07cf47aSGerry Weißbach        // highlight link to current page
334b07cf47aSGerry Weißbach        if ($id == $INFO['id']) {
335b07cf47aSGerry Weißbach            $link['pre']    = '<span class="curid">';
336b07cf47aSGerry Weißbach            $link['suf']    = '</span>';
337b07cf47aSGerry Weißbach        }
338b07cf47aSGerry Weißbach        $link['more']   = '';
339b07cf47aSGerry Weißbach        $link['class']  = $class;
340b07cf47aSGerry Weißbach        $link['url']    = wl($id, $params);
341b07cf47aSGerry Weißbach        $link['name']   = $name;
342b07cf47aSGerry Weißbach        $link['title']  = $this->_getLinkTitle(null, $default, $isImage, $id, $linktype);
343b07cf47aSGerry Weißbach        //add search string
344b07cf47aSGerry Weißbach        if($search){
345b07cf47aSGerry Weißbach            ($conf['userewrite']) ? $link['url'].='?' : $link['url'].='&amp;';
346b07cf47aSGerry Weißbach            if(is_array($search)){
347b07cf47aSGerry Weißbach                $search = array_map('rawurlencode',$search);
348b07cf47aSGerry Weißbach                $link['url'] .= 's[]='.join('&amp;s[]=',$search);
349b07cf47aSGerry Weißbach            }else{
350b07cf47aSGerry Weißbach                $link['url'] .= 's='.rawurlencode($search);
351b07cf47aSGerry Weißbach            }
352b07cf47aSGerry Weißbach        }
353b07cf47aSGerry Weißbach
354b07cf47aSGerry Weißbach        //keep hash
355b07cf47aSGerry Weißbach        if($hash) $link['url'].='#'.$hash;
356b07cf47aSGerry Weißbach
357b07cf47aSGerry Weißbach        //output formatted
358b07cf47aSGerry Weißbach        if($returnonly){
359b07cf47aSGerry Weißbach            return $this->_formatLink($link);
360b07cf47aSGerry Weißbach        }else{
361b07cf47aSGerry Weißbach            $this->doc .= $this->_formatLink($link);
362b07cf47aSGerry Weißbach        }
363b07cf47aSGerry Weißbach    }
364b07cf47aSGerry Weißbach
36512a1f0daSGerry Weißbach    function locallink($hash, $name = NULL, $returnonly = false){
366b07cf47aSGerry Weißbach        global $ID;
367b07cf47aSGerry Weißbach        $name  = $this->_getLinkTitle($name, $hash, $isImage);
368b07cf47aSGerry Weißbach        $hash  = $this->_headerToLink($hash);
369b07cf47aSGerry Weißbach        $title = $name;
370a6f86da8SGerry Weißbach        $this->doc .= '<a href="#'.$hash.'" title="'.$title.'" class="wikilink1">';
371a6f86da8SGerry Weißbach        $this->doc .= $name;
372a6f86da8SGerry Weißbach        $this->doc .= '</a>';
373b07cf47aSGerry Weißbach    }
374b07cf47aSGerry Weißbach
375b07cf47aSGerry Weißbach    function acronym($acronym) {
376b07cf47aSGerry Weißbach
377b07cf47aSGerry Weißbach        if ( empty($this->acronymsExchanged) ) {
378b07cf47aSGerry Weißbach            $this->acronymsExchanged = $this->acronyms;
379b07cf47aSGerry Weißbach            $this->acronyms = array();
380b07cf47aSGerry Weißbach
381b07cf47aSGerry Weißbach            foreach( $this->acronymsExchanged as $key => $value ) {
382b07cf47aSGerry Weißbach                $this->acronyms[str_replace('_', ' ', $key)] = $value;
383b07cf47aSGerry Weißbach            }
384b07cf47aSGerry Weißbach        }
385b07cf47aSGerry Weißbach
386b07cf47aSGerry Weißbach        parent::acronym($acronym);
387b07cf47aSGerry Weißbach    }
388b07cf47aSGerry Weißbach
389b07cf47aSGerry Weißbach    function entity($entity) {
390b07cf47aSGerry Weißbach
391b07cf47aSGerry Weißbach        if ( array_key_exists($entity, $this->entities) ) {
392b07cf47aSGerry Weißbach            $entity = $this->entities[$entity];
393b07cf47aSGerry Weißbach        }
394b07cf47aSGerry Weißbach
395b07cf47aSGerry Weißbach        $this->doc .= $this->_xmlEntities($entity);
396b07cf47aSGerry Weißbach    }
397b07cf47aSGerry Weißbach
398b07cf47aSGerry Weißbach    function _xmlEntities($string) {
399b07cf47aSGerry Weißbach
40065523c28SGerry Weißbach        // No double encode ...
40165523c28SGerry Weißbach        $string = htmlspecialchars($string, ENT_QUOTES, 'UTF-8', false);
40265523c28SGerry Weißbach        // $string = parent::_xmlEntities($string);
403b07cf47aSGerry Weißbach        $string = htmlentities($string, 8, 'UTF-8');
404b07cf47aSGerry Weißbach        $string = $this->superentities($string);
405b07cf47aSGerry Weißbach
406b07cf47aSGerry Weißbach        if ( $this->info['scriptmode'] ) {
407b07cf47aSGerry Weißbach            $string = str_replace(    array( "&lt;%", "%&gt;", "&lt;?", "?&gt;"),
408b07cf47aSGerry Weißbach            array( "<%", "%>", "<?", "?>"),
409b07cf47aSGerry Weißbach            $string);
410b07cf47aSGerry Weißbach        }
411b07cf47aSGerry Weißbach
412b07cf47aSGerry Weißbach        return $string;
413b07cf47aSGerry Weißbach    }
414b07cf47aSGerry Weißbach
415b07cf47aSGerry Weißbach    // Unicode-proof htmlentities.
416b07cf47aSGerry Weißbach    // Returns 'normal' chars as chars and weirdos as numeric html entites.
417b07cf47aSGerry Weißbach    function superentities( $str ){
418b07cf47aSGerry Weißbach        // get rid of existing entities else double-escape
419b84893f2SGerry Weißbach        $str2 = '';
420b07cf47aSGerry Weißbach        $str = html_entity_decode(stripslashes($str),ENT_QUOTES,'UTF-8');
421b07cf47aSGerry Weißbach        $ar = preg_split('/(?<!^)(?!$)(?!\n)/u', $str );  // return array of every multi-byte character
422b07cf47aSGerry Weißbach        foreach ($ar as $c){
423b07cf47aSGerry Weißbach            $o = ord($c);
424b07cf47aSGerry Weißbach            if ( // (strlen($c) > 1) || /* multi-byte [unicode] */
425b07cf47aSGerry Weißbach                ($o > 127) // || /* <- control / latin weirdos -> */
426b07cf47aSGerry Weißbach                // ($o <32 || $o > 126) || /* <- control / latin weirdos -> */
427b07cf47aSGerry Weißbach                // ($o >33 && $o < 40) ||/* quotes + ambersand */
428b07cf47aSGerry Weißbach                // ($o >59 && $o < 63) /* html */
429b07cf47aSGerry Weißbach
430b07cf47aSGerry Weißbach            ) {
431b07cf47aSGerry Weißbach                // convert to numeric entity
432b07cf47aSGerry Weißbach                $c = mb_encode_numericentity($c,array (0x0, 0xffff, 0, 0xffff), 'UTF-8');
433b07cf47aSGerry Weißbach            }
434b07cf47aSGerry Weißbach            $str2 .= $c;
435b07cf47aSGerry Weißbach        }
436b07cf47aSGerry Weißbach        return $str2;
437b07cf47aSGerry Weißbach    }
438f1772eaaSGerry Weißbach
439f1772eaaSGerry Weißbach    /**
440f1772eaaSGerry Weißbach     * Renders internal and external media
441f1772eaaSGerry Weißbach     *
442f1772eaaSGerry Weißbach     * @author Andreas Gohr <andi@splitbrain.org>
443f1772eaaSGerry Weißbach     * @param string $src       media ID
444f1772eaaSGerry Weißbach     * @param string $title     descriptive text
445f1772eaaSGerry Weißbach     * @param string $align     left|center|right
446f1772eaaSGerry Weißbach     * @param int    $width     width of media in pixel
447f1772eaaSGerry Weißbach     * @param int    $height    height of media in pixel
448f1772eaaSGerry Weißbach     * @param string $cache     cache|recache|nocache
449f1772eaaSGerry Weißbach     * @param bool   $render    should the media be embedded inline or just linked
450f1772eaaSGerry Weißbach     * @return string
451f1772eaaSGerry Weißbach     */
452f1772eaaSGerry Weißbach    function _media($src, $title = null, $align = null, $w = null,
453f1772eaaSGerry Weißbach                    $h = null, $cache = null, $render = true) {
454f1772eaaSGerry Weißbach
455f1772eaaSGerry Weißbach        list($ext, $mime) = mimetype($src);
456ef499884SGerry Weißbach        if(substr($mime, 0, 5) == 'image') {
457f1772eaaSGerry Weißbach
458f1772eaaSGerry Weißbach            $info = @getimagesize(mediaFN($src)); //get original size
459ef499884SGerry Weißbach            $srcset = [];
460ef499884SGerry Weißbach
461f1772eaaSGerry Weißbach            if($info !== false) {
4625aaee3e1SGerry Weißbach
463ef499884SGerry Weißbach	            $origWidth = $info[0];
464ef499884SGerry Weißbach	            $origHeight = $info[1];
465ef499884SGerry Weißbach
4665aaee3e1SGerry Weißbach	            if ( !$w && !$h ) $w = $info[0];
467f1772eaaSGerry Weißbach                if(!$h) $h = round(($w * $info[1]) / $info[0]);
468f1772eaaSGerry Weißbach                if(!$w) $w = round(($h * $info[0]) / $info[1]);
469ef499884SGerry Weißbach
470ef499884SGerry Weißbach                // There is a two times image
471673a3048SGerry Weißbach                if ( 2*2/3*$w <= $origWidth ) { // If the image is at least 1.6 times as large ...
472ef499884SGerry Weißbach	                $srcset[] = ml($src, array('w' => 2*$w, 'h' => 2*$h, 'cache' => $cache, 'rev'=>$this->_getLastMediaRevisionAt($src))) . ' 2x';
473ef499884SGerry Weißbach                } else {
474ef499884SGerry Weißbach
475ef499884SGerry Weißbach	                // Check for alternate image
476ef499884SGerry Weißbach	                $ext = strrpos($src, '.');
477ef499884SGerry Weißbach
478f01f7f57SGerry Weißbach                    foreach ( array( '@2x.', '-2x.', '_2x.') as $extension ) {
479f01f7f57SGerry Weißbach    	                $additionalSrc = substr( $src, 0, $ext) . $extension . substr($src, $ext+1);
480ef499884SGerry Weißbach    	                $additionalInfo = @getimagesize(mediaFN($additionalSrc)); //get original size
481ef499884SGerry Weißbach    	                if ( $additionalInfo !== false ) {
482ef499884SGerry Weißbach                            // Image exists
483ef499884SGerry Weißbach                            $srcset[] = ml($additionalSrc, array('w' => 2*$w, 'h' => 2*$h, 'cache' => $cache, 'rev'=>$this->_getLastMediaRevisionAt($srcSetURL))) . ' 2x';
484f01f7f57SGerry Weißbach                            break;
485f01f7f57SGerry Weißbach    	                }
486ef499884SGerry Weißbach                    }
487ef499884SGerry Weißbach                }
488ef499884SGerry Weißbach
489ef499884SGerry Weißbach				$ret = parent::_media($src, $title, $align, $w, $h, $cache, $render);
4908b9e23caSGerry Weißbach                if ( count($srcset) > 0 ) {
491ef499884SGerry Weißbach                    return str_replace("/>", ' srcset="' . implode(',', $srcset) . '" />', $ret );
4928b9e23caSGerry Weißbach                } else {
493ef499884SGerry Weißbach                    return $ret;
4948b9e23caSGerry Weißbach                }
495f1772eaaSGerry Weißbach            }
496b07cf47aSGerry Weißbach        }
497b07cf47aSGerry Weißbach
498f1772eaaSGerry Weißbach        return parent::_media($src, $title, $align, $w, $h, $cache, $render);
499f1772eaaSGerry Weißbach    }
500f1772eaaSGerry Weißbach}
501ef499884SGerry Weißbach
502b07cf47aSGerry Weißbach//Setup VIM: ex: et ts=4 enc=utf-8 :
503