]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - scripts/upgrade.php
Misses this file to merge. I like the comments.
[quix0rs-gnu-social.git] / scripts / upgrade.php
index 07207357e5a1e117076428474bec1de2f1096fc8..2873146b3d90dce5e143cdc474acceb08890fdc4 100644 (file)
@@ -29,7 +29,7 @@ Upgrade database schema and data to latest software
 
 END_OF_UPGRADE_HELP;
 
-require_once INSTALLDIR.'/scripts/commandline.inc';
+require_once INSTALLDIR.'/scripts/commandline.inc.php';
 
 function main()
 {
@@ -48,6 +48,7 @@ function main()
         fixupFileGeometry();
         deleteLocalFileThumbnailsWithoutFilename();
         deleteMissingLocalFileThumbnails();
+        setFilehashOnLocalFiles();
 
         initGroupProfileId();
         initLocalGroup();
@@ -76,7 +77,6 @@ function updateSchemaCore()
     $schema = Schema::get();
     $schemaUpdater = new SchemaUpdater($schema);
     foreach (tableDefs() as $table => $def) {
-        preAlterFixes($schemaUpdater, $table);
         $schemaUpdater->register($table, $def);
     }
     $schemaUpdater->checkSchema();
@@ -84,43 +84,6 @@ function updateSchemaCore()
     printfnq("DONE.\n");
 }
 
-function preAlterFixes($schemaUpdater, $table)
-{
-    switch ($table) {
-    case 'file':
-    case 'file_redirection':
-        $schemadef = $schemaUpdater->schema->getTableDef($table);
-        if (isset($schemadef['fields']['urlhash'])) {
-            // We already have the urlhash field, so no need to migrate it.
-            break;
-        }
-        echo "\nFound old $table table, upgrading it to contain 'urlhash' field...\n";
-        // We have to create a urlhash that is _not_ the primary key,
-        // transfer data and THEN run checkSchema
-        $schemadef['fields']['urlhash'] = array (
-                                              'type' => 'varchar',
-                                              'length' => 64,
-                                              'description' => 'sha256 of destination URL after following redirections',
-                                            );
-        $schemaUpdater->schema->ensureTable($table, $schemadef);
-        echo "DONE.\n";
-
-        $classname = ucfirst($table);
-        $tablefix = new $classname;
-        // urlhash is hash('sha256', $url) in the File table
-        echo "Updating urlhash fields in $table table...\n";
-        // Maybe very MySQL specific :(
-        $tablefix->query(sprintf('UPDATE %1$s SET %2$s=%3$s;',
-                            $schemaUpdater->schema->quoteIdentifier($table),
-                            'urlhash',
-                            // The line below is "result of sha256 on column `url`"
-                            'SHA2(url, 256)'));
-        echo "DONE.\n";
-        echo "Resuming core schema upgrade...";
-        break;
-    }
-}
-
 function updateSchemaPlugins()
 {
     printfnq("Upgrading plugin schema...");
@@ -528,7 +491,9 @@ function deleteMissingLocalFileThumbnails()
     // Checking if there were any File_thumbnail entries without filename
     if ($thumbs->find()) {
         while ($thumbs->fetch()) {
-            if (!file_exists(File_thumbnail::path($thumbs->filename))) {
+            try {
+                $thumbs->getPath();
+            } catch (FileNotFoundException $e) {
                 $thumbs->delete();
             }
         }
@@ -537,4 +502,30 @@ function deleteMissingLocalFileThumbnails()
     printfnq("DONE.\n");
 }
 
+/*
+ * Files are now stored with their hash, so let's generate for previously uploaded files.
+ */
+function setFilehashOnLocalFiles()
+{
+    printfnq('Ensuring all local files have the filehash field set...');
+
+    $file = new File();
+    $file->whereAdd('filename IS NOT NULL');        // local files
+    $file->whereAdd('filehash IS NULL', 'AND');     // without filehash value
+
+    if ($file->find()) {
+        while ($file->fetch()) {
+            try {
+                $orig = clone($file);
+                $file->filehash = hash_file(File::FILEHASH_ALG, $file->getPath());
+                $file->update($orig);
+            } catch (FileNotFoundException $e) {
+                echo "\n    WARNING: file ID {$file->id} does not exist on path '{$e->path}'. Clean up the file table?";
+            }
+        }
+    }
+
+    printfnq("DONE.\n");
+}
+
 main();