File and File_redirection adhoc storage methods updated for urlhash
This commit is contained in:
parent
0dfe39ac87
commit
45dc76de26
|
@ -606,4 +606,40 @@ class File extends Managed_DataObject
|
||||||
}
|
}
|
||||||
return hash(self::URLHASH_ALG, $url);
|
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->httpcode = intval($data['code']);
|
||||||
$file_redir->insert();
|
$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)
|
// @FIXME return true only if something changed (otherwise 0)
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static public function beforeSchemaUpdate()
|
||||||
|
{
|
||||||
|
// NOOP
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,11 +90,13 @@ class MediaFile
|
||||||
|
|
||||||
protected function storeFile()
|
protected function storeFile()
|
||||||
{
|
{
|
||||||
|
$fileurl = File::url($this->filename);
|
||||||
|
|
||||||
$file = new File;
|
$file = new File;
|
||||||
|
|
||||||
$file->filename = $this->filename;
|
$file->filename = $this->filename;
|
||||||
$file->url = File::url($this->filename);
|
$file->urlhash = File::hashurl($fileurl);
|
||||||
|
$file->url = $fileurl;
|
||||||
$filepath = File::path($this->filename);
|
$filepath = File::path($this->filename);
|
||||||
$file->size = filesize($filepath);
|
$file->size = filesize($filepath);
|
||||||
$file->date = time();
|
$file->date = time();
|
||||||
|
|
|
@ -28,9 +28,7 @@
|
||||||
* @link http://status.net/
|
* @link http://status.net/
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (!defined('STATUSNET')) {
|
if (!defined('GNUSOCIAL')) { exit(1); }
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
class SchemaUpdater
|
class SchemaUpdater
|
||||||
{
|
{
|
||||||
|
@ -46,6 +44,11 @@ class SchemaUpdater
|
||||||
*/
|
*/
|
||||||
public function register($tableName, array $tableDef)
|
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;
|
$this->tables[$tableName] = $tableDef;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2087,13 +2087,15 @@ class Ostatus_profile extends Managed_DataObject
|
||||||
'text/html');
|
'text/html');
|
||||||
|
|
||||||
$filepath = File::path($filename);
|
$filepath = File::path($filename);
|
||||||
|
$fileurl = File::url($filename);
|
||||||
|
|
||||||
file_put_contents($filepath, $final);
|
file_put_contents($filepath, $final);
|
||||||
|
|
||||||
$file = new File;
|
$file = new File;
|
||||||
|
|
||||||
$file->filename = $filename;
|
$file->filename = $filename;
|
||||||
$file->url = File::url($filename);
|
$file->urlhash = File::hashurl($fileurl);
|
||||||
|
$file->url = $fileurl;
|
||||||
$file->size = filesize($filepath);
|
$file->size = filesize($filepath);
|
||||||
$file->date = time();
|
$file->date = time();
|
||||||
$file->mimetype = 'text/html';
|
$file->mimetype = 'text/html';
|
||||||
|
|
|
@ -76,7 +76,6 @@ function updateSchemaCore()
|
||||||
$schema = Schema::get();
|
$schema = Schema::get();
|
||||||
$schemaUpdater = new SchemaUpdater($schema);
|
$schemaUpdater = new SchemaUpdater($schema);
|
||||||
foreach (tableDefs() as $table => $def) {
|
foreach (tableDefs() as $table => $def) {
|
||||||
preAlterFixes($schemaUpdater, $table);
|
|
||||||
$schemaUpdater->register($table, $def);
|
$schemaUpdater->register($table, $def);
|
||||||
}
|
}
|
||||||
$schemaUpdater->checkSchema();
|
$schemaUpdater->checkSchema();
|
||||||
|
@ -84,43 +83,6 @@ function updateSchemaCore()
|
||||||
printfnq("DONE.\n");
|
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()
|
function updateSchemaPlugins()
|
||||||
{
|
{
|
||||||
printfnq("Upgrading plugin schema...");
|
printfnq("Upgrading plugin schema...");
|
||||||
|
|
Loading…
Reference in New Issue
Block a user