4 * Changelog handling functions
6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author Andreas Gohr <andi@splitbrain.org>
10 use dokuwiki\ChangeLog\MediaChangeLog
;
11 use dokuwiki\ChangeLog\ChangeLog
;
12 use dokuwiki\ChangeLog\RevisionInfo
;
13 use dokuwiki\File\PageFile
;
16 * parses a changelog line into it's components
18 * @param string $line changelog line
19 * @return array|bool parsed line or false
21 * @author Ben Coburn <btcoburn@silicodon.net>
23 * @deprecated 2023-09-25
25 function parseChangelogLine($line)
27 dbg_deprecated('see ' . ChangeLog
::class . '::parseLogLine()');
28 return ChangeLog
::parseLogLine($line);
32 * Adds an entry to the changelog and saves the metadata for the page
34 * Note: timestamp of the change might not be unique especially after very quick
35 * repeated edits (e.g. change checkbox via do plugin)
37 * @param int $date Timestamp of the change
38 * @param String $id Name of the affected page
39 * @param String $type Type of the change see DOKU_CHANGE_TYPE_*
40 * @param String $summary Summary of the change
41 * @param mixed $extra In case of a revert the revision (timestamp) of the reverted page
42 * @param array $flags Additional flags in a key value array.
44 * - ExternalEdit - mark as an external edit.
45 * @param null|int $sizechange Change of filesize
47 * @author Andreas Gohr <andi@splitbrain.org>
48 * @author Esther Brunner <wikidesign@gmail.com>
49 * @author Ben Coburn <btcoburn@silicodon.net>
50 * @deprecated 2021-11-28
55 $type = DOKU_CHANGE_TYPE_EDIT
,
61 // no more used in DokuWiki core, but left for third-party plugins
62 dbg_deprecated('see ' . PageFile
::class . '::saveWikiText()');
64 /** @var Input $INPUT */
67 // check for special flags as keys
68 if (!is_array($flags)) $flags = [];
69 $flagExternalEdit = isset($flags['ExternalEdit']);
73 if (!$date) $date = time(); //use current time if none supplied
74 $remote = ($flagExternalEdit) ?
'127.0.0.1' : clientIP(true);
75 $user = ($flagExternalEdit) ?
'' : $INPUT->server
->str('REMOTE_USER');
76 $sizechange = ($sizechange === null) ?
'' : (int)$sizechange;
78 // update changelog file and get the added entry that is also to be stored in metadata
79 $pageFile = new PageFile($id);
80 $logEntry = $pageFile->changelog
->addLogEntry([
88 'sizechange' => $sizechange,
92 $pageFile->updateMetadata($logEntry);
96 * Adds an entry to the media changelog
98 * @author Michael Hamann <michael@content-space.de>
99 * @author Andreas Gohr <andi@splitbrain.org>
100 * @author Esther Brunner <wikidesign@gmail.com>
101 * @author Ben Coburn <btcoburn@silicodon.net>
103 * @param int $date Timestamp of the change
104 * @param String $id Name of the affected page
105 * @param String $type Type of the change see DOKU_CHANGE_TYPE_*
106 * @param String $summary Summary of the change
107 * @param mixed $extra In case of a revert the revision (timestamp) of the reverted page
108 * @param array $flags Additional flags in a key value array.
111 * @param null|int $sizechange Change of filesize
113 function addMediaLogEntry(
116 $type = DOKU_CHANGE_TYPE_EDIT
,
122 /** @var Input $INPUT */
125 // check for special flags as keys
126 if (!is_array($flags)) $flags = [];
127 $flagExternalEdit = isset($flags['ExternalEdit']);
131 if (!$date) $date = time(); //use current time if none supplied
132 $remote = ($flagExternalEdit) ?
'127.0.0.1' : clientIP(true);
133 $user = ($flagExternalEdit) ?
'' : $INPUT->server
->str('REMOTE_USER');
134 $sizechange = ($sizechange === null) ?
'' : (int)$sizechange;
136 // update changelog file and get the added entry
137 (new MediaChangeLog($id, 1024))->addLogEntry([
145 'sizechange' => $sizechange,
150 * returns an array of recently changed files using the changelog
152 * The following constants can be used to control which changes are
153 * included. Add them together as needed.
155 * RECENTS_SKIP_DELETED - don't include deleted pages
156 * RECENTS_SKIP_MINORS - don't include minor changes
157 * RECENTS_ONLY_CREATION - only include new created pages and media
158 * RECENTS_SKIP_SUBSPACES - don't include subspaces
159 * RECENTS_MEDIA_CHANGES - return media changes instead of page changes
160 * RECENTS_MEDIA_PAGES_MIXED - return both media changes and page changes
162 * @param int $first number of first entry returned (for paginating
163 * @param int $num return $num entries
164 * @param string $ns restrict to given namespace
165 * @param int $flags see above
166 * @return array recently changed files
168 * @author Ben Coburn <btcoburn@silicodon.net>
169 * @author Kate Arzamastseva <pshns@ukr.net>
171 function getRecents($first, $num, $ns = '', $flags = 0)
181 // read all recent changes. (kept short)
182 if ($flags & RECENTS_MEDIA_CHANGES
) {
183 $lines = @file
($conf['media_changelog']) ?
: [];
185 $lines = @file
($conf['changelog']) ?
: [];
187 if (!is_array($lines)) {
190 $lines_position = count($lines) - 1;
191 $media_lines_position = 0;
194 if ($flags & RECENTS_MEDIA_PAGES_MIXED
) {
195 $media_lines = @file
($conf['media_changelog']) ?
: [];
196 if (!is_array($media_lines)) {
199 $media_lines_position = count($media_lines) - 1;
202 $seen = []; // caches seen lines, _handleRecentLogLine() skips them
205 while ($lines_position >= 0 ||
(($flags & RECENTS_MEDIA_PAGES_MIXED
) && $media_lines_position >= 0)) {
206 if (empty($rec) && $lines_position >= 0) {
207 $rec = _handleRecentLogLine(@$lines[$lines_position], $ns, $flags, $seen);
213 if (($flags & RECENTS_MEDIA_PAGES_MIXED
) && empty($media_rec) && $media_lines_position >= 0) {
214 $media_rec = _handleRecentLogLine(
215 @$media_lines[$media_lines_position],
217 $flags | RECENTS_MEDIA_CHANGES
,
221 $media_lines_position--;
225 if (($flags & RECENTS_MEDIA_PAGES_MIXED
) && @$media_rec['date'] >= @$rec['date']) {
226 $media_lines_position--;
228 $x['mode'] = RevisionInfo
::MODE_MEDIA
;
233 if ($flags & RECENTS_MEDIA_CHANGES
) {
234 $x['mode'] = RevisionInfo
::MODE_MEDIA
;
236 $x['mode'] = RevisionInfo
::MODE_PAGE
;
240 if (--$first >= 0) continue; // skip first entries
243 // break when we have enough entries
244 if ($count >= $num) {
252 * returns an array of files changed since a given time using the
255 * The following constants can be used to control which changes are
256 * included. Add them together as needed.
258 * RECENTS_SKIP_DELETED - don't include deleted pages
259 * RECENTS_SKIP_MINORS - don't include minor changes
260 * RECENTS_ONLY_CREATION - only include new created pages and media
261 * RECENTS_SKIP_SUBSPACES - don't include subspaces
262 * RECENTS_MEDIA_CHANGES - return media changes instead of page changes
264 * @param int $from date of the oldest entry to return
265 * @param int $to date of the newest entry to return (for pagination, optional)
266 * @param string $ns restrict to given namespace (optional)
267 * @param int $flags see above (optional)
268 * @return array of files
270 * @author Michael Hamann <michael@content-space.de>
271 * @author Ben Coburn <btcoburn@silicodon.net>
273 function getRecentsSince($from, $to = null, $ns = '', $flags = 0)
278 if ($to && $to < $from) {
282 // read all recent changes. (kept short)
283 if ($flags & RECENTS_MEDIA_CHANGES
) {
284 $lines = @file
($conf['media_changelog']);
286 $lines = @file
($conf['changelog']);
288 if (!$lines) return $recent;
290 // we start searching at the end of the list
291 $lines = array_reverse($lines);
294 $seen = []; // caches seen lines, _handleRecentLogLine() skips them
296 foreach ($lines as $line) {
297 $rec = _handleRecentLogLine($line, $ns, $flags, $seen);
298 if ($rec !== false) {
299 if ($rec['date'] >= $from) {
300 if (!$to ||
$rec['date'] <= $to) {
309 return array_reverse($recent);
313 * Internal function used by getRecents
314 * Parse a line and checks whether it should be included
316 * don't call directly
319 * @author Andreas Gohr <andi@splitbrain.org>
320 * @author Ben Coburn <btcoburn@silicodon.net>
322 * @param string $line changelog line
323 * @param string $ns restrict to given namespace
324 * @param int $flags flags to control which changes are included
325 * @param array $seen listing of seen pages
326 * @return array|bool false or array with info about a change
328 function _handleRecentLogLine($line, $ns, $flags, &$seen)
330 if (empty($line)) return false; //skip empty lines
332 // split the line into parts
333 $recent = ChangeLog
::parseLogLine($line);
334 if ($recent === false) return false;
337 if (isset($seen[$recent['id']])) return false;
339 // skip changes, of only new items are requested
340 if ($recent['type'] !== DOKU_CHANGE_TYPE_CREATE
&& ($flags & RECENTS_ONLY_CREATION
)) return false;
343 if ($recent['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT
&& ($flags & RECENTS_SKIP_MINORS
)) return false;
345 // remember in seen to skip additional sights
346 $seen[$recent['id']] = 1;
348 // check if it's a hidden page
349 if (isHiddenPage($recent['id'])) return false;
352 if (($ns) && (strpos($recent['id'], $ns . ':') !== 0)) return false;
354 // exclude subnamespaces
355 if (($flags & RECENTS_SKIP_SUBSPACES
) && (getNS($recent['id']) != $ns)) return false;
358 if ($flags & RECENTS_MEDIA_CHANGES
) {
359 $recent['perms'] = auth_quickaclcheck(getNS($recent['id']) . ':*');
361 $recent['perms'] = auth_quickaclcheck($recent['id']);
363 if ($recent['perms'] < AUTH_READ
) return false;
366 if ($flags & RECENTS_SKIP_DELETED
) {
367 $fn = (($flags & RECENTS_MEDIA_CHANGES
) ?
mediaFN($recent['id']) : wikiFN($recent['id']));
368 if (!file_exists($fn)) return false;