@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
3abstract class PhabricatorFilesManagementWorkflow
4 extends PhabricatorManagementWorkflow {
5
6 protected function newIteratorArguments() {
7 return array(
8 array(
9 'name' => 'all',
10 'help' => pht('Operate on all files.'),
11 ),
12 array(
13 'name' => 'names',
14 'wildcard' => true,
15 ),
16 array(
17 'name' => 'from-engine',
18 'param' => 'storage-engine',
19 'help' => pht('Operate on files stored in a specified engine.'),
20 ),
21 );
22 }
23
24 protected function buildIterator(PhutilArgumentParser $args) {
25 $viewer = $this->getViewer();
26
27 $is_all = $args->getArg('all');
28
29 $names = $args->getArg('names');
30 $from_engine = $args->getArg('from-engine');
31
32 $any_constraint = ($from_engine || $names);
33
34 if (!$is_all && !$any_constraint) {
35 throw new PhutilArgumentUsageException(
36 pht(
37 'Specify which files to operate on, or use "--all" to operate on '.
38 'all files.'));
39 }
40
41 if ($is_all && $any_constraint) {
42 throw new PhutilArgumentUsageException(
43 pht(
44 'You can not operate on all files with "--all" and also operate '.
45 'on a subset of files by naming them explicitly or using '.
46 'constraint flags like "--from-engine".'));
47 }
48
49 // If we're migrating specific named files, convert the names into IDs
50 // first.
51 $ids = null;
52 if ($names) {
53 $files = $this->loadFilesWithNames($names);
54 $ids = mpull($files, 'getID');
55 }
56
57 $query = id(new PhabricatorFileQuery())
58 ->setViewer($viewer);
59
60 if ($ids) {
61 $query->withIDs($ids);
62 }
63
64 if ($from_engine) {
65 $query->withStorageEngines(array($from_engine));
66 }
67
68 return new PhabricatorQueryIterator($query);
69 }
70
71 protected function loadFilesWithNames(array $names) {
72 $query = id(new PhabricatorObjectQuery())
73 ->setViewer($this->getViewer())
74 ->withNames($names)
75 ->withTypes(array(PhabricatorFileFilePHIDType::TYPECONST));
76
77 $query->execute();
78 $files = $query->getNamedResults();
79
80 foreach ($names as $name) {
81 if (empty($files[$name])) {
82 throw new PhutilArgumentUsageException(
83 pht(
84 'No file "%s" exists.',
85 $name));
86 }
87 }
88
89 return array_values($files);
90 }
91
92}