MediaWiki  REL1_19
populateLogSearch.php
Go to the documentation of this file.
00001 <?php
00024 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
00025 
00026 class PopulateLogSearch extends LoggedUpdateMaintenance {
00027         static $tableMap = array( 'rev' => 'revision', 'fa' => 'filearchive', 'oi' => 'oldimage', 'ar' => 'archive' );
00028 
00029         public function __construct() {
00030                 parent::__construct();
00031                 $this->mDescription = "Migrate log params to new table and index for searching";
00032                 $this->setBatchSize( 100 );
00033         }
00034 
00035         protected function getUpdateKey() {
00036                 return 'populate log_search';
00037         }
00038 
00039         protected function updateSkippedMessage() {
00040                 return 'log_search table already populated.';
00041         }
00042 
00043         protected function doDBUpdates() {
00044                 $db = $this->getDB( DB_MASTER );
00045                 if ( !$db->tableExists( 'log_search' ) ) {
00046                         $this->error( "log_search does not exist" );
00047                         return false;
00048                 }
00049                 $start = $db->selectField( 'logging', 'MIN(log_id)', false, __FUNCTION__ );
00050                 if ( !$start ) {
00051                         $this->output( "Nothing to do.\n" );
00052                         return true;
00053                 }
00054                 $end = $db->selectField( 'logging', 'MAX(log_id)', false, __FUNCTION__ );
00055 
00056                 # Do remaining chunk
00057                 $end += $this->mBatchSize - 1;
00058                 $blockStart = $start;
00059                 $blockEnd = $start + $this->mBatchSize - 1;
00060 
00061                 $delTypes = array( 'delete', 'suppress' ); // revisiondelete types
00062                 while ( $blockEnd <= $end ) {
00063                         $this->output( "...doing log_id from $blockStart to $blockEnd\n" );
00064                         $cond = "log_id BETWEEN $blockStart AND $blockEnd";
00065                         $res = $db->select( 'logging', '*', $cond, __FUNCTION__ );
00066                         foreach ( $res as $row ) {
00067                                 // RevisionDelete logs - revisions
00068                                 if ( LogEventsList::typeAction( $row, $delTypes, 'revision' ) ) {
00069                                         $params = LogPage::extractParams( $row->log_params );
00070                                         // Param format: <urlparam> <item CSV> [<ofield> <nfield>]
00071                                         if ( count( $params ) < 2 ) continue; // bad row?
00072                                         $field = RevisionDeleter::getRelationType( $params[0] );
00073                                         // B/C, the params may start with a title key (<title> <urlparam> <CSV>)
00074                                         if ( $field == null ) {
00075                                                 array_shift( $params ); // remove title param
00076                                                 $field = RevisionDeleter::getRelationType( $params[0] );
00077                                                 if ( $field == null ) {
00078                                                         $this->output( "Invalid param type for {$row->log_id}\n" );
00079                                                         continue; // skip this row
00080                                                 } else {
00081                                                         // Clean up the row...
00082                                                         $db->update( 'logging',
00083                                                                 array( 'log_params' => implode( ',', $params ) ),
00084                                                                 array( 'log_id' => $row->log_id ) );
00085                                                 }
00086                                         }
00087                                         $items = explode( ',', $params[1] );
00088                                         $log = new LogPage( $row->log_type );
00089                                         // Add item relations...
00090                                         $log->addRelations( $field, $items, $row->log_id );
00091                                         // Determine what table to query...
00092                                         $prefix = substr( $field, 0, strpos( $field, '_' ) ); // db prefix
00093                                         if ( !isset( self::$tableMap[$prefix] ) )
00094                                                 continue; // bad row?
00095                                         $table = self::$tableMap[$prefix];
00096                                         $userField = $prefix . '_user';
00097                                         $userTextField = $prefix . '_user_text';
00098                                         // Add item author relations...
00099                                         $userIds = $userIPs = array();
00100                                         $sres = $db->select( $table,
00101                                                 array( $userField, $userTextField ),
00102                                                 array( $field => $items )
00103                                         );
00104                                         foreach ( $sres as $srow ) {
00105                                                 if ( $srow->$userField > 0 )
00106                                                         $userIds[] = intval( $srow->$userField );
00107                                                 elseif ( $srow->$userTextField != '' )
00108                                                         $userIPs[] = $srow->$userTextField;
00109                                         }
00110                                         // Add item author relations...
00111                                         $log->addRelations( 'target_author_id', $userIds, $row->log_id );
00112                                         $log->addRelations( 'target_author_ip', $userIPs, $row->log_id );
00113                                 // RevisionDelete logs - log events
00114                                 } elseif ( LogEventsList::typeAction( $row, $delTypes, 'event' ) ) {
00115                                         $params = LogPage::extractParams( $row->log_params );
00116                                         // Param format: <item CSV> [<ofield> <nfield>]
00117                                         if ( count( $params ) < 1 ) continue; // bad row
00118                                         $items = explode( ',', $params[0] );
00119                                         $log = new LogPage( $row->log_type );
00120                                         // Add item relations...
00121                                         $log->addRelations( 'log_id', $items, $row->log_id );
00122                                         // Add item author relations...
00123                                         $userIds = $userIPs = array();
00124                                         $sres = $db->select( 'logging',
00125                                                 array( 'log_user', 'log_user_text' ),
00126                                                 array( 'log_id' => $items )
00127                                         );
00128                                         foreach ( $sres as $srow ) {
00129                                                 if ( $srow->log_user > 0 )
00130                                                         $userIds[] = intval( $srow->log_user );
00131                                                 elseif ( IP::isIPAddress( $srow->log_user_text ) )
00132                                                         $userIPs[] = $srow->log_user_text;
00133                                         }
00134                                         $log->addRelations( 'target_author_id', $userIds, $row->log_id );
00135                                         $log->addRelations( 'target_author_ip', $userIPs, $row->log_id );
00136                                 }
00137                         }
00138                         $blockStart += $this->mBatchSize;
00139                         $blockEnd += $this->mBatchSize;
00140                         wfWaitForSlaves();
00141                 }
00142                 $this->output( "Done populating log_search table.\n" );
00143                 return true;
00144         }
00145 }
00146 
00147 $maintClass = "PopulateLogSearch";
00148 require_once( RUN_MAINTENANCE_IF_MAIN );