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