Skip to content

Commit f32fa09

Browse files
authored
Merge pull request dokuwiki#4071 from dokuwiki/setmedia
set media entry to fix Undefined array key
2 parents 6de7d28 + 9349c09 commit f32fa09

12 files changed

+154
-86
lines changed

_test/tests/inc/changelog_getrelativerevision.test.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use dokuwiki\ChangeLog\ChangeLog;
34
use dokuwiki\ChangeLog\PageChangeLog;
45

56
/**
@@ -113,7 +114,8 @@ function test_requestrev_checkcache() {
113114
$rev = 1362525359;
114115
$dir = 1;
115116
$revexpected = 1362525899;
116-
$infoexpected = parseChangelogLine($this->logline);
117+
$infoexpected = ChangeLog::parseLogLine($this->logline);
118+
$infoexpected['mode'] = 'page';
117119

118120
$pagelog = new PageChangeLog($this->pageid, $chunk_size = 8192);
119121
$revfound = $pagelog->getRelativeRevision($rev, $dir);

_test/tests/inc/changelog_getrevisioninfo.test.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use dokuwiki\ChangeLog\ChangeLog;
34
use dokuwiki\ChangeLog\PageChangeLog;
45

56
/**
@@ -41,7 +42,8 @@ function test_changemetadatanotexists() {
4142
*/
4243
function test_requestrev() {
4344
$rev = 1362525899;
44-
$infoexpected = parseChangelogLine($this->logline);
45+
$infoexpected = ChangeLog::parseLogLine($this->logline);
46+
$infoexpected['mode'] = 'page';
4547

4648
$pagelog = new PageChangeLog($this->pageid, $chunk_size = 8192);
4749
$info = $pagelog->getRevisionInfo($rev);
@@ -56,7 +58,8 @@ function test_requestrev() {
5658
*/
5759
function test_requestrev_chuncked() {
5860
$rev = 1362525899;
59-
$infoexpected = parseChangelogLine($this->logline);
61+
$infoexpected = ChangeLog::parseLogLine($this->logline);
62+
$infoexpected['mode'] = 'page';
6063

6164
$pagelog = new PageChangeLog($this->pageid, $chunk_size = 512);
6265
$info = $pagelog->getRevisionInfo($rev);
@@ -68,7 +71,8 @@ function test_requestrev_chuncked() {
6871
*/
6972
function test_requestrev_chunckedsmallerthanlinelength() {
7073
$rev = 1362525899;
71-
$infoexpected = parseChangelogLine($this->logline);
74+
$infoexpected = ChangeLog::parseLogLine($this->logline);
75+
$infoexpected['mode'] = 'page';
7276

7377
$pagelog = new PageChangeLog($this->pageid, $chunk_size = 20);
7478
$info = $pagelog->getRevisionInfo($rev);
@@ -80,7 +84,8 @@ function test_requestrev_chunckedsmallerthanlinelength() {
8084
*/
8185
function test_requestrecentestlogline() {
8286
$rev = 1374261194;
83-
$infoexpected = parseChangelogLine($this->firstlogline);
87+
$infoexpected = ChangeLog::parseLogLine($this->firstlogline);
88+
$infoexpected['mode'] = 'page';
8489

8590
$pagelog = new PageChangeLog($this->pageid, $chunk_size = 8192);
8691
$info = $pagelog->getRevisionInfo($rev);
@@ -95,7 +100,8 @@ function test_requestrecentestlogline() {
95100
*/
96101
function test_requestrecentestlogline_chuncked() {
97102
$rev = 1374261194;
98-
$infoexpected = parseChangelogLine($this->firstlogline);
103+
$infoexpected = ChangeLog::parseLogLine($this->firstlogline);
104+
$infoexpected['mode'] = 'page';
99105

100106
$pagelog = new PageChangeLog($this->pageid, $chunk_size = 512);
101107
$info = $pagelog->getRevisionInfo($rev);
@@ -110,7 +116,7 @@ function test_negativerev() {
110116

111117
$pagelog = new PageChangeLog($this->pageid, $chunk_size = 8192);
112118
$info = $pagelog->getRevisionInfo($rev);
113-
$this->assertEquals(false, $info);
119+
$this->assertFalse($info);
114120
}
115121

116122
/**
@@ -121,15 +127,16 @@ function test_notexistingrev() {
121127

122128
$pagelog = new PageChangeLog($this->pageid, $chunk_size = 8192);
123129
$info = $pagelog->getRevisionInfo($rev);
124-
$this->assertEquals(false, $info);
130+
$this->assertFalse($info);
125131
}
126132

127133
/**
128134
* sometimes chuncksize is set to true
129135
*/
130136
function test_chuncksizetrue() {
131137
$rev = 1362525899;
132-
$infoexpected = parseChangelogLine($this->logline);
138+
$infoexpected = ChangeLog::parseLogLine($this->logline);
139+
$infoexpected['mode'] = 'page';
133140

134141
$pagelog = new PageChangeLog($this->pageid, true);
135142
$info = $pagelog->getRevisionInfo($rev);

inc/ChangeLog/ChangeLog.php

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ public function __construct($id, $chunk_size = 8192)
4545
*/
4646
abstract protected function getFilename($rev = '');
4747

48+
/**
49+
* Returns mode
50+
*
51+
* @return string RevisionInfo::MODE_MEDIA or RevisionInfo::MODE_PAGE
52+
*/
53+
abstract protected function getMode();
54+
4855
/**
4956
* Check whether given revision is the current page
5057
*
@@ -99,17 +106,20 @@ public function lastRevision()
99106
}
100107

101108
/**
102-
* Save revision info to the cache pool
109+
* Parses a changelog line into its components and save revision info to the cache pool
103110
*
104-
* @param array $info Revision info structure
105-
* @return bool
111+
* @param string $value changelog line
112+
* @return array|bool parsed line or false
106113
*/
107-
protected function cacheRevisionInfo($info)
114+
protected function parseAndCacheLogLine($value)
108115
{
109-
if (!is_array($info)) return false;
110-
//$this->cache[$this->id][$info['date']] ??= $info; // since php 7.4
111-
$this->cache[$this->id][$info['date']] ??= $info;
112-
return true;
116+
$info = static::parseLogLine($value);
117+
if (is_array($info)) {
118+
$info['mode'] = $this->getMode();
119+
$this->cache[$this->id][$info['date']] ??= $info;
120+
return $info;
121+
}
122+
return false;
113123
}
114124

115125
/**
@@ -131,6 +141,8 @@ protected function cacheRevisionInfo($info)
131141
* - sum: edit summary (or action reason)
132142
* - extra: extra data (varies by line type)
133143
* - sizechange: change of filesize
144+
* additional:
145+
* - mode: page or media
134146
*
135147
* @author Ben Coburn <[email protected]>
136148
* @author Kate Arzamastseva <[email protected]>
@@ -146,7 +158,7 @@ public function getRevisionInfo($rev, $retrieveCurrentRevInfo = true)
146158
}
147159

148160
// check if it's already in the memory cache
149-
if (isset($this->cache[$this->id]) && isset($this->cache[$this->id][$rev])) {
161+
if (isset($this->cache[$this->id][$rev])) {
150162
return $this->cache[$this->id][$rev];
151163
}
152164

@@ -158,14 +170,11 @@ public function getRevisionInfo($rev, $retrieveCurrentRevInfo = true)
158170
if (empty($lines)) return false;
159171

160172
// parse and cache changelog lines
161-
foreach ($lines as $value) {
162-
$info = static::parseLogLine($value);
163-
$this->cacheRevisionInfo($info);
164-
}
165-
if (!isset($this->cache[$this->id][$rev])) {
166-
return false;
173+
foreach ($lines as $line) {
174+
$this->parseAndCacheLogLine($line);
167175
}
168-
return $this->cache[$this->id][$rev];
176+
177+
return $this->cache[$this->id][$rev] ?? false;
169178
}
170179

171180
/**
@@ -285,8 +294,8 @@ public function getRevisions($first, $num)
285294

286295
// handle lines in reverse order
287296
for ($i = count($lines) - 1; $i >= 0; $i--) {
288-
$info = static::parseLogLine($lines[$i]);
289-
if ($this->cacheRevisionInfo($info)) {
297+
$info = $this->parseAndCacheLogLine($lines[$i]);
298+
if (is_array($info)) {
290299
$revs[] = $info['date'];
291300
}
292301
}
@@ -342,8 +351,8 @@ public function getRelativeRevision($rev, $direction)
342351
$step = -1;
343352
}
344353
for ($i = $start; $i >= 0 && $i < $count; $i += $step) {
345-
$info = static::parseLogLine($lines[$i]);
346-
if ($this->cacheRevisionInfo($info)) {
354+
$info = $this->parseAndCacheLogLine($lines[$i]);
355+
if (is_array($info)) {
347356
//look for revs older/earlier then reference $rev and select $direction-th one
348357
if (($direction > 0 && $info['date'] > $rev) || ($direction < 0 && $info['date'] < $rev)) {
349358
$revCounter++;
@@ -414,12 +423,14 @@ public function getRevisionsAround($rev1, $rev2, $max = 50)
414423
$revs1 = $allRevs;
415424
while ($head > 0) {
416425
for ($i = count($lines) - 1; $i >= 0; $i--) {
417-
$info = static::parseLogLine($lines[$i]);
418-
if ($this->cacheRevisionInfo($info)) {
426+
$info = $this->parseAndCacheLogLine($lines[$i]);
427+
if (is_array($info)) {
419428
$revs1[] = $info['date'];
420429
$index++;
421430

422-
if ($index > (int) ($max / 2)) break 2;
431+
if ($index > (int) ($max / 2)) {
432+
break 2;
433+
}
423434
}
424435
}
425436

@@ -490,16 +501,20 @@ protected function retrieveRevisionsAround($rev, $max)
490501
$tail = $startTail;
491502
while (count($lines) > 0) {
492503
foreach ($lines as $line) {
493-
$info = static::parseLogLine($line);
494-
if ($this->cacheRevisionInfo($info)) {
504+
$info = $this->parseAndCacheLogLine($line);
505+
if (is_array($info)) {
495506
$revs[] = $info['date'];
496507
if ($info['date'] >= $rev) {
497508
//count revs after reference $rev
498509
$afterCount++;
499-
if ($afterCount == 1) $beforeCount = count($revs);
510+
if ($afterCount == 1) {
511+
$beforeCount = count($revs);
512+
}
500513
}
501514
//enough revs after reference $rev?
502-
if ($afterCount > (int) ($max / 2)) break 2;
515+
if ($afterCount > (int) ($max / 2)) {
516+
break 2;
517+
}
503518
}
504519
}
505520
//retrieve next chunk
@@ -530,12 +545,14 @@ protected function retrieveRevisionsAround($rev, $max)
530545
[$lines, $head, $tail] = $this->readAdjacentChunk($fp, $head, $tail, -1);
531546

532547
for ($i = count($lines) - 1; $i >= 0; $i--) {
533-
$info = static::parseLogLine($lines[$i]);
534-
if ($this->cacheRevisionInfo($info)) {
548+
$info = $this->parseAndCacheLogLine($lines[$i]);
549+
if (is_array($info)) {
535550
$revs[] = $info['date'];
536551
$beforeCount++;
537552
//enough revs before reference $rev?
538-
if ($beforeCount > max((int) ($max / 2), $max - $afterCount)) break 2;
553+
if ($beforeCount > max((int) ($max / 2), $max - $afterCount)) {
554+
break 2;
555+
}
539556
}
540557
}
541558
}
@@ -575,14 +592,18 @@ protected function retrieveRevisionsAround($rev, $max)
575592
* - extra: extra data (varies by line type)
576593
* - sizechange: change of filesize
577594
* - timestamp: unix timestamp or false (key set only for external edit occurred)
595+
* additional:
596+
* - mode: page or media
578597
*
579598
* @author Satoshi Sahara <[email protected]>
580599
*/
581600
public function getCurrentRevisionInfo()
582601
{
583602
global $lang;
584603

585-
if (isset($this->currentRevision)) return $this->getRevisionInfo($this->currentRevision);
604+
if (isset($this->currentRevision)) {
605+
return $this->getRevisionInfo($this->currentRevision);
606+
}
586607

587608
// get revision id from the item file timestamp and changelog
588609
$fileLastMod = $this->getFilename();
@@ -616,6 +637,7 @@ public function getCurrentRevisionInfo()
616637
'extra' => '',
617638
'sizechange' => -io_getSizeFile($this->getFilename($lastRev)),
618639
'timestamp' => false,
640+
'mode' => $this->getMode()
619641
];
620642
} else { // item file exists, with timestamp $fileRev
621643
// here, file timestamp $fileRev is different with last revision timestamp $lastRev in changelog
@@ -654,6 +676,7 @@ public function getCurrentRevisionInfo()
654676
'extra' => '',
655677
'sizechange' => $sizechange,
656678
'timestamp' => $timestamp,
679+
'mode' => $this->getMode()
657680
];
658681
}
659682

inc/ChangeLog/MediaChangeLog.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ protected function getFilename($rev = '')
2828
return mediaFN($this->id, $rev);
2929
}
3030

31+
/**
32+
* Returns mode
33+
*
34+
* @return string RevisionInfo::MODE_PAGE
35+
*/
36+
protected function getMode()
37+
{
38+
return RevisionInfo::MODE_MEDIA;
39+
}
3140

3241

3342
/**
@@ -52,6 +61,7 @@ public function addLogEntry(array $info, $timestamp = null)
5261

5362
// update cache
5463
$this->currentRevision = $info['date'];
64+
$info['mode'] = $this->getMode();
5565
$this->cache[$this->id][$this->currentRevision] = $info;
5666
return $info;
5767
}

inc/ChangeLog/PageChangeLog.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ protected function getFilename($rev = '')
2828
return wikiFN($this->id, $rev);
2929
}
3030

31+
/**
32+
* Returns mode
33+
*
34+
* @return string RevisionInfo::MODE_PAGE
35+
*/
36+
protected function getMode()
37+
{
38+
return RevisionInfo::MODE_PAGE;
39+
}
3140

3241

3342
/**
@@ -52,6 +61,7 @@ public function addLogEntry(array $info, $timestamp = null)
5261

5362
// update cache
5463
$this->currentRevision = $info['date'];
64+
$info['mode'] = $this->getMode();
5565
$this->cache[$this->id][$this->currentRevision] = $info;
5666
return $info;
5767
}

0 commit comments

Comments
 (0)