Merge "Fix ORMRow::insert() on PostgreSQL."
[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 string $types or Array: log types to show
39 * @param string $performer the user who made the log entries
40 * @param string|Title $title the page title the log entries are for
41 * @param string $pattern do a prefix search rather than an exact title match
42 * @param array $conds 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 string $tagFilter 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 string $types or array: Log types ('upload', 'delete', etc);
94 * empty string means no restriction
95 */
96 private function limitType( $types ) {
97 global $wgLogRestrictions;
98
99 $user = $this->getUser();
100 // If $types is not an array, make it an array
101 $types = ($types === '') ? array() : (array)$types;
102 // Don't even show header for private logs; don't recognize it...
103 $needReindex = false;
104 foreach ( $types as $type ) {
105 if( isset( $wgLogRestrictions[$type] )
106 && !$user->isAllowed( $wgLogRestrictions[$type] )
107 ) {
108 $needReindex = true;
109 $types = array_diff( $types, array( $type ) );
110 }
111 }
112 if ( $needReindex ) {
113 // Lots of this code makes assumptions that
114 // the first entry in the array is $types[0].
115 $types = array_values( $types );
116 }
117 $this->types = $types;
118 // Don't show private logs to unprivileged users.
119 // Also, only show them upon specific request to avoid suprises.
120 $audience = $types ? 'user' : 'public';
121 $hideLogs = LogEventsList::getExcludeClause( $this->mDb, $audience, $user );
122 if( $hideLogs !== false ) {
123 $this->mConds[] = $hideLogs;
124 }
125 if( count( $types ) ) {
126 $this->mConds['log_type'] = $types;
127 // Set typeCGI; used in url param for paging
128 if( count( $types ) == 1 ) $this->typeCGI = $types[0];
129 }
130 }
131
132 /**
133 * Set the log reader to return only entries by the given user.
134 *
135 * @param string $name (In)valid user name
136 * @return bool
137 */
138 private function limitPerformer( $name ) {
139 if( $name == '' ) {
140 return false;
141 }
142 $usertitle = Title::makeTitleSafe( NS_USER, $name );
143 if( is_null( $usertitle ) ) {
144 return false;
145 }
146 /* Fetch userid at first, if known, provides awesome query plan afterwards */
147 $userid = User::idFromName( $name );
148 if( !$userid ) {
149 /* It should be nicer to abort query at all,
150 but for now it won't pass anywhere behind the optimizer */
151 $this->mConds[] = "NULL";
152 } else {
153 $this->mConds['log_user'] = $userid;
154 // Paranoia: avoid brute force searches (bug 17342)
155 $user = $this->getUser();
156 if( !$user->isAllowed( 'deletedhistory' ) ) {
157 $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0';
158 } elseif( !$user->isAllowed( 'suppressrevision' ) ) {
159 $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::SUPPRESSED_USER ) .
160 ' != ' . LogPage::SUPPRESSED_USER;
161 }
162 $this->performer = $usertitle->getText();
163 }
164 }
165
166 /**
167 * Set the log reader to return only entries affecting the given page.
168 * (For the block and rights logs, this is a user page.)
169 *
170 * @param string $page or Title object: Title name
171 * @param $pattern String
172 * @return bool
173 */
174 private function limitTitle( $page, $pattern ) {
175 global $wgMiserMode;
176
177 if ( $page instanceof Title ) {
178 $title = $page;
179 } else {
180 $title = Title::newFromText( $page );
181 if( strlen( $page ) == 0 || !$title instanceof Title ) {
182 return false;
183 }
184 }
185
186 $this->title = $title->getPrefixedText();
187 $ns = $title->getNamespace();
188 $db = $this->mDb;
189
190 # Using the (log_namespace, log_title, log_timestamp) index with a
191 # range scan (LIKE) on the first two parts, instead of simple equality,
192 # makes it unusable for sorting. Sorted retrieval using another index
193 # would be possible, but then we might have to scan arbitrarily many
194 # nodes of that index. Therefore, we need to avoid this if $wgMiserMode
195 # is on.
196 #
197 # This is not a problem with simple title matches, because then we can
198 # use the page_time index. That should have no more than a few hundred
199 # log entries for even the busiest pages, so it can be safely scanned
200 # in full to satisfy an impossible condition on user or similar.
201 if( $pattern && !$wgMiserMode ) {
202 $this->mConds['log_namespace'] = $ns;
203 $this->mConds[] = 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() );
204 $this->pattern = $pattern;
205 } else {
206 $this->mConds['log_namespace'] = $ns;
207 $this->mConds['log_title'] = $title->getDBkey();
208 }
209 // Paranoia: avoid brute force searches (bug 17342)
210 $user = $this->getUser();
211 if( !$user->isAllowed( 'deletedhistory' ) ) {
212 $this->mConds[] = $db->bitAnd( 'log_deleted', LogPage::DELETED_ACTION) . ' = 0';
213 } elseif( !$user->isAllowed( 'suppressrevision' ) ) {
214 $this->mConds[] = $db->bitAnd( 'log_deleted', LogPage::SUPPRESSED_ACTION) .
215 ' != ' . LogPage::SUPPRESSED_ACTION;
216 }
217 }
218
219 /**
220 * Constructs the most part of the query. Extra conditions are sprinkled in
221 * all over this class.
222 * @return array
223 */
224 public function getQueryInfo() {
225 $basic = DatabaseLogEntry::getSelectQueryData();
226
227 $tables = $basic['tables'];
228 $fields = $basic['fields'];
229 $conds = $basic['conds'];
230 $options = $basic['options'];
231 $joins = $basic['join_conds'];
232
233 $index = array();
234 # Add log_search table if there are conditions on it.
235 # This filters the results to only include log rows that have
236 # log_search records with the specified ls_field and ls_value values.
237 if( array_key_exists( 'ls_field', $this->mConds ) ) {
238 $tables[] = 'log_search';
239 $index['log_search'] = 'ls_field_val';
240 $index['logging'] = 'PRIMARY';
241 if ( !$this->hasEqualsClause( 'ls_field' )
242 || !$this->hasEqualsClause( 'ls_value' ) )
243 {
244 # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
245 # to match a specific (ls_field,ls_value) tuple, then there will be
246 # no duplicate log rows. Otherwise, we need to remove the duplicates.
247 $options[] = 'DISTINCT';
248 }
249 # Avoid usage of the wrong index by limiting
250 # the choices of available indexes. This mainly
251 # avoids site-breaking filesorts.
252 } elseif( $this->title || $this->pattern || $this->performer ) {
253 $index['logging'] = array( 'page_time', 'user_time' );
254 if( count( $this->types ) == 1 ) {
255 $index['logging'][] = 'log_user_type_time';
256 }
257 } elseif( count( $this->types ) == 1 ) {
258 $index['logging'] = 'type_time';
259 } else {
260 $index['logging'] = 'times';
261 }
262 $options['USE INDEX'] = $index;
263 # Don't show duplicate rows when using log_search
264 $joins['log_search'] = array( 'INNER JOIN', 'ls_log_id=log_id' );
265
266 $info = array(
267 'tables' => $tables,
268 'fields' => $fields,
269 'conds' => array_merge( $conds, $this->mConds ),
270 'options' => $options,
271 'join_conds' => $joins,
272 );
273 # Add ChangeTags filter query
274 ChangeTags::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
275 $info['join_conds'], $info['options'], $this->mTagFilter );
276 return $info;
277 }
278
279 /**
280 * Checks if $this->mConds has $field matched to a *single* value
281 * @param $field
282 * @return bool
283 */
284 protected function hasEqualsClause( $field ) {
285 return (
286 array_key_exists( $field, $this->mConds ) &&
287 ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
288 );
289 }
290
291 function getIndexField() {
292 return 'log_timestamp';
293 }
294
295 public function getStartBody() {
296 wfProfileIn( __METHOD__ );
297 # Do a link batch query
298 if( $this->getNumRows() > 0 ) {
299 $lb = new LinkBatch;
300 foreach ( $this->mResult as $row ) {
301 $lb->add( $row->log_namespace, $row->log_title );
302 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
303 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
304 $formatter = LogFormatter::newFromRow( $row );
305 foreach ( $formatter->getPreloadTitles() as $title ) {
306 $lb->addObj( $title );
307 }
308 }
309 $lb->execute();
310 $this->mResult->seek( 0 );
311 }
312 wfProfileOut( __METHOD__ );
313 return '';
314 }
315
316 public function formatRow( $row ) {
317 return $this->mLogEventsList->logLine( $row );
318 }
319
320 public function getType() {
321 return $this->types;
322 }
323
324 /**
325 * @return string
326 */
327 public function getPerformer() {
328 return $this->performer;
329 }
330
331 /**
332 * @return string
333 */
334 public function getPage() {
335 return $this->title;
336 }
337
338 public function getPattern() {
339 return $this->pattern;
340 }
341
342 public function getYear() {
343 return $this->mYear;
344 }
345
346 public function getMonth() {
347 return $this->mMonth;
348 }
349
350 public function getTagFilter() {
351 return $this->mTagFilter;
352 }
353
354 public function doQuery() {
355 // Workaround MySQL optimizer bug
356 $this->mDb->setBigSelects();
357 parent::doQuery();
358 $this->mDb->setBigSelects( 'default' );
359 }
360 }