+// Update PKWK_MAXSHOW_CACHE itself (Add or renew about the $page) (Light)
+// Use without $autolink
+function lastmodified_add($update = '', $remove = '')
+{
+ global $maxshow, $whatsnew, $autolink;
+
+ // AutoLink implimentation needs everything, for now
+ if ($autolink) {
+ put_lastmodified(); // Try to (re)create ALL
+ return;
+ }
+
+ if (($update == '' || check_non_list($update)) && $remove == '')
+ return; // No need
+
+ $file = CACHE_DIR . PKWK_MAXSHOW_CACHE;
+ if (! file_exists($file)) {
+ put_lastmodified(); // Try to (re)create ALL
+ return;
+ }
+
+ // Open
+ pkwk_touch_file($file);
+ $fp = fopen($file, 'r+') or
+ die_message('Cannot open ' . 'CACHE_DIR/' . PKWK_MAXSHOW_CACHE);
+ set_file_buffer($fp, 0);
+ flock($fp, LOCK_EX);
+
+ // Read (keep the order of the lines)
+ $recent_pages = $matches = array();
+ foreach(file_head($file, $maxshow + PKWK_MAXSHOW_ALLOWANCE, FALSE) as $line)
+ if (preg_match('/^([0-9]+)\t(.+)/', $line, $matches))
+ $recent_pages[$matches[2]] = $matches[1];
+
+ // Remove if it exists inside
+ if (isset($recent_pages[$update])) unset($recent_pages[$update]);
+ if (isset($recent_pages[$remove])) unset($recent_pages[$remove]);
+
+ // Add to the top: like array_unshift()
+ if ($update != '')
+ $recent_pages = array($update => get_filetime($update)) + $recent_pages;
+
+ // Check
+ $abort = count($recent_pages) < $maxshow;
+
+ if (! $abort) {
+ // Write
+ ftruncate($fp, 0);
+ rewind($fp);
+ foreach ($recent_pages as $_page=>$time)
+ fputs($fp, $time . "\t" . $_page . "\n");
+ }
+
+ flock($fp, LOCK_UN);
+ fclose($fp);
+
+ if ($abort) {
+ put_lastmodified(); // Try to (re)create ALL
+ return;
+ }
+
+
+
+ // ----
+ // Update the page 'RecentChanges'
+
+ $recent_pages = array_splice($recent_pages, 0, $maxshow);
+ $file = get_filename($whatsnew);
+
+ // Open
+ pkwk_touch_file($file);
+ $fp = fopen($file, 'r+') or
+ die_message('Cannot open ' . htmlsc($whatsnew));
+ set_file_buffer($fp, 0);
+ flock($fp, LOCK_EX);
+
+ // Recreate
+ ftruncate($fp, 0);
+ rewind($fp);
+ foreach ($recent_pages as $_page=>$time)
+ fputs($fp, '-' . htmlsc(format_date($time)) .
+ ' - ' . '[[' . htmlsc($_page) . ']]' . "\n");
+ fputs($fp, '#norelated' . "\n"); // :)
+
+ flock($fp, LOCK_UN);
+ fclose($fp);
+}
+
+// Re-create PKWK_MAXSHOW_CACHE (Heavy)