Remove product literal strings in "pht()", part 5
[phabricator.git] / src / applications / files / management / PhabricatorFilesManagementRebuildWorkflow.php
blobe577a04d559c480c02f338a72a376db91281b788
1 <?php
3 final class PhabricatorFilesManagementRebuildWorkflow
4 extends PhabricatorFilesManagementWorkflow {
6 protected function didConstruct() {
7 $arguments = $this->newIteratorArguments();
9 $arguments[] = array(
10 'name' => 'dry-run',
11 'help' => pht('Show what would be updated.'),
14 $arguments[] = array(
15 'name' => 'rebuild-mime',
16 'help' => pht('Rebuild MIME information.'),
19 $arguments[] = array(
20 'name' => 'rebuild-dimensions',
21 'help' => pht('Rebuild image dimension information.'),
24 $this
25 ->setName('rebuild')
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);
35 $update = array(
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) {
43 $update[$key] = true;
47 $is_dry_run = $args->getArg('dry-run');
49 $failed = array();
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()) {
60 $console->writeOut(
61 "%s\n",
62 pht(
63 '%s: Mime type not changed (%s).',
64 $fid,
65 $new_type));
66 } else {
67 if ($is_dry_run) {
68 $console->writeOut(
69 "%s\n",
70 pht(
71 "%s: Would update Mime type: '%s' -> '%s'.",
72 $fid,
73 $file->getMimeType(),
74 $new_type));
75 } else {
76 $console->writeOut(
77 "%s\n",
78 pht(
79 "%s: Updating Mime type: '%s' -> '%s'.",
80 $fid,
81 $file->getMimeType(),
82 $new_type));
83 $file->setMimeType($new_type);
84 $file->save();
89 if ($update['dimensions']) {
90 if (!$file->isViewableImage()) {
91 $console->writeOut(
92 "%s\n",
93 pht('%s: Not an image file.', $fid));
94 continue;
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) {
101 $console->writeOut(
102 "%s\n",
103 pht('%s: Image dimensions already exist.', $fid));
104 continue;
107 if ($is_dry_run) {
108 $console->writeOut(
109 "%s\n",
110 pht('%s: Would update file dimensions (dry run)', $fid));
111 continue;
114 $console->writeOut(
115 pht('%s: Updating metadata... ', $fid));
117 try {
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);
123 $failed[] = $file;
128 if ($failed) {
129 $console->writeOut("**%s**\n", pht('Failures!'));
130 $ids = array();
131 foreach ($failed as $file) {
132 $ids[] = 'F'.$file->getID();
134 $console->writeOut("%s\n", implode(', ', $ids));
136 return 1;
137 } else {
138 $console->writeOut("**%s**\n", pht('Success!'));
139 return 0;
142 return 0;