From fc821188483e8852d430f2a558357457bbe6d138 Mon Sep 17 00:00:00 2001 From: Josh Cox Date: Mon, 26 Sep 2016 21:59:03 -0400 Subject: [PATCH] Expose conduit API methods for Phurl URLs Summary: Fixes T10681. Adds a search API endpoint and an edit API endpoint for Phurl URLs. I still need to add the ability to search by name, alias, URL, and maybe description. Test Plan: Test the methods through `/conduit/method/phurls.search/` and `/conduit/method/phurls.edit/` Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: Korvin, epriestley, yelirekim Maniphest Tasks: T10681 Differential Revision: https://secure.phabricator.com/D16600 --- .../sql/autopatches/20160927.phurl.ngrams.php | 11 ++++ .../sql/autopatches/20160927.phurl.ngrams.sql | 7 +++ src/__phutil_library_map__.php | 8 +++ ...habricatorPhurlURLEditConduitAPIMethod.php | 18 ++++++ ...bricatorPhurlURLSearchConduitAPIMethod.php | 18 ++++++ .../phurl/query/PhabricatorPhurlURLQuery.php | 6 ++ .../query/PhabricatorPhurlURLSearchEngine.php | 25 ++++++++ .../phurl/storage/PhabricatorPhurlURL.php | 63 +++++++++++++++++-- .../storage/PhabricatorPhurlURLNameNgrams.php | 18 ++++++ 9 files changed, 169 insertions(+), 5 deletions(-) create mode 100644 resources/sql/autopatches/20160927.phurl.ngrams.php create mode 100644 resources/sql/autopatches/20160927.phurl.ngrams.sql create mode 100644 src/applications/phurl/conduit/PhabricatorPhurlURLEditConduitAPIMethod.php create mode 100644 src/applications/phurl/conduit/PhabricatorPhurlURLSearchConduitAPIMethod.php create mode 100644 src/applications/phurl/storage/PhabricatorPhurlURLNameNgrams.php diff --git a/resources/sql/autopatches/20160927.phurl.ngrams.php b/resources/sql/autopatches/20160927.phurl.ngrams.php new file mode 100644 index 0000000000..74cf61efa5 --- /dev/null +++ b/resources/sql/autopatches/20160927.phurl.ngrams.php @@ -0,0 +1,11 @@ +getPHID(), + array( + 'force' => true, + )); +} diff --git a/resources/sql/autopatches/20160927.phurl.ngrams.sql b/resources/sql/autopatches/20160927.phurl.ngrams.sql new file mode 100644 index 0000000000..b5b35a24f5 --- /dev/null +++ b/resources/sql/autopatches/20160927.phurl.ngrams.sql @@ -0,0 +1,7 @@ +CREATE TABLE {$NAMESPACE}_phurl.phurl_phurlname_ngrams ( + id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, + objectID INT UNSIGNED NOT NULL, + ngram CHAR(3) NOT NULL COLLATE {$COLLATE_TEXT}, + KEY `key_object` (objectID), + KEY `key_ngram` (ngram, objectID) +) ENGINE=InnoDB, COLLATE {$COLLATE_TEXT}; diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index eb8c71e31d..4e4192e4be 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -3187,14 +3187,17 @@ phutil_register_library_map(array( 'PhabricatorPhurlURLAccessController' => 'applications/phurl/controller/PhabricatorPhurlURLAccessController.php', 'PhabricatorPhurlURLCommentController' => 'applications/phurl/controller/PhabricatorPhurlURLCommentController.php', 'PhabricatorPhurlURLCreateCapability' => 'applications/phurl/capability/PhabricatorPhurlURLCreateCapability.php', + 'PhabricatorPhurlURLEditConduitAPIMethod' => 'applications/phurl/conduit/PhabricatorPhurlURLEditConduitAPIMethod.php', 'PhabricatorPhurlURLEditController' => 'applications/phurl/controller/PhabricatorPhurlURLEditController.php', 'PhabricatorPhurlURLEditEngine' => 'applications/phurl/editor/PhabricatorPhurlURLEditEngine.php', 'PhabricatorPhurlURLEditor' => 'applications/phurl/editor/PhabricatorPhurlURLEditor.php', 'PhabricatorPhurlURLListController' => 'applications/phurl/controller/PhabricatorPhurlURLListController.php', 'PhabricatorPhurlURLMailReceiver' => 'applications/phurl/mail/PhabricatorPhurlURLMailReceiver.php', + 'PhabricatorPhurlURLNameNgrams' => 'applications/phurl/storage/PhabricatorPhurlURLNameNgrams.php', 'PhabricatorPhurlURLPHIDType' => 'applications/phurl/phid/PhabricatorPhurlURLPHIDType.php', 'PhabricatorPhurlURLQuery' => 'applications/phurl/query/PhabricatorPhurlURLQuery.php', 'PhabricatorPhurlURLReplyHandler' => 'applications/phurl/mail/PhabricatorPhurlURLReplyHandler.php', + 'PhabricatorPhurlURLSearchConduitAPIMethod' => 'applications/phurl/conduit/PhabricatorPhurlURLSearchConduitAPIMethod.php', 'PhabricatorPhurlURLSearchEngine' => 'applications/phurl/query/PhabricatorPhurlURLSearchEngine.php', 'PhabricatorPhurlURLTransaction' => 'applications/phurl/storage/PhabricatorPhurlURLTransaction.php', 'PhabricatorPhurlURLTransactionComment' => 'applications/phurl/storage/PhabricatorPhurlURLTransactionComment.php', @@ -8104,18 +8107,23 @@ phutil_register_library_map(array( 'PhabricatorMentionableInterface', 'PhabricatorFlaggableInterface', 'PhabricatorSpacesInterface', + 'PhabricatorConduitResultInterface', + 'PhabricatorNgramsInterface', ), 'PhabricatorPhurlURLAccessController' => 'PhabricatorPhurlController', 'PhabricatorPhurlURLCommentController' => 'PhabricatorPhurlController', 'PhabricatorPhurlURLCreateCapability' => 'PhabricatorPolicyCapability', + 'PhabricatorPhurlURLEditConduitAPIMethod' => 'PhabricatorEditEngineAPIMethod', 'PhabricatorPhurlURLEditController' => 'PhabricatorPhurlController', 'PhabricatorPhurlURLEditEngine' => 'PhabricatorEditEngine', 'PhabricatorPhurlURLEditor' => 'PhabricatorApplicationTransactionEditor', 'PhabricatorPhurlURLListController' => 'PhabricatorPhurlController', 'PhabricatorPhurlURLMailReceiver' => 'PhabricatorObjectMailReceiver', + 'PhabricatorPhurlURLNameNgrams' => 'PhabricatorSearchNgrams', 'PhabricatorPhurlURLPHIDType' => 'PhabricatorPHIDType', 'PhabricatorPhurlURLQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', 'PhabricatorPhurlURLReplyHandler' => 'PhabricatorApplicationTransactionReplyHandler', + 'PhabricatorPhurlURLSearchConduitAPIMethod' => 'PhabricatorSearchEngineAPIMethod', 'PhabricatorPhurlURLSearchEngine' => 'PhabricatorApplicationSearchEngine', 'PhabricatorPhurlURLTransaction' => 'PhabricatorApplicationTransaction', 'PhabricatorPhurlURLTransactionComment' => 'PhabricatorApplicationTransactionComment', diff --git a/src/applications/phurl/conduit/PhabricatorPhurlURLEditConduitAPIMethod.php b/src/applications/phurl/conduit/PhabricatorPhurlURLEditConduitAPIMethod.php new file mode 100644 index 0000000000..2f78c87489 --- /dev/null +++ b/src/applications/phurl/conduit/PhabricatorPhurlURLEditConduitAPIMethod.php @@ -0,0 +1,18 @@ +withNgramsConstraint( + id(new PhabricatorPhurlURLNameNgrams()), + $ngrams); + } + public function withLongURLs(array $long_urls) { $this->longURLs = $long_urls; return $this; diff --git a/src/applications/phurl/query/PhabricatorPhurlURLSearchEngine.php b/src/applications/phurl/query/PhabricatorPhurlURLSearchEngine.php index 9c91adf17d..f2e2c99399 100644 --- a/src/applications/phurl/query/PhabricatorPhurlURLSearchEngine.php +++ b/src/applications/phurl/query/PhabricatorPhurlURLSearchEngine.php @@ -25,6 +25,19 @@ final class PhabricatorPhurlURLSearchEngine ->setLabel(pht('Created By')) ->setKey('authorPHIDs') ->setDatasource(new PhabricatorPeopleUserFunctionDatasource()), + id(new PhabricatorSearchTextField()) + ->setLabel(pht('Name Contains')) + ->setKey('name') + ->setDescription(pht('Search for Phurl URLs by name substring.')), + id(new PhabricatorSearchStringListField()) + ->setLabel(pht('Aliases')) + ->setKey('aliases') + ->setDescription(pht('Search for Phurl URLs by alias.')), + id(new PhabricatorSearchStringListField()) + ->setLabel(pht('Long URLs')) + ->setKey('longurls') + ->setDescription( + pht('Search for Phurl URLs by the non-shortened URL.')), ); } @@ -35,6 +48,18 @@ final class PhabricatorPhurlURLSearchEngine $query->withAuthorPHIDs($map['authorPHIDs']); } + if ($map['name'] !== null) { + $query->withNameNgrams($map['name']); + } + + if ($map['aliases']) { + $query->withAliases($map['aliases']); + } + + if ($map['longurls']) { + $query->withLongURLs($map['longurls']); + } + return $query; } diff --git a/src/applications/phurl/storage/PhabricatorPhurlURL.php b/src/applications/phurl/storage/PhabricatorPhurlURL.php index db82e20baf..c36bf6e8cb 100644 --- a/src/applications/phurl/storage/PhabricatorPhurlURL.php +++ b/src/applications/phurl/storage/PhabricatorPhurlURL.php @@ -9,7 +9,9 @@ final class PhabricatorPhurlURL extends PhabricatorPhurlDAO PhabricatorDestructibleInterface, PhabricatorMentionableInterface, PhabricatorFlaggableInterface, - PhabricatorSpacesInterface { + PhabricatorSpacesInterface, + PhabricatorConduitResultInterface, + PhabricatorNgramsInterface { protected $name; protected $alias; @@ -103,12 +105,12 @@ final class PhabricatorPhurlURL extends PhabricatorPhurlDAO } else { $path = '/u/'.$this->getID(); } - $short_domain = PhabricatorEnv::getEnvConfig('phurl.short-uri'); - if (!$short_domain) { - return $path; + $domain = PhabricatorEnv::getEnvConfig('phurl.short-uri'); + if (!$domain) { + $domain = PhabricatorEnv::getEnvConfig('phabricator.base-uri'); } - $uri = new PhutilURI($short_domain); + $uri = new PhutilURI($domain); $uri->setPath($path); return (string)$uri; } @@ -202,4 +204,55 @@ final class PhabricatorPhurlURL extends PhabricatorPhurlDAO public function getSpacePHID() { return $this->spacePHID; } + +/* -( PhabricatorConduitResultInterface )---------------------------------- */ + + + public function getFieldSpecificationsForConduit() { + return array( + id(new PhabricatorConduitSearchFieldSpecification()) + ->setKey('name') + ->setType('string') + ->setDescription(pht('URL name.')), + id(new PhabricatorConduitSearchFieldSpecification()) + ->setKey('alias') + ->setType('string') + ->setDescription(pht('The alias for the URL.')), + id(new PhabricatorConduitSearchFieldSpecification()) + ->setKey('longurl') + ->setType('string') + ->setDescription(pht('The pre-shortened URL.')), + id(new PhabricatorConduitSearchFieldSpecification()) + ->setKey('description') + ->setType('string') + ->setDescription(pht('A description of the URL.')), + ); + } + + public function getFieldValuesForConduit() { + return array( + 'name' => $this->getName(), + 'alias' => $this->getAlias(), + 'description' => $this->getDescription(), + 'urls' => array( + 'long' => $this->getLongURL(), + 'short' => $this->getRedirectURI(), + ), + ); + } + + public function getConduitSearchAttachments() { + return array(); + } + +/* -( PhabricatorNgramInterface )------------------------------------------ */ + + + public function newNgrams() { + return array( + id(new PhabricatorPhurlURLNameNgrams()) + ->setValue($this->getName()), + ); + } + } diff --git a/src/applications/phurl/storage/PhabricatorPhurlURLNameNgrams.php b/src/applications/phurl/storage/PhabricatorPhurlURLNameNgrams.php new file mode 100644 index 0000000000..0a760e32b9 --- /dev/null +++ b/src/applications/phurl/storage/PhabricatorPhurlURLNameNgrams.php @@ -0,0 +1,18 @@ +