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