MediaWiki  REL1_22
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 ) {
00079                         continue; // bad row?
00080                     }
00081                     $field = RevisionDeleter::getRelationType( $params[0] );
00082                     // B/C, the params may start with a title key (<title> <urlparam> <CSV>)
00083                     if ( $field == null ) {
00084                         array_shift( $params ); // remove title param
00085                         $field = RevisionDeleter::getRelationType( $params[0] );
00086                         if ( $field == null ) {
00087                             $this->output( "Invalid param type for {$row->log_id}\n" );
00088                             continue; // skip this row
00089                         } else {
00090                             // Clean up the row...
00091                             $db->update( 'logging',
00092                                 array( 'log_params' => implode( ',', $params ) ),
00093                                 array( 'log_id' => $row->log_id ) );
00094                         }
00095                     }
00096                     $items = explode( ',', $params[1] );
00097                     $log = new LogPage( $row->log_type );
00098                     // Add item relations...
00099                     $log->addRelations( $field, $items, $row->log_id );
00100                     // Determine what table to query...
00101                     $prefix = substr( $field, 0, strpos( $field, '_' ) ); // db prefix
00102                     if ( !isset( self::$tableMap[$prefix] ) ) {
00103                         continue; // bad row?
00104                     }
00105                     $table = self::$tableMap[$prefix];
00106                     $userField = $prefix . '_user';
00107                     $userTextField = $prefix . '_user_text';
00108                     // Add item author relations...
00109                     $userIds = $userIPs = array();
00110                     $sres = $db->select( $table,
00111                         array( $userField, $userTextField ),
00112                         array( $field => $items )
00113                     );
00114                     foreach ( $sres as $srow ) {
00115                         if ( $srow->$userField > 0 ) {
00116                             $userIds[] = intval( $srow->$userField );
00117                         } elseif ( $srow->$userTextField != '' ) {
00118                             $userIPs[] = $srow->$userTextField;
00119                         }
00120                     }
00121                     // Add item author relations...
00122                     $log->addRelations( 'target_author_id', $userIds, $row->log_id );
00123                     $log->addRelations( 'target_author_ip', $userIPs, $row->log_id );
00124                 // RevisionDelete logs - log events
00125                 } elseif ( LogEventsList::typeAction( $row, $delTypes, 'event' ) ) {
00126                     $params = LogPage::extractParams( $row->log_params );
00127                     // Param format: <item CSV> [<ofield> <nfield>]
00128                     if ( count( $params ) < 1 ) {
00129                         continue; // bad row
00130                     }
00131                     $items = explode( ',', $params[0] );
00132                     $log = new LogPage( $row->log_type );
00133                     // Add item relations...
00134                     $log->addRelations( 'log_id', $items, $row->log_id );
00135                     // Add item author relations...
00136                     $userIds = $userIPs = array();
00137                     $sres = $db->select( 'logging',
00138                         array( 'log_user', 'log_user_text' ),
00139                         array( 'log_id' => $items )
00140                     );
00141                     foreach ( $sres as $srow ) {
00142                         if ( $srow->log_user > 0 ) {
00143                             $userIds[] = intval( $srow->log_user );
00144                         } elseif ( IP::isIPAddress( $srow->log_user_text ) ) {
00145                             $userIPs[] = $srow->log_user_text;
00146                         }
00147                     }
00148                     $log->addRelations( 'target_author_id', $userIds, $row->log_id );
00149                     $log->addRelations( 'target_author_ip', $userIPs, $row->log_id );
00150                 }
00151             }
00152             $blockStart += $this->mBatchSize;
00153             $blockEnd += $this->mBatchSize;
00154             wfWaitForSlaves();
00155         }
00156         $this->output( "Done populating log_search table.\n" );
00157         return true;
00158     }
00159 }
00160 
00161 $maintClass = "PopulateLogSearch";
00162 require_once RUN_MAINTENANCE_IF_MAIN;