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) {
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
26 $viewer = $this->requireViewer();
27 if (!$viewer->getIsAdmin()) {
28 $query->withRelatedPHIDs(array($viewer->getPHID()));
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(
66 protected function buildCustomSearchFields() {
67 $types = PhabricatorUserLogType
::getAllLogTypes();
68 $types = mpull($types, 'getLogTypeName', 'getLogTypeKey');
71 id(new PhabricatorUsersSearchField())
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())
83 ->setLabel(pht('Actions'))
84 ->setDescription(pht('Search for particular types of activity.'))
85 ->setDatasource(new PhabricatorUserLogTypeDatasource()),
86 id(new PhabricatorSearchTextField())
88 ->setLabel(pht('Filter IP'))
89 ->setDescription(pht('Search for actions by remote address.')),
90 id(new PhabricatorSearchStringListField())
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() {
115 public function buildSavedQueryFromBuiltin($query_key) {
116 $query = $this->newSavedQuery();
117 $query->setQueryKey($query_key);
119 switch ($query_key) {
124 return parent
::buildSavedQueryFromBuiltin($query_key);
127 protected function renderResultList(
129 PhabricatorSavedQuery
$query,
131 assert_instances_of($logs, 'PhabricatorUserLog');
133 $viewer = $this->requireViewer();
135 $table = id(new PhabricatorUserLogView())
139 if ($viewer->getIsAdmin()) {
140 $table->setSearchBaseURI($this->getApplicationURI('logs/'));
143 return id(new PhabricatorApplicationSearchResultView())
147 protected function newExportFields() {
148 $viewer = $this->requireViewer();
151 $fields[] = id(new PhabricatorPHIDExportField())
152 ->setKey('actorPHID')
153 ->setLabel(pht('Actor PHID')),
154 $fields[] = id(new PhabricatorStringExportField())
156 ->setLabel(pht('Actor')),
157 $fields[] = id(new PhabricatorPHIDExportField())
159 ->setLabel(pht('User PHID')),
160 $fields[] = id(new PhabricatorStringExportField())
162 ->setLabel(pht('User')),
163 $fields[] = id(new PhabricatorStringExportField())
165 ->setLabel(pht('Action')),
166 $fields[] = id(new PhabricatorStringExportField())
167 ->setKey('actionName')
168 ->setLabel(pht('Action Name')),
169 $fields[] = id(new PhabricatorStringExportField())
171 ->setLabel(pht('Session')),
172 $fields[] = id(new PhabricatorStringExportField())
174 ->setLabel(pht('Old Value')),
175 $fields[] = id(new PhabricatorStringExportField())
177 ->setLabel(pht('New Value')),
180 if ($viewer->getIsAdmin()) {
181 $fields[] = id(new PhabricatorStringExportField())
182 ->setKey('remoteAddress')
183 ->setLabel(pht('Remote Address'));
189 protected function newExportData(array $logs) {
190 $viewer = $this->requireViewer();
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');
204 foreach ($logs as $log) {
206 $user_phid = $log->getUserPHID();
208 $user_name = $handles[$user_phid]->getName();
213 $actor_phid = $log->getActorPHID();
215 $actor_name = $handles[$actor_phid]->getName();
220 $action = $log->getAction();
221 $action_name = idx($types, $action, pht('Unknown ("%s")', $action));
224 'actorPHID' => $actor_phid,
225 'actor' => $actor_name,
226 'userPHID' => $user_phid,
227 'user' => $user_name,
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();