File_redirection also got urlhash column

This commit is contained in:
Mikael Nordfeldth 2015-02-19 18:34:48 +01:00
parent 176bde269f
commit 27480d8e8e
3 changed files with 44 additions and 16 deletions

View File

@ -479,6 +479,16 @@ class File extends Managed_DataObject
return $this->url; return $this->url;
} }
static public function getByUrl($url)
{
$file = new File();
$file->urlhash = self::hashurl($url);
if (!$file->find(true)) {
throw new NoResultException($file);
}
return $file;
}
public function updateUrl($url) public function updateUrl($url)
{ {
$file = File::getKV('urlhash', self::hashurl($url)); $file = File::getKV('urlhash', self::hashurl($url));

View File

@ -29,7 +29,8 @@ class File_redirection extends Managed_DataObject
/* the code below is auto generated do not remove the above tag */ /* the code below is auto generated do not remove the above tag */
public $__table = 'file_redirection'; // table name public $__table = 'file_redirection'; // table name
public $url; // varchar(255) primary_key not_null public $urlhash; // varchar(64) primary_key not_null
public $url; // text
public $file_id; // int(4) public $file_id; // int(4)
public $redirections; // int(4) public $redirections; // int(4)
public $httpcode; // int(4) public $httpcode; // int(4)
@ -42,19 +43,30 @@ class File_redirection extends Managed_DataObject
{ {
return array( return array(
'fields' => array( 'fields' => array(
'url' => array('type' => 'varchar', 'length' => 255, 'not null' => true, 'description' => 'short URL (or any other kind of redirect) for file (id)'), 'urlhash' => array('type' => 'varchar', 'length' => 64, 'description' => 'sha256 hash of the URL'),
'url' => array('type' => 'text', 'description' => 'short URL (or any other kind of redirect) for file (id)'),
'file_id' => array('type' => 'int', 'description' => 'short URL for what URL/file'), 'file_id' => array('type' => 'int', 'description' => 'short URL for what URL/file'),
'redirections' => array('type' => 'int', 'description' => 'redirect count'), 'redirections' => array('type' => 'int', 'description' => 'redirect count'),
'httpcode' => array('type' => 'int', 'description' => 'HTTP status code (20x, 30x, etc.)'), 'httpcode' => array('type' => 'int', 'description' => 'HTTP status code (20x, 30x, etc.)'),
'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'), 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
), ),
'primary key' => array('url'), 'primary key' => array('urlhash'),
'foreign keys' => array( 'foreign keys' => array(
'file_redirection_file_id_fkey' => array('file' => array('file_id' => 'id')), 'file_redirection_file_id_fkey' => array('file' => array('file_id' => 'id')),
), ),
); );
} }
static public function getByUrl($url)
{
$file = new File_redirection();
$file->urlhash = File::hashurl($url);
if (!$file->find(true)) {
throw new NoResultException($file);
}
return $file;
}
static function _commonHttp($url, $redirs) { static function _commonHttp($url, $redirs) {
$request = new HTTPClient($url); $request = new HTTPClient($url);
$request->setConfig(array( $request->setConfig(array(
@ -161,17 +173,18 @@ class File_redirection extends Managed_DataObject
*/ */
public function where($in_url, $discover=true) { public function where($in_url, $discover=true) {
// let's see if we know this... // let's see if we know this...
$a = File::getKV('url', $in_url); try {
$a = File::getByUrl($in_url);
if (!empty($a)) {
// this is a direct link to $a->url // this is a direct link to $a->url
return $a->url; return $a->url;
} else { } catch (NoResultException $e) {
$b = File_redirection::getKV('url', $in_url); try {
if (!empty($b)) { $b = File_redirection::getByUrl($in_url);
// this is a redirect to $b->file_id // this is a redirect to $b->file_id
$a = File::getKV('id', $b->file_id); $a = File::getKV('id', $b->file_id);
return $a->url; return $a->url;
} catch (NoResultException $e) {
// Oh well, let's keep going
} }
} }
@ -274,6 +287,7 @@ class File_redirection extends Managed_DataObject
$file_redir = File_redirection::getKV('url', $short_url); $file_redir = File_redirection::getKV('url', $short_url);
if (!$file_redir instanceof File_redirection) { if (!$file_redir instanceof File_redirection) {
$file_redir = new File_redirection; $file_redir = new File_redirection;
$file_redir->urlhash = File::hashurl($short_url);
$file_redir->url = $short_url; $file_redir->url = $short_url;
$file_redir->file_id = $file_id; $file_redir->file_id = $file_id;
$file_redir->insert(); $file_redir->insert();
@ -334,6 +348,7 @@ class File_redirection extends Managed_DataObject
function saveNew($data, $file_id, $url) { function saveNew($data, $file_id, $url) {
$file_redir = new File_redirection; $file_redir = new File_redirection;
$file_redir->urlhash = File::hashurl($short_url);
$file_redir->url = $url; $file_redir->url = $url;
$file_redir->file_id = $file_id; $file_redir->file_id = $file_id;
$file_redir->redirections = intval($data['redirects']); $file_redir->redirections = intval($data['redirects']);

View File

@ -88,12 +88,13 @@ function preAlterFixes($schemaUpdater, $table)
{ {
switch ($table) { switch ($table) {
case 'file': case 'file':
case 'file_redirection':
$schemadef = $schemaUpdater->schema->getTableDef($table); $schemadef = $schemaUpdater->schema->getTableDef($table);
if (isset($schemadef['fields']['urlhash'])) { if (isset($schemadef['fields']['urlhash'])) {
// We already have the urlhash field, so no need to migrate it. // We already have the urlhash field, so no need to migrate it.
break; break;
} }
printfnq("\nFound old $table table, upgrading it to contain 'urlhash' field...\n"); echo "\nFound old $table table, upgrading it to contain 'urlhash' field...\n";
// We have to create a urlhash that is _not_ the primary key, // We have to create a urlhash that is _not_ the primary key,
// transfer data and THEN run checkSchema // transfer data and THEN run checkSchema
$schemadef['fields']['urlhash'] = array ( $schemadef['fields']['urlhash'] = array (
@ -102,18 +103,20 @@ function preAlterFixes($schemaUpdater, $table)
'description' => 'sha256 of destination URL after following redirections', 'description' => 'sha256 of destination URL after following redirections',
); );
$schemaUpdater->schema->ensureTable($table, $schemadef); $schemaUpdater->schema->ensureTable($table, $schemadef);
printfnq("DONE.\n"); echo "DONE.\n";
$filefix = new File(); $classname = ucfirst($table);
$tablefix = new $classname;
// urlhash is hash('sha256', $url) in the File table // urlhash is hash('sha256', $url) in the File table
printfnq("Updating urlhash fields in $table table...\n"); echo "Updating urlhash fields in $table table...\n";
$filefix->query(sprintf('UPDATE %1$s SET %2$s=%3$s;', // Maybe very MySQL specific :(
$tablefix->query(sprintf('UPDATE %1$s SET %2$s=%3$s;',
$schemaUpdater->schema->quoteIdentifier($table), $schemaUpdater->schema->quoteIdentifier($table),
'urlhash', 'urlhash',
// The line below is "result of sha256 on column `url`" // The line below is "result of sha256 on column `url`"
'SHA2(url, 256)')); 'SHA2(url, 256)'));
printfnq("DONE.\n"); echo "DONE.\n";
printfnq("Resuming core schema upgrade..."); echo "Resuming core schema upgrade...";
break; break;
} }
} }