1<?php
2
3use dokuwiki\Extension\SyntaxPlugin;
4use dokuwiki\File\MediaResolver;
5use dokuwiki\plugin\gallery\classes\BasicFormatter;
6use dokuwiki\plugin\gallery\classes\FeedGallery;
7use dokuwiki\plugin\gallery\classes\ListGallery;
8use dokuwiki\plugin\gallery\classes\NamespaceGallery;
9use dokuwiki\plugin\gallery\classes\Options;
10use dokuwiki\plugin\gallery\classes\XHTMLFormatter;
11
12/**
13 * Embed an image gallery
14 *
15 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
16 * @author     Andreas Gohr <andi@splitbrain.org>
17 * @author     Joe Lapp <joe.lapp@pobox.com>
18 * @author     Dave Doyle <davedoyle.canadalawbook.ca>
19 */
20class syntax_plugin_gallery_main extends SyntaxPlugin
21{
22    /** @inheritdoc */
23    public function getType()
24    {
25        return 'substition';
26    }
27
28    /** @inheritdoc */
29    public function getPType()
30    {
31        return 'block';
32    }
33
34    /** @inheritdoc */
35    public function getSort()
36    {
37        return 301;
38    }
39
40    /** @inheritdoc */
41    public function connectTo($mode)
42    {
43        $this->Lexer->addSpecialPattern('\{\{gallery>[^}]*\}\}', $mode, 'plugin_gallery_main');
44    }
45
46    /** @inheritdoc */
47    public function handle($match, $state, $pos, Doku_Handler $handler)
48    {
49        global $ID;
50        $match = substr($match, 10, -2); //strip markup from start and end
51
52        $options = new Options();
53
54        // unique gallery ID
55        $options->galleryID = substr(md5($match), 0, 4);
56
57        // alignment
58        if (substr($match, 0, 1) == ' ') $options->align += Options::ALIGN_RIGHT;
59        if (substr($match, -1, 1) == ' ') $options->align += Options::ALIGN_LEFT;
60
61        // extract src and params
62        [$src, $params] = sexplode('?', $match, 2);
63        $src = trim($src);
64
65        // resolve relative namespace
66        if (!preg_match('/^https?:\/\//i', $src)) {
67            $mediaResolver = new MediaResolver($ID);
68            $src = $mediaResolver->resolveId($src);
69        }
70
71        // parse parameters
72        $options->parseParameters($params);
73
74        return [
75            $src, $options
76        ];
77    }
78
79    /** @inheritdoc */
80    public function render($mode, Doku_Renderer $R, $data)
81    {
82        [$src, $options] = $data;
83
84        try {
85            if (is_array($src)) {
86                $gallery = new ListGallery($src, $options);
87            } elseif (preg_match('/^https?:\/\//i', $src)) {
88                $gallery = new FeedGallery($src, $options);
89            } else {
90                $gallery = new NamespaceGallery($src, $options);
91            }
92
93            $R->info['cache'] = $options->cache;
94            if ($mode == 'xhtml') {
95                $formatter = new XHTMLFormatter($R, $options);
96            } else {
97                $formatter = new BasicFormatter($R, $options);
98            }
99            $formatter->render($gallery);
100        } catch (Exception $e) {
101            msg(hsc($e->getMessage()), -1);
102            $R->cdata($this->getLang('fail'));
103        }
104        return true;
105    }
106}
107