xref: /dokuwiki/inc/ChangeLog/MediaChangeLog.php (revision 01e8d739c8b53aeb1d0a653331d65eb1f8394002)
1<?php
2
3namespace dokuwiki\ChangeLog;
4
5/**
6 * Class MediaChangeLog; handles changelog of a media file
7 */
8class MediaChangeLog extends ChangeLog
9{
10    /**
11     * Returns path to changelog
12     *
13     * @return string path to file
14     */
15    protected function getChangelogFilename()
16    {
17        return mediaMetaFN($this->id, '.changes');
18    }
19
20    /**
21     * Returns path to current page/media
22     *
23     * @param string|int $rev empty string or revision timestamp
24     * @return string path to file
25     */
26    protected function getFilename($rev = '')
27    {
28        return mediaFN($this->id, $rev);
29    }
30
31    /**
32     * Returns mode
33     *
34     * @return string RevisionInfo::MODE_PAGE
35     */
36    protected function getMode()
37    {
38        return RevisionInfo::MODE_MEDIA;
39    }
40
41    /**
42     * Returns path to the global media-changelog file
43     *
44     * @return string path to file
45     */
46    protected function getGlobalChangelogFilename()
47    {
48        global $conf;
49        return $conf['media_changelog'];
50    }
51
52    /**
53     * Copy the externally-modified media file to the attic at the synthesized revision date.
54     * If the file mtime is older than the last known revision (broken chronology),
55     * touch the file forward so future reads see a consistent state.
56     *
57     * @param array $revInfo synthesized revision info
58     * @return bool true on success (or nothing to copy), false if the attic copy failed
59     */
60    protected function saveExternalAttic(array $revInfo)
61    {
62        global $conf;
63
64        $file = $this->getFilename();
65        if (!file_exists($file)) return true;
66
67        // rescue: file mtime older than last revision — touch forward to the synthesized date
68        if (empty($revInfo['timestamp'])) {
69            if (!@touch($file, $revInfo['date'])) return false;
70            clearstatcache(false, $file);
71        }
72
73        $atticfile = $this->getFilename($revInfo['date']);
74        io_makeFileDir($atticfile);
75        if (!@copy($file, $atticfile)) return false;
76        if (!empty($conf['fmode'])) @chmod($atticfile, $conf['fmode']);
77        return true;
78    }
79}
80