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