xref: /dokuwiki/inc/ChangeLog/ChangeLog.php (revision 923e149a1ff0b1a20a788f3f4d615b8877370532)
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
110c3a5702SAndreas Gohr    /** @var string */
120c3a5702SAndreas Gohr    protected $id;
130c3a5702SAndreas Gohr    /** @var int */
140c3a5702SAndreas Gohr    protected $chunk_size;
150c3a5702SAndreas Gohr    /** @var array */
160c3a5702SAndreas Gohr    protected $cache;
170c3a5702SAndreas Gohr
180c3a5702SAndreas Gohr    /**
190c3a5702SAndreas Gohr     * Constructor
200c3a5702SAndreas Gohr     *
210c3a5702SAndreas Gohr     * @param string $id page id
220c3a5702SAndreas Gohr     * @param int $chunk_size maximum block size read from file
230c3a5702SAndreas Gohr     */
240c3a5702SAndreas Gohr    public function __construct($id, $chunk_size = 8192)
250c3a5702SAndreas Gohr    {
260c3a5702SAndreas Gohr        global $cache_revinfo;
270c3a5702SAndreas Gohr
280c3a5702SAndreas Gohr        $this->cache =& $cache_revinfo;
290c3a5702SAndreas Gohr        if (!isset($this->cache[$id])) {
300c3a5702SAndreas Gohr            $this->cache[$id] = array();
310c3a5702SAndreas Gohr        }
320c3a5702SAndreas Gohr
330c3a5702SAndreas Gohr        $this->id = $id;
340c3a5702SAndreas Gohr        $this->setChunkSize($chunk_size);
350c3a5702SAndreas Gohr
360c3a5702SAndreas Gohr    }
370c3a5702SAndreas Gohr
380c3a5702SAndreas Gohr    /**
390c3a5702SAndreas Gohr     * Set chunk size for file reading
400c3a5702SAndreas Gohr     * Chunk size zero let read whole file at once
410c3a5702SAndreas Gohr     *
420c3a5702SAndreas Gohr     * @param int $chunk_size maximum block size read from file
430c3a5702SAndreas Gohr     */
440c3a5702SAndreas Gohr    public function setChunkSize($chunk_size)
450c3a5702SAndreas Gohr    {
460c3a5702SAndreas Gohr        if (!is_numeric($chunk_size)) $chunk_size = 0;
470c3a5702SAndreas Gohr
480c3a5702SAndreas Gohr        $this->chunk_size = (int)max($chunk_size, 0);
490c3a5702SAndreas Gohr    }
500c3a5702SAndreas Gohr
510c3a5702SAndreas Gohr    /**
520c3a5702SAndreas Gohr     * Returns path to changelog
530c3a5702SAndreas Gohr     *
540c3a5702SAndreas Gohr     * @return string path to file
550c3a5702SAndreas Gohr     */
560c3a5702SAndreas Gohr    abstract protected function getChangelogFilename();
570c3a5702SAndreas Gohr
580c3a5702SAndreas Gohr    /**
590c3a5702SAndreas Gohr     * Returns path to current page/media
600c3a5702SAndreas Gohr     *
610c3a5702SAndreas Gohr     * @return string path to file
620c3a5702SAndreas Gohr     */
630c3a5702SAndreas Gohr    abstract protected function getFilename();
640c3a5702SAndreas Gohr
650c3a5702SAndreas Gohr    /**
660c3a5702SAndreas Gohr     * Get the changelog information for a specific page id and revision (timestamp)
670c3a5702SAndreas Gohr     *
680c3a5702SAndreas Gohr     * Adjacent changelog lines are optimistically parsed and cached to speed up
690c3a5702SAndreas Gohr     * consecutive calls to getRevisionInfo. For large changelog files, only the chunk
700c3a5702SAndreas Gohr     * containing the requested changelog line is read.
710c3a5702SAndreas Gohr     *
720c3a5702SAndreas Gohr     * @param int $rev revision timestamp
730c3a5702SAndreas Gohr     * @return bool|array false or array with entries:
740c3a5702SAndreas Gohr     *      - date:  unix timestamp
750c3a5702SAndreas Gohr     *      - ip:    IPv4 address (127.0.0.1)
760c3a5702SAndreas Gohr     *      - type:  log line type
770c3a5702SAndreas Gohr     *      - id:    page id
780c3a5702SAndreas Gohr     *      - user:  user name
790c3a5702SAndreas Gohr     *      - sum:   edit summary (or action reason)
800c3a5702SAndreas Gohr     *      - extra: extra data (varies by line type)
810c3a5702SAndreas Gohr     *
820c3a5702SAndreas Gohr     * @author Ben Coburn <btcoburn@silicodon.net>
830c3a5702SAndreas Gohr     * @author Kate Arzamastseva <pshns@ukr.net>
840c3a5702SAndreas Gohr     */
850c3a5702SAndreas Gohr    public function getRevisionInfo($rev)
860c3a5702SAndreas Gohr    {
870c3a5702SAndreas Gohr        $rev = max($rev, 0);
880c3a5702SAndreas Gohr
890c3a5702SAndreas Gohr        // check if it's already in the memory cache
900c3a5702SAndreas Gohr        if (isset($this->cache[$this->id]) && isset($this->cache[$this->id][$rev])) {
910c3a5702SAndreas Gohr            return $this->cache[$this->id][$rev];
920c3a5702SAndreas Gohr        }
930c3a5702SAndreas Gohr
940c3a5702SAndreas Gohr        //read lines from changelog
950c3a5702SAndreas Gohr        list($fp, $lines) = $this->readloglines($rev);
960c3a5702SAndreas Gohr        if ($fp) {
970c3a5702SAndreas Gohr            fclose($fp);
980c3a5702SAndreas Gohr        }
990c3a5702SAndreas Gohr        if (empty($lines)) return false;
1000c3a5702SAndreas Gohr
1010c3a5702SAndreas Gohr        // parse and cache changelog lines
1020c3a5702SAndreas Gohr        foreach ($lines as $value) {
1030c3a5702SAndreas Gohr            $tmp = parseChangelogLine($value);
1040c3a5702SAndreas Gohr            if ($tmp !== false) {
1050c3a5702SAndreas Gohr                $this->cache[$this->id][$tmp['date']] = $tmp;
1060c3a5702SAndreas Gohr            }
1070c3a5702SAndreas Gohr        }
1080c3a5702SAndreas Gohr        if (!isset($this->cache[$this->id][$rev])) {
1090c3a5702SAndreas Gohr            return false;
1100c3a5702SAndreas Gohr        }
1110c3a5702SAndreas Gohr        return $this->cache[$this->id][$rev];
1120c3a5702SAndreas Gohr    }
1130c3a5702SAndreas Gohr
1140c3a5702SAndreas Gohr    /**
1150c3a5702SAndreas Gohr     * Return a list of page revisions numbers
1160c3a5702SAndreas Gohr     *
1170c3a5702SAndreas Gohr     * Does not guarantee that the revision exists in the attic,
1180c3a5702SAndreas Gohr     * only that a line with the date exists in the changelog.
1190c3a5702SAndreas Gohr     * By default the current revision is skipped.
1200c3a5702SAndreas Gohr     *
1210c3a5702SAndreas Gohr     * The current revision is automatically skipped when the page exists.
1220c3a5702SAndreas Gohr     * See $INFO['meta']['last_change'] for the current revision.
1230c3a5702SAndreas Gohr     * A negative $first let read the current revision too.
1240c3a5702SAndreas Gohr     *
1250c3a5702SAndreas Gohr     * For efficiency, the log lines are parsed and cached for later
1260c3a5702SAndreas Gohr     * calls to getRevisionInfo. Large changelog files are read
1270c3a5702SAndreas Gohr     * backwards in chunks until the requested number of changelog
1280c3a5702SAndreas Gohr     * lines are recieved.
1290c3a5702SAndreas Gohr     *
1300c3a5702SAndreas Gohr     * @param int $first skip the first n changelog lines
1310c3a5702SAndreas Gohr     * @param int $num number of revisions to return
1320c3a5702SAndreas Gohr     * @return array with the revision timestamps
1330c3a5702SAndreas Gohr     *
1340c3a5702SAndreas Gohr     * @author Ben Coburn <btcoburn@silicodon.net>
1350c3a5702SAndreas Gohr     * @author Kate Arzamastseva <pshns@ukr.net>
1360c3a5702SAndreas Gohr     */
1370c3a5702SAndreas Gohr    public function getRevisions($first, $num)
1380c3a5702SAndreas Gohr    {
1390c3a5702SAndreas Gohr        $revs = array();
1400c3a5702SAndreas Gohr        $lines = array();
1410c3a5702SAndreas Gohr        $count = 0;
1420c3a5702SAndreas Gohr
1430c3a5702SAndreas Gohr        $num = max($num, 0);
1440c3a5702SAndreas Gohr        if ($num == 0) {
1450c3a5702SAndreas Gohr            return $revs;
1460c3a5702SAndreas Gohr        }
1470c3a5702SAndreas Gohr
1480c3a5702SAndreas Gohr        if ($first < 0) {
1490c3a5702SAndreas Gohr            $first = 0;
1500c3a5702SAndreas Gohr        } else {
1510c3a5702SAndreas Gohr            if (file_exists($this->getFilename())) {
1520c3a5702SAndreas Gohr                // skip current revision if the page exists
1530c3a5702SAndreas Gohr                $first = max($first + 1, 0);
1540c3a5702SAndreas Gohr            }
1550c3a5702SAndreas Gohr        }
1560c3a5702SAndreas Gohr
1570c3a5702SAndreas Gohr        $file = $this->getChangelogFilename();
1580c3a5702SAndreas Gohr
1590c3a5702SAndreas Gohr        if (!file_exists($file)) {
1600c3a5702SAndreas Gohr            return $revs;
1610c3a5702SAndreas Gohr        }
1620c3a5702SAndreas Gohr        if (filesize($file) < $this->chunk_size || $this->chunk_size == 0) {
1630c3a5702SAndreas Gohr            // read whole file
1640c3a5702SAndreas Gohr            $lines = file($file);
1650c3a5702SAndreas Gohr            if ($lines === false) {
1660c3a5702SAndreas Gohr                return $revs;
1670c3a5702SAndreas Gohr            }
1680c3a5702SAndreas Gohr        } else {
1690c3a5702SAndreas Gohr            // read chunks backwards
1700c3a5702SAndreas Gohr            $fp = fopen($file, 'rb'); // "file pointer"
1710c3a5702SAndreas Gohr            if ($fp === false) {
1720c3a5702SAndreas Gohr                return $revs;
1730c3a5702SAndreas Gohr            }
1740c3a5702SAndreas Gohr            fseek($fp, 0, SEEK_END);
1750c3a5702SAndreas Gohr            $tail = ftell($fp);
1760c3a5702SAndreas Gohr
1770c3a5702SAndreas Gohr            // chunk backwards
1780c3a5702SAndreas Gohr            $finger = max($tail - $this->chunk_size, 0);
1790c3a5702SAndreas Gohr            while ($count < $num + $first) {
1800c3a5702SAndreas Gohr                $nl = $this->getNewlinepointer($fp, $finger);
1810c3a5702SAndreas Gohr
1820c3a5702SAndreas Gohr                // was the chunk big enough? if not, take another bite
1830c3a5702SAndreas Gohr                if ($nl > 0 && $tail <= $nl) {
1840c3a5702SAndreas Gohr                    $finger = max($finger - $this->chunk_size, 0);
1850c3a5702SAndreas Gohr                    continue;
1860c3a5702SAndreas Gohr                } else {
1870c3a5702SAndreas Gohr                    $finger = $nl;
1880c3a5702SAndreas Gohr                }
1890c3a5702SAndreas Gohr
1900c3a5702SAndreas Gohr                // read chunk
1910c3a5702SAndreas Gohr                $chunk = '';
1920c3a5702SAndreas Gohr                $read_size = max($tail - $finger, 0); // found chunk size
1930c3a5702SAndreas Gohr                $got = 0;
1940c3a5702SAndreas Gohr                while ($got < $read_size && !feof($fp)) {
1950c3a5702SAndreas Gohr                    $tmp = @fread($fp, max(min($this->chunk_size, $read_size - $got), 0));
1960c3a5702SAndreas Gohr                    if ($tmp === false) {
1970c3a5702SAndreas Gohr                        break;
1980c3a5702SAndreas Gohr                    } //error state
1990c3a5702SAndreas Gohr                    $got += strlen($tmp);
2000c3a5702SAndreas Gohr                    $chunk .= $tmp;
2010c3a5702SAndreas Gohr                }
2020c3a5702SAndreas Gohr                $tmp = explode("\n", $chunk);
2030c3a5702SAndreas Gohr                array_pop($tmp); // remove trailing newline
2040c3a5702SAndreas Gohr
2050c3a5702SAndreas Gohr                // combine with previous chunk
2060c3a5702SAndreas Gohr                $count += count($tmp);
2070c3a5702SAndreas Gohr                $lines = array_merge($tmp, $lines);
2080c3a5702SAndreas Gohr
2090c3a5702SAndreas Gohr                // next chunk
2100c3a5702SAndreas Gohr                if ($finger == 0) {
2110c3a5702SAndreas Gohr                    break;
2120c3a5702SAndreas Gohr                } // already read all the lines
2130c3a5702SAndreas Gohr                else {
2140c3a5702SAndreas Gohr                    $tail = $finger;
2150c3a5702SAndreas Gohr                    $finger = max($tail - $this->chunk_size, 0);
2160c3a5702SAndreas Gohr                }
2170c3a5702SAndreas Gohr            }
2180c3a5702SAndreas Gohr            fclose($fp);
2190c3a5702SAndreas Gohr        }
2200c3a5702SAndreas Gohr
2210c3a5702SAndreas Gohr        // skip parsing extra lines
2220c3a5702SAndreas Gohr        $num = max(min(count($lines) - $first, $num), 0);
2230c3a5702SAndreas Gohr        if ($first > 0 && $num > 0) {
2240c3a5702SAndreas Gohr            $lines = array_slice($lines, max(count($lines) - $first - $num, 0), $num);
2250c3a5702SAndreas Gohr        } else {
2260c3a5702SAndreas Gohr            if ($first > 0 && $num == 0) {
2270c3a5702SAndreas Gohr                $lines = array_slice($lines, 0, max(count($lines) - $first, 0));
2280c3a5702SAndreas Gohr            } elseif ($first == 0 && $num > 0) {
2290c3a5702SAndreas Gohr                $lines = array_slice($lines, max(count($lines) - $num, 0));
2300c3a5702SAndreas Gohr            }
2310c3a5702SAndreas Gohr        }
2320c3a5702SAndreas Gohr
2330c3a5702SAndreas Gohr        // handle lines in reverse order
2340c3a5702SAndreas Gohr        for ($i = count($lines) - 1; $i >= 0; $i--) {
2350c3a5702SAndreas Gohr            $tmp = parseChangelogLine($lines[$i]);
2360c3a5702SAndreas Gohr            if ($tmp !== false) {
2370c3a5702SAndreas Gohr                $this->cache[$this->id][$tmp['date']] = $tmp;
2380c3a5702SAndreas Gohr                $revs[] = $tmp['date'];
2390c3a5702SAndreas Gohr            }
2400c3a5702SAndreas Gohr        }
2410c3a5702SAndreas Gohr
2420c3a5702SAndreas Gohr        return $revs;
2430c3a5702SAndreas Gohr    }
2440c3a5702SAndreas Gohr
2450c3a5702SAndreas Gohr    /**
2460c3a5702SAndreas Gohr     * Get the nth revision left or right handside  for a specific page id and revision (timestamp)
2470c3a5702SAndreas Gohr     *
2480c3a5702SAndreas Gohr     * For large changelog files, only the chunk containing the
2490c3a5702SAndreas Gohr     * reference revision $rev is read and sometimes a next chunck.
2500c3a5702SAndreas Gohr     *
2510c3a5702SAndreas Gohr     * Adjacent changelog lines are optimistically parsed and cached to speed up
2520c3a5702SAndreas Gohr     * consecutive calls to getRevisionInfo.
2530c3a5702SAndreas Gohr     *
2540c3a5702SAndreas Gohr     * @param int $rev revision timestamp used as startdate (doesn't need to be revisionnumber)
2550c3a5702SAndreas Gohr     * @param int $direction give position of returned revision with respect to $rev; positive=next, negative=prev
2560c3a5702SAndreas Gohr     * @return bool|int
2570c3a5702SAndreas Gohr     *      timestamp of the requested revision
2580c3a5702SAndreas Gohr     *      otherwise false
2590c3a5702SAndreas Gohr     */
2600c3a5702SAndreas Gohr    public function getRelativeRevision($rev, $direction)
2610c3a5702SAndreas Gohr    {
2620c3a5702SAndreas Gohr        $rev = max($rev, 0);
2630c3a5702SAndreas Gohr        $direction = (int)$direction;
2640c3a5702SAndreas Gohr
2650c3a5702SAndreas Gohr        //no direction given or last rev, so no follow-up
2660c3a5702SAndreas Gohr        if (!$direction || ($direction > 0 && $this->isCurrentRevision($rev))) {
2670c3a5702SAndreas Gohr            return false;
2680c3a5702SAndreas Gohr        }
2690c3a5702SAndreas Gohr
2700c3a5702SAndreas Gohr        //get lines from changelog
2710c3a5702SAndreas Gohr        list($fp, $lines, $head, $tail, $eof) = $this->readloglines($rev);
2720c3a5702SAndreas Gohr        if (empty($lines)) return false;
2730c3a5702SAndreas Gohr
2740c3a5702SAndreas Gohr        // look for revisions later/earlier then $rev, when founded count till the wanted revision is reached
2750c3a5702SAndreas Gohr        // also parse and cache changelog lines for getRevisionInfo().
2760c3a5702SAndreas Gohr        $revcounter = 0;
2770c3a5702SAndreas Gohr        $relativerev = false;
2780c3a5702SAndreas Gohr        $checkotherchunck = true; //always runs once
2790c3a5702SAndreas Gohr        while (!$relativerev && $checkotherchunck) {
2800c3a5702SAndreas Gohr            $tmp = array();
2810c3a5702SAndreas Gohr            //parse in normal or reverse order
2820c3a5702SAndreas Gohr            $count = count($lines);
2830c3a5702SAndreas Gohr            if ($direction > 0) {
2840c3a5702SAndreas Gohr                $start = 0;
2850c3a5702SAndreas Gohr                $step = 1;
2860c3a5702SAndreas Gohr            } else {
2870c3a5702SAndreas Gohr                $start = $count - 1;
2880c3a5702SAndreas Gohr                $step = -1;
2890c3a5702SAndreas Gohr            }
2900c3a5702SAndreas Gohr            for ($i = $start; $i >= 0 && $i < $count; $i = $i + $step) {
2910c3a5702SAndreas Gohr                $tmp = parseChangelogLine($lines[$i]);
2920c3a5702SAndreas Gohr                if ($tmp !== false) {
2930c3a5702SAndreas Gohr                    $this->cache[$this->id][$tmp['date']] = $tmp;
2940c3a5702SAndreas Gohr                    //look for revs older/earlier then reference $rev and select $direction-th one
2950c3a5702SAndreas Gohr                    if (($direction > 0 && $tmp['date'] > $rev) || ($direction < 0 && $tmp['date'] < $rev)) {
2960c3a5702SAndreas Gohr                        $revcounter++;
2970c3a5702SAndreas Gohr                        if ($revcounter == abs($direction)) {
2980c3a5702SAndreas Gohr                            $relativerev = $tmp['date'];
2990c3a5702SAndreas Gohr                        }
3000c3a5702SAndreas Gohr                    }
3010c3a5702SAndreas Gohr                }
3020c3a5702SAndreas Gohr            }
3030c3a5702SAndreas Gohr
3040c3a5702SAndreas Gohr            //true when $rev is found, but not the wanted follow-up.
3050c3a5702SAndreas Gohr            $checkotherchunck = $fp
3060c3a5702SAndreas Gohr                && ($tmp['date'] == $rev || ($revcounter > 0 && !$relativerev))
3070c3a5702SAndreas Gohr                && !(($tail == $eof && $direction > 0) || ($head == 0 && $direction < 0));
3080c3a5702SAndreas Gohr
3090c3a5702SAndreas Gohr            if ($checkotherchunck) {
3100c3a5702SAndreas Gohr                list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, $direction);
3110c3a5702SAndreas Gohr
3120c3a5702SAndreas Gohr                if (empty($lines)) break;
3130c3a5702SAndreas Gohr            }
3140c3a5702SAndreas Gohr        }
3150c3a5702SAndreas Gohr        if ($fp) {
3160c3a5702SAndreas Gohr            fclose($fp);
3170c3a5702SAndreas Gohr        }
3180c3a5702SAndreas Gohr
3190c3a5702SAndreas Gohr        return $relativerev;
3200c3a5702SAndreas Gohr    }
3210c3a5702SAndreas Gohr
3220c3a5702SAndreas Gohr    /**
3230c3a5702SAndreas Gohr     * Returns revisions around rev1 and rev2
3240c3a5702SAndreas Gohr     * When available it returns $max entries for each revision
3250c3a5702SAndreas Gohr     *
3260c3a5702SAndreas Gohr     * @param int $rev1 oldest revision timestamp
3270c3a5702SAndreas Gohr     * @param int $rev2 newest revision timestamp (0 looks up last revision)
3280c3a5702SAndreas Gohr     * @param int $max maximum number of revisions returned
3290c3a5702SAndreas Gohr     * @return array with two arrays with revisions surrounding rev1 respectively rev2
3300c3a5702SAndreas Gohr     */
3310c3a5702SAndreas Gohr    public function getRevisionsAround($rev1, $rev2, $max = 50)
3320c3a5702SAndreas Gohr    {
3330c3a5702SAndreas Gohr        $max = floor(abs($max) / 2) * 2 + 1;
3340c3a5702SAndreas Gohr        $rev1 = max($rev1, 0);
3350c3a5702SAndreas Gohr        $rev2 = max($rev2, 0);
3360c3a5702SAndreas Gohr
3370c3a5702SAndreas Gohr        if ($rev2) {
3380c3a5702SAndreas Gohr            if ($rev2 < $rev1) {
3390c3a5702SAndreas Gohr                $rev = $rev2;
3400c3a5702SAndreas Gohr                $rev2 = $rev1;
3410c3a5702SAndreas Gohr                $rev1 = $rev;
3420c3a5702SAndreas Gohr            }
3430c3a5702SAndreas Gohr        } else {
3440c3a5702SAndreas Gohr            //empty right side means a removed page. Look up last revision.
3450c3a5702SAndreas Gohr            $revs = $this->getRevisions(-1, 1);
3460c3a5702SAndreas Gohr            $rev2 = $revs[0];
3470c3a5702SAndreas Gohr        }
3480c3a5702SAndreas Gohr        //collect revisions around rev2
3490c3a5702SAndreas Gohr        list($revs2, $allrevs, $fp, $lines, $head, $tail) = $this->retrieveRevisionsAround($rev2, $max);
3500c3a5702SAndreas Gohr
3510c3a5702SAndreas Gohr        if (empty($revs2)) return array(array(), array());
3520c3a5702SAndreas Gohr
3530c3a5702SAndreas Gohr        //collect revisions around rev1
3540c3a5702SAndreas Gohr        $index = array_search($rev1, $allrevs);
3550c3a5702SAndreas Gohr        if ($index === false) {
3560c3a5702SAndreas Gohr            //no overlapping revisions
3570c3a5702SAndreas Gohr            list($revs1, , , , ,) = $this->retrieveRevisionsAround($rev1, $max);
3580c3a5702SAndreas Gohr            if (empty($revs1)) $revs1 = array();
3590c3a5702SAndreas Gohr        } else {
3600c3a5702SAndreas Gohr            //revisions overlaps, reuse revisions around rev2
3610c3a5702SAndreas Gohr            $revs1 = $allrevs;
3620c3a5702SAndreas Gohr            while ($head > 0) {
3630c3a5702SAndreas Gohr                for ($i = count($lines) - 1; $i >= 0; $i--) {
3640c3a5702SAndreas Gohr                    $tmp = parseChangelogLine($lines[$i]);
3650c3a5702SAndreas Gohr                    if ($tmp !== false) {
3660c3a5702SAndreas Gohr                        $this->cache[$this->id][$tmp['date']] = $tmp;
3670c3a5702SAndreas Gohr                        $revs1[] = $tmp['date'];
3680c3a5702SAndreas Gohr                        $index++;
3690c3a5702SAndreas Gohr
3700c3a5702SAndreas Gohr                        if ($index > floor($max / 2)) break 2;
3710c3a5702SAndreas Gohr                    }
3720c3a5702SAndreas Gohr                }
3730c3a5702SAndreas Gohr
3740c3a5702SAndreas Gohr                list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, -1);
3750c3a5702SAndreas Gohr            }
3760c3a5702SAndreas Gohr            sort($revs1);
3770c3a5702SAndreas Gohr            //return wanted selection
3780c3a5702SAndreas Gohr            $revs1 = array_slice($revs1, max($index - floor($max / 2), 0), $max);
3790c3a5702SAndreas Gohr        }
3800c3a5702SAndreas Gohr
3810c3a5702SAndreas Gohr        return array(array_reverse($revs1), array_reverse($revs2));
3820c3a5702SAndreas Gohr    }
3830c3a5702SAndreas Gohr
384*923e149aSMichael Große
385*923e149aSMichael Große    /**
386*923e149aSMichael Große     * Checks if the ID has old revisons
387*923e149aSMichael Große     * @return boolean
388*923e149aSMichael Große     */
389*923e149aSMichael Große    public function hasRevisions() {
390*923e149aSMichael Große        $file = $this->getChangelogFilename();
391*923e149aSMichael Große        return file_exists($file);
392*923e149aSMichael Große    }
393*923e149aSMichael Große
3940c3a5702SAndreas Gohr    /**
3950c3a5702SAndreas Gohr     * Returns lines from changelog.
3960c3a5702SAndreas Gohr     * If file larger than $chuncksize, only chunck is read that could contain $rev.
3970c3a5702SAndreas Gohr     *
3980c3a5702SAndreas Gohr     * @param int $rev revision timestamp
3990c3a5702SAndreas Gohr     * @return array|false
4000c3a5702SAndreas Gohr     *     if success returns array(fp, array(changeloglines), $head, $tail, $eof)
4010c3a5702SAndreas Gohr     *     where fp only defined for chuck reading, needs closing.
4020c3a5702SAndreas Gohr     *     otherwise false
4030c3a5702SAndreas Gohr     */
4040c3a5702SAndreas Gohr    protected function readloglines($rev)
4050c3a5702SAndreas Gohr    {
4060c3a5702SAndreas Gohr        $file = $this->getChangelogFilename();
4070c3a5702SAndreas Gohr
4080c3a5702SAndreas Gohr        if (!file_exists($file)) {
4090c3a5702SAndreas Gohr            return false;
4100c3a5702SAndreas Gohr        }
4110c3a5702SAndreas Gohr
4120c3a5702SAndreas Gohr        $fp = null;
4130c3a5702SAndreas Gohr        $head = 0;
4140c3a5702SAndreas Gohr        $tail = 0;
4150c3a5702SAndreas Gohr        $eof = 0;
4160c3a5702SAndreas Gohr
4170c3a5702SAndreas Gohr        if (filesize($file) < $this->chunk_size || $this->chunk_size == 0) {
4180c3a5702SAndreas Gohr            // read whole file
4190c3a5702SAndreas Gohr            $lines = file($file);
4200c3a5702SAndreas Gohr            if ($lines === false) {
4210c3a5702SAndreas Gohr                return false;
4220c3a5702SAndreas Gohr            }
4230c3a5702SAndreas Gohr        } else {
4240c3a5702SAndreas Gohr            // read by chunk
4250c3a5702SAndreas Gohr            $fp = fopen($file, 'rb'); // "file pointer"
4260c3a5702SAndreas Gohr            if ($fp === false) {
4270c3a5702SAndreas Gohr                return false;
4280c3a5702SAndreas Gohr            }
4290c3a5702SAndreas Gohr            $head = 0;
4300c3a5702SAndreas Gohr            fseek($fp, 0, SEEK_END);
4310c3a5702SAndreas Gohr            $eof = ftell($fp);
4320c3a5702SAndreas Gohr            $tail = $eof;
4330c3a5702SAndreas Gohr
4340c3a5702SAndreas Gohr            // find chunk
4350c3a5702SAndreas Gohr            while ($tail - $head > $this->chunk_size) {
4360c3a5702SAndreas Gohr                $finger = $head + floor(($tail - $head) / 2.0);
4370c3a5702SAndreas Gohr                $finger = $this->getNewlinepointer($fp, $finger);
4380c3a5702SAndreas Gohr                $tmp = fgets($fp);
4390c3a5702SAndreas Gohr                if ($finger == $head || $finger == $tail) {
4400c3a5702SAndreas Gohr                    break;
4410c3a5702SAndreas Gohr                }
4420c3a5702SAndreas Gohr                $tmp = parseChangelogLine($tmp);
4430c3a5702SAndreas Gohr                $finger_rev = $tmp['date'];
4440c3a5702SAndreas Gohr
4450c3a5702SAndreas Gohr                if ($finger_rev > $rev) {
4460c3a5702SAndreas Gohr                    $tail = $finger;
4470c3a5702SAndreas Gohr                } else {
4480c3a5702SAndreas Gohr                    $head = $finger;
4490c3a5702SAndreas Gohr                }
4500c3a5702SAndreas Gohr            }
4510c3a5702SAndreas Gohr
4520c3a5702SAndreas Gohr            if ($tail - $head < 1) {
4530c3a5702SAndreas Gohr                // cound not find chunk, assume requested rev is missing
4540c3a5702SAndreas Gohr                fclose($fp);
4550c3a5702SAndreas Gohr                return false;
4560c3a5702SAndreas Gohr            }
4570c3a5702SAndreas Gohr
4580c3a5702SAndreas Gohr            $lines = $this->readChunk($fp, $head, $tail);
4590c3a5702SAndreas Gohr        }
4600c3a5702SAndreas Gohr        return array(
4610c3a5702SAndreas Gohr            $fp,
4620c3a5702SAndreas Gohr            $lines,
4630c3a5702SAndreas Gohr            $head,
4640c3a5702SAndreas Gohr            $tail,
4650c3a5702SAndreas Gohr            $eof,
4660c3a5702SAndreas Gohr        );
4670c3a5702SAndreas Gohr    }
4680c3a5702SAndreas Gohr
4690c3a5702SAndreas Gohr    /**
4700c3a5702SAndreas Gohr     * Read chunk and return array with lines of given chunck.
4710c3a5702SAndreas Gohr     * Has no check if $head and $tail are really at a new line
4720c3a5702SAndreas Gohr     *
4730c3a5702SAndreas Gohr     * @param resource $fp resource filepointer
4740c3a5702SAndreas Gohr     * @param int $head start point chunck
4750c3a5702SAndreas Gohr     * @param int $tail end point chunck
4760c3a5702SAndreas Gohr     * @return array lines read from chunck
4770c3a5702SAndreas Gohr     */
4780c3a5702SAndreas Gohr    protected function readChunk($fp, $head, $tail)
4790c3a5702SAndreas Gohr    {
4800c3a5702SAndreas Gohr        $chunk = '';
4810c3a5702SAndreas Gohr        $chunk_size = max($tail - $head, 0); // found chunk size
4820c3a5702SAndreas Gohr        $got = 0;
4830c3a5702SAndreas Gohr        fseek($fp, $head);
4840c3a5702SAndreas Gohr        while ($got < $chunk_size && !feof($fp)) {
4850c3a5702SAndreas Gohr            $tmp = @fread($fp, max(min($this->chunk_size, $chunk_size - $got), 0));
4860c3a5702SAndreas Gohr            if ($tmp === false) { //error state
4870c3a5702SAndreas Gohr                break;
4880c3a5702SAndreas Gohr            }
4890c3a5702SAndreas Gohr            $got += strlen($tmp);
4900c3a5702SAndreas Gohr            $chunk .= $tmp;
4910c3a5702SAndreas Gohr        }
4920c3a5702SAndreas Gohr        $lines = explode("\n", $chunk);
4930c3a5702SAndreas Gohr        array_pop($lines); // remove trailing newline
4940c3a5702SAndreas Gohr        return $lines;
4950c3a5702SAndreas Gohr    }
4960c3a5702SAndreas Gohr
4970c3a5702SAndreas Gohr    /**
4980c3a5702SAndreas Gohr     * Set pointer to first new line after $finger and return its position
4990c3a5702SAndreas Gohr     *
5000c3a5702SAndreas Gohr     * @param resource $fp filepointer
5010c3a5702SAndreas Gohr     * @param int $finger a pointer
5020c3a5702SAndreas Gohr     * @return int pointer
5030c3a5702SAndreas Gohr     */
5040c3a5702SAndreas Gohr    protected function getNewlinepointer($fp, $finger)
5050c3a5702SAndreas Gohr    {
5060c3a5702SAndreas Gohr        fseek($fp, $finger);
5070c3a5702SAndreas Gohr        $nl = $finger;
5080c3a5702SAndreas Gohr        if ($finger > 0) {
5090c3a5702SAndreas Gohr            fgets($fp); // slip the finger forward to a new line
5100c3a5702SAndreas Gohr            $nl = ftell($fp);
5110c3a5702SAndreas Gohr        }
5120c3a5702SAndreas Gohr        return $nl;
5130c3a5702SAndreas Gohr    }
5140c3a5702SAndreas Gohr
5150c3a5702SAndreas Gohr    /**
5160c3a5702SAndreas Gohr     * Check whether given revision is the current page
5170c3a5702SAndreas Gohr     *
5180c3a5702SAndreas Gohr     * @param int $rev timestamp of current page
5190c3a5702SAndreas Gohr     * @return bool true if $rev is current revision, otherwise false
5200c3a5702SAndreas Gohr     */
5210c3a5702SAndreas Gohr    public function isCurrentRevision($rev)
5220c3a5702SAndreas Gohr    {
5230c3a5702SAndreas Gohr        return $rev == @filemtime($this->getFilename());
5240c3a5702SAndreas Gohr    }
5250c3a5702SAndreas Gohr
5260c3a5702SAndreas Gohr    /**
5270c3a5702SAndreas Gohr     * Return an existing revision for a specific date which is
5280c3a5702SAndreas Gohr     * the current one or younger or equal then the date
5290c3a5702SAndreas Gohr     *
5300c3a5702SAndreas Gohr     * @param number $date_at timestamp
5310c3a5702SAndreas Gohr     * @return string revision ('' for current)
5320c3a5702SAndreas Gohr     */
5330c3a5702SAndreas Gohr    public function getLastRevisionAt($date_at)
5340c3a5702SAndreas Gohr    {
5350c3a5702SAndreas Gohr        //requested date_at(timestamp) younger or equal then modified_time($this->id) => load current
5360c3a5702SAndreas Gohr        if (file_exists($this->getFilename()) && $date_at >= @filemtime($this->getFilename())) {
5370c3a5702SAndreas Gohr            return '';
5380c3a5702SAndreas Gohr        } else {
5390c3a5702SAndreas Gohr            if ($rev = $this->getRelativeRevision($date_at + 1, -1)) { //+1 to get also the requested date revision
5400c3a5702SAndreas Gohr                return $rev;
5410c3a5702SAndreas Gohr            } else {
5420c3a5702SAndreas Gohr                return false;
5430c3a5702SAndreas Gohr            }
5440c3a5702SAndreas Gohr        }
5450c3a5702SAndreas Gohr    }
5460c3a5702SAndreas Gohr
5470c3a5702SAndreas Gohr    /**
5480c3a5702SAndreas Gohr     * Returns the next lines of the changelog  of the chunck before head or after tail
5490c3a5702SAndreas Gohr     *
5500c3a5702SAndreas Gohr     * @param resource $fp filepointer
5510c3a5702SAndreas Gohr     * @param int $head position head of last chunk
5520c3a5702SAndreas Gohr     * @param int $tail position tail of last chunk
5530c3a5702SAndreas Gohr     * @param int $direction positive forward, negative backward
5540c3a5702SAndreas Gohr     * @return array with entries:
5550c3a5702SAndreas Gohr     *    - $lines: changelog lines of readed chunk
5560c3a5702SAndreas Gohr     *    - $head: head of chunk
5570c3a5702SAndreas Gohr     *    - $tail: tail of chunk
5580c3a5702SAndreas Gohr     */
5590c3a5702SAndreas Gohr    protected function readAdjacentChunk($fp, $head, $tail, $direction)
5600c3a5702SAndreas Gohr    {
5610c3a5702SAndreas Gohr        if (!$fp) return array(array(), $head, $tail);
5620c3a5702SAndreas Gohr
5630c3a5702SAndreas Gohr        if ($direction > 0) {
5640c3a5702SAndreas Gohr            //read forward
5650c3a5702SAndreas Gohr            $head = $tail;
5660c3a5702SAndreas Gohr            $tail = $head + floor($this->chunk_size * (2 / 3));
5670c3a5702SAndreas Gohr            $tail = $this->getNewlinepointer($fp, $tail);
5680c3a5702SAndreas Gohr        } else {
5690c3a5702SAndreas Gohr            //read backward
5700c3a5702SAndreas Gohr            $tail = $head;
5710c3a5702SAndreas Gohr            $head = max($tail - $this->chunk_size, 0);
5720c3a5702SAndreas Gohr            while (true) {
5730c3a5702SAndreas Gohr                $nl = $this->getNewlinepointer($fp, $head);
5740c3a5702SAndreas Gohr                // was the chunk big enough? if not, take another bite
5750c3a5702SAndreas Gohr                if ($nl > 0 && $tail <= $nl) {
5760c3a5702SAndreas Gohr                    $head = max($head - $this->chunk_size, 0);
5770c3a5702SAndreas Gohr                } else {
5780c3a5702SAndreas Gohr                    $head = $nl;
5790c3a5702SAndreas Gohr                    break;
5800c3a5702SAndreas Gohr                }
5810c3a5702SAndreas Gohr            }
5820c3a5702SAndreas Gohr        }
5830c3a5702SAndreas Gohr
5840c3a5702SAndreas Gohr        //load next chunck
5850c3a5702SAndreas Gohr        $lines = $this->readChunk($fp, $head, $tail);
5860c3a5702SAndreas Gohr        return array($lines, $head, $tail);
5870c3a5702SAndreas Gohr    }
5880c3a5702SAndreas Gohr
5890c3a5702SAndreas Gohr    /**
5900c3a5702SAndreas Gohr     * Collect the $max revisions near to the timestamp $rev
5910c3a5702SAndreas Gohr     *
5920c3a5702SAndreas Gohr     * @param int $rev revision timestamp
5930c3a5702SAndreas Gohr     * @param int $max maximum number of revisions to be returned
5940c3a5702SAndreas Gohr     * @return bool|array
5950c3a5702SAndreas Gohr     *     return array with entries:
5960c3a5702SAndreas Gohr     *       - $requestedrevs: array of with $max revision timestamps
5970c3a5702SAndreas Gohr     *       - $revs: all parsed revision timestamps
5980c3a5702SAndreas Gohr     *       - $fp: filepointer only defined for chuck reading, needs closing.
5990c3a5702SAndreas Gohr     *       - $lines: non-parsed changelog lines before the parsed revisions
6000c3a5702SAndreas Gohr     *       - $head: position of first readed changelogline
6010c3a5702SAndreas Gohr     *       - $lasttail: position of end of last readed changelogline
6020c3a5702SAndreas Gohr     *     otherwise false
6030c3a5702SAndreas Gohr     */
6040c3a5702SAndreas Gohr    protected function retrieveRevisionsAround($rev, $max)
6050c3a5702SAndreas Gohr    {
6060c3a5702SAndreas Gohr        //get lines from changelog
6070c3a5702SAndreas Gohr        list($fp, $lines, $starthead, $starttail, /* $eof */) = $this->readloglines($rev);
6080c3a5702SAndreas Gohr        if (empty($lines)) return false;
6090c3a5702SAndreas Gohr
6100c3a5702SAndreas Gohr        //parse chunk containing $rev, and read forward more chunks until $max/2 is reached
6110c3a5702SAndreas Gohr        $head = $starthead;
6120c3a5702SAndreas Gohr        $tail = $starttail;
6130c3a5702SAndreas Gohr        $revs = array();
6140c3a5702SAndreas Gohr        $aftercount = $beforecount = 0;
6150c3a5702SAndreas Gohr        while (count($lines) > 0) {
6160c3a5702SAndreas Gohr            foreach ($lines as $line) {
6170c3a5702SAndreas Gohr                $tmp = parseChangelogLine($line);
6180c3a5702SAndreas Gohr                if ($tmp !== false) {
6190c3a5702SAndreas Gohr                    $this->cache[$this->id][$tmp['date']] = $tmp;
6200c3a5702SAndreas Gohr                    $revs[] = $tmp['date'];
6210c3a5702SAndreas Gohr                    if ($tmp['date'] >= $rev) {
6220c3a5702SAndreas Gohr                        //count revs after reference $rev
6230c3a5702SAndreas Gohr                        $aftercount++;
6240c3a5702SAndreas Gohr                        if ($aftercount == 1) $beforecount = count($revs);
6250c3a5702SAndreas Gohr                    }
6260c3a5702SAndreas Gohr                    //enough revs after reference $rev?
6270c3a5702SAndreas Gohr                    if ($aftercount > floor($max / 2)) break 2;
6280c3a5702SAndreas Gohr                }
6290c3a5702SAndreas Gohr            }
6300c3a5702SAndreas Gohr            //retrieve next chunk
6310c3a5702SAndreas Gohr            list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, 1);
6320c3a5702SAndreas Gohr        }
6330c3a5702SAndreas Gohr        if ($aftercount == 0) return false;
6340c3a5702SAndreas Gohr
6350c3a5702SAndreas Gohr        $lasttail = $tail;
6360c3a5702SAndreas Gohr
6370c3a5702SAndreas Gohr        //read additional chuncks backward until $max/2 is reached and total number of revs is equal to $max
6380c3a5702SAndreas Gohr        $lines = array();
6390c3a5702SAndreas Gohr        $i = 0;
6400c3a5702SAndreas Gohr        if ($aftercount > 0) {
6410c3a5702SAndreas Gohr            $head = $starthead;
6420c3a5702SAndreas Gohr            $tail = $starttail;
6430c3a5702SAndreas Gohr            while ($head > 0) {
6440c3a5702SAndreas Gohr                list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, -1);
6450c3a5702SAndreas Gohr
6460c3a5702SAndreas Gohr                for ($i = count($lines) - 1; $i >= 0; $i--) {
6470c3a5702SAndreas Gohr                    $tmp = parseChangelogLine($lines[$i]);
6480c3a5702SAndreas Gohr                    if ($tmp !== false) {
6490c3a5702SAndreas Gohr                        $this->cache[$this->id][$tmp['date']] = $tmp;
6500c3a5702SAndreas Gohr                        $revs[] = $tmp['date'];
6510c3a5702SAndreas Gohr                        $beforecount++;
6520c3a5702SAndreas Gohr                        //enough revs before reference $rev?
6530c3a5702SAndreas Gohr                        if ($beforecount > max(floor($max / 2), $max - $aftercount)) break 2;
6540c3a5702SAndreas Gohr                    }
6550c3a5702SAndreas Gohr                }
6560c3a5702SAndreas Gohr            }
6570c3a5702SAndreas Gohr        }
6580c3a5702SAndreas Gohr        sort($revs);
6590c3a5702SAndreas Gohr
6600c3a5702SAndreas Gohr        //keep only non-parsed lines
6610c3a5702SAndreas Gohr        $lines = array_slice($lines, 0, $i);
6620c3a5702SAndreas Gohr        //trunk desired selection
6630c3a5702SAndreas Gohr        $requestedrevs = array_slice($revs, -$max, $max);
6640c3a5702SAndreas Gohr
6650c3a5702SAndreas Gohr        return array($requestedrevs, $revs, $fp, $lines, $head, $lasttail);
6660c3a5702SAndreas Gohr    }
6670c3a5702SAndreas Gohr}
668