2013-05-17 19:00:31 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhabricatorFilesManagementRebuildWorkflow
|
|
|
|
extends PhabricatorFilesManagementWorkflow {
|
|
|
|
|
2015-01-15 21:42:07 +01:00
|
|
|
protected function didConstruct() {
|
2013-05-17 19:00:31 +02:00
|
|
|
$this
|
|
|
|
->setName('rebuild')
|
|
|
|
->setSynopsis('Rebuild metadata of old files.')
|
|
|
|
->setArguments(
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'name' => 'all',
|
|
|
|
'help' => 'Update all files.',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => 'dry-run',
|
|
|
|
'help' => 'Show what would be updated.',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => 'rebuild-mime',
|
|
|
|
'help' => 'Rebuild MIME information.',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => 'rebuild-dimensions',
|
|
|
|
'help' => 'Rebuild image dimension information.',
|
|
|
|
),
|
2013-05-29 15:28:57 +02:00
|
|
|
array(
|
|
|
|
'name' => 'names',
|
|
|
|
'wildcard' => true,
|
|
|
|
),
|
2013-05-17 19:00:31 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute(PhutilArgumentParser $args) {
|
|
|
|
$console = PhutilConsole::getConsole();
|
|
|
|
|
2013-05-29 15:28:57 +02:00
|
|
|
$iterator = $this->buildIterator($args);
|
|
|
|
if (!$iterator) {
|
2013-05-17 19:00:31 +02:00
|
|
|
throw new PhutilArgumentUsageException(
|
2014-06-09 20:36:49 +02:00
|
|
|
'Either specify a list of files to update, or use `--all` '.
|
|
|
|
'to update all files.');
|
2013-05-17 19:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$update = array(
|
|
|
|
'mime' => $args->getArg('rebuild-mime'),
|
|
|
|
'dimensions' => $args->getArg('rebuild-dimensions'),
|
|
|
|
);
|
|
|
|
|
|
|
|
// If the user didn't select anything, rebuild everything.
|
|
|
|
if (!array_filter($update)) {
|
|
|
|
foreach ($update as $key => $ignored) {
|
|
|
|
$update[$key] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$is_dry_run = $args->getArg('dry-run');
|
|
|
|
|
|
|
|
$failed = array();
|
|
|
|
|
|
|
|
foreach ($iterator as $file) {
|
|
|
|
$fid = 'F'.$file->getID();
|
|
|
|
|
|
|
|
if ($update['mime']) {
|
|
|
|
$tmp = new TempFile();
|
|
|
|
Filesystem::writeFile($tmp, $file->loadFileData());
|
|
|
|
$new_type = Filesystem::getMimeType($tmp);
|
|
|
|
|
|
|
|
if ($new_type == $file->getMimeType()) {
|
|
|
|
$console->writeOut(
|
|
|
|
"%s: Mime type not changed (%s).\n",
|
|
|
|
$fid,
|
|
|
|
$new_type);
|
|
|
|
} else {
|
|
|
|
if ($is_dry_run) {
|
|
|
|
$console->writeOut(
|
|
|
|
"%s: Would update Mime type: '%s' -> '%s'.\n",
|
|
|
|
$fid,
|
|
|
|
$file->getMimeType(),
|
|
|
|
$new_type);
|
|
|
|
} else {
|
|
|
|
$console->writeOut(
|
|
|
|
"%s: Updating Mime type: '%s' -> '%s'.\n",
|
|
|
|
$fid,
|
|
|
|
$file->getMimeType(),
|
|
|
|
$new_type);
|
|
|
|
$file->setMimeType($new_type);
|
|
|
|
$file->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($update['dimensions']) {
|
|
|
|
if (!$file->isViewableImage()) {
|
|
|
|
$console->writeOut(
|
|
|
|
"%s: Not an image file.\n",
|
|
|
|
$fid);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$metadata = $file->getMetadata();
|
|
|
|
$image_width = idx($metadata, PhabricatorFile::METADATA_IMAGE_WIDTH);
|
|
|
|
$image_height = idx($metadata, PhabricatorFile::METADATA_IMAGE_HEIGHT);
|
|
|
|
if ($image_width && $image_height) {
|
|
|
|
$console->writeOut(
|
|
|
|
"%s: Image dimensions already exist.\n",
|
|
|
|
$fid);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($is_dry_run) {
|
|
|
|
$console->writeOut(
|
|
|
|
"%s: Would update file dimensions (dry run)\n",
|
|
|
|
$fid);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$console->writeOut(
|
2014-06-09 20:36:49 +02:00
|
|
|
'%s: Updating metadata... ',
|
2013-05-17 19:00:31 +02:00
|
|
|
$fid);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$file->updateDimensions();
|
|
|
|
$console->writeOut("done.\n");
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
$console->writeOut("failed!\n");
|
|
|
|
$console->writeErr("%s\n", (string)$ex);
|
|
|
|
$failed[] = $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($failed) {
|
|
|
|
$console->writeOut("**Failures!**\n");
|
|
|
|
$ids = array();
|
|
|
|
foreach ($failed as $file) {
|
|
|
|
$ids[] = 'F'.$file->getID();
|
|
|
|
}
|
|
|
|
$console->writeOut("%s\n", implode(', ', $ids));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
$console->writeOut("**Success!**\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|