]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
File and File_redirection adhoc storage methods updated for urlhash
authorMikael Nordfeldth <mmn@hethane.se>
Thu, 19 Feb 2015 17:59:28 +0000 (18:59 +0100)
committerMikael Nordfeldth <mmn@hethane.se>
Thu, 19 Feb 2015 18:05:24 +0000 (19:05 +0100)
classes/File.php
classes/File_redirection.php
classes/Managed_DataObject.php
lib/mediafile.php
lib/schemaupdater.php
plugins/OStatus/classes/Ostatus_profile.php
scripts/upgrade.php

index 7d09b74c43283555a047cf8b0ab64c690933ef94..89dcf61cc550f776648492a9e3a9ab0c8ea5413a 100644 (file)
@@ -606,4 +606,40 @@ class File extends Managed_DataObject
         }
         return hash(self::URLHASH_ALG, $url);
     }
+
+    static public function beforeSchemaUpdate()
+    {
+        $table = strtolower(get_called_class());
+        $schema = Schema::get();
+        $schemadef = $schema->getTableDef($table);
+
+        // 2015-02-19 We have to upgrade our table definitions to have the urlhash field populated
+        if (isset($schemadef['fields']['urlhash']) && in_array('file_urlhash_key', $schemadef['unique keys'])) {
+            // We already have the urlhash field, so no need to migrate it.
+            return;
+        }
+        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',
+                                            );
+        $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;',
+                            $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...";
+    }
 }
index d341839cf23385dd8125a76c73dfc1c031667225..b585ae78848cc148437dd0baf5e35a32d3d6d706 100644 (file)
@@ -355,4 +355,40 @@ class File_redirection extends Managed_DataObject
         $file_redir->httpcode = intval($data['code']);
         $file_redir->insert();
     }
+
+    static public function beforeSchemaUpdate()
+    {
+        $table = strtolower(get_called_class());
+        $schema = Schema::get();
+        $schemadef = $schema->getTableDef($table);
+
+        // 2015-02-19 We have to upgrade our table definitions to have the urlhash field populated
+        if (isset($schemadef['fields']['urlhash']) && in_array('urlhash', $schemadef['primary key'])) {
+            // We already have the urlhash field, so no need to migrate it.
+            return;
+        }
+        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',
+                                            );
+        $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;',
+                            $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...";
+    }
 }
index dc4352270d7232960a0c4847ee1c164f0eed93cd..b324984b7f8e7512d4c8b77606654ca6b10b8ae2 100644 (file)
@@ -396,4 +396,9 @@ abstract class Managed_DataObject extends Memcached_DataObject
         // @FIXME return true only if something changed (otherwise 0)
         return $result;
     }
+
+    static public function beforeSchemaUpdate()
+    {
+        // NOOP
+    }
 }
index 3bbbd162f0c18985d7cfc6238ab2b2d39854d8a5..f18a557fa47048999c3f377e6e67fa3f90c76b21 100644 (file)
@@ -90,11 +90,13 @@ class MediaFile
 
     protected function storeFile()
     {
+        $fileurl = File::url($this->filename);
 
         $file = new File;
 
         $file->filename = $this->filename;
-        $file->url      = File::url($this->filename);
+        $file->urlhash  = File::hashurl($fileurl);
+        $file->url      = $fileurl;
         $filepath       = File::path($this->filename);
         $file->size     = filesize($filepath);
         $file->date     = time();
index 1960a0693001c6948191dcf629b71c1bab38945e..ae746c10b546a4919fde4ab68f63a6943c5194cc 100644 (file)
@@ -28,9 +28,7 @@
  * @link      http://status.net/
  */
 
-if (!defined('STATUSNET')) {
-    exit(1);
-}
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 class SchemaUpdater
 {
@@ -46,6 +44,11 @@ class SchemaUpdater
      */
     public function register($tableName, array $tableDef)
     {
+        // Check if the table we're registering is related to a Managed_DataObject
+        if (is_a(ucfirst($tableName), 'Managed_DataObject', true)) {
+            call_user_func("{$tableName}::beforeSchemaUpdate");
+        }
+
         $this->tables[$tableName] = $tableDef;
     }
 
index 4be4e5112f67f5a4e8464e425135b5bd6b744c05..6180d054816023d814eee058ee97ea98aab148f5 100644 (file)
@@ -2087,13 +2087,15 @@ class Ostatus_profile extends Managed_DataObject
                                    'text/html');
 
         $filepath = File::path($filename);
+        $fileurl = File::url($filename);
 
         file_put_contents($filepath, $final);
 
         $file = new File;
 
         $file->filename = $filename;
-        $file->url      = File::url($filename);
+        $file->urlhash  = File::hashurl($fileurl);
+        $file->url      = $fileurl;
         $file->size     = filesize($filepath);
         $file->date     = time();
         $file->mimetype = 'text/html';
index 07207357e5a1e117076428474bec1de2f1096fc8..c221a495af1cc04489643ce24bb3ce8a10bc1cd8 100644 (file)
@@ -76,7 +76,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 +83,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...");