xref: /dokuwiki/inc/Feed/FeedMediaProcessor.php (revision 093fe67e98c0cdb4b73fd46938e49b64971483c2)
1fe9d054bSAndreas Gohr<?php
2fe9d054bSAndreas Gohr
3fe9d054bSAndreas Gohrnamespace dokuwiki\Feed;
4fe9d054bSAndreas Gohr
5fe9d054bSAndreas Gohruse dokuwiki\ChangeLog\MediaChangeLog;
6fe9d054bSAndreas Gohruse dokuwiki\File\MediaFile;
7fe9d054bSAndreas Gohruse dokuwiki\Ui\Media\Display;
8fe9d054bSAndreas Gohr
9fe9d054bSAndreas Gohrclass FeedMediaProcessor extends FeedItemProcessor
10fe9d054bSAndreas Gohr{
11fe9d054bSAndreas Gohr    /** @inheritdoc */
12fe9d054bSAndreas Gohr    public function getURL($linkto)
13fe9d054bSAndreas Gohr    {
14*093fe67eSAndreas Gohr        $opt = match ($linkto) {
15*093fe67eSAndreas Gohr            'page' => [
16fe9d054bSAndreas Gohr                'image' => $this->getId(),
17fe9d054bSAndreas Gohr                'ns' => getNS($this->getId()),
18fe9d054bSAndreas Gohr                'rev' => $this->getRev()
19*093fe67eSAndreas Gohr            ],
20*093fe67eSAndreas Gohr            'rev' => [
21fe9d054bSAndreas Gohr                'image' => $this->getId(),
22fe9d054bSAndreas Gohr                'ns' => getNS($this->getId()),
23fe9d054bSAndreas Gohr                'rev' => $this->getRev(),
24fe9d054bSAndreas Gohr                'tab_details' => 'history'
25*093fe67eSAndreas Gohr            ],
26*093fe67eSAndreas Gohr            'current' => [
27fe9d054bSAndreas Gohr                'image' => $this->getId(),
28fe9d054bSAndreas Gohr                'ns' => getNS($this->getId())
29*093fe67eSAndreas Gohr            ],
30*093fe67eSAndreas Gohr            default => [
31fe9d054bSAndreas Gohr                'image' => $this->getId(),
32fe9d054bSAndreas Gohr                'ns' => getNS($this->getId()),
33fe9d054bSAndreas Gohr                'rev' => $this->getRev(),
34fe9d054bSAndreas Gohr                'tab_details' => 'history',
35fe9d054bSAndreas Gohr                'media_do' => 'diff'
36*093fe67eSAndreas Gohr            ],
37*093fe67eSAndreas Gohr        };
38fe9d054bSAndreas Gohr
39fe9d054bSAndreas Gohr        return media_managerURL($opt, '&', true);
40fe9d054bSAndreas Gohr    }
41fe9d054bSAndreas Gohr
42fe9d054bSAndreas Gohr    public function getBody($content)
43fe9d054bSAndreas Gohr    {
44fe9d054bSAndreas Gohr        switch ($content) {
45fe9d054bSAndreas Gohr            case 'diff':
46fe9d054bSAndreas Gohr            case 'htmldiff':
47fe9d054bSAndreas Gohr                $prev = $this->getPrev();
48fe9d054bSAndreas Gohr
49fe9d054bSAndreas Gohr                if ($prev) {
50fe9d054bSAndreas Gohr                    if ($this->isExisting()) {
51fe9d054bSAndreas Gohr                        $src1 = new MediaFile($this->getId(), $prev);
52fe9d054bSAndreas Gohr                        $src2 = new MediaFile($this->getId());
53fe9d054bSAndreas Gohr                    } else {
54fe9d054bSAndreas Gohr                        $src1 = new MediaFile($this->getId(), $prev);
55fe9d054bSAndreas Gohr                        $src2 = null;
56fe9d054bSAndreas Gohr                    }
57fe9d054bSAndreas Gohr                } else {
58fe9d054bSAndreas Gohr                    $src1 = null;
59fe9d054bSAndreas Gohr                    $src2 = new MediaFile($this->getId());
60fe9d054bSAndreas Gohr                }
61fe9d054bSAndreas Gohr                return $this->createDiffTable($src1, $src2);
62fe9d054bSAndreas Gohr
63fe9d054bSAndreas Gohr            case 'abstract':
64fe9d054bSAndreas Gohr            case 'html':
65fe9d054bSAndreas Gohr            default:
66fe9d054bSAndreas Gohr                $src = new Display(new MediaFile($this->getId()));
67fe9d054bSAndreas Gohr                return $this->cleanHTML($src->getPreviewHtml(500, 500));
68fe9d054bSAndreas Gohr        }
69fe9d054bSAndreas Gohr    }
70fe9d054bSAndreas Gohr
71fe9d054bSAndreas Gohr    /**
72fe9d054bSAndreas Gohr     * @inheritdoc
73fe9d054bSAndreas Gohr     * @todo read exif keywords
74fe9d054bSAndreas Gohr     */
75fe9d054bSAndreas Gohr    public function getCategory()
76fe9d054bSAndreas Gohr    {
77fe9d054bSAndreas Gohr        return (array)getNS($this->getId());
78fe9d054bSAndreas Gohr    }
79fe9d054bSAndreas Gohr
80fe9d054bSAndreas Gohr    /**
81fe9d054bSAndreas Gohr     * Get the revision timestamp of this page
82fe9d054bSAndreas Gohr     *
83fe9d054bSAndreas Gohr     * Note: we only handle most current revisions in feeds, so the revision is usually just the
84fe9d054bSAndreas Gohr     * lastmodifed timestamp of the page file. However, if the page does not exist, we need to
85fe9d054bSAndreas Gohr     * determine the revision from the changelog.
86fe9d054bSAndreas Gohr     * @return int
87fe9d054bSAndreas Gohr     */
88fe9d054bSAndreas Gohr    public function getRev()
89fe9d054bSAndreas Gohr    {
90fe9d054bSAndreas Gohr        $rev = parent::getRev();
91fe9d054bSAndreas Gohr        if ($rev) return $rev;
92fe9d054bSAndreas Gohr
93fe9d054bSAndreas Gohr        if (media_exists($this->id)) {
94fe9d054bSAndreas Gohr            $this->data['rev'] = filemtime(mediaFN($this->id));
95fe9d054bSAndreas Gohr            $this->data['exists'] = true;
96fe9d054bSAndreas Gohr        } else {
97fe9d054bSAndreas Gohr            $this->loadRevisions();
98fe9d054bSAndreas Gohr        }
99fe9d054bSAndreas Gohr        return $this->data['rev'];
100fe9d054bSAndreas Gohr    }
101fe9d054bSAndreas Gohr
102fe9d054bSAndreas Gohr    /**
103fe9d054bSAndreas Gohr     * Get the previous revision timestamp of this page
104fe9d054bSAndreas Gohr     *
105fe9d054bSAndreas Gohr     * @return int|null The previous revision or null if there is none
106fe9d054bSAndreas Gohr     */
107fe9d054bSAndreas Gohr    public function getPrev()
108fe9d054bSAndreas Gohr    {
109fe9d054bSAndreas Gohr        if ($this->data['prev'] ?? 0) return $this->data['prev'];
110fe9d054bSAndreas Gohr        $this->loadRevisions();
111fe9d054bSAndreas Gohr        return $this->data['prev'];
112fe9d054bSAndreas Gohr    }
113fe9d054bSAndreas Gohr
114fe9d054bSAndreas Gohr    /**
115fe9d054bSAndreas Gohr     * Does this page exist?
116fe9d054bSAndreas Gohr     *
117fe9d054bSAndreas Gohr     * @return bool
118fe9d054bSAndreas Gohr     */
119fe9d054bSAndreas Gohr    public function isExisting()
120fe9d054bSAndreas Gohr    {
121fe9d054bSAndreas Gohr        if (!isset($this->data['exists'])) {
122fe9d054bSAndreas Gohr            $this->data['exists'] = media_exists($this->id);
123fe9d054bSAndreas Gohr        }
124fe9d054bSAndreas Gohr        return $this->data['exists'];
125fe9d054bSAndreas Gohr    }
126fe9d054bSAndreas Gohr
127fe9d054bSAndreas Gohr    /**
128fe9d054bSAndreas Gohr     * Load the current and previous revision from the changelog
129fe9d054bSAndreas Gohr     * @return void
130fe9d054bSAndreas Gohr     */
131fe9d054bSAndreas Gohr    protected function loadRevisions()
132fe9d054bSAndreas Gohr    {
133fe9d054bSAndreas Gohr        $changelog = new MediaChangeLog($this->id);
134389c05a6SAndreas Gohr        $revs = $changelog->getRevisions(-1, 2);
135fe9d054bSAndreas Gohr        if (!isset($this->data['rev'])) {
136fe9d054bSAndreas Gohr            // prefer an already set date, only set if missing
137fe9d054bSAndreas Gohr            // it should usally not happen that neither is available
138fe9d054bSAndreas Gohr            $this->data['rev'] = $revs[0] ?? 0;
139fe9d054bSAndreas Gohr        }
140fe9d054bSAndreas Gohr        // a previous revision might not exist
141fe9d054bSAndreas Gohr        $this->data['prev'] = $revs[1] ?? null;
142fe9d054bSAndreas Gohr    }
143fe9d054bSAndreas Gohr
144fe9d054bSAndreas Gohr    /**
145fe9d054bSAndreas Gohr     * Create a table showing the two media files
146fe9d054bSAndreas Gohr     *
147fe9d054bSAndreas Gohr     * @param MediaFile|null $src1
148fe9d054bSAndreas Gohr     * @param MediaFile|null $src2
149fe9d054bSAndreas Gohr     * @return string
150fe9d054bSAndreas Gohr     */
151fe9d054bSAndreas Gohr    protected function createDiffTable($src1, $src2)
152fe9d054bSAndreas Gohr    {
153fe9d054bSAndreas Gohr        global $lang;
154fe9d054bSAndreas Gohr
155fe9d054bSAndreas Gohr        $content = '<table>';
156fe9d054bSAndreas Gohr        $content .= '<tr>';
157fe9d054bSAndreas Gohr        $content .= '<th width="50%">' . ($src1 ? $src1->getRev() : '') . '</th>';
158fe9d054bSAndreas Gohr        $content .= '<th width="50%">' . $lang['current'] . '</th>';
159fe9d054bSAndreas Gohr        $content .= '</tr>';
160fe9d054bSAndreas Gohr        $content .= '<tr>';
161fe9d054bSAndreas Gohr
162fe9d054bSAndreas Gohr        $content .= '<td align="center">';
163fe9d054bSAndreas Gohr        if ($src1) {
164fe9d054bSAndreas Gohr            $display = new Display($src1);
165fe9d054bSAndreas Gohr            $display->getPreviewHtml(300, 300);
166fe9d054bSAndreas Gohr        }
167fe9d054bSAndreas Gohr        $content .= '</td>';
168fe9d054bSAndreas Gohr
169fe9d054bSAndreas Gohr        $content .= '<td align="center">';
170fe9d054bSAndreas Gohr        if ($src2) {
171fe9d054bSAndreas Gohr            $display = new Display($src2);
172fe9d054bSAndreas Gohr            $display->getPreviewHtml(300, 300);
173fe9d054bSAndreas Gohr        }
174fe9d054bSAndreas Gohr        $content .= '</td>';
175fe9d054bSAndreas Gohr
176fe9d054bSAndreas Gohr        $content .= '</tr>';
177fe9d054bSAndreas Gohr        $content .= '</table>';
178fe9d054bSAndreas Gohr
179fe9d054bSAndreas Gohr        return $this->cleanHTML($content);
180fe9d054bSAndreas Gohr    }
181fe9d054bSAndreas Gohr}
182