@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 NuanceManagementWorkflow
4 extends PhabricatorManagementWorkflow {
5
6 protected function loadSource(PhutilArgumentParser $argv, $key) {
7 $source = $argv->getArg($key);
8 if (!phutil_nonempty_string($source)) {
9 throw new PhutilArgumentUsageException(
10 pht(
11 'Specify a source with %s.',
12 '--'.$key));
13 }
14
15 $query = id(new NuanceSourceQuery())
16 ->setViewer($this->getViewer())
17 ->setRaisePolicyExceptions(true);
18
19 $type_unknown = PhabricatorPHIDConstants::PHID_TYPE_UNKNOWN;
20
21 if (ctype_digit($source)) {
22 $kind = 'id';
23 $query->withIDs(array($source));
24 } else if (phid_get_type($source) !== $type_unknown) {
25 $kind = 'phid';
26 $query->withPHIDs($source);
27 } else {
28 $kind = 'name';
29 $query->withNameNgrams($source);
30 }
31
32 $sources = $query->execute();
33
34 if (!$sources) {
35 switch ($kind) {
36 case 'id':
37 $message = pht(
38 'No source exists with ID "%s".',
39 $source);
40 break;
41 case 'phid':
42 $message = pht(
43 'No source exists with PHID "%s".',
44 $source);
45 break;
46 default:
47 $message = pht(
48 'No source exists with a name matching "%s".',
49 $source);
50 break;
51 }
52
53 throw new PhutilArgumentUsageException($message);
54 } else if (count($sources) > 1) {
55 $message = pht(
56 'More than one source matches "%s". Choose a narrower query, or '.
57 'use an ID or PHID to select a source. Matching sources: %s.',
58 $source,
59 implode(', ', mpull($sources, 'getName')));
60
61 throw new PhutilArgumentUsageException($message);
62 }
63
64 return head($sources);
65 }
66
67 protected function loadITem(PhutilArgumentParser $argv, $key) {
68 $item = $argv->getArg($key);
69 if (!phutil_nonempty_string($item)) {
70 throw new PhutilArgumentUsageException(
71 pht(
72 'Specify a item with %s.',
73 '--'.$key));
74 }
75
76 $query = id(new NuanceItemQuery())
77 ->setViewer($this->getViewer())
78 ->setRaisePolicyExceptions(true);
79
80 $type_unknown = PhabricatorPHIDConstants::PHID_TYPE_UNKNOWN;
81
82 if (ctype_digit($item)) {
83 $kind = 'id';
84 $query->withIDs(array($item));
85 } else if (phid_get_type($item) !== $type_unknown) {
86 $kind = 'phid';
87 $query->withPHIDs($item);
88 } else {
89 throw new PhutilArgumentUsageException(
90 pht(
91 'Specify the ID or PHID of an item to update. Parameter "%s" '.
92 'is not an ID or PHID.',
93 $item));
94 }
95
96 $items = $query->execute();
97
98 if (!$items) {
99 switch ($kind) {
100 case 'id':
101 $message = pht(
102 'No item exists with ID "%s".',
103 $item);
104 break;
105 case 'phid':
106 $message = pht(
107 'No item exists with PHID "%s".',
108 $item);
109 break;
110 }
111
112 throw new PhutilArgumentUsageException($message);
113 }
114
115 return head($items);
116 }
117}