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