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