xref: /plugin/mediathumbnails/syntax.php (revision 2f64379eb49b362daf059966efccfcd4353a1803)
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
14class syntax_plugin_mediathumbnails extends DokuWiki_Syntax_Plugin {
15
16	/**
17     * @return string Syntax mode type
18     */
19    public function getType()
20    {
21        return 'substition';
22    }
23
24    /**
25     * @return string Paragraph type
26     */
27    public function getPType()
28    {
29        return 'normal';
30    }
31
32    /**
33     * @return int Sort order - Low numbers go before high numbers
34     */
35    public function getSort()
36    {
37        return 1;
38    }
39
40    /**
41     * Connect lookup pattern to lexer.
42     *
43     * @param string $mode Parser mode
44     */
45    public function connectTo($mode)
46    {
47		$this->Lexer->addSpecialPattern("{{thumbnail>.+?}}", $mode, substr(get_class($this), 7));
48	}
49
50    /**
51     * Handle matches of the mediathumbnails syntax
52     *
53     * @param string       $match   The match of the syntax
54     * @param int          $state   The state of the handler
55     * @param int          $pos     The position in the document
56     * @param Doku_Handler $handler The handler
57     *
58     * @return array Data for the renderer
59     */
60    public function handle($match, $state, $pos, Doku_Handler $handler)
61    {
62		// Locate the given media file and check if it can be opened as zip
63		$mediapath_file = substr($match, 12, -2); //strip markup
64		$filepath_local_file = mediaFN($mediapath_file);
65		$timestamp_local_file = file_exists($filepath_local_file) ? filemtime($filepath_local_file) : false;
66
67		$zip = new ZipArchive;
68		if ($zip->open($filepath_local_file) !== TRUE) {
69			// media file does not exist
70			return array($mediapath_file);
71		}
72
73		// The media file exists and acts as a zip file!
74
75		// Check all possible paths (configured in configuration key 'thumb_paths') if there is a file available
76		$thumb_paths_to_investigate = $this->getConf('thumb_paths');
77
78		foreach($thumb_paths_to_investigate as $thumbnail_path) {
79			$thumbnail_ending = strrchr($thumbnail_path,'.');
80
81			if ($zip->locateName($thumbnail_path) !== false) {
82
83				// The thumbnail file exists, so prepare more information, now!
84				$extended_filename = basename($filepath_local_file) . ".thumb" . $thumbnail_ending;
85				$filepath_thumbnail = dirname($filepath_local_file) . DIRECTORY_SEPARATOR . $extended_filename;
86				$mediapath_thumbnail = substr($mediapath_file,0,strrpos($mediapath_file,':')) . ":" . $extended_filename;
87
88				if (file_exists($filepath_thumbnail) && filemtime($filepath_thumbnail) == $timestamp_local_file) {
89					// A thumbnail file for the current file version has already been created, don't extract it again, but give the renderer all needed information!
90					return array($mediapath_file, $mediapath_thumbnail);
91				}
92
93				// Get the thumbnail file!
94				$fp = $zip->getStream($thumbnail_path);
95				if(!$fp) {
96					return array();
97				}
98
99				$thumbnaildata = '';
100				while (!feof($fp)) {
101					$thumbnaildata .= fread($fp, 8192);
102				}
103
104				fclose($fp);
105
106				// Write thumbnail file to media folder
107				file_put_contents($filepath_thumbnail, $thumbnaildata);
108
109				// Set timestamp to the media file's timestamp (this is used to check in later passes if the file already exists in the correct version).
110				touch($filepath_thumbnail, $timestamp_local_file);
111
112				// Give media path to renderer
113				return array($mediapath_file, $mediapath_thumbnail);
114			}
115		}
116
117		return array($mediapath_file);
118    }
119
120    /**
121     * Render xhtml output or metadata
122     *
123     * @param string        $mode     Renderer mode (supported modes: xhtml)
124     * @param Doku_Renderer $renderer The renderer
125     * @param array         $data     The data from the handler() function
126     *
127     * @return bool If rendering was successful.
128     */
129    public function render($mode, Doku_Renderer $renderer, $data)
130    {
131		list ($mediapath_file, $mediapath_thumbnail) = $data;
132
133        if ($mode == 'xhtml') {
134
135			// check if a thumbnail file was found
136			if (!$mediapath_thumbnail) {
137				if ($this->getConf('show_missing_thumb_error')) {
138					$renderer->doc .= trim($this->getConf('no_thumb_error_message')) . " " . $mediapath_file;
139					return true;
140				} else {
141					return false;
142				}
143			}
144
145			$src = ml($mediapath_thumbnail,array());
146
147			$i             = array();
148			$i['width']    = $this->getConf('thumb_width');
149			//$i['height']   = '';
150			$i['title']      = $mediapath_file;
151			$i['class']    = 'tn';
152			$iatt = buildAttributes($i);
153
154			$renderer->doc .= 	($this->getConf('link_to_media_file') ? '<a href="/lib/exe/fetch.php?media=' . $mediapath_file . '">' : '') .
155								'<img src="'.$src.'" '.$iatt.' />' .
156								($this->getConf('link_to_media_file') ? '</a>' : '');
157            return true;
158
159        } elseif ($mode == 'odt') {
160
161			// TODO: yet to implement
162			$renderer->cdata("");
163			return true;
164
165		}
166
167        return false;
168    }
169}
170
171