xref: /dokuwiki/inc/ChangeLog/ChangeLog.php (revision 162709daa8557d651e7afd7a6aa3e62f08bf0ade)
10c3a5702SAndreas Gohr<?php
20c3a5702SAndreas Gohr
30c3a5702SAndreas Gohrnamespace dokuwiki\ChangeLog;
40c3a5702SAndreas Gohr
50c3a5702SAndreas Gohr/**
60c3a5702SAndreas Gohr * methods for handling of changelog of pages or media files
70c3a5702SAndreas Gohr */
80c3a5702SAndreas Gohrabstract class ChangeLog
90c3a5702SAndreas Gohr{
100c3a5702SAndreas Gohr    /** @var string */
110c3a5702SAndreas Gohr    protected $id;
120c3a5702SAndreas Gohr    /** @var int */
130c3a5702SAndreas Gohr    protected $chunk_size;
140c3a5702SAndreas Gohr    /** @var array */
15*162709daSSatoshi Sahara    protected $flags;
16*162709daSSatoshi Sahara    /** @var array */
170c3a5702SAndreas Gohr    protected $cache;
180c3a5702SAndreas Gohr
190c3a5702SAndreas Gohr    /**
200c3a5702SAndreas Gohr     * Constructor
210c3a5702SAndreas Gohr     *
220c3a5702SAndreas Gohr     * @param string $id page id
230c3a5702SAndreas Gohr     * @param int $chunk_size maximum block size read from file
240c3a5702SAndreas Gohr     */
250c3a5702SAndreas Gohr    public function __construct($id, $chunk_size = 8192)
260c3a5702SAndreas Gohr    {
270c3a5702SAndreas Gohr        global $cache_revinfo;
280c3a5702SAndreas Gohr
290c3a5702SAndreas Gohr        $this->cache =& $cache_revinfo;
300c3a5702SAndreas Gohr        if (!isset($this->cache[$id])) {
310c3a5702SAndreas Gohr            $this->cache[$id] = array();
320c3a5702SAndreas Gohr        }
330c3a5702SAndreas Gohr
340c3a5702SAndreas Gohr        $this->id = $id;
350c3a5702SAndreas Gohr        $this->setChunkSize($chunk_size);
360c3a5702SAndreas Gohr
37*162709daSSatoshi Sahara        $this->flags['ignore_external_edit'] = false;
38*162709daSSatoshi Sahara        // FIXME: see unittest class changelog_getrevisionsaround_test
39*162709daSSatoshi Sahara        // test page "mailinglist.txt" is newer than last entry of meta/mailinglist.changes
40*162709daSSatoshi Sahara        // temporary disable external edit check to prevent failures during unittest
41*162709daSSatoshi Sahara        if (defined('DOKU_UNITTEST')) {
42*162709daSSatoshi Sahara            $this->setFlags('ignore_external_edit', true);
43*162709daSSatoshi Sahara        }
440c3a5702SAndreas Gohr    }
450c3a5702SAndreas Gohr
460c3a5702SAndreas Gohr    /**
470c3a5702SAndreas Gohr     * Set chunk size for file reading
480c3a5702SAndreas Gohr     * Chunk size zero let read whole file at once
490c3a5702SAndreas Gohr     *
500c3a5702SAndreas Gohr     * @param int $chunk_size maximum block size read from file
510c3a5702SAndreas Gohr     */
520c3a5702SAndreas Gohr    public function setChunkSize($chunk_size)
530c3a5702SAndreas Gohr    {
540c3a5702SAndreas Gohr        if (!is_numeric($chunk_size)) $chunk_size = 0;
550c3a5702SAndreas Gohr
560c3a5702SAndreas Gohr        $this->chunk_size = (int)max($chunk_size, 0);
570c3a5702SAndreas Gohr    }
580c3a5702SAndreas Gohr
590c3a5702SAndreas Gohr    /**
60*162709daSSatoshi Sahara     * Set flags for ChangeLog
61*162709daSSatoshi Sahara     *
62*162709daSSatoshi Sahara     * @param string $name
63*162709daSSatoshi Sahara     * @param mixed $value
64*162709daSSatoshi Sahara     * @return bool
65*162709daSSatoshi Sahara     */
66*162709daSSatoshi Sahara    public function setFlags($name, $value)
67*162709daSSatoshi Sahara    {
68*162709daSSatoshi Sahara        if (array_key_exists($name, $this->flags)) {
69*162709daSSatoshi Sahara            $this->flags[$name] = $value;
70*162709daSSatoshi Sahara            return true;
71*162709daSSatoshi Sahara        }
72*162709daSSatoshi Sahara        return false;
73*162709daSSatoshi Sahara    }
74*162709daSSatoshi Sahara
75*162709daSSatoshi Sahara    /**
760c3a5702SAndreas Gohr     * Returns path to changelog
770c3a5702SAndreas Gohr     *
780c3a5702SAndreas Gohr     * @return string path to file
790c3a5702SAndreas Gohr     */
800c3a5702SAndreas Gohr    abstract protected function getChangelogFilename();
810c3a5702SAndreas Gohr
820c3a5702SAndreas Gohr    /**
830c3a5702SAndreas Gohr     * Returns path to current page/media
840c3a5702SAndreas Gohr     *
850c3a5702SAndreas Gohr     * @return string path to file
860c3a5702SAndreas Gohr     */
870c3a5702SAndreas Gohr    abstract protected function getFilename();
880c3a5702SAndreas Gohr
890c3a5702SAndreas Gohr    /**
900c3a5702SAndreas Gohr     * Get the changelog information for a specific page id and revision (timestamp)
910c3a5702SAndreas Gohr     *
920c3a5702SAndreas Gohr     * Adjacent changelog lines are optimistically parsed and cached to speed up
930c3a5702SAndreas Gohr     * consecutive calls to getRevisionInfo. For large changelog files, only the chunk
940c3a5702SAndreas Gohr     * containing the requested changelog line is read.
950c3a5702SAndreas Gohr     *
960c3a5702SAndreas Gohr     * @param int $rev revision timestamp
970c3a5702SAndreas Gohr     * @return bool|array false or array with entries:
980c3a5702SAndreas Gohr     *      - date:  unix timestamp
990c3a5702SAndreas Gohr     *      - ip:    IPv4 address (127.0.0.1)
1000c3a5702SAndreas Gohr     *      - type:  log line type
1010c3a5702SAndreas Gohr     *      - id:    page id
1020c3a5702SAndreas Gohr     *      - user:  user name
1030c3a5702SAndreas Gohr     *      - sum:   edit summary (or action reason)
1040c3a5702SAndreas Gohr     *      - extra: extra data (varies by line type)
105*162709daSSatoshi Sahara     *      - sizechange: change of filesize
1060c3a5702SAndreas Gohr     *
1070c3a5702SAndreas Gohr     * @author Ben Coburn <btcoburn@silicodon.net>
1080c3a5702SAndreas Gohr     * @author Kate Arzamastseva <pshns@ukr.net>
1090c3a5702SAndreas Gohr     */
1100c3a5702SAndreas Gohr    public function getRevisionInfo($rev)
1110c3a5702SAndreas Gohr    {
112a3984ddfSSatoshi Sahara        $rev = max(0, $rev);
113a3984ddfSSatoshi Sahara        if (!$rev) return false;
1140c3a5702SAndreas Gohr
1150c3a5702SAndreas Gohr        // check if it's already in the memory cache
1160c3a5702SAndreas Gohr        if (isset($this->cache[$this->id]) && isset($this->cache[$this->id][$rev])) {
1170c3a5702SAndreas Gohr            return $this->cache[$this->id][$rev];
1180c3a5702SAndreas Gohr        }
1190c3a5702SAndreas Gohr
1200c3a5702SAndreas Gohr        //read lines from changelog
1210c3a5702SAndreas Gohr        list($fp, $lines) = $this->readloglines($rev);
1220c3a5702SAndreas Gohr        if ($fp) {
1230c3a5702SAndreas Gohr            fclose($fp);
1240c3a5702SAndreas Gohr        }
1250c3a5702SAndreas Gohr        if (empty($lines)) return false;
1260c3a5702SAndreas Gohr
1270c3a5702SAndreas Gohr        // parse and cache changelog lines
1280c3a5702SAndreas Gohr        foreach ($lines as $value) {
1290c3a5702SAndreas Gohr            $tmp = parseChangelogLine($value);
1300c3a5702SAndreas Gohr            if ($tmp !== false) {
1310c3a5702SAndreas Gohr                $this->cache[$this->id][$tmp['date']] = $tmp;
1320c3a5702SAndreas Gohr            }
1330c3a5702SAndreas Gohr        }
1340c3a5702SAndreas Gohr        if (!isset($this->cache[$this->id][$rev])) {
1350c3a5702SAndreas Gohr            return false;
1360c3a5702SAndreas Gohr        }
1370c3a5702SAndreas Gohr        return $this->cache[$this->id][$rev];
1380c3a5702SAndreas Gohr    }
1390c3a5702SAndreas Gohr
1400c3a5702SAndreas Gohr    /**
1410c3a5702SAndreas Gohr     * Return a list of page revisions numbers
1420c3a5702SAndreas Gohr     *
1430c3a5702SAndreas Gohr     * Does not guarantee that the revision exists in the attic,
1440c3a5702SAndreas Gohr     * only that a line with the date exists in the changelog.
1450c3a5702SAndreas Gohr     * By default the current revision is skipped.
1460c3a5702SAndreas Gohr     *
1470c3a5702SAndreas Gohr     * The current revision is automatically skipped when the page exists.
1480c3a5702SAndreas Gohr     * See $INFO['meta']['last_change'] for the current revision.
1490c3a5702SAndreas Gohr     * A negative $first let read the current revision too.
1500c3a5702SAndreas Gohr     *
1510c3a5702SAndreas Gohr     * For efficiency, the log lines are parsed and cached for later
1520c3a5702SAndreas Gohr     * calls to getRevisionInfo. Large changelog files are read
1530c3a5702SAndreas Gohr     * backwards in chunks until the requested number of changelog
1540c3a5702SAndreas Gohr     * lines are recieved.
1550c3a5702SAndreas Gohr     *
1560c3a5702SAndreas Gohr     * @param int $first skip the first n changelog lines
1570c3a5702SAndreas Gohr     * @param int $num number of revisions to return
1580c3a5702SAndreas Gohr     * @return array with the revision timestamps
1590c3a5702SAndreas Gohr     *
1600c3a5702SAndreas Gohr     * @author Ben Coburn <btcoburn@silicodon.net>
1610c3a5702SAndreas Gohr     * @author Kate Arzamastseva <pshns@ukr.net>
1620c3a5702SAndreas Gohr     */
1630c3a5702SAndreas Gohr    public function getRevisions($first, $num)
1640c3a5702SAndreas Gohr    {
1650c3a5702SAndreas Gohr        $revs = array();
1660c3a5702SAndreas Gohr        $lines = array();
1670c3a5702SAndreas Gohr        $count = 0;
1680c3a5702SAndreas Gohr
1690c3a5702SAndreas Gohr        $num = max($num, 0);
1700c3a5702SAndreas Gohr        if ($num == 0) {
1710c3a5702SAndreas Gohr            return $revs;
1720c3a5702SAndreas Gohr        }
1730c3a5702SAndreas Gohr
1740c3a5702SAndreas Gohr        if ($first < 0) {
1750c3a5702SAndreas Gohr            $first = 0;
1760c3a5702SAndreas Gohr        } else {
1770c3a5702SAndreas Gohr            if (file_exists($this->getFilename())) {
1780c3a5702SAndreas Gohr                // skip current revision if the page exists
1790c3a5702SAndreas Gohr                $first = max($first + 1, 0);
1800c3a5702SAndreas Gohr            }
1810c3a5702SAndreas Gohr        }
1820c3a5702SAndreas Gohr
1830c3a5702SAndreas Gohr        $file = $this->getChangelogFilename();
1840c3a5702SAndreas Gohr
1850c3a5702SAndreas Gohr        if (!file_exists($file)) {
1860c3a5702SAndreas Gohr            return $revs;
1870c3a5702SAndreas Gohr        }
1880c3a5702SAndreas Gohr        if (filesize($file) < $this->chunk_size || $this->chunk_size == 0) {
1890c3a5702SAndreas Gohr            // read whole file
1900c3a5702SAndreas Gohr            $lines = file($file);
1910c3a5702SAndreas Gohr            if ($lines === false) {
1920c3a5702SAndreas Gohr                return $revs;
1930c3a5702SAndreas Gohr            }
1940c3a5702SAndreas Gohr        } else {
1950c3a5702SAndreas Gohr            // read chunks backwards
1960c3a5702SAndreas Gohr            $fp = fopen($file, 'rb'); // "file pointer"
1970c3a5702SAndreas Gohr            if ($fp === false) {
1980c3a5702SAndreas Gohr                return $revs;
1990c3a5702SAndreas Gohr            }
2000c3a5702SAndreas Gohr            fseek($fp, 0, SEEK_END);
2010c3a5702SAndreas Gohr            $tail = ftell($fp);
2020c3a5702SAndreas Gohr
2030c3a5702SAndreas Gohr            // chunk backwards
2040c3a5702SAndreas Gohr            $finger = max($tail - $this->chunk_size, 0);
2050c3a5702SAndreas Gohr            while ($count < $num + $first) {
2060c3a5702SAndreas Gohr                $nl = $this->getNewlinepointer($fp, $finger);
2070c3a5702SAndreas Gohr
2080c3a5702SAndreas Gohr                // was the chunk big enough? if not, take another bite
2090c3a5702SAndreas Gohr                if ($nl > 0 && $tail <= $nl) {
2100c3a5702SAndreas Gohr                    $finger = max($finger - $this->chunk_size, 0);
2110c3a5702SAndreas Gohr                    continue;
2120c3a5702SAndreas Gohr                } else {
2130c3a5702SAndreas Gohr                    $finger = $nl;
2140c3a5702SAndreas Gohr                }
2150c3a5702SAndreas Gohr
2160c3a5702SAndreas Gohr                // read chunk
2170c3a5702SAndreas Gohr                $chunk = '';
2180c3a5702SAndreas Gohr                $read_size = max($tail - $finger, 0); // found chunk size
2190c3a5702SAndreas Gohr                $got = 0;
2200c3a5702SAndreas Gohr                while ($got < $read_size && !feof($fp)) {
2210c3a5702SAndreas Gohr                    $tmp = @fread($fp, max(min($this->chunk_size, $read_size - $got), 0));
2220c3a5702SAndreas Gohr                    if ($tmp === false) {
2230c3a5702SAndreas Gohr                        break;
2240c3a5702SAndreas Gohr                    } //error state
2250c3a5702SAndreas Gohr                    $got += strlen($tmp);
2260c3a5702SAndreas Gohr                    $chunk .= $tmp;
2270c3a5702SAndreas Gohr                }
2280c3a5702SAndreas Gohr                $tmp = explode("\n", $chunk);
2290c3a5702SAndreas Gohr                array_pop($tmp); // remove trailing newline
2300c3a5702SAndreas Gohr
2310c3a5702SAndreas Gohr                // combine with previous chunk
2320c3a5702SAndreas Gohr                $count += count($tmp);
2330c3a5702SAndreas Gohr                $lines = array_merge($tmp, $lines);
2340c3a5702SAndreas Gohr
2350c3a5702SAndreas Gohr                // next chunk
2360c3a5702SAndreas Gohr                if ($finger == 0) {
2370c3a5702SAndreas Gohr                    break;
238e24a74c0SAndreas Gohr                } else { // already read all the lines
2390c3a5702SAndreas Gohr                    $tail = $finger;
2400c3a5702SAndreas Gohr                    $finger = max($tail - $this->chunk_size, 0);
2410c3a5702SAndreas Gohr                }
2420c3a5702SAndreas Gohr            }
2430c3a5702SAndreas Gohr            fclose($fp);
2440c3a5702SAndreas Gohr        }
2450c3a5702SAndreas Gohr
2460c3a5702SAndreas Gohr        // skip parsing extra lines
2470c3a5702SAndreas Gohr        $num = max(min(count($lines) - $first, $num), 0);
2480c3a5702SAndreas Gohr        if ($first > 0 && $num > 0) {
2490c3a5702SAndreas Gohr            $lines = array_slice($lines, max(count($lines) - $first - $num, 0), $num);
2500c3a5702SAndreas Gohr        } else {
2510c3a5702SAndreas Gohr            if ($first > 0 && $num == 0) {
2520c3a5702SAndreas Gohr                $lines = array_slice($lines, 0, max(count($lines) - $first, 0));
2530c3a5702SAndreas Gohr            } elseif ($first == 0 && $num > 0) {
2540c3a5702SAndreas Gohr                $lines = array_slice($lines, max(count($lines) - $num, 0));
2550c3a5702SAndreas Gohr            }
2560c3a5702SAndreas Gohr        }
2570c3a5702SAndreas Gohr
2580c3a5702SAndreas Gohr        // handle lines in reverse order
2590c3a5702SAndreas Gohr        for ($i = count($lines) - 1; $i >= 0; $i--) {
2600c3a5702SAndreas Gohr            $tmp = parseChangelogLine($lines[$i]);
2610c3a5702SAndreas Gohr            if ($tmp !== false) {
2620c3a5702SAndreas Gohr                $this->cache[$this->id][$tmp['date']] = $tmp;
2630c3a5702SAndreas Gohr                $revs[] = $tmp['date'];
2640c3a5702SAndreas Gohr            }
2650c3a5702SAndreas Gohr        }
2660c3a5702SAndreas Gohr
2670c3a5702SAndreas Gohr        return $revs;
2680c3a5702SAndreas Gohr    }
2690c3a5702SAndreas Gohr
2700c3a5702SAndreas Gohr    /**
2710c3a5702SAndreas Gohr     * Get the nth revision left or right handside  for a specific page id and revision (timestamp)
2720c3a5702SAndreas Gohr     *
2730c3a5702SAndreas Gohr     * For large changelog files, only the chunk containing the
2740c3a5702SAndreas Gohr     * reference revision $rev is read and sometimes a next chunck.
2750c3a5702SAndreas Gohr     *
2760c3a5702SAndreas Gohr     * Adjacent changelog lines are optimistically parsed and cached to speed up
2770c3a5702SAndreas Gohr     * consecutive calls to getRevisionInfo.
2780c3a5702SAndreas Gohr     *
2790c3a5702SAndreas Gohr     * @param int $rev revision timestamp used as startdate (doesn't need to be revisionnumber)
2800c3a5702SAndreas Gohr     * @param int $direction give position of returned revision with respect to $rev; positive=next, negative=prev
2810c3a5702SAndreas Gohr     * @return bool|int
2820c3a5702SAndreas Gohr     *      timestamp of the requested revision
2830c3a5702SAndreas Gohr     *      otherwise false
2840c3a5702SAndreas Gohr     */
2850c3a5702SAndreas Gohr    public function getRelativeRevision($rev, $direction)
2860c3a5702SAndreas Gohr    {
2870c3a5702SAndreas Gohr        $rev = max($rev, 0);
2880c3a5702SAndreas Gohr        $direction = (int)$direction;
2890c3a5702SAndreas Gohr
2900c3a5702SAndreas Gohr        //no direction given or last rev, so no follow-up
2910c3a5702SAndreas Gohr        if (!$direction || ($direction > 0 && $this->isCurrentRevision($rev))) {
2920c3a5702SAndreas Gohr            return false;
2930c3a5702SAndreas Gohr        }
2940c3a5702SAndreas Gohr
2950c3a5702SAndreas Gohr        //get lines from changelog
2960c3a5702SAndreas Gohr        list($fp, $lines, $head, $tail, $eof) = $this->readloglines($rev);
2970c3a5702SAndreas Gohr        if (empty($lines)) return false;
2980c3a5702SAndreas Gohr
2990c3a5702SAndreas Gohr        // look for revisions later/earlier then $rev, when founded count till the wanted revision is reached
3000c3a5702SAndreas Gohr        // also parse and cache changelog lines for getRevisionInfo().
3010c3a5702SAndreas Gohr        $revcounter = 0;
3020c3a5702SAndreas Gohr        $relativerev = false;
3030c3a5702SAndreas Gohr        $checkotherchunck = true; //always runs once
3040c3a5702SAndreas Gohr        while (!$relativerev && $checkotherchunck) {
3050c3a5702SAndreas Gohr            $tmp = array();
3060c3a5702SAndreas Gohr            //parse in normal or reverse order
3070c3a5702SAndreas Gohr            $count = count($lines);
3080c3a5702SAndreas Gohr            if ($direction > 0) {
3090c3a5702SAndreas Gohr                $start = 0;
3100c3a5702SAndreas Gohr                $step = 1;
3110c3a5702SAndreas Gohr            } else {
3120c3a5702SAndreas Gohr                $start = $count - 1;
3130c3a5702SAndreas Gohr                $step = -1;
3140c3a5702SAndreas Gohr            }
3150c3a5702SAndreas Gohr            for ($i = $start; $i >= 0 && $i < $count; $i = $i + $step) {
3160c3a5702SAndreas Gohr                $tmp = parseChangelogLine($lines[$i]);
3170c3a5702SAndreas Gohr                if ($tmp !== false) {
3180c3a5702SAndreas Gohr                    $this->cache[$this->id][$tmp['date']] = $tmp;
3190c3a5702SAndreas Gohr                    //look for revs older/earlier then reference $rev and select $direction-th one
3200c3a5702SAndreas Gohr                    if (($direction > 0 && $tmp['date'] > $rev) || ($direction < 0 && $tmp['date'] < $rev)) {
3210c3a5702SAndreas Gohr                        $revcounter++;
3220c3a5702SAndreas Gohr                        if ($revcounter == abs($direction)) {
3230c3a5702SAndreas Gohr                            $relativerev = $tmp['date'];
3240c3a5702SAndreas Gohr                        }
3250c3a5702SAndreas Gohr                    }
3260c3a5702SAndreas Gohr                }
3270c3a5702SAndreas Gohr            }
3280c3a5702SAndreas Gohr
3290c3a5702SAndreas Gohr            //true when $rev is found, but not the wanted follow-up.
3300c3a5702SAndreas Gohr            $checkotherchunck = $fp
3310c3a5702SAndreas Gohr                && ($tmp['date'] == $rev || ($revcounter > 0 && !$relativerev))
3320c3a5702SAndreas Gohr                && !(($tail == $eof && $direction > 0) || ($head == 0 && $direction < 0));
3330c3a5702SAndreas Gohr
3340c3a5702SAndreas Gohr            if ($checkotherchunck) {
3350c3a5702SAndreas Gohr                list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, $direction);
3360c3a5702SAndreas Gohr
3370c3a5702SAndreas Gohr                if (empty($lines)) break;
3380c3a5702SAndreas Gohr            }
3390c3a5702SAndreas Gohr        }
3400c3a5702SAndreas Gohr        if ($fp) {
3410c3a5702SAndreas Gohr            fclose($fp);
3420c3a5702SAndreas Gohr        }
3430c3a5702SAndreas Gohr
3440c3a5702SAndreas Gohr        return $relativerev;
3450c3a5702SAndreas Gohr    }
3460c3a5702SAndreas Gohr
3470c3a5702SAndreas Gohr    /**
3480c3a5702SAndreas Gohr     * Returns revisions around rev1 and rev2
3490c3a5702SAndreas Gohr     * When available it returns $max entries for each revision
3500c3a5702SAndreas Gohr     *
3510c3a5702SAndreas Gohr     * @param int $rev1 oldest revision timestamp
3520c3a5702SAndreas Gohr     * @param int $rev2 newest revision timestamp (0 looks up last revision)
3530c3a5702SAndreas Gohr     * @param int $max maximum number of revisions returned
3540c3a5702SAndreas Gohr     * @return array with two arrays with revisions surrounding rev1 respectively rev2
3550c3a5702SAndreas Gohr     */
3560c3a5702SAndreas Gohr    public function getRevisionsAround($rev1, $rev2, $max = 50)
3570c3a5702SAndreas Gohr    {
3580c3a5702SAndreas Gohr        $max = floor(abs($max) / 2) * 2 + 1;
3590c3a5702SAndreas Gohr        $rev1 = max($rev1, 0);
3600c3a5702SAndreas Gohr        $rev2 = max($rev2, 0);
3610c3a5702SAndreas Gohr
3620c3a5702SAndreas Gohr        if ($rev2) {
3630c3a5702SAndreas Gohr            if ($rev2 < $rev1) {
3640c3a5702SAndreas Gohr                $rev = $rev2;
3650c3a5702SAndreas Gohr                $rev2 = $rev1;
3660c3a5702SAndreas Gohr                $rev1 = $rev;
3670c3a5702SAndreas Gohr            }
3680c3a5702SAndreas Gohr        } else {
3690c3a5702SAndreas Gohr            //empty right side means a removed page. Look up last revision.
3700c3a5702SAndreas Gohr            $revs = $this->getRevisions(-1, 1);
3710c3a5702SAndreas Gohr            $rev2 = $revs[0];
3720c3a5702SAndreas Gohr        }
3730c3a5702SAndreas Gohr        //collect revisions around rev2
3740c3a5702SAndreas Gohr        list($revs2, $allrevs, $fp, $lines, $head, $tail) = $this->retrieveRevisionsAround($rev2, $max);
3750c3a5702SAndreas Gohr
3760c3a5702SAndreas Gohr        if (empty($revs2)) return array(array(), array());
3770c3a5702SAndreas Gohr
3780c3a5702SAndreas Gohr        //collect revisions around rev1
3790c3a5702SAndreas Gohr        $index = array_search($rev1, $allrevs);
3800c3a5702SAndreas Gohr        if ($index === false) {
3810c3a5702SAndreas Gohr            //no overlapping revisions
3820c3a5702SAndreas Gohr            list($revs1, , , , ,) = $this->retrieveRevisionsAround($rev1, $max);
3830c3a5702SAndreas Gohr            if (empty($revs1)) $revs1 = array();
3840c3a5702SAndreas Gohr        } else {
3850c3a5702SAndreas Gohr            //revisions overlaps, reuse revisions around rev2
3860c3a5702SAndreas Gohr            $revs1 = $allrevs;
3870c3a5702SAndreas Gohr            while ($head > 0) {
3880c3a5702SAndreas Gohr                for ($i = count($lines) - 1; $i >= 0; $i--) {
3890c3a5702SAndreas Gohr                    $tmp = parseChangelogLine($lines[$i]);
3900c3a5702SAndreas Gohr                    if ($tmp !== false) {
3910c3a5702SAndreas Gohr                        $this->cache[$this->id][$tmp['date']] = $tmp;
3920c3a5702SAndreas Gohr                        $revs1[] = $tmp['date'];
3930c3a5702SAndreas Gohr                        $index++;
3940c3a5702SAndreas Gohr
3950c3a5702SAndreas Gohr                        if ($index > floor($max / 2)) break 2;
3960c3a5702SAndreas Gohr                    }
3970c3a5702SAndreas Gohr                }
3980c3a5702SAndreas Gohr
3990c3a5702SAndreas Gohr                list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, -1);
4000c3a5702SAndreas Gohr            }
4010c3a5702SAndreas Gohr            sort($revs1);
4020c3a5702SAndreas Gohr            //return wanted selection
4030c3a5702SAndreas Gohr            $revs1 = array_slice($revs1, max($index - floor($max / 2), 0), $max);
4040c3a5702SAndreas Gohr        }
4050c3a5702SAndreas Gohr
4060c3a5702SAndreas Gohr        return array(array_reverse($revs1), array_reverse($revs2));
4070c3a5702SAndreas Gohr    }
4080c3a5702SAndreas Gohr
409923e149aSMichael Große
410923e149aSMichael Große    /**
411923e149aSMichael Große     * Checks if the ID has old revisons
412923e149aSMichael Große     * @return boolean
413923e149aSMichael Große     */
414923e149aSMichael Große    public function hasRevisions() {
415923e149aSMichael Große        $file = $this->getChangelogFilename();
416923e149aSMichael Große        return file_exists($file);
417923e149aSMichael Große    }
418923e149aSMichael Große
4190c3a5702SAndreas Gohr    /**
4200c3a5702SAndreas Gohr     * Returns lines from changelog.
4210c3a5702SAndreas Gohr     * If file larger than $chuncksize, only chunck is read that could contain $rev.
4220c3a5702SAndreas Gohr     *
4230c3a5702SAndreas Gohr     * @param int $rev revision timestamp
4240c3a5702SAndreas Gohr     * @return array|false
4250c3a5702SAndreas Gohr     *     if success returns array(fp, array(changeloglines), $head, $tail, $eof)
4260c3a5702SAndreas Gohr     *     where fp only defined for chuck reading, needs closing.
4270c3a5702SAndreas Gohr     *     otherwise false
4280c3a5702SAndreas Gohr     */
4290c3a5702SAndreas Gohr    protected function readloglines($rev)
4300c3a5702SAndreas Gohr    {
4310c3a5702SAndreas Gohr        $file = $this->getChangelogFilename();
4320c3a5702SAndreas Gohr
4330c3a5702SAndreas Gohr        if (!file_exists($file)) {
4340c3a5702SAndreas Gohr            return false;
4350c3a5702SAndreas Gohr        }
4360c3a5702SAndreas Gohr
4370c3a5702SAndreas Gohr        $fp = null;
4380c3a5702SAndreas Gohr        $head = 0;
4390c3a5702SAndreas Gohr        $tail = 0;
4400c3a5702SAndreas Gohr        $eof = 0;
4410c3a5702SAndreas Gohr
4420c3a5702SAndreas Gohr        if (filesize($file) < $this->chunk_size || $this->chunk_size == 0) {
4430c3a5702SAndreas Gohr            // read whole file
4440c3a5702SAndreas Gohr            $lines = file($file);
4450c3a5702SAndreas Gohr            if ($lines === false) {
4460c3a5702SAndreas Gohr                return false;
4470c3a5702SAndreas Gohr            }
4480c3a5702SAndreas Gohr        } else {
4490c3a5702SAndreas Gohr            // read by chunk
4500c3a5702SAndreas Gohr            $fp = fopen($file, 'rb'); // "file pointer"
4510c3a5702SAndreas Gohr            if ($fp === false) {
4520c3a5702SAndreas Gohr                return false;
4530c3a5702SAndreas Gohr            }
4540c3a5702SAndreas Gohr            $head = 0;
4550c3a5702SAndreas Gohr            fseek($fp, 0, SEEK_END);
4560c3a5702SAndreas Gohr            $eof = ftell($fp);
4570c3a5702SAndreas Gohr            $tail = $eof;
4580c3a5702SAndreas Gohr
4590c3a5702SAndreas Gohr            // find chunk
4600c3a5702SAndreas Gohr            while ($tail - $head > $this->chunk_size) {
4610c3a5702SAndreas Gohr                $finger = $head + floor(($tail - $head) / 2.0);
4620c3a5702SAndreas Gohr                $finger = $this->getNewlinepointer($fp, $finger);
4630c3a5702SAndreas Gohr                $tmp = fgets($fp);
4640c3a5702SAndreas Gohr                if ($finger == $head || $finger == $tail) {
4650c3a5702SAndreas Gohr                    break;
4660c3a5702SAndreas Gohr                }
4670c3a5702SAndreas Gohr                $tmp = parseChangelogLine($tmp);
4680c3a5702SAndreas Gohr                $finger_rev = $tmp['date'];
4690c3a5702SAndreas Gohr
4700c3a5702SAndreas Gohr                if ($finger_rev > $rev) {
4710c3a5702SAndreas Gohr                    $tail = $finger;
4720c3a5702SAndreas Gohr                } else {
4730c3a5702SAndreas Gohr                    $head = $finger;
4740c3a5702SAndreas Gohr                }
4750c3a5702SAndreas Gohr            }
4760c3a5702SAndreas Gohr
4770c3a5702SAndreas Gohr            if ($tail - $head < 1) {
4780c3a5702SAndreas Gohr                // cound not find chunk, assume requested rev is missing
4790c3a5702SAndreas Gohr                fclose($fp);
4800c3a5702SAndreas Gohr                return false;
4810c3a5702SAndreas Gohr            }
4820c3a5702SAndreas Gohr
4830c3a5702SAndreas Gohr            $lines = $this->readChunk($fp, $head, $tail);
4840c3a5702SAndreas Gohr        }
4850c3a5702SAndreas Gohr        return array(
4860c3a5702SAndreas Gohr            $fp,
4870c3a5702SAndreas Gohr            $lines,
4880c3a5702SAndreas Gohr            $head,
4890c3a5702SAndreas Gohr            $tail,
4900c3a5702SAndreas Gohr            $eof,
4910c3a5702SAndreas Gohr        );
4920c3a5702SAndreas Gohr    }
4930c3a5702SAndreas Gohr
4940c3a5702SAndreas Gohr    /**
4950c3a5702SAndreas Gohr     * Read chunk and return array with lines of given chunck.
4960c3a5702SAndreas Gohr     * Has no check if $head and $tail are really at a new line
4970c3a5702SAndreas Gohr     *
4980c3a5702SAndreas Gohr     * @param resource $fp resource filepointer
4990c3a5702SAndreas Gohr     * @param int $head start point chunck
5000c3a5702SAndreas Gohr     * @param int $tail end point chunck
5010c3a5702SAndreas Gohr     * @return array lines read from chunck
5020c3a5702SAndreas Gohr     */
5030c3a5702SAndreas Gohr    protected function readChunk($fp, $head, $tail)
5040c3a5702SAndreas Gohr    {
5050c3a5702SAndreas Gohr        $chunk = '';
5060c3a5702SAndreas Gohr        $chunk_size = max($tail - $head, 0); // found chunk size
5070c3a5702SAndreas Gohr        $got = 0;
5080c3a5702SAndreas Gohr        fseek($fp, $head);
5090c3a5702SAndreas Gohr        while ($got < $chunk_size && !feof($fp)) {
5100c3a5702SAndreas Gohr            $tmp = @fread($fp, max(min($this->chunk_size, $chunk_size - $got), 0));
5110c3a5702SAndreas Gohr            if ($tmp === false) { //error state
5120c3a5702SAndreas Gohr                break;
5130c3a5702SAndreas Gohr            }
5140c3a5702SAndreas Gohr            $got += strlen($tmp);
5150c3a5702SAndreas Gohr            $chunk .= $tmp;
5160c3a5702SAndreas Gohr        }
5170c3a5702SAndreas Gohr        $lines = explode("\n", $chunk);
5180c3a5702SAndreas Gohr        array_pop($lines); // remove trailing newline
5190c3a5702SAndreas Gohr        return $lines;
5200c3a5702SAndreas Gohr    }
5210c3a5702SAndreas Gohr
5220c3a5702SAndreas Gohr    /**
5230c3a5702SAndreas Gohr     * Set pointer to first new line after $finger and return its position
5240c3a5702SAndreas Gohr     *
5250c3a5702SAndreas Gohr     * @param resource $fp filepointer
5260c3a5702SAndreas Gohr     * @param int $finger a pointer
5270c3a5702SAndreas Gohr     * @return int pointer
5280c3a5702SAndreas Gohr     */
5290c3a5702SAndreas Gohr    protected function getNewlinepointer($fp, $finger)
5300c3a5702SAndreas Gohr    {
5310c3a5702SAndreas Gohr        fseek($fp, $finger);
5320c3a5702SAndreas Gohr        $nl = $finger;
5330c3a5702SAndreas Gohr        if ($finger > 0) {
5340c3a5702SAndreas Gohr            fgets($fp); // slip the finger forward to a new line
5350c3a5702SAndreas Gohr            $nl = ftell($fp);
5360c3a5702SAndreas Gohr        }
5370c3a5702SAndreas Gohr        return $nl;
5380c3a5702SAndreas Gohr    }
5390c3a5702SAndreas Gohr
5400c3a5702SAndreas Gohr    /**
5410c3a5702SAndreas Gohr     * Check whether given revision is the current page
5420c3a5702SAndreas Gohr     *
5430c3a5702SAndreas Gohr     * @param int $rev timestamp of current page
5440c3a5702SAndreas Gohr     * @return bool true if $rev is current revision, otherwise false
5450c3a5702SAndreas Gohr     */
5460c3a5702SAndreas Gohr    public function isCurrentRevision($rev)
5470c3a5702SAndreas Gohr    {
5480c3a5702SAndreas Gohr        return $rev == @filemtime($this->getFilename());
5490c3a5702SAndreas Gohr    }
5500c3a5702SAndreas Gohr
5510c3a5702SAndreas Gohr    /**
5520c3a5702SAndreas Gohr     * Return an existing revision for a specific date which is
5530c3a5702SAndreas Gohr     * the current one or younger or equal then the date
5540c3a5702SAndreas Gohr     *
5550c3a5702SAndreas Gohr     * @param number $date_at timestamp
5560c3a5702SAndreas Gohr     * @return string revision ('' for current)
5570c3a5702SAndreas Gohr     */
5580c3a5702SAndreas Gohr    public function getLastRevisionAt($date_at)
5590c3a5702SAndreas Gohr    {
5600c3a5702SAndreas Gohr        //requested date_at(timestamp) younger or equal then modified_time($this->id) => load current
5610c3a5702SAndreas Gohr        if (file_exists($this->getFilename()) && $date_at >= @filemtime($this->getFilename())) {
5620c3a5702SAndreas Gohr            return '';
5630c3a5702SAndreas Gohr        } else {
5640c3a5702SAndreas Gohr            if ($rev = $this->getRelativeRevision($date_at + 1, -1)) { //+1 to get also the requested date revision
5650c3a5702SAndreas Gohr                return $rev;
5660c3a5702SAndreas Gohr            } else {
5670c3a5702SAndreas Gohr                return false;
5680c3a5702SAndreas Gohr            }
5690c3a5702SAndreas Gohr        }
5700c3a5702SAndreas Gohr    }
5710c3a5702SAndreas Gohr
5720c3a5702SAndreas Gohr    /**
5730c3a5702SAndreas Gohr     * Returns the next lines of the changelog  of the chunck before head or after tail
5740c3a5702SAndreas Gohr     *
5750c3a5702SAndreas Gohr     * @param resource $fp filepointer
5760c3a5702SAndreas Gohr     * @param int $head position head of last chunk
5770c3a5702SAndreas Gohr     * @param int $tail position tail of last chunk
5780c3a5702SAndreas Gohr     * @param int $direction positive forward, negative backward
5790c3a5702SAndreas Gohr     * @return array with entries:
5800c3a5702SAndreas Gohr     *    - $lines: changelog lines of readed chunk
5810c3a5702SAndreas Gohr     *    - $head: head of chunk
5820c3a5702SAndreas Gohr     *    - $tail: tail of chunk
5830c3a5702SAndreas Gohr     */
5840c3a5702SAndreas Gohr    protected function readAdjacentChunk($fp, $head, $tail, $direction)
5850c3a5702SAndreas Gohr    {
5860c3a5702SAndreas Gohr        if (!$fp) return array(array(), $head, $tail);
5870c3a5702SAndreas Gohr
5880c3a5702SAndreas Gohr        if ($direction > 0) {
5890c3a5702SAndreas Gohr            //read forward
5900c3a5702SAndreas Gohr            $head = $tail;
5910c3a5702SAndreas Gohr            $tail = $head + floor($this->chunk_size * (2 / 3));
5920c3a5702SAndreas Gohr            $tail = $this->getNewlinepointer($fp, $tail);
5930c3a5702SAndreas Gohr        } else {
5940c3a5702SAndreas Gohr            //read backward
5950c3a5702SAndreas Gohr            $tail = $head;
5960c3a5702SAndreas Gohr            $head = max($tail - $this->chunk_size, 0);
5970c3a5702SAndreas Gohr            while (true) {
5980c3a5702SAndreas Gohr                $nl = $this->getNewlinepointer($fp, $head);
5990c3a5702SAndreas Gohr                // was the chunk big enough? if not, take another bite
6000c3a5702SAndreas Gohr                if ($nl > 0 && $tail <= $nl) {
6010c3a5702SAndreas Gohr                    $head = max($head - $this->chunk_size, 0);
6020c3a5702SAndreas Gohr                } else {
6030c3a5702SAndreas Gohr                    $head = $nl;
6040c3a5702SAndreas Gohr                    break;
6050c3a5702SAndreas Gohr                }
6060c3a5702SAndreas Gohr            }
6070c3a5702SAndreas Gohr        }
6080c3a5702SAndreas Gohr
6090c3a5702SAndreas Gohr        //load next chunck
6100c3a5702SAndreas Gohr        $lines = $this->readChunk($fp, $head, $tail);
6110c3a5702SAndreas Gohr        return array($lines, $head, $tail);
6120c3a5702SAndreas Gohr    }
6130c3a5702SAndreas Gohr
6140c3a5702SAndreas Gohr    /**
6150c3a5702SAndreas Gohr     * Collect the $max revisions near to the timestamp $rev
6160c3a5702SAndreas Gohr     *
6170c3a5702SAndreas Gohr     * @param int $rev revision timestamp
6180c3a5702SAndreas Gohr     * @param int $max maximum number of revisions to be returned
6190c3a5702SAndreas Gohr     * @return bool|array
6200c3a5702SAndreas Gohr     *     return array with entries:
6210c3a5702SAndreas Gohr     *       - $requestedrevs: array of with $max revision timestamps
6220c3a5702SAndreas Gohr     *       - $revs: all parsed revision timestamps
6230c3a5702SAndreas Gohr     *       - $fp: filepointer only defined for chuck reading, needs closing.
6240c3a5702SAndreas Gohr     *       - $lines: non-parsed changelog lines before the parsed revisions
6250c3a5702SAndreas Gohr     *       - $head: position of first readed changelogline
6260c3a5702SAndreas Gohr     *       - $lasttail: position of end of last readed changelogline
6270c3a5702SAndreas Gohr     *     otherwise false
6280c3a5702SAndreas Gohr     */
6290c3a5702SAndreas Gohr    protected function retrieveRevisionsAround($rev, $max)
6300c3a5702SAndreas Gohr    {
631a3984ddfSSatoshi Sahara        $lastChangelogRev = 0;
632a3984ddfSSatoshi Sahara        $externaleditRevinfo = $this->getExternalEditRevInfo();
633a3984ddfSSatoshi Sahara
634a3984ddfSSatoshi Sahara        if ($externaleditRevinfo) {
635a3984ddfSSatoshi Sahara            $revisions = $this->getRevisions(-1, 1);
636a3984ddfSSatoshi Sahara            $lastChangelogRev = $revisions[0];
637a3984ddfSSatoshi Sahara            if ($externaleditRevinfo['date'] == $rev) {
638a3984ddfSSatoshi Sahara                $rev = $lastChangelogRev; //replace by an existing changelog line
639a3984ddfSSatoshi Sahara            }
640a3984ddfSSatoshi Sahara        }
641a3984ddfSSatoshi Sahara
642a3984ddfSSatoshi Sahara        $revs = array();
643a3984ddfSSatoshi Sahara        $aftercount = $beforecount = 0;
644a3984ddfSSatoshi Sahara
6450c3a5702SAndreas Gohr        //get lines from changelog
6460c3a5702SAndreas Gohr        list($fp, $lines, $starthead, $starttail, /* $eof */) = $this->readloglines($rev);
647a3984ddfSSatoshi Sahara        if (empty($lines)) {
648a3984ddfSSatoshi Sahara            if ($externaleditRevinfo) {
649a3984ddfSSatoshi Sahara                $revs[] = $externaleditRevinfo['date'];
650a3984ddfSSatoshi Sahara                return array($revs, $revs, false, [], 0, 0);
651a3984ddfSSatoshi Sahara            } else {
652a3984ddfSSatoshi Sahara                return false;
653a3984ddfSSatoshi Sahara            }
654a3984ddfSSatoshi Sahara        }
6550c3a5702SAndreas Gohr
6560c3a5702SAndreas Gohr        //parse chunk containing $rev, and read forward more chunks until $max/2 is reached
6570c3a5702SAndreas Gohr        $head = $starthead;
6580c3a5702SAndreas Gohr        $tail = $starttail;
6590c3a5702SAndreas Gohr        while (count($lines) > 0) {
6600c3a5702SAndreas Gohr            foreach ($lines as $line) {
6610c3a5702SAndreas Gohr                $tmp = parseChangelogLine($line);
6620c3a5702SAndreas Gohr                if ($tmp !== false) {
6630c3a5702SAndreas Gohr                    $this->cache[$this->id][$tmp['date']] = $tmp;
6640c3a5702SAndreas Gohr                    $revs[] = $tmp['date'];
665a3984ddfSSatoshi Sahara                    //add external edit next to the first existing line from the changelog
666a3984ddfSSatoshi Sahara                    if ($externaleditRevinfo && $tmp['date'] == $lastChangelogRev) {
667a3984ddfSSatoshi Sahara                        $revs[] = $externaleditRevinfo['date'];
668a3984ddfSSatoshi Sahara                    }
6690c3a5702SAndreas Gohr                    if ($tmp['date'] >= $rev) {
6700c3a5702SAndreas Gohr                        //count revs after reference $rev
6710c3a5702SAndreas Gohr                        $aftercount++;
6720c3a5702SAndreas Gohr                        if ($aftercount == 1) $beforecount = count($revs);
6730c3a5702SAndreas Gohr                    }
6740c3a5702SAndreas Gohr                    //enough revs after reference $rev?
6750c3a5702SAndreas Gohr                    if ($aftercount > floor($max / 2)) break 2;
6760c3a5702SAndreas Gohr                }
6770c3a5702SAndreas Gohr            }
6780c3a5702SAndreas Gohr            //retrieve next chunk
6790c3a5702SAndreas Gohr            list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, 1);
6800c3a5702SAndreas Gohr        }
6810c3a5702SAndreas Gohr        if ($aftercount == 0) return false;
6820c3a5702SAndreas Gohr
6830c3a5702SAndreas Gohr        $lasttail = $tail;
6840c3a5702SAndreas Gohr
6850c3a5702SAndreas Gohr        //read additional chuncks backward until $max/2 is reached and total number of revs is equal to $max
6860c3a5702SAndreas Gohr        $lines = array();
6870c3a5702SAndreas Gohr        $i = 0;
6880c3a5702SAndreas Gohr        if ($aftercount > 0) {
6890c3a5702SAndreas Gohr            $head = $starthead;
6900c3a5702SAndreas Gohr            $tail = $starttail;
6910c3a5702SAndreas Gohr            while ($head > 0) {
6920c3a5702SAndreas Gohr                list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, -1);
6930c3a5702SAndreas Gohr
6940c3a5702SAndreas Gohr                for ($i = count($lines) - 1; $i >= 0; $i--) {
6950c3a5702SAndreas Gohr                    $tmp = parseChangelogLine($lines[$i]);
6960c3a5702SAndreas Gohr                    if ($tmp !== false) {
6970c3a5702SAndreas Gohr                        $this->cache[$this->id][$tmp['date']] = $tmp;
6980c3a5702SAndreas Gohr                        $revs[] = $tmp['date'];
6990c3a5702SAndreas Gohr                        $beforecount++;
7000c3a5702SAndreas Gohr                        //enough revs before reference $rev?
7010c3a5702SAndreas Gohr                        if ($beforecount > max(floor($max / 2), $max - $aftercount)) break 2;
7020c3a5702SAndreas Gohr                    }
7030c3a5702SAndreas Gohr                }
7040c3a5702SAndreas Gohr            }
7050c3a5702SAndreas Gohr        }
7060c3a5702SAndreas Gohr        sort($revs);
7070c3a5702SAndreas Gohr
7080c3a5702SAndreas Gohr        //keep only non-parsed lines
7090c3a5702SAndreas Gohr        $lines = array_slice($lines, 0, $i);
7100c3a5702SAndreas Gohr        //trunk desired selection
7110c3a5702SAndreas Gohr        $requestedrevs = array_slice($revs, -$max, $max);
7120c3a5702SAndreas Gohr
7130c3a5702SAndreas Gohr        return array($requestedrevs, $revs, $fp, $lines, $head, $lasttail);
7140c3a5702SAndreas Gohr    }
715a3984ddfSSatoshi Sahara
716a3984ddfSSatoshi Sahara    /**
717a3984ddfSSatoshi Sahara     * Returns revision logline in same format as @see ChangeLog::getRevisionInfo()
718a3984ddfSSatoshi Sahara     *
719a3984ddfSSatoshi Sahara     * @return bool|array false if not external edit/deletion, otherwise array with entries:
720a3984ddfSSatoshi Sahara     *      - date:  unix timestamp for external edit or 'unknown' for external deletion
721a3984ddfSSatoshi Sahara     *      - ip:    IPv4 address (127.0.0.1)
722a3984ddfSSatoshi Sahara     *      - type:  log line type
723a3984ddfSSatoshi Sahara     *      - id:    page id
724a3984ddfSSatoshi Sahara     *      - user:  user name
725a3984ddfSSatoshi Sahara     *      - sum:   edit summary (or action reason)
726a3984ddfSSatoshi Sahara     *      - extra: extra data (varies by line type)
727*162709daSSatoshi Sahara     *      - sizechange: change of filesize
728e49fa56bSSatoshi Sahara     *
729e49fa56bSSatoshi Sahara     * @author  Gerrit Uitslag <klapinklapin@gmail.com>
730a3984ddfSSatoshi Sahara     */
731a3984ddfSSatoshi Sahara    public function getExternalEditRevInfo()
732a3984ddfSSatoshi Sahara    {
733a3984ddfSSatoshi Sahara        global $lang;
734a3984ddfSSatoshi Sahara        global $cache_externaledit; //caches external edits per page
735a3984ddfSSatoshi Sahara
736*162709daSSatoshi Sahara        if ($this->flags['ignore_external_edit']) return false;
737*162709daSSatoshi Sahara
738a3984ddfSSatoshi Sahara        // check if it's already in the memory cache
739a3984ddfSSatoshi Sahara        if (isset($cache_externaledit[$this->id])) {
740a3984ddfSSatoshi Sahara            if ($cache_externaledit[$this->id] === false) {
741a3984ddfSSatoshi Sahara                return false;
742a3984ddfSSatoshi Sahara            } else {
743a3984ddfSSatoshi Sahara                return $this->cache[$this->id][$cache_externaledit[$this->id]];
744a3984ddfSSatoshi Sahara            }
745a3984ddfSSatoshi Sahara        }
746a3984ddfSSatoshi Sahara        $externaleditRevinfo = false;
747a3984ddfSSatoshi Sahara        $cache_externaledit[$this->id] = false;
748a3984ddfSSatoshi Sahara
749a3984ddfSSatoshi Sahara        //in attic no revision of current existing wiki page, so external edit occurred
750e49fa56bSSatoshi Sahara        $fileLastMod = $this->getFilename();
751a3984ddfSSatoshi Sahara        $lastMod = @filemtime($fileLastMod); // from wiki page, suppresses warning in case the file not exists
752a3984ddfSSatoshi Sahara        $lastRev = $this->getRevisions(-1, 1); // from changelog
753a3984ddfSSatoshi Sahara        $lastRev = (int) (empty($lastRev) ? 0 : $lastRev[0]);
754e49fa56bSSatoshi Sahara        if (!file_exists($this->getFilename($lastMod)) && file_exists($fileLastMod) && $lastRev < $lastMod) {
755a3984ddfSSatoshi Sahara            $cache_externaledit[$this->id] = $lastMod;
756*162709daSSatoshi Sahara            $fileLastRev = $this->getFilename($lastRev); //returns current wikipage path if $lastRev==false
757a3984ddfSSatoshi Sahara            $revinfo = $this->getRevisionInfo($lastRev);
758a3984ddfSSatoshi Sahara            if (empty($lastRev) || !file_exists($fileLastRev) || $revinfo['type'] == DOKU_CHANGE_TYPE_DELETE) {
759a3984ddfSSatoshi Sahara                $filesize_old = 0;
760a3984ddfSSatoshi Sahara            } else {
761a3984ddfSSatoshi Sahara                $filesize_old = io_getSizeFile($fileLastRev);
762a3984ddfSSatoshi Sahara            }
763a3984ddfSSatoshi Sahara            $filesize_new = filesize($fileLastMod);
764a3984ddfSSatoshi Sahara            $sizechange = $filesize_new - $filesize_old;
765a3984ddfSSatoshi Sahara            $isJustCreated = empty($lastRev) || !file_exists($fileLastRev);
766a3984ddfSSatoshi Sahara
767a3984ddfSSatoshi Sahara            $externaleditRevinfo = [
768a3984ddfSSatoshi Sahara                'date' => $lastMod,
769a3984ddfSSatoshi Sahara                'ip'   => '127.0.0.1',
770a3984ddfSSatoshi Sahara                'type' => $isJustCreated ? DOKU_CHANGE_TYPE_CREATE : DOKU_CHANGE_TYPE_EDIT,
771a3984ddfSSatoshi Sahara                'id'   => $this->id,
772a3984ddfSSatoshi Sahara                'user' => '',
773a3984ddfSSatoshi Sahara                'sum'  => ($isJustCreated ? $lang['created'] .' - ' : '') . $lang['external_edit'],
774a3984ddfSSatoshi Sahara                'extra' => '',
775a3984ddfSSatoshi Sahara                'sizechange' => $sizechange
776a3984ddfSSatoshi Sahara            ];
777a3984ddfSSatoshi Sahara            $cache_externaledit[$this->id] = $externaleditRevinfo['date'];
778a3984ddfSSatoshi Sahara            $this->cache[$this->id][$externaleditRevinfo['date']] = $externaleditRevinfo;
779a3984ddfSSatoshi Sahara        }
780a3984ddfSSatoshi Sahara
781a3984ddfSSatoshi Sahara        $revinfo = $this->getRevisionInfo($lastRev);
782a3984ddfSSatoshi Sahara        //deleted wiki page, but not registered in changelog
783a3984ddfSSatoshi Sahara        if (!file_exists($fileLastMod) // there is no current page=>true
784a3984ddfSSatoshi Sahara            && !empty($lastRev) && $revinfo['type'] !== DOKU_CHANGE_TYPE_DELETE) {
785e49fa56bSSatoshi Sahara            $fileLastRev = $this->getFilename($lastRev);
786a3984ddfSSatoshi Sahara            $externaleditRevinfo = [
787a3984ddfSSatoshi Sahara                'date' => 9999999999, //unknown deletion date, always higher as latest rev
788a3984ddfSSatoshi Sahara                'ip'   => '127.0.0.1',
789a3984ddfSSatoshi Sahara                'type' => DOKU_CHANGE_TYPE_DELETE,
790a3984ddfSSatoshi Sahara                'id'   => $this->id,
791a3984ddfSSatoshi Sahara                'user' => '',
792a3984ddfSSatoshi Sahara                'sum'  => $lang['deleted']. ' - ' . $lang['external_edit'],
793a3984ddfSSatoshi Sahara                'extra' => '',
794a3984ddfSSatoshi Sahara                'sizechange' => -io_getSizeFile($fileLastRev)
795a3984ddfSSatoshi Sahara            ];
796a3984ddfSSatoshi Sahara            $cache_externaledit[$this->id] = $externaleditRevinfo['date'];
797a3984ddfSSatoshi Sahara            $this->cache[$this->id][$externaleditRevinfo['date']] = $externaleditRevinfo;
798a3984ddfSSatoshi Sahara        }
799a3984ddfSSatoshi Sahara
800a3984ddfSSatoshi Sahara        return $externaleditRevinfo;
801a3984ddfSSatoshi Sahara    }
8020c3a5702SAndreas Gohr}
803