Bugfixx: improve rebustness of new filepath_to_pagename and pageindex rebuilder

This commit is contained in:
Starbeamrainbowlabs 2020-08-08 22:18:12 +01:00
parent c0fa5b8ae4
commit 5fed4cb5ab
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
2 changed files with 14 additions and 18 deletions

View File

@ -167,18 +167,16 @@ function path_resolve(string $path, string $basePath = null) {
function filepath_to_pagename(string $filepath) : string {
global $env;
// Strip the storage prefix, but only if it isn't a dot
if(starts_with($filepath, $env->storage_prefix) && $env->storage_prefix !== ".") {
$filepath = substr($filepath, strlen($env->storage_prefix));
// Strip the forward slash at the beginning
if($filepath[0] == "/" && $env->storage_prefix[-1] !== "/")
$filepath = substr($filepath, 1);
}
if(starts_with($filepath, $env->storage_prefix) && $env->storage_prefix !== ".")
$filepath = mb_substr($filepath, mb_strlen($env->storage_prefix));
if(preg_match("/\.r[0-9]+$/", $filepath) !== false)
$filepath = substr($filepath, 0, strrpos($filepath, ".r"));
// If a revision number is detected, strip it
if(preg_match("/\.r[0-9]+$/", $filepath) > 0)
$filepath = mb_substr($filepath, 0, mb_strrpos($filepath, ".r"));
// Strip the .md file extension
if(ends_with($filepath, ".md"))
$filepath = substr($filepath, 0, -3);
$filepath = mb_substr($filepath, 0, -3);
return $filepath;
}

View File

@ -22,25 +22,21 @@ if(!file_exists($paths->pageindex))
// Create a new entry
$newentry = new stdClass();
$newentry->filename = substr( // Store the filename, whilst trimming the storage prefix
$newentry->filename = mb_substr( // Store the filename, whilst trimming the storage prefix
$pagefilename,
mb_strlen(preg_replace("/^\.\//iu", "", $env->storage_prefix)) // glob_recursive trim the ./ from returned filenames , so we need to as well
);
// Remove the `./` from the beginning if it's still hanging around
if(substr($newentry->filename, 0, 2) == "./")
$newentry->filename = substr($newentry->filename, 2);
if(mb_substr($newentry->filename, 0, 2) == "./")
$newentry->filename = mb_substr($newentry->filename, 2);
$newentry->size = filesize($pagefilename); // Store the page size
$newentry->lastmodified = filemtime($pagefilename); // Store the date last modified
// Todo find a way to keep the last editor independent of the page index
$newentry->lasteditor = "unknown"; // Set the editor to "unknown"
// POTENTIAL BUG: If $env->storage_prefix is not ., then this we need to be more intelligent here
// Extract the name of the (sub)page without the ".md"
$pagekey = mb_substr($newentry->filename, 0, -3);
$pagekey = filepath_to_pagename($newentry->filename);
error_log("pagename '$newentry->filename' → filepath '$pagekey'");
if(file_exists($env->storage_prefix . $pagekey) && // If it exists...
!is_dir($env->storage_prefix . $pagekey)) // ...and isn't a directory
@ -84,6 +80,7 @@ if(!file_exists($paths->pageindex))
}
}
// If the initial revision doesn't exist on disk, create it (if it does, then we handle that later)
if(function_exists("history_add_revision") && !file_exists("{$pagefilename}.r0")) { // Can't use module_exists - too early
copy($pagefilename, "{$pagefilename}.r0");
$newentry->history = [ (object) [
@ -111,6 +108,7 @@ if(!file_exists($paths->pageindex))
$revid_b = intval($revid_b[0]);
return $revid_a - $revid_b;
});
// We can guarantee that the direcotry separator is present on the end - it's added explicitly earlier
$strlen_storageprefix = strlen($env->storage_prefix);
foreach($history_revs as $filename) {
preg_match("/[0-9]+$/", $filename, $revid);