MediaWiki  REL1_22
LogPager.php
Go to the documentation of this file.
00001 <?php
00029 class LogPager extends ReverseChronologicalPager {
00030     private $types = array(), $performer = '', $title = '', $pattern = '';
00031     private $typeCGI = '';
00032     public $mLogEventsList;
00033 
00047     public function __construct( $list, $types = array(), $performer = '', $title = '', $pattern = '',
00048         $conds = array(), $year = false, $month = false, $tagFilter = '' ) {
00049         parent::__construct( $list->getContext() );
00050         $this->mConds = $conds;
00051 
00052         $this->mLogEventsList = $list;
00053 
00054         $this->limitType( $types ); // also excludes hidden types
00055         $this->limitPerformer( $performer );
00056         $this->limitTitle( $title, $pattern );
00057         $this->getDateCond( $year, $month );
00058         $this->mTagFilter = $tagFilter;
00059     }
00060 
00061     public function getDefaultQuery() {
00062         $query = parent::getDefaultQuery();
00063         $query['type'] = $this->typeCGI; // arrays won't work here
00064         $query['user'] = $this->performer;
00065         $query['month'] = $this->mMonth;
00066         $query['year'] = $this->mYear;
00067         return $query;
00068     }
00069 
00070     // Call ONLY after calling $this->limitType() already!
00071     public function getFilterParams() {
00072         global $wgFilterLogTypes;
00073         $filters = array();
00074         if ( count( $this->types ) ) {
00075             return $filters;
00076         }
00077         foreach ( $wgFilterLogTypes as $type => $default ) {
00078             // Avoid silly filtering
00079             if ( $type !== 'patrol' || $this->getUser()->useNPPatrol() ) {
00080                 $hide = $this->getRequest()->getInt( "hide_{$type}_log", $default );
00081                 $filters[$type] = $hide;
00082                 if ( $hide ) {
00083                     $this->mConds[] = 'log_type != ' . $this->mDb->addQuotes( $type );
00084                 }
00085             }
00086         }
00087         return $filters;
00088     }
00089 
00097     private function limitType( $types ) {
00098         global $wgLogRestrictions;
00099 
00100         $user = $this->getUser();
00101         // If $types is not an array, make it an array
00102         $types = ( $types === '' ) ? array() : (array)$types;
00103         // Don't even show header for private logs; don't recognize it...
00104         $needReindex = false;
00105         foreach ( $types as $type ) {
00106             if ( isset( $wgLogRestrictions[$type] )
00107                 && !$user->isAllowed( $wgLogRestrictions[$type] )
00108             ) {
00109                 $needReindex = true;
00110                 $types = array_diff( $types, array( $type ) );
00111             }
00112         }
00113         if ( $needReindex ) {
00114             // Lots of this code makes assumptions that
00115             // the first entry in the array is $types[0].
00116             $types = array_values( $types );
00117         }
00118         $this->types = $types;
00119         // Don't show private logs to unprivileged users.
00120         // Also, only show them upon specific request to avoid suprises.
00121         $audience = $types ? 'user' : 'public';
00122         $hideLogs = LogEventsList::getExcludeClause( $this->mDb, $audience, $user );
00123         if ( $hideLogs !== false ) {
00124             $this->mConds[] = $hideLogs;
00125         }
00126         if ( count( $types ) ) {
00127             $this->mConds['log_type'] = $types;
00128             // Set typeCGI; used in url param for paging
00129             if ( count( $types ) == 1 ) {
00130                 $this->typeCGI = $types[0];
00131             }
00132         }
00133     }
00134 
00141     private function limitPerformer( $name ) {
00142         if ( $name == '' ) {
00143             return false;
00144         }
00145         $usertitle = Title::makeTitleSafe( NS_USER, $name );
00146         if ( is_null( $usertitle ) ) {
00147             return false;
00148         }
00149         /* Fetch userid at first, if known, provides awesome query plan afterwards */
00150         $userid = User::idFromName( $name );
00151         if ( !$userid ) {
00152             /* It should be nicer to abort query at all,
00153                but for now it won't pass anywhere behind the optimizer */
00154             $this->mConds[] = "NULL";
00155         } else {
00156             $this->mConds['log_user'] = $userid;
00157             // Paranoia: avoid brute force searches (bug 17342)
00158             $user = $this->getUser();
00159             if ( !$user->isAllowed( 'deletedhistory' ) ) {
00160                 $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0';
00161             } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
00162                 $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::SUPPRESSED_USER ) .
00163                     ' != ' . LogPage::SUPPRESSED_USER;
00164             }
00165             $this->performer = $usertitle->getText();
00166         }
00167     }
00168 
00177     private function limitTitle( $page, $pattern ) {
00178         global $wgMiserMode;
00179 
00180         if ( $page instanceof Title ) {
00181             $title = $page;
00182         } else {
00183             $title = Title::newFromText( $page );
00184             if ( strlen( $page ) == 0 || !$title instanceof Title ) {
00185                 return false;
00186             }
00187         }
00188 
00189         $this->title = $title->getPrefixedText();
00190         $ns = $title->getNamespace();
00191         $db = $this->mDb;
00192 
00193         # Using the (log_namespace, log_title, log_timestamp) index with a
00194         # range scan (LIKE) on the first two parts, instead of simple equality,
00195         # makes it unusable for sorting.  Sorted retrieval using another index
00196         # would be possible, but then we might have to scan arbitrarily many
00197         # nodes of that index. Therefore, we need to avoid this if $wgMiserMode
00198         # is on.
00199         #
00200         # This is not a problem with simple title matches, because then we can
00201         # use the page_time index.  That should have no more than a few hundred
00202         # log entries for even the busiest pages, so it can be safely scanned
00203         # in full to satisfy an impossible condition on user or similar.
00204         if ( $pattern && !$wgMiserMode ) {
00205             $this->mConds['log_namespace'] = $ns;
00206             $this->mConds[] = 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() );
00207             $this->pattern = $pattern;
00208         } else {
00209             $this->mConds['log_namespace'] = $ns;
00210             $this->mConds['log_title'] = $title->getDBkey();
00211         }
00212         // Paranoia: avoid brute force searches (bug 17342)
00213         $user = $this->getUser();
00214         if ( !$user->isAllowed( 'deletedhistory' ) ) {
00215             $this->mConds[] = $db->bitAnd( 'log_deleted', LogPage::DELETED_ACTION ) . ' = 0';
00216         } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
00217             $this->mConds[] = $db->bitAnd( 'log_deleted', LogPage::SUPPRESSED_ACTION ) .
00218                 ' != ' . LogPage::SUPPRESSED_ACTION;
00219         }
00220     }
00221 
00227     public function getQueryInfo() {
00228         $basic = DatabaseLogEntry::getSelectQueryData();
00229 
00230         $tables = $basic['tables'];
00231         $fields = $basic['fields'];
00232         $conds = $basic['conds'];
00233         $options = $basic['options'];
00234         $joins = $basic['join_conds'];
00235 
00236         $index = array();
00237         # Add log_search table if there are conditions on it.
00238         # This filters the results to only include log rows that have
00239         # log_search records with the specified ls_field and ls_value values.
00240         if ( array_key_exists( 'ls_field', $this->mConds ) ) {
00241             $tables[] = 'log_search';
00242             $index['log_search'] = 'ls_field_val';
00243             $index['logging'] = 'PRIMARY';
00244             if ( !$this->hasEqualsClause( 'ls_field' )
00245                 || !$this->hasEqualsClause( 'ls_value' ) )
00246             {
00247                 # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
00248                 # to match a specific (ls_field,ls_value) tuple, then there will be
00249                 # no duplicate log rows. Otherwise, we need to remove the duplicates.
00250                 $options[] = 'DISTINCT';
00251             }
00252         }
00253         if ( count( $index ) ) {
00254             $options['USE INDEX'] = $index;
00255         }
00256         # Don't show duplicate rows when using log_search
00257         $joins['log_search'] = array( 'INNER JOIN', 'ls_log_id=log_id' );
00258 
00259         $info = array(
00260             'tables' => $tables,
00261             'fields' => $fields,
00262             'conds' => array_merge( $conds, $this->mConds ),
00263             'options' => $options,
00264             'join_conds' => $joins,
00265         );
00266         # Add ChangeTags filter query
00267         ChangeTags::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
00268             $info['join_conds'], $info['options'], $this->mTagFilter );
00269         return $info;
00270     }
00271 
00277     protected function hasEqualsClause( $field ) {
00278         return (
00279             array_key_exists( $field, $this->mConds ) &&
00280             ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
00281         );
00282     }
00283 
00284     function getIndexField() {
00285         return 'log_timestamp';
00286     }
00287 
00288     public function getStartBody() {
00289         wfProfileIn( __METHOD__ );
00290         # Do a link batch query
00291         if ( $this->getNumRows() > 0 ) {
00292             $lb = new LinkBatch;
00293             foreach ( $this->mResult as $row ) {
00294                 $lb->add( $row->log_namespace, $row->log_title );
00295                 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
00296                 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
00297                 $formatter = LogFormatter::newFromRow( $row );
00298                 foreach ( $formatter->getPreloadTitles() as $title ) {
00299                     $lb->addObj( $title );
00300                 }
00301             }
00302             $lb->execute();
00303             $this->mResult->seek( 0 );
00304         }
00305         wfProfileOut( __METHOD__ );
00306         return '';
00307     }
00308 
00309     public function formatRow( $row ) {
00310         return $this->mLogEventsList->logLine( $row );
00311     }
00312 
00313     public function getType() {
00314         return $this->types;
00315     }
00316 
00320     public function getPerformer() {
00321         return $this->performer;
00322     }
00323 
00327     public function getPage() {
00328         return $this->title;
00329     }
00330 
00331     public function getPattern() {
00332         return $this->pattern;
00333     }
00334 
00335     public function getYear() {
00336         return $this->mYear;
00337     }
00338 
00339     public function getMonth() {
00340         return $this->mMonth;
00341     }
00342 
00343     public function getTagFilter() {
00344         return $this->mTagFilter;
00345     }
00346 
00347     public function doQuery() {
00348         // Workaround MySQL optimizer bug
00349         $this->mDb->setBigSelects();
00350         parent::doQuery();
00351         $this->mDb->setBigSelects( 'default' );
00352     }
00353 }