1
0
Fork 0
mirror of https://github.com/sbrl/Pepperminty-Wiki.git synced 2024-11-25 05:22:59 +00:00

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 { function filepath_to_pagename(string $filepath) : string {
global $env; global $env;
// Strip the storage prefix, but only if it isn't a dot // Strip the storage prefix, but only if it isn't a dot
if(starts_with($filepath, $env->storage_prefix) && $env->storage_prefix !== ".") { if(starts_with($filepath, $env->storage_prefix) && $env->storage_prefix !== ".")
$filepath = substr($filepath, strlen($env->storage_prefix)); $filepath = mb_substr($filepath, mb_strlen($env->storage_prefix));
// Strip the forward slash at the beginning
if($filepath[0] == "/" && $env->storage_prefix[-1] !== "/")
$filepath = substr($filepath, 1);
}
if(preg_match("/\.r[0-9]+$/", $filepath) !== false) // If a revision number is detected, strip it
$filepath = substr($filepath, 0, strrpos($filepath, ".r")); 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")) if(ends_with($filepath, ".md"))
$filepath = substr($filepath, 0, -3); $filepath = mb_substr($filepath, 0, -3);
return $filepath; return $filepath;
} }

View file

@ -22,25 +22,21 @@ if(!file_exists($paths->pageindex))
// Create a new entry // Create a new entry
$newentry = new stdClass(); $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, $pagefilename,
mb_strlen(preg_replace("/^\.\//iu", "", $env->storage_prefix)) // glob_recursive trim the ./ from returned filenames , so we need to as well 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 // Remove the `./` from the beginning if it's still hanging around
if(substr($newentry->filename, 0, 2) == "./") if(mb_substr($newentry->filename, 0, 2) == "./")
$newentry->filename = substr($newentry->filename, 2); $newentry->filename = mb_substr($newentry->filename, 2);
$newentry->size = filesize($pagefilename); // Store the page size $newentry->size = filesize($pagefilename); // Store the page size
$newentry->lastmodified = filemtime($pagefilename); // Store the date last modified $newentry->lastmodified = filemtime($pagefilename); // Store the date last modified
// Todo find a way to keep the last editor independent of the page index // Todo find a way to keep the last editor independent of the page index
$newentry->lasteditor = "unknown"; // Set the editor to "unknown" $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" // 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... if(file_exists($env->storage_prefix . $pagekey) && // If it exists...
!is_dir($env->storage_prefix . $pagekey)) // ...and isn't a directory !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 if(function_exists("history_add_revision") && !file_exists("{$pagefilename}.r0")) { // Can't use module_exists - too early
copy($pagefilename, "{$pagefilename}.r0"); copy($pagefilename, "{$pagefilename}.r0");
$newentry->history = [ (object) [ $newentry->history = [ (object) [
@ -111,6 +108,7 @@ if(!file_exists($paths->pageindex))
$revid_b = intval($revid_b[0]); $revid_b = intval($revid_b[0]);
return $revid_a - $revid_b; 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); $strlen_storageprefix = strlen($env->storage_prefix);
foreach($history_revs as $filename) { foreach($history_revs as $filename) {
preg_match("/[0-9]+$/", $filename, $revid); preg_match("/[0-9]+$/", $filename, $revid);