Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / people / query / PhabricatorPeopleLogSearchEngine.php
blob7e7fca1cd4456c8643eca5e4d9cbaabc72a95b7e
1 <?php
3 final class PhabricatorPeopleLogSearchEngine
4 extends PhabricatorApplicationSearchEngine {
6 public function getResultTypeDescription() {
7 return pht('Account Activity');
10 public function getApplicationClassName() {
11 return 'PhabricatorPeopleApplication';
14 public function getPageSize(PhabricatorSavedQuery $saved) {
15 return 500;
18 public function newQuery() {
19 $query = new PhabricatorPeopleLogQuery();
21 // NOTE: If the viewer isn't an administrator, always restrict the query to
22 // related records. This echoes the policy logic of these logs. This is
23 // mostly a performance optimization, to prevent us from having to pull
24 // large numbers of logs that the user will not be able to see and filter
25 // them in-process.
26 $viewer = $this->requireViewer();
27 if (!$viewer->getIsAdmin()) {
28 $query->withRelatedPHIDs(array($viewer->getPHID()));
31 return $query;
34 protected function buildQueryFromParameters(array $map) {
35 $query = $this->newQuery();
37 if ($map['userPHIDs']) {
38 $query->withUserPHIDs($map['userPHIDs']);
41 if ($map['actorPHIDs']) {
42 $query->withActorPHIDs($map['actorPHIDs']);
45 if ($map['actions']) {
46 $query->withActions($map['actions']);
49 if (strlen($map['ip'])) {
50 $query->withRemoteAddressPrefix($map['ip']);
53 if ($map['sessions']) {
54 $query->withSessionKeys($map['sessions']);
57 if ($map['createdStart'] || $map['createdEnd']) {
58 $query->withDateCreatedBetween(
59 $map['createdStart'],
60 $map['createdEnd']);
63 return $query;
66 protected function buildCustomSearchFields() {
67 $types = PhabricatorUserLogType::getAllLogTypes();
68 $types = mpull($types, 'getLogTypeName', 'getLogTypeKey');
70 return array(
71 id(new PhabricatorUsersSearchField())
72 ->setKey('userPHIDs')
73 ->setAliases(array('users', 'user', 'userPHID'))
74 ->setLabel(pht('Users'))
75 ->setDescription(pht('Search for activity affecting specific users.')),
76 id(new PhabricatorUsersSearchField())
77 ->setKey('actorPHIDs')
78 ->setAliases(array('actors', 'actor', 'actorPHID'))
79 ->setLabel(pht('Actors'))
80 ->setDescription(pht('Search for activity by specific users.')),
81 id(new PhabricatorSearchDatasourceField())
82 ->setKey('actions')
83 ->setLabel(pht('Actions'))
84 ->setDescription(pht('Search for particular types of activity.'))
85 ->setDatasource(new PhabricatorUserLogTypeDatasource()),
86 id(new PhabricatorSearchTextField())
87 ->setKey('ip')
88 ->setLabel(pht('Filter IP'))
89 ->setDescription(pht('Search for actions by remote address.')),
90 id(new PhabricatorSearchStringListField())
91 ->setKey('sessions')
92 ->setLabel(pht('Sessions'))
93 ->setDescription(pht('Search for activity in particular sessions.')),
94 id(new PhabricatorSearchDateField())
95 ->setLabel(pht('Created After'))
96 ->setKey('createdStart'),
97 id(new PhabricatorSearchDateField())
98 ->setLabel(pht('Created Before'))
99 ->setKey('createdEnd'),
103 protected function getURI($path) {
104 return '/people/logs/'.$path;
107 protected function getBuiltinQueryNames() {
108 $names = array(
109 'all' => pht('All'),
112 return $names;
115 public function buildSavedQueryFromBuiltin($query_key) {
116 $query = $this->newSavedQuery();
117 $query->setQueryKey($query_key);
119 switch ($query_key) {
120 case 'all':
121 return $query;
124 return parent::buildSavedQueryFromBuiltin($query_key);
127 protected function renderResultList(
128 array $logs,
129 PhabricatorSavedQuery $query,
130 array $handles) {
131 assert_instances_of($logs, 'PhabricatorUserLog');
133 $viewer = $this->requireViewer();
135 $table = id(new PhabricatorUserLogView())
136 ->setUser($viewer)
137 ->setLogs($logs);
139 if ($viewer->getIsAdmin()) {
140 $table->setSearchBaseURI($this->getApplicationURI('logs/'));
143 return id(new PhabricatorApplicationSearchResultView())
144 ->setTable($table);
147 protected function newExportFields() {
148 $viewer = $this->requireViewer();
150 $fields = array(
151 $fields[] = id(new PhabricatorPHIDExportField())
152 ->setKey('actorPHID')
153 ->setLabel(pht('Actor PHID')),
154 $fields[] = id(new PhabricatorStringExportField())
155 ->setKey('actor')
156 ->setLabel(pht('Actor')),
157 $fields[] = id(new PhabricatorPHIDExportField())
158 ->setKey('userPHID')
159 ->setLabel(pht('User PHID')),
160 $fields[] = id(new PhabricatorStringExportField())
161 ->setKey('user')
162 ->setLabel(pht('User')),
163 $fields[] = id(new PhabricatorStringExportField())
164 ->setKey('action')
165 ->setLabel(pht('Action')),
166 $fields[] = id(new PhabricatorStringExportField())
167 ->setKey('actionName')
168 ->setLabel(pht('Action Name')),
169 $fields[] = id(new PhabricatorStringExportField())
170 ->setKey('session')
171 ->setLabel(pht('Session')),
172 $fields[] = id(new PhabricatorStringExportField())
173 ->setKey('old')
174 ->setLabel(pht('Old Value')),
175 $fields[] = id(new PhabricatorStringExportField())
176 ->setKey('new')
177 ->setLabel(pht('New Value')),
180 if ($viewer->getIsAdmin()) {
181 $fields[] = id(new PhabricatorStringExportField())
182 ->setKey('remoteAddress')
183 ->setLabel(pht('Remote Address'));
186 return $fields;
189 protected function newExportData(array $logs) {
190 $viewer = $this->requireViewer();
193 $phids = array();
194 foreach ($logs as $log) {
195 $phids[] = $log->getUserPHID();
196 $phids[] = $log->getActorPHID();
198 $handles = $viewer->loadHandles($phids);
200 $types = PhabricatorUserLogType::getAllLogTypes();
201 $types = mpull($types, 'getLogTypeName', 'getLogTypeKey');
203 $export = array();
204 foreach ($logs as $log) {
206 $user_phid = $log->getUserPHID();
207 if ($user_phid) {
208 $user_name = $handles[$user_phid]->getName();
209 } else {
210 $user_name = null;
213 $actor_phid = $log->getActorPHID();
214 if ($actor_phid) {
215 $actor_name = $handles[$actor_phid]->getName();
216 } else {
217 $actor_name = null;
220 $action = $log->getAction();
221 $action_name = idx($types, $action, pht('Unknown ("%s")', $action));
223 $map = array(
224 'actorPHID' => $actor_phid,
225 'actor' => $actor_name,
226 'userPHID' => $user_phid,
227 'user' => $user_name,
228 'action' => $action,
229 'actionName' => $action_name,
230 'session' => substr($log->getSession(), 0, 6),
231 'old' => $log->getOldValue(),
232 'new' => $log->getNewValue(),
235 if ($viewer->getIsAdmin()) {
236 $map['remoteAddress'] = $log->getRemoteAddr();
239 $export[] = $map;
242 return $export;