diff --git a/core/05-functions.php b/core/05-functions.php index 417c3cd..bf874fe 100644 --- a/core/05-functions.php +++ b/core/05-functions.php @@ -130,6 +130,31 @@ function glob_recursive($pattern, $flags = 0) return $files; } +/** + * Normalize file name & path. + * Used to convert filenames returned by glob_recursive() to a format used in pageindex. + * + * @package core + * @author Alx84 + * @param string $filename A filename with storage prefix as retuned by glob_recursive() + * @return string Normalized filename + */ +function normalize_filename($filename) +{ + global $env; + // glob_recursive() returns values like "./storage_prefix/folder/filename.md" + // in the pageindex we save them as "folder/filename.md" + $result = mb_substr( // Store the filename, whilst trimming the storage prefix + $filename, + 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(mb_substr($result, 0, 2) == "./") + $result = mb_substr($result, 2); + + return $result; +} + /** * Resolves a relative path against a given base directory. * @since 0.20.0 diff --git a/core/20-pageindex-loader.php b/core/20-pageindex-loader.php index f3a0e7b..b94b47e 100644 --- a/core/20-pageindex-loader.php +++ b/core/20-pageindex-loader.php @@ -3,68 +3,73 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +/** +* Rebuilds the page index based on what files are found +* @param bool $output Whether to send progress information to the user's browser. +*/ +function pageindex_rebuild(bool $output = true) : void { + + global $env, $pageindex; + + if($output && !is_cli()) { + header("content-type: text/event-stream"); + ob_end_flush(); + } -/* - * Sort out the pageindex. Create it if it doesn't exist, and load + parse it - * if it does. - */ -if(!file_exists($paths->pageindex)) -{ $glob_str = $env->storage_prefix . "*.md"; $existingpages = glob_recursive($glob_str); $existingpages_count = count($existingpages); + // Debug statements. Uncomment when debugging the pageindex regenerator. // var_dump($env->storage_prefix); // var_dump($glob_str); // var_dump($existingpages); + + // save our existing pageindex, if it is available at this point + // we will use it to salvage some data out of it, like tags and authors + if (is_a($pageindex, 'stdClass')) $old_pageindex = $pageindex; + else $old_pageindex = new stdClass(); + + + // compose a new pageindex into a global variable $pageindex = new stdClass(); // We use a for loop here because foreach doesn't loop over new values inserted // while we were looping for($i = 0; $i < $existingpages_count; $i++) { $pagefilename = $existingpages[$i]; - - // Create a new entry + + // Create a new entry for each md file we found $newentry = new stdClass(); - $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(mb_substr($newentry->filename, 0, 2) == "./") - $newentry->filename = mb_substr($newentry->filename, 2); + + // glob_recursive() returns values like "./storage_prefix/folder/filename.md" + // in the pageindex we save them as "folder/filename.md" + $newentry->filename = normalize_filename($pagefilename); + $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" - + // Extract the name of the (sub)page without the ".md" $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 { // This page (potentially) has an associated file! // Let's investigate. - + // Blindly add the file to the pageindex for now. // Future We might want to do a security check on the file later on. // File a bug if you think we should do this. $newentry->uploadedfile = true; // Yes this page does have an uploaded file associated with it $newentry->uploadedfilepath = $pagekey; // It's stored here - + // Work out what kind of file it really is $mimechecker = finfo_open(FILEINFO_MIME_TYPE); $newentry->uploadedfilemime = finfo_file($mimechecker, $env->storage_prefix . $pagekey); } - - // Debug statements. Uncomment when debugging the pageindex regenerator. - // echo("pagekey: "); - // var_dump($pagekey); - // echo("newentry: "); - // var_dump($newentry); - + // Subpage parent checker if(strpos($pagekey, "/") !== false) { @@ -83,7 +88,15 @@ if(!file_exists($paths->pageindex)) $existingpages[] = $subpage_parent_filename; } } - + + // Attempt to salvage tags and lasteditor from the previous pageindex + if (@$old_pageindex->$pagekey->tags) + $newentry->tags = $old_pageindex->$pagekey->tags; + $newentry->lasteditor = "unknown"; + if (@$old_pageindex->$pagekey->lasteditor) + $newentry->lasteditor = $old_pageindex->$pagekey->lasteditor; + + // 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"); @@ -91,20 +104,31 @@ if(!file_exists($paths->pageindex)) "type" => "edit", "rid" => 0, "timestamp" => $newentry->lastmodified, - "filename" => "{$pagefilename}.r0", + "filename" => normalize_filename("{$pagefilename}.r0"), "newsize" => $newentry->size, "sizediff" => $newentry->size, - "editor" => "unknown" + "editor" => $newentry->lasteditor ] ]; } // Store the new entry in the new page index $pageindex->$pagekey = $newentry; + + if($output) { + $message = "[" . ($i + 1) . " / $existingpages_count] Added $pagefilename to the pageindex."; + if(!is_cli()) $message = "data: $message\n\n"; + else $message = "$message\r"; + echo($message); + flush(); + } } - + if(function_exists("history_add_revision")) { - $history_revs = glob_recursive($env->storage_prefix . "*.r*"); - // It's very important that we read the history revisions in the right order and that we don't skip any + + // collect from the filesystem what revision files we have + $history_revs = glob_recursive($env->storage_prefix . "*.md.r*"); + + // sort them in the ascending order of their revision numbers - it's very important for further processing usort($history_revs, function($a, $b) { preg_match("/[0-9]+$/", $a, $revid_a); $revid_a = intval($revid_a[0]); @@ -112,29 +136,40 @@ 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); error_log("raw revid | ".var_export($revid, true)); if(count($revid) === 0) continue; $revid = intval($revid[0]); - + $pagename = filepath_to_pagename($filename); - $filepath_stripped = substr($filename, $strlen_storageprefix); - + $filepath_stripped = normalize_filename($filename); + if(!isset($pageindex->$pagename->history)) $pageindex->$pagename->history = []; - + if(isset($pageindex->$pagename->history[$revid])) continue; - + error_log("pagename: $pagename, revid: $revid, pageindex entry: ".var_export($pageindex->$pagename, true)); $newsize = filesize($filename); $prevsize = 0; if($revid > 0 && isset($pageindex->$pagename->history[$revid - 1])) { $prevsize = filesize(end($pageindex->$pagename->history)->filename); } + + // Let's attempt to salvage the editor for this revision from the old pageindex + // For that we walk through history of edits from old pageindex to find what editor was set for this specific file + $revision_editor = "unknown"; + if ($old_pageindex->$pagename->history) { + foreach ($old_pageindex->$pagename->history as $revision) + if ($revision->filename == $filepath_stripped && isset($revision->editor)) + $revision_editor = $revision->editor; + } + + // save the revision into history $pageindex->$pagename->history[$revid] = (object) [ "type" => "edit", "rid" => $revid, @@ -142,13 +177,30 @@ if(!file_exists($paths->pageindex)) "filename" => $filepath_stripped, "newsize" => $newsize, "sizediff" => $newsize - $prevsize, - "editor" => "unknown" + "editor" => $revision_editor ]; } } - + save_pageindex(); unset($existingpages); + + + if($output && !is_cli()) { + echo("data: Done! \n\n"); + flush(); + } + + +} + +/* + * Sort out the pageindex. Create it if it doesn't exist, and load + parse it + * if it does. + */ +if(!file_exists($paths->pageindex)) +{ + pageindex_rebuild(false); } else { diff --git a/modules/feature-guiconfig.php b/modules/feature-guiconfig.php index 7415cfe..62c1e19 100644 --- a/modules/feature-guiconfig.php +++ b/modules/feature-guiconfig.php @@ -47,10 +47,10 @@ register_module([ $content .= "

You're currently running Pepperminty Wiki $version+" . substr($commit, 0, 7) . ".

\n"; $content .= "

Actions

"; + // rebuild search index button $content .= "\n"; $content .= "
\n"; $content .= "
\n"; - $invindex_rebuild_script = <<