Skip to content

Commit 2a3c3b2

Browse files
author
epriestley
committedMar 8, 2016
Provide bin/nuance import and ngram indexes for sources
Summary: Ref T10537. More infrastructure: - Put a `bin/nuance` in place with `bin/nuance import`. This has no useful behavior yet. - Allow sources to be searched by substring. This supports `bin/nuance import --source whatever` so you don't have to dig up PHIDs. Test Plan: - Applied migrations. - Ran `bin/nuance import --source ...` (no meaningful effect, but works fine). - Searched for sources by substring in the UI. Reviewers: chad Reviewed By: chad Maniphest Tasks: T10537 Differential Revision: https://secure.phabricator.com/D15436
1 parent 3f4cc3a commit 2a3c3b2

21 files changed

+241
-17
lines changed
 

‎bin/nuance

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../scripts/setup/manage_nuance.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE {$NAMESPACE}_nuance.nuance_sourcename_ngrams (
2+
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
3+
objectID INT UNSIGNED NOT NULL,
4+
ngram CHAR(3) NOT NULL COLLATE {$COLLATE_TEXT},
5+
KEY `key_object` (objectID),
6+
KEY `key_ngram` (ngram, objectID)
7+
) ENGINE=InnoDB, COLLATE {$COLLATE_TEXT};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
$table = new NuanceSource();
4+
5+
foreach (new LiskMigrationIterator($table) as $source) {
6+
PhabricatorSearchWorker::queueDocumentForIndexing(
7+
$source->getPHID(),
8+
array(
9+
'force' => true,
10+
));
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE {$NAMESPACE}_nuance.nuance_source
2+
CHANGE name name VARCHAR(255) NOT NULL COLLATE {$COLLATE_SORT};

‎scripts/setup/manage_nuance.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
$root = dirname(dirname(dirname(__FILE__)));
5+
require_once $root.'/scripts/__init_script__.php';
6+
7+
$args = new PhutilArgumentParser($argv);
8+
$args->setTagline(pht('manage Nuance'));
9+
$args->setSynopsis(<<<EOSYNOPSIS
10+
**nuance** __command__ [__options__]
11+
Manage and debug Nuance.
12+
13+
EOSYNOPSIS
14+
);
15+
$args->parseStandardArguments();
16+
17+
$workflows = id(new PhutilClassMapQuery())
18+
->setAncestorClass('NuanceManagementWorkflow')
19+
->execute();
20+
$workflows[] = new PhutilHelpArgumentWorkflow();
21+
$args->parseWorkflows($workflows);

‎src/__phutil_library_map__.php

+7
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,8 @@
14341434
'NuanceItemTransactionComment' => 'applications/nuance/storage/NuanceItemTransactionComment.php',
14351435
'NuanceItemTransactionQuery' => 'applications/nuance/query/NuanceItemTransactionQuery.php',
14361436
'NuanceItemViewController' => 'applications/nuance/controller/NuanceItemViewController.php',
1437+
'NuanceManagementImportWorkflow' => 'applications/nuance/management/NuanceManagementImportWorkflow.php',
1438+
'NuanceManagementWorkflow' => 'applications/nuance/management/NuanceManagementWorkflow.php',
14371439
'NuancePhabricatorFormSourceDefinition' => 'applications/nuance/source/NuancePhabricatorFormSourceDefinition.php',
14381440
'NuanceQuery' => 'applications/nuance/query/NuanceQuery.php',
14391441
'NuanceQueue' => 'applications/nuance/storage/NuanceQueue.php',
@@ -1473,6 +1475,7 @@
14731475
'NuanceSourceEditor' => 'applications/nuance/editor/NuanceSourceEditor.php',
14741476
'NuanceSourceListController' => 'applications/nuance/controller/NuanceSourceListController.php',
14751477
'NuanceSourceManageCapability' => 'applications/nuance/capability/NuanceSourceManageCapability.php',
1478+
'NuanceSourceNameNgrams' => 'applications/nuance/storage/NuanceSourceNameNgrams.php',
14761479
'NuanceSourcePHIDType' => 'applications/nuance/phid/NuanceSourcePHIDType.php',
14771480
'NuanceSourceQuery' => 'applications/nuance/query/NuanceSourceQuery.php',
14781481
'NuanceSourceSearchEngine' => 'applications/nuance/query/NuanceSourceSearchEngine.php',
@@ -5682,6 +5685,8 @@
56825685
'NuanceItemTransactionComment' => 'PhabricatorApplicationTransactionComment',
56835686
'NuanceItemTransactionQuery' => 'PhabricatorApplicationTransactionQuery',
56845687
'NuanceItemViewController' => 'NuanceController',
5688+
'NuanceManagementImportWorkflow' => 'NuanceManagementWorkflow',
5689+
'NuanceManagementWorkflow' => 'PhabricatorManagementWorkflow',
56855690
'NuancePhabricatorFormSourceDefinition' => 'NuanceSourceDefinition',
56865691
'NuanceQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
56875692
'NuanceQueue' => array(
@@ -5721,6 +5726,7 @@
57215726
'NuanceDAO',
57225727
'PhabricatorApplicationTransactionInterface',
57235728
'PhabricatorPolicyInterface',
5729+
'PhabricatorNgramsInterface',
57245730
),
57255731
'NuanceSourceActionController' => 'NuanceController',
57265732
'NuanceSourceController' => 'NuanceController',
@@ -5733,6 +5739,7 @@
57335739
'NuanceSourceEditor' => 'PhabricatorApplicationTransactionEditor',
57345740
'NuanceSourceListController' => 'NuanceSourceController',
57355741
'NuanceSourceManageCapability' => 'PhabricatorPolicyCapability',
5742+
'NuanceSourceNameNgrams' => 'PhabricatorSearchNgrams',
57365743
'NuanceSourcePHIDType' => 'PhabricatorPHIDType',
57375744
'NuanceSourceQuery' => 'NuanceQuery',
57385745
'NuanceSourceSearchEngine' => 'PhabricatorApplicationSearchEngine',

‎src/applications/almanac/storage/AlmanacDevice.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public function destroyObjectPermanently(
246246
}
247247

248248

249-
/* -( PhabricatorNgramInterface )------------------------------------------ */
249+
/* -( PhabricatorNgramsInterface )----------------------------------------- */
250250

251251

252252
public function newNgrams() {

‎src/applications/almanac/storage/AlmanacNamespace.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public function destroyObjectPermanently(
210210
}
211211

212212

213-
/* -( PhabricatorNgramInterface )------------------------------------------ */
213+
/* -( PhabricatorNgramsInterface )----------------------------------------- */
214214

215215

216216
public function newNgrams() {

‎src/applications/almanac/storage/AlmanacNetwork.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function destroyObjectPermanently(
116116
}
117117

118118

119-
/* -( PhabricatorNgramInterface )------------------------------------------ */
119+
/* -( PhabricatorNgramsInterface )----------------------------------------- */
120120

121121

122122
public function newNgrams() {

‎src/applications/almanac/storage/AlmanacService.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public function destroyObjectPermanently(
251251
}
252252

253253

254-
/* -( PhabricatorNgramInterface )------------------------------------------ */
254+
/* -( PhabricatorNgramsInterface )----------------------------------------- */
255255

256256

257257
public function newNgrams() {

‎src/applications/drydock/storage/DrydockBlueprint.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ public function attachCustomFields(PhabricatorCustomFieldAttachment $fields) {
350350
}
351351

352352

353-
/* -( PhabricatorNgramInterface )------------------------------------------ */
353+
/* -( PhabricatorNgramsInterface )----------------------------------------- */
354354

355355

356356
public function newNgrams() {

‎src/applications/harbormaster/storage/configuration/HarbormasterBuildPlan.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public function describeAutomaticCapability($capability) {
197197
}
198198

199199

200-
/* -( PhabricatorNgramInterface )------------------------------------------ */
200+
/* -( PhabricatorNgramsInterface )----------------------------------------- */
201201

202202

203203
public function newNgrams() {

‎src/applications/nuance/editor/NuanceSourceEditor.php

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public function getEditorObjectsDescription() {
1111
return pht('Nuance Sources');
1212
}
1313

14+
protected function supportsSearch() {
15+
return true;
16+
}
17+
1418
public function getTransactionTypes() {
1519
$types = parent::getTransactionTypes();
1620

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
final class NuanceManagementImportWorkflow
4+
extends NuanceManagementWorkflow {
5+
6+
protected function didConstruct() {
7+
$this
8+
->setName('import')
9+
->setExamples('**import** [__options__]')
10+
->setSynopsis(pht('Import data from a source.'))
11+
->setArguments(
12+
array(
13+
array(
14+
'name' => 'source',
15+
'param' => 'source',
16+
'help' => pht('Choose which source to import.'),
17+
),
18+
));
19+
}
20+
21+
public function execute(PhutilArgumentParser $args) {
22+
$source = $this->loadSource($args, 'source');
23+
24+
$definition = $source->getDefinition()
25+
->setViewer($this->getViewer())
26+
->setSource($source);
27+
28+
if (!$definition->hasImportCursors()) {
29+
throw new PhutilArgumentUsageException(
30+
pht(
31+
'This source ("%s") does not expose import cursors.',
32+
$source->getName()));
33+
}
34+
35+
$cursors = $definition->getImportCursors();
36+
if (!$cursors) {
37+
throw new PhutilArgumentUsageException(
38+
pht(
39+
'This source ("%s") does not have any import cursors.',
40+
$source->getName()));
41+
}
42+
43+
echo tsprintf(
44+
"%s\n",
45+
pht('OK, but actual importing is not implemented yet.'));
46+
47+
return 0;
48+
}
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
abstract class NuanceManagementWorkflow
4+
extends PhabricatorManagementWorkflow {
5+
6+
protected function loadSource(PhutilArgumentParser $argv, $key) {
7+
$source = $argv->getArg($key);
8+
if (!strlen($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+
}

‎src/applications/nuance/query/NuanceSourceQuery.php

+16-6
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,20 @@ public function withHasImportCursors($has_cursors) {
3434
return $this;
3535
}
3636

37+
public function withNameNgrams($ngrams) {
38+
return $this->withNgramsConstraint(
39+
new NuanceSourceNameNgrams(),
40+
$ngrams);
41+
}
42+
3743
public function newResultObject() {
3844
return new NuanceSource();
3945
}
4046

47+
protected function getPrimaryTableAlias() {
48+
return 'source';
49+
}
50+
4151
protected function loadPage() {
4252
return $this->loadStandardPage($this->newResultObject());
4353
}
@@ -65,28 +75,28 @@ protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
6575
if ($this->types !== null) {
6676
$where[] = qsprintf(
6777
$conn,
68-
'type IN (%Ls)',
78+
'source.type IN (%Ls)',
6979
$this->types);
7080
}
7181

7282
if ($this->ids !== null) {
7383
$where[] = qsprintf(
7484
$conn,
75-
'id IN (%Ld)',
85+
'source.id IN (%Ld)',
7686
$this->ids);
7787
}
7888

7989
if ($this->phids !== null) {
8090
$where[] = qsprintf(
8191
$conn,
82-
'phid IN (%Ls)',
92+
'source.phid IN (%Ls)',
8393
$this->phids);
8494
}
8595

8696
if ($this->isDisabled !== null) {
8797
$where[] = qsprintf(
8898
$conn,
89-
'isDisabled = %d',
99+
'source.isDisabled = %d',
90100
(int)$this->isDisabled);
91101
}
92102

@@ -106,7 +116,7 @@ protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
106116
} else {
107117
$where[] = qsprintf(
108118
$conn,
109-
'type IN (%Ls)',
119+
'source.type IN (%Ls)',
110120
$cursor_types);
111121
}
112122
} else {
@@ -115,7 +125,7 @@ protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
115125
} else {
116126
$where[] = qsprintf(
117127
$conn,
118-
'type NOT IN (%Ls)',
128+
'source.type NOT IN (%Ls)',
119129
$cursor_types);
120130
}
121131
}

‎src/applications/nuance/query/NuanceSourceSearchEngine.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,20 @@ public function newQuery() {
1818
protected function buildQueryFromParameters(array $map) {
1919
$query = $this->newQuery();
2020

21+
if ($map['match'] !== null) {
22+
$query->withNameNgrams($map['match']);
23+
}
24+
2125
return $query;
2226
}
2327

2428
protected function buildCustomSearchFields() {
25-
return array();
29+
return array(
30+
id(new PhabricatorSearchTextField())
31+
->setLabel(pht('Name Contains'))
32+
->setKey('match')
33+
->setDescription(pht('Search for sources by name substring.')),
34+
);
2635
}
2736

2837
protected function getURI($path) {

0 commit comments

Comments
 (0)
Failed to load comments.