diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index f482fe3c26..d3cefe96c2 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -1147,6 +1147,7 @@ phutil_register_library_map(array( 'PhabricatorMySQLConfigOptions' => 'applications/config/option/PhabricatorMySQLConfigOptions.php', 'PhabricatorMySQLFileStorageEngine' => 'applications/files/engine/PhabricatorMySQLFileStorageEngine.php', 'PhabricatorNamedQuery' => 'applications/search/storage/PhabricatorNamedQuery.php', + 'PhabricatorNamedQueryQuery' => 'applications/search/query/PhabricatorNamedQueryQuery.php', 'PhabricatorNoteExample' => 'applications/uiexample/examples/PhabricatorNoteExample.php', 'PhabricatorNotificationBuilder' => 'applications/notification/builder/PhabricatorNotificationBuilder.php', 'PhabricatorNotificationClearController' => 'applications/notification/controller/PhabricatorNotificationClearController.php', @@ -2923,7 +2924,12 @@ phutil_register_library_map(array( 'PhabricatorMustVerifyEmailController' => 'PhabricatorAuthController', 'PhabricatorMySQLConfigOptions' => 'PhabricatorApplicationConfigOptions', 'PhabricatorMySQLFileStorageEngine' => 'PhabricatorFileStorageEngine', - 'PhabricatorNamedQuery' => 'PhabricatorSearchDAO', + 'PhabricatorNamedQuery' => + array( + 0 => 'PhabricatorSearchDAO', + 1 => 'PhabricatorPolicyInterface', + ), + 'PhabricatorNamedQueryQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', 'PhabricatorNoteExample' => 'PhabricatorUIExample', 'PhabricatorNotificationClearController' => 'PhabricatorNotificationController', 'PhabricatorNotificationConfigOptions' => 'PhabricatorApplicationConfigOptions', diff --git a/src/applications/paste/controller/PhabricatorPasteQueriesController.php b/src/applications/paste/controller/PhabricatorPasteQueriesController.php index 300caa0ca3..feb47c9ddd 100644 --- a/src/applications/paste/controller/PhabricatorPasteQueriesController.php +++ b/src/applications/paste/controller/PhabricatorPasteQueriesController.php @@ -10,32 +10,28 @@ final class PhabricatorPasteQueriesController $nav = $this->buildSideNavView(""); $filter = $nav->getSelectedFilter(); - $table = new PhabricatorNamedQuery(); - $conn = $table->establishConnection('r'); - $data = queryfx_all( - $conn, - 'SELECT * FROM %T WHERE userPHID=%s AND engineClassName=%s', - $table->getTableName(), - $user->getPHID(), - 'PhabricatorPasteSearchEngine'); + $named_queries = id(new PhabricatorNamedQueryQuery()) + ->setViewer($user) + ->withUserPHIDs(array($user->getPHID())) + ->withEngineClassNames(array('PhabricatorPasteSearchEngine')) + ->execute(); $list = new PhabricatorObjectItemListView(); $list->setUser($user); - foreach ($data as $key => $saved_query) { - $date_created = phabricator_datetime($saved_query["dateCreated"], $user); + foreach ($named_queries as $named_query) { + $date_created = phabricator_datetime( + $named_query->getDateCreated(), + $user); + $item = id(new PhabricatorObjectItemView()) - ->setHeader($saved_query["queryName"]) - ->setHref('/paste/query/'.$saved_query["queryKey"].'/') - ->addByline(pht('Date Created: ').$date_created); + ->setHeader($named_query->getQueryName()) + ->setHref('/paste/query/'.$named_query->getQueryKey().'/') + ->addIcon('none', $date_created); + $list->addItem($item); } - $pager = new AphrontCursorPagerView(); - $pager->readFromRequest($request); - - $list->setPager($pager); - $list->setNoDataString(pht("No results found for this query.")); $nav->appendChild( diff --git a/src/applications/search/query/PhabricatorNamedQueryQuery.php b/src/applications/search/query/PhabricatorNamedQueryQuery.php new file mode 100644 index 0000000000..84f9a5eaa8 --- /dev/null +++ b/src/applications/search/query/PhabricatorNamedQueryQuery.php @@ -0,0 +1,84 @@ +ids = $ids; + return $this; + } + + public function withUserPHIDs(array $user_phids) { + $this->userPHIDs = $user_phids; + return $this; + } + + public function withEngineClassNames(array $engine_class_names) { + $this->engineClassNames = $engine_class_names; + return $this; + } + + public function withQueryKeys(array $query_keys) { + $this->queryKeys = $query_keys; + return $this; + } + + protected function loadPage() { + $table = new PhabricatorNamedQuery(); + $conn_r = $table->establishConnection('r'); + + $data = queryfx_all( + $conn_r, + 'SELECT * FROM %T %Q %Q %Q', + $table->getTableName(), + $this->buildWhereClause($conn_r), + $this->buildOrderClause($conn_r), + $this->buildLimitClause($conn_r)); + + return $table->loadAllFromArray($data); + } + + private function buildWhereClause($conn_r) { + $where = array(); + + if ($this->ids) { + $where[] = qsprintf( + $conn_r, + 'id IN (%Ld)', + $this->ids); + } + + if ($this->engineClassNames) { + $where[] = qsprintf( + $conn_r, + 'engineClassName IN (%Ls)', + $this->engineClassNames); + } + + if ($this->userPHIDs) { + $where[] = qsprintf( + $conn_r, + 'userPHID IN (%Ls)', + $this->userPHIDs); + } + + if ($this->queryKeys) { + $where[] = qsprintf( + $conn_r, + 'queryKey IN (%Ls)', + $this->queryKeys); + } + + $where[] = $this->buildPagingClause($conn_r); + + return $this->formatWhereClause($where); + } +} diff --git a/src/applications/search/storage/PhabricatorNamedQuery.php b/src/applications/search/storage/PhabricatorNamedQuery.php index 5f97187e50..4b5fea57be 100644 --- a/src/applications/search/storage/PhabricatorNamedQuery.php +++ b/src/applications/search/storage/PhabricatorNamedQuery.php @@ -3,11 +3,33 @@ /** * @group search */ -final class PhabricatorNamedQuery extends PhabricatorSearchDAO { +final class PhabricatorNamedQuery extends PhabricatorSearchDAO + implements PhabricatorPolicyInterface { protected $queryKey = ""; protected $queryName = ""; protected $userPHID = ""; protected $engineClassName = ""; + +/* -( PhabricatorPolicyInterface )----------------------------------------- */ + + + public function getCapabilities() { + return array( + PhabricatorPolicyCapability::CAN_VIEW, + ); + } + + public function getPolicy($capability) { + return PhabricatorPolicies::POLICY_NOONE; + } + + public function hasAutomaticCapability($capability, PhabricatorUser $viewer) { + if ($viewer->getPHID() == $this->userPHID) { + return true; + } + return false; + } + }