xref: /plugin/mediathumbnails/syntax.php (revision 47274618f0925b0bf577e4d4daf86dbbad1b4bd2)
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
65		$thumb = new thumbnail($mediapath_file,$this);
66		if ($thumb->create()) {
67			return array($mediapath_file,$thumb->getMediapath());
68		}
69
70		return array($mediapath_file);
71    }
72
73    /**
74     * Render xhtml output or metadata
75     *
76     * @param string        $mode     Renderer mode (supported modes: xhtml)
77     * @param Doku_Renderer $renderer The renderer
78     * @param array         $data     The data from the handler() function
79     *
80     * @return bool If rendering was successful.
81     */
82    public function render($mode, Doku_Renderer $renderer, $data)
83    {
84		list ($mediapath_file, $mediapath_thumbnail) = $data;
85
86        if ($mode == 'xhtml') {
87
88			// check if a thumbnail file was found
89			if (!$mediapath_thumbnail) {
90				if ($this->getConf('show_missing_thumb_error')) {
91					$renderer->doc .= trim($this->getConf('no_thumb_error_message')) . " " . $mediapath_file;
92					return true;
93				} else {
94					return false;
95				}
96			}
97
98			$src = ml($mediapath_thumbnail,array());
99
100			$i             = array();
101			$i['width']    = $this->getConf('thumb_max_dimension'); //TODO: ausrichtung herausrechnen!
102			//$i['height']   = '';
103			$i['title']      = $mediapath_file;
104			$i['class']    = 'tn';
105			$iatt = buildAttributes($i);
106
107			$renderer->doc .= 	($this->getConf('link_to_media_file') ? '<a href="/lib/exe/fetch.php?media=' . $mediapath_file . '">' : '') .
108								'<img src="'.$src.'" '.$iatt.' />' .
109								($this->getConf('link_to_media_file') ? '</a>' : '');
110            return true;
111
112        } elseif ($mode == 'odt') {
113
114			// TODO: yet to implement
115			$renderer->cdata("");
116			return true;
117
118		}
119
120        return false;
121    }
122}
123
124function getFileSuffix(string $file) {
125	return substr(strrchr($file,'.'),1);
126}
127
128class thumbnail {
129
130	private $source_filepath;
131	private $source_mediapath;
132	private ?thumb_engine $thumb_engine = null;
133	private int $max_dimension;
134
135	private static $formats;
136	private static ?bool $pdf_support = null;
137	private static ?bool $image_support = null;
138	private static ?bool $no_ghostscript_support = null;
139
140	private static function testDependencies() {
141		// TODO: move support tests to a Singleton
142		self::$image_support = false;
143		self::$pdf_support = false;
144		self::$no_ghostscript_support = false;
145		if (class_exists ("Imagick")) {
146			// determine file formats supported by ImageMagick
147			self::$formats = \Imagick::queryformats();
148
149			if (count(self::$formats) > 0) {
150				self::$image_support = true;
151				if (in_array("PDF", self::$formats)) {
152					// Check if GhostScript will answer!
153					try {
154						// blank.pdf is an empty reference PDF file to test if GhostScript will react upon loading the file into ImageMagick
155						$im = new imagick(realpath("lib/plugins/mediathumbnails/blank.pdf")."[0]");
156						$im->clear();
157						$im->destroy();
158						self::$pdf_support = true;
159					} catch (ImagickException $e) {
160						if (strpos($e,"PDFDelegateFailed") !== false) {
161							self::$no_ghostscript_support = true;
162						}
163						self::$pdf_support = false;
164					}
165
166				}
167			}
168
169		}
170	}
171	public static function supportsPDF() {
172		if (self::$pdf_support === null) {
173			self::testDependencies();
174		}
175		return self::$pdf_support;
176	}
177	public static function supportsImages() {
178		if (self::$image_support === null) {
179			self::testDependencies();
180		}
181		return self::$image_support;
182	}
183	public static function ghostScriptFailed() {
184		if (self::$no_ghostscript_support === null) {
185			self::testDependencies();
186		}
187		return self::$no_ghostscript_support;
188	}
189
190	public function __construct(string $source_filepath, DokuWiki_Syntax_Plugin $plugin, bool $ismediapath = true) {
191
192		if ($ismediapath) {
193			$this->source_mediapath = $source_filepath;
194			$this->source_filepath = mediaFN($source_filepath);
195		} else {
196			$this->source_mediapath = false;
197			$this->source_filepath = $source_filepath;
198		}
199
200		$this->max_dimension = $plugin->getConf('thumb_max_dimension');
201
202		// Now attach the correct thumb_engine for the file type of the source file
203		//TODO: check for extension "fileinfo", then check for MIME type: if (mime_content_type($filepath_local_file) == "application/pdf") {
204		$sourceFileSuffix = getFileSuffix($this->source_filepath);
205		if ($sourceFileSuffix == "pdf") {
206			// file suffix is pdf, so assume it's a PDF file
207			if (self::supportsPDF()) {
208				$this->thumb_engine = new thumb_pdf_engine($this);
209			} else {
210				if (self::ghostScriptFailed()) {
211					dbg("plugin mediathumbnails: PDF files are supported, but not on this system.\nMost likely, ImageMagick and its PHP extension imagick are installed properly, but GhostScript is not.\nPlease refer to the plugin documentation for a description of the dependencies.");
212				} else {
213					dbg("plugin mediathumbnails: PDF files are supported, but not on this system.\nMost likely, ImageMagick or its PHP extension imagick are not installed properly.\nPlease refer to the plugin documentation for a description of the dependencies.");
214				}
215			}
216		} else if (self::supportsImages() && in_array(strtoupper($sourceFileSuffix), self::$formats)) {
217			// file suffix is in support list of ImageMagick
218			$this->thumb_engine = new thumb_img_engine($this);
219		} else if (!self::supportsImages()) {
220			dbg("plugin mediathumbnails: Image files are supported, but not on this system.\nPlease refer to the plugin documentation for a description of the dependencies.");
221		} else {
222			// last resort: check if the source file is a ZIP file and look for thumbnails, therein
223			$this->thumb_engine = new thumb_zip_engine($this,$plugin->getConf('thumb_paths'));
224		}
225	}
226
227	public function getMaxDimension() {
228		return $this->max_dimension;
229	}
230
231	public function create() {
232		if (!$this->thumb_engine) {
233			return false;
234		}
235
236		return $this->thumb_engine->act();
237	}
238
239	public function getSourceFilepath() {
240		return $this->source_filepath;
241	}
242
243	protected function getFilename() {
244
245		return basename($this->source_filepath) . ".thumb".$this->max_dimension.".".$this->thumb_engine->getFileSuffix();
246	}
247
248	public function getFilepath() {
249		return dirname($this->source_filepath) . DIRECTORY_SEPARATOR . $this->getFilename();
250	}
251
252	public function getMediapath() {
253		if ($this->source_mediapath !== false) {
254			return substr($this->source_mediapath,0,strrpos($this->source_mediapath,':')) . ":" . $this->getFilename();
255		} else {
256			return false;
257		}
258	}
259
260	public function getTimestamp() {
261		return file_exists($this->getFilepath()) ? filemtime($this->getFilepath()) : false;
262	}
263}
264
265abstract class thumb_engine {
266
267	private ?thumbnail $thumbnail = null;
268
269	public function __construct(thumbnail $thumbnail) {
270		$this->thumbnail = $thumbnail;
271	}
272
273	protected function getSourceFilepath() {
274		return $this->thumbnail->getSourceFilepath();
275	}
276
277	protected function getTargetFilepath() {
278		return $this->thumbnail->getFilepath();
279	}
280
281	protected function getTargetMaxDimension() {
282		return $this->thumbnail->getMaxDimension();
283	}
284
285	public function act() {
286		if ($this->act_internal()) {
287			// Set timestamp to the source file's timestamp (this is used to check in later passes if the file already exists in the correct version).
288			if (filemtime($this->getSourceFilepath()) !== filemtime($this->getTargetFilepath())) {
289				touch($this->getTargetFilepath(), filemtime($this->getSourceFilepath()));
290			}
291			return true;
292		}
293		return false;
294	}
295
296	// Checks if a thumbnail file for the current file version has already been created
297	protected function thumb_needs_update() {
298		return !file_exists($this->getTargetFilepath()) || filemtime($this->getTargetFilepath()) !== filemtime($this->getSourceFilepath());
299	}
300
301	public abstract function act_internal();
302
303	public abstract function getFileSuffix();
304}
305
306class thumb_pdf_engine extends thumb_engine {
307
308	public function getFileSuffix() {
309		return "jpg";
310	}
311
312	public function act_internal() {
313		if ($this->thumb_needs_update()) {
314			$im = new imagick($this->getSourceFilepath()."[0]");
315			$im->setImageColorspace(255);
316			$im->setResolution(300, 300);
317			$im->setCompressionQuality(95);
318			$im->setImageFormat('jpeg');
319			//$im->resizeImage($this->getTargetMaxDimension(),0,imagick::FILTER_LANCZOS,0.9);
320			//$im->thumbnailImage($this->getTargetMaxDimension(),$this->getTargetMaxDimension(),true,false);
321			$im->writeImage($this->getTargetFilepath());
322			$im->clear();
323			$im->destroy();
324
325			// unfortunately, resizeImage or thumbnailImage leads to a black thumbnail in my setup, so I reopen the file and resize it now.
326			$im = new imagick($this->getTargetFilepath());
327			$im->thumbnailImage($this->getTargetMaxDimension(),$this->getTargetMaxDimension(),true,false);
328			$im->writeImage($this->getTargetFilepath());
329			$im->clear();
330			$im->destroy();
331
332			return true;
333		} else {
334			return true;
335		}
336	}
337}
338
339class thumb_img_engine extends thumb_engine {
340
341	public function getFileSuffix() {
342		return getFileSuffix($this->getSourceFilepath());
343	}
344
345	public function act_internal() {
346		if ($this->thumb_needs_update()) {
347			$im = new imagick( $this->getSourceFilepath() );
348			$im->thumbnailImage($this->getTargetMaxDimension(),$this->getTargetMaxDimension(),true,false);
349			$im->writeImage($this->getTargetFilepath());
350			$im->clear();
351			$im->destroy();
352
353			return true;
354		} else {
355			return true;
356		}
357	}
358}
359
360class thumb_zip_engine extends thumb_engine {
361
362	private array $thumb_paths;
363	private $file_suffix = "";
364
365	public function __construct(thumbnail $thumbnail, array $thumb_paths) {
366		parent::__construct($thumbnail);
367		$this->thumb_paths = $thumb_paths;
368	}
369
370	public function getFileSuffix() {
371		return $this->file_suffix;
372	}
373
374	public function act_internal() {
375
376		$zip = new ZipArchive;
377		if ($zip->open($this->getSourceFilepath()) !== true) {
378			// file is no zip or cannot be opened
379			return false;
380		}
381
382		// The media file exists and acts as a zip file!
383
384		// Check all possible paths (configured in configuration key 'thumb_paths') if there is a file available
385		foreach($this->thumb_paths as $thumbnail_path) {
386			$this->file_suffix = substr(strrchr($thumbnail_path,'.'),1);
387
388			if ($zip->locateName($thumbnail_path) !== false) {
389
390				if (!$this->thumb_needs_update()) {
391					return true;
392				}
393
394				// Get the thumbnail file!
395				$fp = $zip->getStream($thumbnail_path);
396				if(!$fp) {
397					return false;
398				}
399
400				$thumbnaildata = '';
401				while (!feof($fp)) {
402					$thumbnaildata .= fread($fp, 8192);
403				}
404
405				fclose($fp);
406
407				// Write thumbnail file to media folder
408				file_put_contents($this->getTargetFilepath(), $thumbnaildata);
409
410				return true;
411			}
412		}
413
414		return true;
415	}
416}