@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PhabricatorFilesManagementRebuildWorkflow
4 extends PhabricatorFilesManagementWorkflow {
5
6 protected function didConstruct() {
7 $arguments = $this->newIteratorArguments();
8
9 $arguments[] = array(
10 'name' => 'dry-run',
11 'help' => pht('Show what would be updated.'),
12 );
13
14 $arguments[] = array(
15 'name' => 'rebuild-mime',
16 'help' => pht('Rebuild MIME information.'),
17 );
18
19 $arguments[] = array(
20 'name' => 'rebuild-dimensions',
21 'help' => pht('Rebuild image dimension information.'),
22 );
23
24 $this
25 ->setName('rebuild')
26 ->setSynopsis(pht('Rebuild metadata of old files.'))
27 ->setArguments($arguments);
28 }
29
30 public function execute(PhutilArgumentParser $args) {
31 $console = PhutilConsole::getConsole();
32
33 $iterator = $this->buildIterator($args);
34
35 $update = array(
36 'mime' => $args->getArg('rebuild-mime'),
37 'dimensions' => $args->getArg('rebuild-dimensions'),
38 );
39
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;
44 }
45 }
46
47 $is_dry_run = $args->getArg('dry-run');
48
49 $failed = array();
50
51 foreach ($iterator as $file) {
52 $fid = 'F'.$file->getID();
53
54 if ($update['mime']) {
55 $tmp = new TempFile();
56 Filesystem::writeFile($tmp, $file->loadFileData());
57 $new_type = Filesystem::getMimeType($tmp);
58
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();
85 }
86 }
87 }
88
89 if ($update['dimensions']) {
90 if (!$file->isViewableImage()) {
91 $console->writeOut(
92 "%s\n",
93 pht('%s: Not an image file.', $fid));
94 continue;
95 }
96
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;
105 }
106
107 if ($is_dry_run) {
108 $console->writeOut(
109 "%s\n",
110 pht('%s: Would update file dimensions (dry run)', $fid));
111 continue;
112 }
113
114 $console->writeOut(
115 pht('%s: Updating metadata... ', $fid));
116
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;
124 }
125 }
126 }
127
128 if ($failed) {
129 $console->writeOut("**%s**\n", pht('Failures!'));
130 $ids = array();
131 foreach ($failed as $file) {
132 $ids[] = 'F'.$file->getID();
133 }
134 $console->writeOut("%s\n", implode(', ', $ids));
135
136 return 1;
137 } else {
138 $console->writeOut("**%s**\n", pht('Success!'));
139 return 0;
140 }
141
142 }
143}