xref: /plugin/mediathumbnails/syntax.php (revision e19533e13ade8665caee6886acf0de2dbdc8883d)
1<?php
2/**
3 * DokuWiki Plugin mediathumbnails (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Thomas Schäfer <thomas.schaefer@itschert.net>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14require('thumbnail.php');
15
16class syntax_plugin_mediathumbnails extends DokuWiki_Syntax_Plugin {
17
18	/**
19     * @return string Syntax mode type
20     */
21    public function getType()
22    {
23        return 'substition';
24    }
25
26    /**
27     * @return string Paragraph type
28     */
29    public function getPType()
30    {
31        return 'normal';
32    }
33
34    /**
35     * @return int Sort order - Low numbers go before high numbers
36     */
37    public function getSort()
38    {
39        return 1;
40    }
41
42    /**
43     * Connect lookup pattern to lexer.
44     *
45     * @param string $mode Parser mode
46     */
47    public function connectTo($mode)
48    {
49		$this->Lexer->addSpecialPattern("{{[ ]*thumbnail>.+?}}", $mode, substr(get_class($this), 7));
50	}
51
52    /**
53     * Handle matches of the mediathumbnails syntax
54     *
55     * @param string       $match   The match of the syntax
56     * @param int          $state   The state of the handler
57     * @param int          $pos     The position in the document
58     * @param Doku_Handler $handler The handler
59     *
60     * @return array Data for the renderer
61     */
62    public function handle($match, $state, $pos, Doku_Handler $handler)
63    {
64        // extract the internal reference from the syntax so it can be handled like a normal media file
65        $internalreference = str_replace("thumbnail>","",$match);//substr($match, 12, -2); //strip markup
66
67        // let dokuwiki core parse the media syntax inside the thumbnail syntax
68		$image_params = Doku_Handler_Parse_Media($internalreference);
69
70		$thumb = new thumbnail($image_params['src'],$this);
71
72        // if source file does not exist, return an array with the second element being null
73        if (!$thumb->getSourceFileExists()) {
74            return array('missing_src_file',$image_params['src'],null);
75        }
76
77        // create thumbnail if missing
78        $thumb->create_if_missing();
79
80		if ($thumb->creation_has_failed()) {
81            // thumbnail creation failed, return an array with the second element being null
82            return array('missing_thumb_file',$thumb->getMediapath(),null);
83        }
84
85        // use the thumbnail's mediapath and the image reference's parameters for rendering
86        $thumbnail_params = $image_params;
87        $thumbnail_params['src'] = $thumb->getMediapath();
88
89		return array($thumb->getSourceFilepath(),$thumb->getMediapath(),$thumbnail_params);
90    }
91
92    /**
93     * Render xhtml output or metadata
94     *
95     * @param string        $mode     Renderer mode (supported modes: xhtml)
96     * @param Doku_Renderer $renderer The renderer
97     * @param array         $data     The data from the handler() function
98     *
99     * @return bool If rendering was successful.
100     */
101    public function render($mode, Doku_Renderer $renderer, $data)
102    {
103		list ($errortype, $errorpath, $image_params) = $data;
104
105        if ($mode == 'xhtml' || $mode == 'odt') {
106
107            // check if media source file exists
108			if ($errortype === 'missing_src_file') {
109				if ($this->getConf('show_missing_thumb_error')) {
110					$renderer->doc .= trim($this->getConf('no_media_error_message')) . " " . $errorpath;
111					return true;
112				} else {
113					return false;
114				}
115			}
116
117			// check if a thumbnail file was found
118			if ($errortype === 'missing_thumb_file') {
119				if ($this->getConf('show_missing_thumb_error')) {
120					$renderer->doc .= trim($this->getConf('no_thumb_error_message')) . " " . $errorpath;
121					return true;
122				} else {
123					return false;
124				}
125			}
126
127            $capped_width = $image_params['width'] > $this->getConf('thumb_max_dimension') ? $this->getConf('thumb_max_dimension') : $image_params['width'];
128            $capped_height = $image_params['height'] > $this->getConf('thumb_max_dimension') ? $this->getConf('thumb_max_dimension') : $image_params['height'];
129
130            $renderer->internalmedia(
131                $image_params['src'],
132                $image_params['title'],
133                $image_params['align'],
134                $capped_width,
135                $capped_height,
136                $image_params['cache'],
137                $image_params['linking'],
138                false
139            );
140
141            return true;
142
143        }
144
145        return false;
146    }
147}