Split LogPager out of LogEventsList.php
[lhc/web/wiklou.git] / includes / logging / LogPager.php
1 <?php
2 /**
3 * Contain classes to list log entries
4 *
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>, 2008 Aaron Schulz
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 */
25
26 /**
27 * @ingroup Pager
28 */
29 class LogPager extends ReverseChronologicalPager {
30 private $types = array(), $performer = '', $title = '', $pattern = '';
31 private $typeCGI = '';
32 public $mLogEventsList;
33
34 /**
35 * Constructor
36 *
37 * @param $list LogEventsList
38 * @param $types String or Array: log types to show
39 * @param $performer String: the user who made the log entries
40 * @param $title String|Title: the page title the log entries are for
41 * @param $pattern String: do a prefix search rather than an exact title match
42 * @param $conds Array: extra conditions for the query
43 * @param $year Integer: the year to start from
44 * @param $month Integer: the month to start from
45 * @param $tagFilter String: tag
46 */
47 public function __construct( $list, $types = array(), $performer = '', $title = '', $pattern = '',
48 $conds = array(), $year = false, $month = false, $tagFilter = '' ) {
49 parent::__construct( $list->getContext() );
50 $this->mConds = $conds;
51
52 $this->mLogEventsList = $list;
53
54 $this->limitType( $types ); // also excludes hidden types
55 $this->limitPerformer( $performer );
56 $this->limitTitle( $title, $pattern );
57 $this->getDateCond( $year, $month );
58 $this->mTagFilter = $tagFilter;
59 }
60
61 public function getDefaultQuery() {
62 $query = parent::getDefaultQuery();
63 $query['type'] = $this->typeCGI; // arrays won't work here
64 $query['user'] = $this->performer;
65 $query['month'] = $this->mMonth;
66 $query['year'] = $this->mYear;
67 return $query;
68 }
69
70 // Call ONLY after calling $this->limitType() already!
71 public function getFilterParams() {
72 global $wgFilterLogTypes;
73 $filters = array();
74 if( count($this->types) ) {
75 return $filters;
76 }
77 foreach( $wgFilterLogTypes as $type => $default ) {
78 // Avoid silly filtering
79 if( $type !== 'patrol' || $this->getUser()->useNPPatrol() ) {
80 $hide = $this->getRequest()->getInt( "hide_{$type}_log", $default );
81 $filters[$type] = $hide;
82 if( $hide )
83 $this->mConds[] = 'log_type != ' . $this->mDb->addQuotes( $type );
84 }
85 }
86 return $filters;
87 }
88
89 /**
90 * Set the log reader to return only entries of the given type.
91 * Type restrictions enforced here
92 *
93 * @param $types String or array: Log types ('upload', 'delete', etc);
94 * empty string means no restriction
95 */
96 private function limitType( $types ) {
97 global $wgLogRestrictions;
98 // If $types is not an array, make it an array
99 $types = ($types === '') ? array() : (array)$types;
100 // Don't even show header for private logs; don't recognize it...
101 $needReindex = false;
102 foreach ( $types as $type ) {
103 if( isset( $wgLogRestrictions[$type] )
104 && !$this->getUser()->isAllowed($wgLogRestrictions[$type])
105 ) {
106 $needReindex = true;
107 $types = array_diff( $types, array( $type ) );
108 }
109 }
110 if ( $needReindex ) {
111 // Lots of this code makes assumptions that
112 // the first entry in the array is $types[0].
113 $types = array_values( $types );
114 }
115 $this->types = $types;
116 // Don't show private logs to unprivileged users.
117 // Also, only show them upon specific request to avoid suprises.
118 $audience = $types ? 'user' : 'public';
119 $hideLogs = LogEventsList::getExcludeClause( $this->mDb, $audience );
120 if( $hideLogs !== false ) {
121 $this->mConds[] = $hideLogs;
122 }
123 if( count($types) ) {
124 $this->mConds['log_type'] = $types;
125 // Set typeCGI; used in url param for paging
126 if( count($types) == 1 ) $this->typeCGI = $types[0];
127 }
128 }
129
130 /**
131 * Set the log reader to return only entries by the given user.
132 *
133 * @param $name String: (In)valid user name
134 */
135 private function limitPerformer( $name ) {
136 if( $name == '' ) {
137 return false;
138 }
139 $usertitle = Title::makeTitleSafe( NS_USER, $name );
140 if( is_null($usertitle) ) {
141 return false;
142 }
143 /* Fetch userid at first, if known, provides awesome query plan afterwards */
144 $userid = User::idFromName( $name );
145 if( !$userid ) {
146 /* It should be nicer to abort query at all,
147 but for now it won't pass anywhere behind the optimizer */
148 $this->mConds[] = "NULL";
149 } else {
150 $this->mConds['log_user'] = $userid;
151 // Paranoia: avoid brute force searches (bug 17342)
152 $user = $this->getUser();
153 if( !$user->isAllowed( 'deletedhistory' ) ) {
154 $this->mConds[] = $this->mDb->bitAnd('log_deleted', LogPage::DELETED_USER) . ' = 0';
155 } elseif( !$user->isAllowed( 'suppressrevision' ) ) {
156 $this->mConds[] = $this->mDb->bitAnd('log_deleted', LogPage::SUPPRESSED_USER) .
157 ' != ' . LogPage::SUPPRESSED_USER;
158 }
159 $this->performer = $usertitle->getText();
160 }
161 }
162
163 /**
164 * Set the log reader to return only entries affecting the given page.
165 * (For the block and rights logs, this is a user page.)
166 *
167 * @param $page String or Title object: Title name
168 * @param $pattern String
169 */
170 private function limitTitle( $page, $pattern ) {
171 global $wgMiserMode;
172
173 if ( $page instanceof Title ) {
174 $title = $page;
175 } else {
176 $title = Title::newFromText( $page );
177 if( strlen( $page ) == 0 || !$title instanceof Title ) {
178 return false;
179 }
180 }
181
182 $this->title = $title->getPrefixedText();
183 $ns = $title->getNamespace();
184 $db = $this->mDb;
185
186 # Using the (log_namespace, log_title, log_timestamp) index with a
187 # range scan (LIKE) on the first two parts, instead of simple equality,
188 # makes it unusable for sorting. Sorted retrieval using another index
189 # would be possible, but then we might have to scan arbitrarily many
190 # nodes of that index. Therefore, we need to avoid this if $wgMiserMode
191 # is on.
192 #
193 # This is not a problem with simple title matches, because then we can
194 # use the page_time index. That should have no more than a few hundred
195 # log entries for even the busiest pages, so it can be safely scanned
196 # in full to satisfy an impossible condition on user or similar.
197 if( $pattern && !$wgMiserMode ) {
198 $this->mConds['log_namespace'] = $ns;
199 $this->mConds[] = 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() );
200 $this->pattern = $pattern;
201 } else {
202 $this->mConds['log_namespace'] = $ns;
203 $this->mConds['log_title'] = $title->getDBkey();
204 }
205 // Paranoia: avoid brute force searches (bug 17342)
206 $user = $this->getUser();
207 if( !$user->isAllowed( 'deletedhistory' ) ) {
208 $this->mConds[] = $db->bitAnd('log_deleted', LogPage::DELETED_ACTION) . ' = 0';
209 } elseif( !$user->isAllowed( 'suppressrevision' ) ) {
210 $this->mConds[] = $db->bitAnd('log_deleted', LogPage::SUPPRESSED_ACTION) .
211 ' != ' . LogPage::SUPPRESSED_ACTION;
212 }
213 }
214
215 /**
216 * Constructs the most part of the query. Extra conditions are sprinkled in
217 * all over this class.
218 * @return array
219 */
220 public function getQueryInfo() {
221 $basic = DatabaseLogEntry::getSelectQueryData();
222
223 $tables = $basic['tables'];
224 $fields = $basic['fields'];
225 $conds = $basic['conds'];
226 $options = $basic['options'];
227 $joins = $basic['join_conds'];
228
229 $index = array();
230 # Add log_search table if there are conditions on it.
231 # This filters the results to only include log rows that have
232 # log_search records with the specified ls_field and ls_value values.
233 if( array_key_exists( 'ls_field', $this->mConds ) ) {
234 $tables[] = 'log_search';
235 $index['log_search'] = 'ls_field_val';
236 $index['logging'] = 'PRIMARY';
237 if ( !$this->hasEqualsClause( 'ls_field' )
238 || !$this->hasEqualsClause( 'ls_value' ) )
239 {
240 # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
241 # to match a specific (ls_field,ls_value) tuple, then there will be
242 # no duplicate log rows. Otherwise, we need to remove the duplicates.
243 $options[] = 'DISTINCT';
244 }
245 # Avoid usage of the wrong index by limiting
246 # the choices of available indexes. This mainly
247 # avoids site-breaking filesorts.
248 } elseif( $this->title || $this->pattern || $this->performer ) {
249 $index['logging'] = array( 'page_time', 'user_time' );
250 if( count($this->types) == 1 ) {
251 $index['logging'][] = 'log_user_type_time';
252 }
253 } elseif( count($this->types) == 1 ) {
254 $index['logging'] = 'type_time';
255 } else {
256 $index['logging'] = 'times';
257 }
258 $options['USE INDEX'] = $index;
259 # Don't show duplicate rows when using log_search
260 $joins['log_search'] = array( 'INNER JOIN', 'ls_log_id=log_id' );
261
262 $info = array(
263 'tables' => $tables,
264 'fields' => $fields,
265 'conds' => array_merge( $conds, $this->mConds ),
266 'options' => $options,
267 'join_conds' => $joins,
268 );
269 # Add ChangeTags filter query
270 ChangeTags::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
271 $info['join_conds'], $info['options'], $this->mTagFilter );
272 return $info;
273 }
274
275 /**
276 * Checks if $this->mConds has $field matched to a *single* value
277 * @param $field
278 * @return bool
279 */
280 protected function hasEqualsClause( $field ) {
281 return (
282 array_key_exists( $field, $this->mConds ) &&
283 ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
284 );
285 }
286
287 function getIndexField() {
288 return 'log_timestamp';
289 }
290
291 public function getStartBody() {
292 wfProfileIn( __METHOD__ );
293 # Do a link batch query
294 if( $this->getNumRows() > 0 ) {
295 $lb = new LinkBatch;
296 foreach ( $this->mResult as $row ) {
297 $lb->add( $row->log_namespace, $row->log_title );
298 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
299 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
300 }
301 $lb->execute();
302 $this->mResult->seek( 0 );
303 }
304 wfProfileOut( __METHOD__ );
305 return '';
306 }
307
308 public function formatRow( $row ) {
309 return $this->mLogEventsList->logLine( $row );
310 }
311
312 public function getType() {
313 return $this->types;
314 }
315
316 /**
317 * @return string
318 */
319 public function getPerformer() {
320 return $this->performer;
321 }
322
323 /**
324 * @return string
325 */
326 public function getPage() {
327 return $this->title;
328 }
329
330 public function getPattern() {
331 return $this->pattern;
332 }
333
334 public function getYear() {
335 return $this->mYear;
336 }
337
338 public function getMonth() {
339 return $this->mMonth;
340 }
341
342 public function getTagFilter() {
343 return $this->mTagFilter;
344 }
345
346 public function doQuery() {
347 // Workaround MySQL optimizer bug
348 $this->mDb->setBigSelects();
349 parent::doQuery();
350 $this->mDb->setBigSelects( 'default' );
351 }
352 }