Commit 45dc76de authored by mmn's avatar mmn

File and File_redirection adhoc storage methods updated for urlhash

parent 0dfe39ac
......@@ -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...";
}
}
......@@ -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...";
}
}
......@@ -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
}
}
......@@ -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();
......
......@@ -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;
}
......
......@@ -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';
......
......@@ -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...");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment