Skip to content

Commit ca7a792

Browse files
author
epriestley
committedSep 30, 2013
Convert bin/files to ObjectQuery
Summary: Ref T603. This has some custom logic which ObjectQuery can now perform more simply and more correctly. Test Plan: Ran `bin/files purge F1`, `bin/files purge D1`, `bin/files purge --all`. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T603 Differential Revision: https://secure.phabricator.com/D7180
1 parent dd206a5 commit ca7a792

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed
 

‎src/applications/files/management/PhabricatorFilesManagementWorkflow.php

+17-28
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,33 @@ public function isExecutable() {
88
}
99

1010
protected function buildIterator(PhutilArgumentParser $args) {
11+
$names = $args->getArg('names');
12+
1113
if ($args->getArg('all')) {
12-
if ($args->getArg('names')) {
14+
if ($names) {
1315
throw new PhutilArgumentUsageException(
1416
"Specify either a list of files or `--all`, but not both.");
1517
}
1618
return new LiskMigrationIterator(new PhabricatorFile());
1719
}
1820

19-
if ($args->getArg('names')) {
20-
$iterator = array();
21-
22-
// TODO: (T603) Convert this to ObjectNameQuery.
23-
24-
foreach ($args->getArg('names') as $name) {
25-
$name = trim($name);
26-
27-
$id = preg_replace('/^F/i', '', $name);
28-
if (ctype_digit($id)) {
29-
$file = id(new PhabricatorFile())->loadOneWhere(
30-
'id = %d',
31-
$id);
32-
if (!$file) {
33-
throw new PhutilArgumentUsageException(
34-
"No file exists with ID '{$name}'.");
35-
}
36-
} else {
37-
$file = id(new PhabricatorFile())->loadOneWhere(
38-
'phid = %s',
39-
$name);
40-
if (!$file) {
41-
throw new PhutilArgumentUsageException(
42-
"No file exists with PHID '{$name}'.");
43-
}
21+
if ($names) {
22+
$query = id(new PhabricatorObjectQuery())
23+
->setViewer(PhabricatorUser::getOmnipotentUser())
24+
->withNames($names)
25+
->withTypes(array(PhabricatorFilePHIDTypeFile::TYPECONST));
26+
27+
$query->execute();
28+
$files = $query->getNamedResults();
29+
30+
foreach ($names as $name) {
31+
if (empty($files[$name])) {
32+
throw new PhutilArgumentUsageException(
33+
"No file '{$name}' exists!");
4434
}
45-
$iterator[] = $file;
4635
}
4736

48-
return $iterator;
37+
return array_values($files);
4938
}
5039

5140
return null;

‎src/applications/phid/query/PhabricatorObjectQuery.php

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ final class PhabricatorObjectQuery
55

66
private $phids = array();
77
private $names = array();
8+
private $types;
89

910
private $namedResults;
1011

@@ -18,12 +19,20 @@ public function withNames(array $names) {
1819
return $this;
1920
}
2021

22+
public function withTypes(array $types) {
23+
$this->types = $types;
24+
return $this;
25+
}
26+
2127
public function loadPage() {
2228
if ($this->namedResults === null) {
2329
$this->namedResults = array();
2430
}
2531

2632
$types = PhabricatorPHIDType::getAllTypes();
33+
if ($this->types) {
34+
$types = array_select_keys($types, $this->types);
35+
}
2736

2837
$names = array_unique($this->names);
2938
$phids = $this->phids;

0 commit comments

Comments
 (0)
Failed to load comments.