From: Mikael Nordfeldth Date: Thu, 19 Feb 2015 17:59:28 +0000 (+0100) Subject: File and File_redirection adhoc storage methods updated for urlhash X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=45dc76de268f708bc7bf4e8bd1c6b1f2fd5eb033;p=quix0rs-gnu-social.git File and File_redirection adhoc storage methods updated for urlhash --- diff --git a/classes/File.php b/classes/File.php index 7d09b74c43..89dcf61cc5 100644 --- a/classes/File.php +++ b/classes/File.php @@ -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..."; + } } diff --git a/classes/File_redirection.php b/classes/File_redirection.php index d341839cf2..b585ae7884 100644 --- a/classes/File_redirection.php +++ b/classes/File_redirection.php @@ -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..."; + } } diff --git a/classes/Managed_DataObject.php b/classes/Managed_DataObject.php index dc4352270d..b324984b7f 100644 --- a/classes/Managed_DataObject.php +++ b/classes/Managed_DataObject.php @@ -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 + } } diff --git a/lib/mediafile.php b/lib/mediafile.php index 3bbbd162f0..f18a557fa4 100644 --- a/lib/mediafile.php +++ b/lib/mediafile.php @@ -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(); diff --git a/lib/schemaupdater.php b/lib/schemaupdater.php index 1960a06930..ae746c10b5 100644 --- a/lib/schemaupdater.php +++ b/lib/schemaupdater.php @@ -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; } diff --git a/plugins/OStatus/classes/Ostatus_profile.php b/plugins/OStatus/classes/Ostatus_profile.php index 4be4e5112f..6180d05481 100644 --- a/plugins/OStatus/classes/Ostatus_profile.php +++ b/plugins/OStatus/classes/Ostatus_profile.php @@ -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'; diff --git a/scripts/upgrade.php b/scripts/upgrade.php index 07207357e5..c221a495af 100644 --- a/scripts/upgrade.php +++ b/scripts/upgrade.php @@ -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...");