17d559c7fSBen Coburn<?php 2d4f83172SAndreas Gohr 37d559c7fSBen Coburn/** 47d559c7fSBen Coburn * Changelog handling functions 57d559c7fSBen Coburn * 67d559c7fSBen Coburn * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 77d559c7fSBen Coburn * @author Andreas Gohr <andi@splitbrain.org> 87d559c7fSBen Coburn */ 9d4f83172SAndreas Gohr 1024870174SAndreas Gohruse dokuwiki\ChangeLog\MediaChangeLog; 111d11f1d3SSatoshi Saharause dokuwiki\ChangeLog\ChangeLog; 127fba736bSSatoshi Saharause dokuwiki\File\PageFile; 131d11f1d3SSatoshi Sahara 147d559c7fSBen Coburn/** 157d559c7fSBen Coburn * parses a changelog line into it's components 167d559c7fSBen Coburn * 174f1e2cb3SGerrit Uitslag * @param string $line changelog line 184f1e2cb3SGerrit Uitslag * @return array|bool parsed line or false 19*71951841SGerrit Uitslag * 20*71951841SGerrit Uitslag * @author Ben Coburn <btcoburn@silicodon.net> 21*71951841SGerrit Uitslag * 22*71951841SGerrit Uitslag * @deprecated 2023-09-25 237d559c7fSBen Coburn */ 24d868eb89SAndreas Gohrfunction parseChangelogLine($line) 25d868eb89SAndreas Gohr{ 26*71951841SGerrit Uitslag dbg_deprecated('see ' . ChangeLog::class . '::parseLogLine()'); 271d11f1d3SSatoshi Sahara return ChangeLog::parseLogLine($line); 287d559c7fSBen Coburn} 297d559c7fSBen Coburn 307d559c7fSBen Coburn/** 3163f13cadSDamien Regad * Adds an entry to the changelog and saves the metadata for the page 327d559c7fSBen Coburn * 336527839fSSatoshi Sahara * Note: timestamp of the change might not be unique especially after very quick 346527839fSSatoshi Sahara * repeated edits (e.g. change checkbox via do plugin) 356527839fSSatoshi Sahara * 366527839fSSatoshi Sahara * @param int $date Timestamp of the change 37a365baeeSDominik Eckelmann * @param String $id Name of the affected page 38a365baeeSDominik Eckelmann * @param String $type Type of the change see DOKU_CHANGE_TYPE_* 39a365baeeSDominik Eckelmann * @param String $summary Summary of the change 40eeda7adaSGerrit Uitslag * @param mixed $extra In case of a revert the revision (timestamp) of the reverted page 41a365baeeSDominik Eckelmann * @param array $flags Additional flags in a key value array. 424f1e2cb3SGerrit Uitslag * Available flags: 43a365baeeSDominik Eckelmann * - ExternalEdit - mark as an external edit. 44ac3ed4afSGerrit Uitslag * @param null|int $sizechange Change of filesize 45a365baeeSDominik Eckelmann * 467d559c7fSBen Coburn * @author Andreas Gohr <andi@splitbrain.org> 477d559c7fSBen Coburn * @author Esther Brunner <wikidesign@gmail.com> 487d559c7fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 4969f9b481SSatoshi Sahara * @deprecated 2021-11-28 507d559c7fSBen Coburn */ 515d9428a0SSatoshi Saharafunction addLogEntry( 525d9428a0SSatoshi Sahara $date, 535d9428a0SSatoshi Sahara $id, 545d9428a0SSatoshi Sahara $type = DOKU_CHANGE_TYPE_EDIT, 555d9428a0SSatoshi Sahara $summary = '', 565d9428a0SSatoshi Sahara $extra = '', 575d9428a0SSatoshi Sahara $flags = null, 58d868eb89SAndreas Gohr $sizechange = null 59d868eb89SAndreas Gohr) { 6069f9b481SSatoshi Sahara // no more used in DokuWiki core, but left for third-party plugins 6179a2d784SGerrit Uitslag dbg_deprecated('see ' . PageFile::class . '::saveWikiText()'); 6269f9b481SSatoshi Sahara 63585bf44eSChristopher Smith /** @var Input $INPUT */ 64585bf44eSChristopher Smith global $INPUT; 657d559c7fSBen Coburn 665aa52fafSBen Coburn // check for special flags as keys 6724870174SAndreas Gohr if (!is_array($flags)) $flags = []; 685aa52fafSBen Coburn $flagExternalEdit = isset($flags['ExternalEdit']); 695aa52fafSBen Coburn 707d559c7fSBen Coburn $id = cleanid($id); 717d559c7fSBen Coburn 727d559c7fSBen Coburn if (!$date) $date = time(); //use current time if none supplied 7324870174SAndreas Gohr $remote = ($flagExternalEdit) ? '127.0.0.1' : clientIP(true); 7424870174SAndreas Gohr $user = ($flagExternalEdit) ? '' : $INPUT->server->str('REMOTE_USER'); 751d11f1d3SSatoshi Sahara $sizechange = ($sizechange === null) ? '' : (int)$sizechange; 767d559c7fSBen Coburn 771d11f1d3SSatoshi Sahara // update changelog file and get the added entry that is also to be stored in metadata 787fba736bSSatoshi Sahara $pageFile = new PageFile($id); 797fba736bSSatoshi Sahara $logEntry = $pageFile->changelog->addLogEntry([ 807d559c7fSBen Coburn 'date' => $date, 817d559c7fSBen Coburn 'ip' => $remote, 82c7192766SSatoshi Sahara 'type' => $type, 837d559c7fSBen Coburn 'id' => $id, 847d559c7fSBen Coburn 'user' => $user, 85c7192766SSatoshi Sahara 'sum' => $summary, 86c7192766SSatoshi Sahara 'extra' => $extra, 87c7192766SSatoshi Sahara 'sizechange' => $sizechange, 881d11f1d3SSatoshi Sahara ]); 897d559c7fSBen Coburn 907d559c7fSBen Coburn // update metadata 917fba736bSSatoshi Sahara $pageFile->updateMetadata($logEntry); 927d559c7fSBen Coburn} 937d559c7fSBen Coburn 947d559c7fSBen Coburn/** 95eeda7adaSGerrit Uitslag * Adds an entry to the media changelog 9699c8d7f2Smichael * 9799c8d7f2Smichael * @author Michael Hamann <michael@content-space.de> 9899c8d7f2Smichael * @author Andreas Gohr <andi@splitbrain.org> 9999c8d7f2Smichael * @author Esther Brunner <wikidesign@gmail.com> 10099c8d7f2Smichael * @author Ben Coburn <btcoburn@silicodon.net> 1014f1e2cb3SGerrit Uitslag * 1024f1e2cb3SGerrit Uitslag * @param int $date Timestamp of the change 1034f1e2cb3SGerrit Uitslag * @param String $id Name of the affected page 1044f1e2cb3SGerrit Uitslag * @param String $type Type of the change see DOKU_CHANGE_TYPE_* 1054f1e2cb3SGerrit Uitslag * @param String $summary Summary of the change 106eeda7adaSGerrit Uitslag * @param mixed $extra In case of a revert the revision (timestamp) of the reverted page 1074f1e2cb3SGerrit Uitslag * @param array $flags Additional flags in a key value array. 1084f1e2cb3SGerrit Uitslag * Available flags: 1094f1e2cb3SGerrit Uitslag * - (none, so far) 110ac3ed4afSGerrit Uitslag * @param null|int $sizechange Change of filesize 11199c8d7f2Smichael */ 11264159a61SAndreas Gohrfunction addMediaLogEntry( 11364159a61SAndreas Gohr $date, 11464159a61SAndreas Gohr $id, 11564159a61SAndreas Gohr $type = DOKU_CHANGE_TYPE_EDIT, 11664159a61SAndreas Gohr $summary = '', 11764159a61SAndreas Gohr $extra = '', 11864159a61SAndreas Gohr $flags = null, 119d868eb89SAndreas Gohr $sizechange = null 120d868eb89SAndreas Gohr) { 121585bf44eSChristopher Smith /** @var Input $INPUT */ 122585bf44eSChristopher Smith global $INPUT; 12399c8d7f2Smichael 124facfe250SSatoshi Sahara // check for special flags as keys 12524870174SAndreas Gohr if (!is_array($flags)) $flags = []; 126facfe250SSatoshi Sahara $flagExternalEdit = isset($flags['ExternalEdit']); 127facfe250SSatoshi Sahara 12899c8d7f2Smichael $id = cleanid($id); 12999c8d7f2Smichael 13099c8d7f2Smichael if (!$date) $date = time(); //use current time if none supplied 13124870174SAndreas Gohr $remote = ($flagExternalEdit) ? '127.0.0.1' : clientIP(true); 13224870174SAndreas Gohr $user = ($flagExternalEdit) ? '' : $INPUT->server->str('REMOTE_USER'); 1331d11f1d3SSatoshi Sahara $sizechange = ($sizechange === null) ? '' : (int)$sizechange; 13499c8d7f2Smichael 1351d11f1d3SSatoshi Sahara // update changelog file and get the added entry 13624870174SAndreas Gohr (new MediaChangeLog($id, 1024))->addLogEntry([ 13799c8d7f2Smichael 'date' => $date, 13899c8d7f2Smichael 'ip' => $remote, 139c7192766SSatoshi Sahara 'type' => $type, 14099c8d7f2Smichael 'id' => $id, 14199c8d7f2Smichael 'user' => $user, 142c7192766SSatoshi Sahara 'sum' => $summary, 143c7192766SSatoshi Sahara 'extra' => $extra, 144c7192766SSatoshi Sahara 'sizechange' => $sizechange, 1451d11f1d3SSatoshi Sahara ]); 14699c8d7f2Smichael} 14799c8d7f2Smichael 14899c8d7f2Smichael/** 149252acce3SSatoshi Sahara * returns an array of recently changed files using the changelog 1507d559c7fSBen Coburn * 1517d559c7fSBen Coburn * The following constants can be used to control which changes are 1527d559c7fSBen Coburn * included. Add them together as needed. 1537d559c7fSBen Coburn * 1547d559c7fSBen Coburn * RECENTS_SKIP_DELETED - don't include deleted pages 1557d559c7fSBen Coburn * RECENTS_SKIP_MINORS - don't include minor changes 15608e9b52fSPhy * RECENTS_ONLY_CREATION - only include new created pages and media 1577d559c7fSBen Coburn * RECENTS_SKIP_SUBSPACES - don't include subspaces 1580b926329SKate Arzamastseva * RECENTS_MEDIA_CHANGES - return media changes instead of page changes 1590b926329SKate Arzamastseva * RECENTS_MEDIA_PAGES_MIXED - return both media changes and page changes 1607d559c7fSBen Coburn * 1617d559c7fSBen Coburn * @param int $first number of first entry returned (for paginating 1627d559c7fSBen Coburn * @param int $num return $num entries 1637d559c7fSBen Coburn * @param string $ns restrict to given namespace 16459f20ea3SMichael Hamann * @param int $flags see above 16559f20ea3SMichael Hamann * @return array recently changed files 1667d559c7fSBen Coburn * 1677d559c7fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 16829778747SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net> 1697d559c7fSBen Coburn */ 170d868eb89SAndreas Gohrfunction getRecents($first, $num, $ns = '', $flags = 0) 171d868eb89SAndreas Gohr{ 1727d559c7fSBen Coburn global $conf; 17324870174SAndreas Gohr $recent = []; 1747d559c7fSBen Coburn $count = 0; 1757d559c7fSBen Coburn 1767d559c7fSBen Coburn if (!$num) 1777d559c7fSBen Coburn return $recent; 1787d559c7fSBen Coburn 1797d559c7fSBen Coburn // read all recent changes. (kept short) 1800b926329SKate Arzamastseva if ($flags & RECENTS_MEDIA_CHANGES) { 1818e3e8693SAndreas Gohr $lines = @file($conf['media_changelog']) ?: []; 18299c8d7f2Smichael } else { 1838e3e8693SAndreas Gohr $lines = @file($conf['changelog']) ?: []; 18499c8d7f2Smichael } 1851b266025SPhy if (!is_array($lines)) { 18624870174SAndreas Gohr $lines = []; 1871b266025SPhy } 18829778747SKate Arzamastseva $lines_position = count($lines) - 1; 18959f20ea3SMichael Hamann $media_lines_position = 0; 19024870174SAndreas Gohr $media_lines = []; 19129778747SKate Arzamastseva 1920b926329SKate Arzamastseva if ($flags & RECENTS_MEDIA_PAGES_MIXED) { 1938e3e8693SAndreas Gohr $media_lines = @file($conf['media_changelog']) ?: []; 1941b266025SPhy if (!is_array($media_lines)) { 19524870174SAndreas Gohr $media_lines = []; 1961b266025SPhy } 19729778747SKate Arzamastseva $media_lines_position = count($media_lines) - 1; 19829778747SKate Arzamastseva } 19929778747SKate Arzamastseva 20024870174SAndreas Gohr $seen = []; // caches seen lines, _handleRecent() skips them 2017d559c7fSBen Coburn 2027d559c7fSBen Coburn // handle lines 2030b926329SKate Arzamastseva while ($lines_position >= 0 || (($flags & RECENTS_MEDIA_PAGES_MIXED) && $media_lines_position >= 0)) { 20429778747SKate Arzamastseva if (empty($rec) && $lines_position >= 0) { 2051d901ab2SAndreas Gohr $rec = _handleRecent(@$lines[$lines_position], $ns, $flags, $seen); 20629778747SKate Arzamastseva if (!$rec) { 20729778747SKate Arzamastseva $lines_position--; 20829778747SKate Arzamastseva continue; 20929778747SKate Arzamastseva } 21029778747SKate Arzamastseva } 2110b926329SKate Arzamastseva if (($flags & RECENTS_MEDIA_PAGES_MIXED) && empty($media_rec) && $media_lines_position >= 0) { 21264159a61SAndreas Gohr $media_rec = _handleRecent( 21364159a61SAndreas Gohr @$media_lines[$media_lines_position], 21464159a61SAndreas Gohr $ns, 21564159a61SAndreas Gohr $flags | RECENTS_MEDIA_CHANGES, 21664159a61SAndreas Gohr $seen 21764159a61SAndreas Gohr ); 21829778747SKate Arzamastseva if (!$media_rec) { 21929778747SKate Arzamastseva $media_lines_position--; 22029778747SKate Arzamastseva continue; 22129778747SKate Arzamastseva } 22229778747SKate Arzamastseva } 2230b926329SKate Arzamastseva if (($flags & RECENTS_MEDIA_PAGES_MIXED) && @$media_rec['date'] >= @$rec['date']) { 22429778747SKate Arzamastseva $media_lines_position--; 22529778747SKate Arzamastseva $x = $media_rec; 226b5941dfaSKate Arzamastseva $x['media'] = true; 22729778747SKate Arzamastseva $media_rec = false; 22829778747SKate Arzamastseva } else { 22929778747SKate Arzamastseva $lines_position--; 23029778747SKate Arzamastseva $x = $rec; 23135bad86aSTherealperO if ($flags & RECENTS_MEDIA_CHANGES) { 23235bad86aSTherealperO $x['media'] = true; 23335bad86aSTherealperO } else { 23435bad86aSTherealperO $x['media'] = false; 23535bad86aSTherealperO } 23629778747SKate Arzamastseva $rec = false; 23729778747SKate Arzamastseva } 2387d559c7fSBen Coburn if (--$first >= 0) continue; // skip first entries 23929778747SKate Arzamastseva $recent[] = $x; 2407d559c7fSBen Coburn $count++; 2417d559c7fSBen Coburn // break when we have enough entries 242177d6836SAndreas Gohr if ($count >= $num) { 243d4f83172SAndreas Gohr break; 244d4f83172SAndreas Gohr } 2457d559c7fSBen Coburn } 2467d559c7fSBen Coburn return $recent; 2477d559c7fSBen Coburn} 2487d559c7fSBen Coburn 2497d559c7fSBen Coburn/** 25099c8d7f2Smichael * returns an array of files changed since a given time using the 25199c8d7f2Smichael * changelog 25299c8d7f2Smichael * 25399c8d7f2Smichael * The following constants can be used to control which changes are 25499c8d7f2Smichael * included. Add them together as needed. 25599c8d7f2Smichael * 25699c8d7f2Smichael * RECENTS_SKIP_DELETED - don't include deleted pages 25799c8d7f2Smichael * RECENTS_SKIP_MINORS - don't include minor changes 25808e9b52fSPhy * RECENTS_ONLY_CREATION - only include new created pages and media 25999c8d7f2Smichael * RECENTS_SKIP_SUBSPACES - don't include subspaces 2600b926329SKate Arzamastseva * RECENTS_MEDIA_CHANGES - return media changes instead of page changes 26199c8d7f2Smichael * 26299c8d7f2Smichael * @param int $from date of the oldest entry to return 26399c8d7f2Smichael * @param int $to date of the newest entry to return (for pagination, optional) 26499c8d7f2Smichael * @param string $ns restrict to given namespace (optional) 26559f20ea3SMichael Hamann * @param int $flags see above (optional) 26659f20ea3SMichael Hamann * @return array of files 26799c8d7f2Smichael * 26899c8d7f2Smichael * @author Michael Hamann <michael@content-space.de> 26999c8d7f2Smichael * @author Ben Coburn <btcoburn@silicodon.net> 27099c8d7f2Smichael */ 271d868eb89SAndreas Gohrfunction getRecentsSince($from, $to = null, $ns = '', $flags = 0) 272d868eb89SAndreas Gohr{ 27399c8d7f2Smichael global $conf; 27424870174SAndreas Gohr $recent = []; 27599c8d7f2Smichael 27699c8d7f2Smichael if ($to && $to < $from) 27799c8d7f2Smichael return $recent; 27899c8d7f2Smichael 27999c8d7f2Smichael // read all recent changes. (kept short) 2800b926329SKate Arzamastseva if ($flags & RECENTS_MEDIA_CHANGES) { 28199c8d7f2Smichael $lines = @file($conf['media_changelog']); 28299c8d7f2Smichael } else { 28399c8d7f2Smichael $lines = @file($conf['changelog']); 28499c8d7f2Smichael } 285e920a0a1SAndreas Gohr if (!$lines) return $recent; 28699c8d7f2Smichael 28799c8d7f2Smichael // we start searching at the end of the list 28899c8d7f2Smichael $lines = array_reverse($lines); 28999c8d7f2Smichael 29099c8d7f2Smichael // handle lines 29124870174SAndreas Gohr $seen = []; // caches seen lines, _handleRecent() skips them 29299c8d7f2Smichael 29399c8d7f2Smichael foreach ($lines as $line) { 29499c8d7f2Smichael $rec = _handleRecent($line, $ns, $flags, $seen); 29599c8d7f2Smichael if ($rec !== false) { 29699c8d7f2Smichael if ($rec['date'] >= $from) { 29799c8d7f2Smichael if (!$to || $rec['date'] <= $to) { 29899c8d7f2Smichael $recent[] = $rec; 29999c8d7f2Smichael } 30099c8d7f2Smichael } else { 30199c8d7f2Smichael break; 30299c8d7f2Smichael } 30399c8d7f2Smichael } 30499c8d7f2Smichael } 30599c8d7f2Smichael 30699c8d7f2Smichael return array_reverse($recent); 30799c8d7f2Smichael} 30899c8d7f2Smichael 30999c8d7f2Smichael/** 3107d559c7fSBen Coburn * Internal function used by getRecents 3117d559c7fSBen Coburn * 3127d559c7fSBen Coburn * don't call directly 3137d559c7fSBen Coburn * 3147d559c7fSBen Coburn * @see getRecents() 3157d559c7fSBen Coburn * @author Andreas Gohr <andi@splitbrain.org> 3167d559c7fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 3174f1e2cb3SGerrit Uitslag * 3184f1e2cb3SGerrit Uitslag * @param string $line changelog line 3194f1e2cb3SGerrit Uitslag * @param string $ns restrict to given namespace 3204f1e2cb3SGerrit Uitslag * @param int $flags flags to control which changes are included 3214f1e2cb3SGerrit Uitslag * @param array $seen listing of seen pages 3224f1e2cb3SGerrit Uitslag * @return array|bool false or array with info about a change 3237d559c7fSBen Coburn */ 324d868eb89SAndreas Gohrfunction _handleRecent($line, $ns, $flags, &$seen) 325d868eb89SAndreas Gohr{ 3267d559c7fSBen Coburn if (empty($line)) return false; //skip empty lines 3277d559c7fSBen Coburn 3287d559c7fSBen Coburn // split the line into parts 3291d11f1d3SSatoshi Sahara $recent = ChangeLog::parseLogLine($line); 330252acce3SSatoshi Sahara if ($recent === false) return false; 3317d559c7fSBen Coburn 3327d559c7fSBen Coburn // skip seen ones 3337d559c7fSBen Coburn if (isset($seen[$recent['id']])) return false; 3347d559c7fSBen Coburn 33508e9b52fSPhy // skip changes, of only new items are requested 33608e9b52fSPhy if ($recent['type'] !== DOKU_CHANGE_TYPE_CREATE && ($flags & RECENTS_ONLY_CREATION)) return false; 33768f43bcfSTero Kivinen 3387d559c7fSBen Coburn // skip minors 339ebf1501fSBen Coburn if ($recent['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT && ($flags & RECENTS_SKIP_MINORS)) return false; 3407d559c7fSBen Coburn 3417d559c7fSBen Coburn // remember in seen to skip additional sights 3427d559c7fSBen Coburn $seen[$recent['id']] = 1; 3437d559c7fSBen Coburn 3447d559c7fSBen Coburn // check if it's a hidden page 3457d559c7fSBen Coburn if (isHiddenPage($recent['id'])) return false; 3467d559c7fSBen Coburn 3477d559c7fSBen Coburn // filter namespace 3487d559c7fSBen Coburn if (($ns) && (strpos($recent['id'], $ns . ':') !== 0)) return false; 3497d559c7fSBen Coburn 3507d559c7fSBen Coburn // exclude subnamespaces 3517d559c7fSBen Coburn if (($flags & RECENTS_SKIP_SUBSPACES) && (getNS($recent['id']) != $ns)) return false; 3527d559c7fSBen Coburn 3537d559c7fSBen Coburn // check ACL 35442025dfdSMichael Hamann if ($flags & RECENTS_MEDIA_CHANGES) { 35542025dfdSMichael Hamann $recent['perms'] = auth_quickaclcheck(getNS($recent['id']) . ':*'); 35642025dfdSMichael Hamann } else { 35799c8d7f2Smichael $recent['perms'] = auth_quickaclcheck($recent['id']); 35842025dfdSMichael Hamann } 35999c8d7f2Smichael if ($recent['perms'] < AUTH_READ) return false; 3607d559c7fSBen Coburn 361eeda7adaSGerrit Uitslag // check existence 3621d901ab2SAndreas Gohr if ($flags & RECENTS_SKIP_DELETED) { 36342025dfdSMichael Hamann $fn = (($flags & RECENTS_MEDIA_CHANGES) ? mediaFN($recent['id']) : wikiFN($recent['id'])); 36479e79377SAndreas Gohr if (!file_exists($fn)) return false; 3651d901ab2SAndreas Gohr } 3667d559c7fSBen Coburn 3677d559c7fSBen Coburn return $recent; 3687d559c7fSBen Coburn} 369