Add urlhash field to File_thumbnail for indexing

This commit is contained in:
Mikael Nordfeldth 2016-02-10 04:15:41 +01:00
parent 49b7648fea
commit 893d888152
2 changed files with 46 additions and 6 deletions

View File

@ -27,17 +27,21 @@ class File_thumbnail extends Managed_DataObject
{
public $__table = 'file_thumbnail'; // table name
public $file_id; // int(4) primary_key not_null
public $urlhash; // varchar(64) indexed
public $url; // text
public $filename; // text
public $width; // int(4) primary_key
public $height; // int(4) primary_key
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
const URLHASH_ALG = 'sha256';
public static function schemaDef()
{
return array(
'fields' => array(
'file_id' => array('type' => 'int', 'not null' => true, 'description' => 'thumbnail for what URL/file'),
'urlhash' => array('type' => 'varchar', 'length' => 64, 'description' => 'sha256 of url field if non-empty'),
'url' => array('type' => 'text', 'description' => 'URL of thumbnail'),
'filename' => array('type' => 'text', 'description' => 'if stored locally, filename is put here'),
'width' => array('type' => 'int', 'description' => 'width of thumbnail'),
@ -47,6 +51,7 @@ class File_thumbnail extends Managed_DataObject
'primary key' => array('file_id', 'width', 'height'),
'indexes' => array(
'file_thumbnail_file_id_idx' => array('file_id'),
'file_thumbnail_urlhash_idx' => array('urlhash'),
),
'foreign keys' => array(
'file_thumbnail_file_id_fkey' => array('file', array('file_id' => 'id')),
@ -136,12 +141,6 @@ class File_thumbnail extends Managed_DataObject
{
if (!empty($this->filename) || $this->getFile()->isLocal()) {
// A locally stored File, so we can dynamically generate a URL.
if (!empty($this->url)) {
// Let's just clear this field as there is no point in having it for local files.
$orig = clone($this);
$this->url = '';
$this->update($orig);
}
$url = common_local_url('attachment_thumbnail', array('attachment'=>$this->file_id));
if (strpos($url, '?') === false) {
$url .= '?';
@ -192,4 +191,31 @@ class File_thumbnail extends Managed_DataObject
{
return File::getByID($this->file_id);
}
static public function hashurl($url)
{
if (!mb_strlen($url)) {
throw new Exception('No URL provided to hash algorithm.');
}
return hash(self::URLHASH_ALG, $url);
}
public function onInsert()
{
$this->setUrlhash();
}
public function onUpdate($dataObject=false)
{
// if we have nothing to compare with OR it has changed from previous entry
if (!$dataObject instanceof Managed_DataObject || $this->url !== $dataObject->url) {
$this->setUrlhash();
}
}
public function setUrlhash()
{
$this->urlhash = mb_strlen($this->url)>0 ? self::hashurl($this->url) : null;
}
}

View File

@ -47,6 +47,7 @@ function main()
fixupFileGeometry();
deleteLocalFileThumbnailsWithoutFilename();
deleteMissingLocalFileThumbnails();
fixupFileThumbnailUrlhash();
setFilehashOnLocalFiles();
initGroupProfileId();
@ -520,6 +521,19 @@ function setFilehashOnLocalFiles()
printfnq("DONE.\n");
}
function fixupFileThumbnailUrlhash()
{
printfnq("Setting urlhash for File_thumbnail entries: ");
$thumb = new File_thumbnail();
$thumb->query('UPDATE '.$thumb->escapedTableName().' SET urlhash=SHA2(url, 256) WHERE'.
' url IS NOT NULL AND'. // find all entries with a url value
' url != "" AND'. // precaution against non-null empty strings
' urlhash IS NULL'); // but don't touch those we've already calculated
printfnq("DONE.\n");
}
function migrateProfilePrefs()
{
printfnq("Finding and possibly migrating Profile_prefs entries: ");