diff options
-rw-r--r-- | _test/tests/Remote/ApiCoreTest.php | 47 | ||||
-rw-r--r-- | inc/Remote/ApiCore.php | 52 |
2 files changed, 99 insertions, 0 deletions
diff --git a/_test/tests/Remote/ApiCoreTest.php b/_test/tests/Remote/ApiCoreTest.php index 684e9d3a7..4d5a02bc9 100644 --- a/_test/tests/Remote/ApiCoreTest.php +++ b/_test/tests/Remote/ApiCoreTest.php @@ -856,6 +856,53 @@ You can use up to five different levels of', $this->assertEquals(221, $e->getCode(), 'Non existing media id given'); } + //core.getMediaHistory + public function testGetMediaHistory() + { + global $conf; + + $_SERVER['REMOTE_USER'] = 'testuser'; + + //image to be uploaded + $orig = mediaFN('wiki:dokuwiki-128.png'); + $tmp = $conf['tmpdir'] . 'test.png'; + + //create image to be revised + $id = 'test:image3.png'; + $media = mediaFN($id); + + $rev = []; + for ($i = 0; $i < 2; $i++) { + $this->waitForTick(); + copy($orig, $tmp); + media_save(['name' => $tmp], $id, true, AUTH_UPLOAD, 'rename'); + $rev[$i] = filemtime($media); + } + + $params = ['media' => $id, 'first' => 0]; // offset 0 + $versions = $this->remote->call('core.getMediaHistory', $params); + $versions = json_decode(json_encode($versions), true); + $this->assertEquals(2, count($versions)); + $this->assertEquals($rev[1], $versions[0]['revision']); + $this->assertEquals($rev[0], $versions[1]['revision']); + + $params = ['media' => $id, 'first' => 1]; // offset 1 + $versions = $this->remote->call('core.getMediaHistory', $params); + $versions = json_decode(json_encode($versions), true); + $this->assertEquals(1, count($versions)); + $this->assertEquals($rev[0], $versions[0]['revision']); + + $params = ['media' => $id, 'first' => 2]; // offset 2 + $versions = $this->remote->call('core.getMediaHistory', $params); + $versions = json_decode(json_encode($versions), true); + $this->assertEquals(0, count($versions)); + + $params = ['media' => $id, 'first' => 2]; // offset 3 + $versions = $this->remote->call('core.getMediaHistory', $params); + $versions = json_decode(json_encode($versions), true); + $this->assertEquals(0, count($versions)); + } + //core.saveMedia public function testSaveMedia() { diff --git a/inc/Remote/ApiCore.php b/inc/Remote/ApiCore.php index 5a173cf89..1a859ddfe 100644 --- a/inc/Remote/ApiCore.php +++ b/inc/Remote/ApiCore.php @@ -4,6 +4,7 @@ namespace dokuwiki\Remote; use Doku_Renderer_xhtml; use dokuwiki\ChangeLog\PageChangeLog; +use dokuwiki\ChangeLog\MediaChangeLog; use dokuwiki\Extension\AuthPlugin; use dokuwiki\Extension\Event; use dokuwiki\Remote\Response\Link; @@ -66,6 +67,7 @@ class ApiCore 'core.getMediaInfo' => new ApiCall([$this, 'getMediaInfo'], 'media'), 'core.getMediaUsage' => new ApiCall([$this, 'getMediaUsage'], 'media'), // todo: implement getMediaHistory + 'core.getMediaHistory' => new ApiCall([$this, 'getMediaHistory'], 'media'), 'core.saveMedia' => new ApiCall([$this, 'saveMedia'], 'media'), 'core.deleteMedia' => new ApiCall([$this, 'deleteMedia'], 'media'), @@ -904,6 +906,56 @@ class ApiCore } /** + * Returns a list of available revisions of a given wiki media + * + * + * + * @link https://www.dokuwiki.org/config:recent + * @param string $media file id + * @param int $first skip the first n changelog lines, 0 starts at the current revision + * @return MediaChange[] + * @throws AccessDeniedException + * @throws RemoteException + * @author + */ + public function getMediaHistory($media, $first = 0) + { + global $conf; + + $media = cleanID($media); + // check that this media exists + if (auth_quickaclcheck($media) < AUTH_READ) { + throw new AccessDeniedException('You are not allowed to read this media file', 211); + } + if (!media_exists($media, 0)) { + throw new RemoteException('The requested media file does not exist', 221); + } + + $medialog = new MediaChangeLog($media); + $medialog->setChunkSize(1024); + // old revisions are counted from 0, so we need to subtract 1 for the current one + $revisions = $medialog->getRevisions($first - 1, $conf['recent']); + + $result = []; + foreach ($revisions as $rev) { + if (!media_exists($media, $rev)) continue; // skip non-existing revisions + $info = $medialog->getRevisionInfo($rev); + + $result[] = new MediaChange( + $media, + $rev, + $info['user'], + $info['ip'], + $info['sum'], + $info['type'], + $info['sizechange'] + ); + } + + return $result; + } + + /** * Uploads a file to the wiki * * The file data has to be passed as a base64 encoded string. |