}
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...";
+ }
}
$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...";
+ }
}
// @FIXME return true only if something changed (otherwise 0)
return $result;
}
+
+ static public function beforeSchemaUpdate()
+ {
+ // NOOP
+ }
}
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();
* @link http://status.net/
*/
-if (!defined('STATUSNET')) {
- exit(1);
-}
+if (!defined('GNUSOCIAL')) { exit(1); }
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;
}
'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';
$schema = Schema::get();
$schemaUpdater = new SchemaUpdater($schema);
foreach (tableDefs() as $table => $def) {
- preAlterFixes($schemaUpdater, $table);
$schemaUpdater->register($table, $def);
}
$schemaUpdater->checkSchema();
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...");