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')
|
2015-05-22 09:27:56 +02:00
|
|
|
->setSynopsis(pht('Rebuild metadata of old files.'))
|
2013-05-17 19:00:31 +02:00
|
|
|
->setArguments(
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'name' => 'all',
|
2015-05-22 09:27:56 +02:00
|
|
|
'help' => pht('Update all files.'),
|
2013-05-17 19:00:31 +02:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => 'dry-run',
|
2015-05-22 09:27:56 +02:00
|
|
|
'help' => pht('Show what would be updated.'),
|
2013-05-17 19:00:31 +02:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => 'rebuild-mime',
|
2015-05-22 09:27:56 +02:00
|
|
|
'help' => pht('Rebuild MIME information.'),
|
2013-05-17 19:00:31 +02:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => 'rebuild-dimensions',
|
2015-05-22 09:27:56 +02:00
|
|
|
'help' => pht('Rebuild image dimension information.'),
|
2013-05-17 19:00:31 +02:00
|
|
|
),
|
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(
|
2015-05-22 09:27:56 +02:00
|
|
|
pht(
|
|
|
|
'Either specify a list of files to update, or use `%s` '.
|
|
|
|
'to update all files.',
|
|
|
|
'--all'));
|
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(
|
2015-05-22 09:27:56 +02:00
|
|
|
"%s\n",
|
|
|
|
pht(
|
|
|
|
'%s: Mime type not changed (%s).',
|
|
|
|
$fid,
|
|
|
|
$new_type));
|
2013-05-17 19:00:31 +02:00
|
|
|
} else {
|
|
|
|
if ($is_dry_run) {
|
|
|
|
$console->writeOut(
|
2015-05-22 09:27:56 +02:00
|
|
|
"%s\n",
|
|
|
|
pht(
|
|
|
|
"%s: Would update Mime type: '%s' -> '%s'.",
|
|
|
|
$fid,
|
|
|
|
$file->getMimeType(),
|
|
|
|
$new_type));
|
2013-05-17 19:00:31 +02:00
|
|
|
} else {
|
|
|
|
$console->writeOut(
|
2015-05-22 09:27:56 +02:00
|
|
|
"%s\n",
|
|
|
|
pht(
|
|
|
|
"%s: Updating Mime type: '%s' -> '%s'.",
|
|
|
|
$fid,
|
|
|
|
$file->getMimeType(),
|
|
|
|
$new_type));
|
2013-05-17 19:00:31 +02:00
|
|
|
$file->setMimeType($new_type);
|
|
|
|
$file->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($update['dimensions']) {
|
|
|
|
if (!$file->isViewableImage()) {
|
|
|
|
$console->writeOut(
|
2015-05-22 09:27:56 +02:00
|
|
|
"%s\n",
|
|
|
|
pht('%s: Not an image file.', $fid));
|
2013-05-17 19:00:31 +02:00
|
|
|
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(
|
2015-05-22 09:27:56 +02:00
|
|
|
"%s\n",
|
|
|
|
pht('%s: Image dimensions already exist.', $fid));
|
2013-05-17 19:00:31 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($is_dry_run) {
|
|
|
|
$console->writeOut(
|
2015-05-22 09:27:56 +02:00
|
|
|
"%s\n",
|
|
|
|
pht('%s: Would update file dimensions (dry run)', $fid));
|
2013-05-17 19:00:31 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$console->writeOut(
|
2015-05-22 09:27:56 +02:00
|
|
|
pht('%s: Updating metadata... ', $fid));
|
2013-05-17 19:00:31 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
$file->updateDimensions();
|
2015-05-22 09:27:56 +02:00
|
|
|
$console->writeOut("%s\n", pht('Done.'));
|
2013-05-17 19:00:31 +02:00
|
|
|
} catch (Exception $ex) {
|
2015-05-22 09:27:56 +02:00
|
|
|
$console->writeOut("%s\n", pht('Failed!'));
|
2013-05-17 19:00:31 +02:00
|
|
|
$console->writeErr("%s\n", (string)$ex);
|
|
|
|
$failed[] = $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($failed) {
|
2015-05-22 09:27:56 +02:00
|
|
|
$console->writeOut("**%s**\n", pht('Failures!'));
|
2013-05-17 19:00:31 +02:00
|
|
|
$ids = array();
|
|
|
|
foreach ($failed as $file) {
|
|
|
|
$ids[] = 'F'.$file->getID();
|
|
|
|
}
|
|
|
|
$console->writeOut("%s\n", implode(', ', $ids));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
} else {
|
2015-05-22 09:27:56 +02:00
|
|
|
$console->writeOut("**%s**\n", pht('Success!'));
|
2013-05-17 19:00:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|