From c58bd95842b207d7174f9f508a91500c4a759d45 Mon Sep 17 00:00:00 2001 From: Bob Trahan Date: Mon, 7 May 2012 13:35:09 -0700 Subject: [PATCH] Conduit user.query method Summary: also includes a PhabricatorPeopleQuery object Test Plan: queried for various combinations username, realname, id, phid, and email. verified correct results. Reviewers: epriestley Reviewed By: epriestley CC: zeeg, aran, Koolvin Maniphest Tasks: T1075 Differential Revision: https://secure.phabricator.com/D2416 --- src/__phutil_library_map__.php | 4 + .../query/ConduitAPI_user_query_Method.php | 94 +++++++++++++++ .../conduit/method/user/query/__init__.php | 13 ++ .../people/query/PhabricatorPeopleQuery.php | 114 ++++++++++++++++++ src/applications/people/query/__init__.php | 16 +++ 5 files changed, 241 insertions(+) create mode 100644 src/applications/conduit/method/user/query/ConduitAPI_user_query_Method.php create mode 100644 src/applications/conduit/method/user/query/__init__.php create mode 100644 src/applications/people/query/PhabricatorPeopleQuery.php create mode 100644 src/applications/people/query/__init__.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 33246c7a81..d02070ce58 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -185,6 +185,7 @@ phutil_register_library_map(array( 'ConduitAPI_user_addstatus_Method' => 'applications/conduit/method/user/addstatus', 'ConduitAPI_user_find_Method' => 'applications/conduit/method/user/find', 'ConduitAPI_user_info_Method' => 'applications/conduit/method/user/info', + 'ConduitAPI_user_query_Method' => 'applications/conduit/method/user/query', 'ConduitAPI_user_removestatus_Method' => 'applications/conduit/method/user/removestatus', 'ConduitAPI_user_whoami_Method' => 'applications/conduit/method/user/whoami', 'ConduitException' => 'applications/conduit/protocol/exception', @@ -784,6 +785,7 @@ phutil_register_library_map(array( 'PhabricatorPeopleListController' => 'applications/people/controller/list', 'PhabricatorPeopleLogsController' => 'applications/people/controller/logs', 'PhabricatorPeopleProfileController' => 'applications/people/controller/profile', + 'PhabricatorPeopleQuery' => 'applications/people/query', 'PhabricatorPolicies' => 'applications/policy/constants/policy', 'PhabricatorPolicyCapability' => 'applications/policy/constants/capability', 'PhabricatorPolicyConstants' => 'applications/policy/constants/base', @@ -1213,6 +1215,7 @@ phutil_register_library_map(array( 'ConduitAPI_user_addstatus_Method' => 'ConduitAPI_user_Method', 'ConduitAPI_user_find_Method' => 'ConduitAPI_user_Method', 'ConduitAPI_user_info_Method' => 'ConduitAPI_user_Method', + 'ConduitAPI_user_query_Method' => 'ConduitAPI_user_Method', 'ConduitAPI_user_removestatus_Method' => 'ConduitAPI_user_Method', 'ConduitAPI_user_whoami_Method' => 'ConduitAPI_user_Method', 'DarkConsoleConfigPlugin' => 'DarkConsolePlugin', @@ -1689,6 +1692,7 @@ phutil_register_library_map(array( 'PhabricatorPeopleListController' => 'PhabricatorPeopleController', 'PhabricatorPeopleLogsController' => 'PhabricatorPeopleController', 'PhabricatorPeopleProfileController' => 'PhabricatorPeopleController', + 'PhabricatorPeopleQuery' => 'PhabricatorOffsetPagedQuery', 'PhabricatorPolicies' => 'PhabricatorPolicyConstants', 'PhabricatorPolicyCapability' => 'PhabricatorPolicyConstants', 'PhabricatorPolicyQuery' => 'PhabricatorQuery', diff --git a/src/applications/conduit/method/user/query/ConduitAPI_user_query_Method.php b/src/applications/conduit/method/user/query/ConduitAPI_user_query_Method.php new file mode 100644 index 0000000000..608272746d --- /dev/null +++ b/src/applications/conduit/method/user/query/ConduitAPI_user_query_Method.php @@ -0,0 +1,94 @@ + 'optional list', + 'emails' => 'optional list', + 'realnames' => 'optional list', + 'phids' => 'optional list', + 'ids' => 'optional list', + 'offset' => 'optional int', + 'limit' => 'optional int (default = 100)', + ); + + } + + public function defineReturnType() { + return 'list'; + } + + public function defineErrorTypes() { + return array( + 'ERR-INVALID-PARAMETER' => 'Missing or malformed parameter.', + ); + } + + protected function execute(ConduitAPIRequest $request) { + $usernames = $request->getValue('usernames', array()); + $emails = $request->getValue('emails', array()); + $realnames = $request->getValue('realnames', array()); + $phids = $request->getValue('phids', array()); + $ids = $request->getValue('ids', array()); + $offset = $request->getValue('offset', 0); + $limit = $request->getValue('limit', 100); + + $query = new PhabricatorPeopleQuery(); + if ($usernames) { + $query->withUsernames($usernames); + } + if ($emails) { + // TODO -- validate emails and maybs + // throw new ConduitException('ERR-INVALID-PARAMETER'); + $query->withEmails($emails); + } + if ($realnames) { + $query->withRealnames($realnames); + } + if ($phids) { + $query->withPHIDs($phids); + } + if ($ids) { + $query->withIDs($ids); + } + if ($limit) { + $query->setLimit($limit); + } + if ($offset) { + $query->setOffset($offset); + } + $users = $query->execute(); + + $results = array(); + foreach ($users as $user) { + $results[] = $this->buildUserInformationDictionary($user); + } + return $results; + } +} diff --git a/src/applications/conduit/method/user/query/__init__.php b/src/applications/conduit/method/user/query/__init__.php new file mode 100644 index 0000000000..22b6e03e38 --- /dev/null +++ b/src/applications/conduit/method/user/query/__init__.php @@ -0,0 +1,13 @@ +ids = $ids; + return $this; + } + public function withPhids(array $phids) { + $this->phids = $phids; + return $this; + } + public function withEmails(array $emails) { + $this->emails = $emails; + return $this; + } + public function withRealnames(array $realnames) { + $this->realnames = $realnames; + return $this; + } + public function withUsernames(array $usernames) { + $this->usernames = $usernames; + return $this; + } + + public function execute() { + $table = new PhabricatorUser(); + $conn_r = $table->establishConnection('r'); + + $joins_clause = $this->buildJoinsClause($conn_r); + $where_clause = $this->buildWhereClause($conn_r); + $limit_clause = $this->buildLimitClause($conn_r); + + $data = queryfx_all( + $conn_r, + 'SELECT * FROM %T user %Q %Q %Q', + $table->getTableName(), + $joins_clause, + $where_clause, + $limit_clause); + + $users = $table->loadAllFromArray($data); + + return $users; + } + + private function buildJoinsClause($conn_r) { + $joins = array(); + + if ($this->emails) { + $email_table = new PhabricatorUserEmail(); + $joins[] = qsprintf( + $conn_r, + 'JOIN %T email ON email.userPHID = user.PHID', + $email_table->getTableName()); + } + + $joins = implode(' ', $joins); + return $joins; + } + + private function buildWhereClause($conn_r) { + $where = array(); + + if ($this->usernames) { + $where[] = qsprintf($conn_r, + 'userName IN (%Ls)', + $this->usernames); + } + if ($this->emails) { + $where[] = qsprintf($conn_r, + 'email.address IN (%Ls)', + $this->emails); + } + if ($this->realnames) { + $where[] = qsprintf($conn_r, + 'realName IN (%Ls)', + $this->realnames); + } + if ($this->phids) { + $where[] = qsprintf($conn_r, + 'phid IN (%Ls)', + $this->phids); + } + if ($this->ids) { + $where[] = qsprintf($conn_r, + 'id IN (%Ld)', + $this->ids); + } + + return $this->formatWhereClause($where); + } +} diff --git a/src/applications/people/query/__init__.php b/src/applications/people/query/__init__.php new file mode 100644 index 0000000000..6431f5f0b2 --- /dev/null +++ b/src/applications/people/query/__init__.php @@ -0,0 +1,16 @@ +