3 final class PhabricatorFilesManagementRebuildWorkflow
4 extends PhabricatorFilesManagementWorkflow
{
6 protected function didConstruct() {
7 $arguments = $this->newIteratorArguments();
11 'help' => pht('Show what would be updated.'),
15 'name' => 'rebuild-mime',
16 'help' => pht('Rebuild MIME information.'),
20 'name' => 'rebuild-dimensions',
21 'help' => pht('Rebuild image dimension information.'),
26 ->setSynopsis(pht('Rebuild metadata of old files.'))
27 ->setArguments($arguments);
30 public function execute(PhutilArgumentParser
$args) {
31 $console = PhutilConsole
::getConsole();
33 $iterator = $this->buildIterator($args);
36 'mime' => $args->getArg('rebuild-mime'),
37 'dimensions' => $args->getArg('rebuild-dimensions'),
40 // If the user didn't select anything, rebuild everything.
41 if (!array_filter($update)) {
42 foreach ($update as $key => $ignored) {
47 $is_dry_run = $args->getArg('dry-run');
51 foreach ($iterator as $file) {
52 $fid = 'F'.$file->getID();
54 if ($update['mime']) {
55 $tmp = new TempFile();
56 Filesystem
::writeFile($tmp, $file->loadFileData());
57 $new_type = Filesystem
::getMimeType($tmp);
59 if ($new_type == $file->getMimeType()) {
63 '%s: Mime type not changed (%s).',
71 "%s: Would update Mime type: '%s' -> '%s'.",
79 "%s: Updating Mime type: '%s' -> '%s'.",
83 $file->setMimeType($new_type);
89 if ($update['dimensions']) {
90 if (!$file->isViewableImage()) {
93 pht('%s: Not an image file.', $fid));
97 $metadata = $file->getMetadata();
98 $image_width = idx($metadata, PhabricatorFile
::METADATA_IMAGE_WIDTH
);
99 $image_height = idx($metadata, PhabricatorFile
::METADATA_IMAGE_HEIGHT
);
100 if ($image_width && $image_height) {
103 pht('%s: Image dimensions already exist.', $fid));
110 pht('%s: Would update file dimensions (dry run)', $fid));
115 pht('%s: Updating metadata... ', $fid));
118 $file->updateDimensions();
119 $console->writeOut("%s\n", pht('Done.'));
120 } catch (Exception
$ex) {
121 $console->writeOut("%s\n", pht('Failed!'));
122 $console->writeErr("%s\n", (string)$ex);
129 $console->writeOut("**%s**\n", pht('Failures!'));
131 foreach ($failed as $file) {
132 $ids[] = 'F'.$file->getID();
134 $console->writeOut("%s\n", implode(', ', $ids));
138 $console->writeOut("**%s**\n", pht('Success!'));