00001 <?php
00024 require_once( dirname(__FILE__) . '/Maintenance.php' );
00025
00026 class PopulateLogSearch extends Maintenance {
00027
00028 const LOG_SEARCH_BATCH_SIZE = 100;
00029
00030 static $tableMap = array('rev' => 'revision', 'fa' => 'filearchive', 'oi' => 'oldimage', 'ar' => 'archive');
00031
00032 public function __construct() {
00033 parent::__construct();
00034 $this->mDescription = "Migrate log params to new table and index for searching";
00035 }
00036
00037 public function execute() {
00038 $db = wfGetDB( DB_MASTER );
00039 if ( !$db->tableExists( 'log_search' ) ) {
00040 $this->error( "log_search does not exist", true );
00041 }
00042 $start = $db->selectField( 'logging', 'MIN(log_id)', false, __FUNCTION__ );
00043 if( !$start ) {
00044 $this->output( "Nothing to do.\n" );
00045 return true;
00046 }
00047 $end = $db->selectField( 'logging', 'MAX(log_id)', false, __FUNCTION__ );
00048
00049 # Do remaining chunk
00050 $end += self::LOG_SEARCH_BATCH_SIZE - 1;
00051 $blockStart = $start;
00052 $blockEnd = $start + self::LOG_SEARCH_BATCH_SIZE - 1;
00053
00054 $delTypes = array('delete','suppress');
00055 while( $blockEnd <= $end ) {
00056 $this->output( "...doing log_id from $blockStart to $blockEnd\n" );
00057 $cond = "log_id BETWEEN $blockStart AND $blockEnd";
00058 $res = $db->select( 'logging', '*', $cond, __FUNCTION__ );
00059 $batch = array();
00060 foreach( $res as $row ) {
00061
00062 if( LogEventsList::typeAction( $row, $delTypes, 'revision' ) ) {
00063 $params = LogPage::extractParams( $row->log_params );
00064
00065 if( count($params) < 2 ) continue;
00066 $field = RevisionDeleter::getRelationType($params[0]);
00067
00068 if( $field == null ) {
00069 array_shift($params);
00070 $field = RevisionDeleter::getRelationType($params[0]);
00071 if( $field == null ) {
00072 $this->output( "Invalid param type for {$row->log_id}\n" );
00073 continue;
00074 } else {
00075
00076 $db->update( 'logging',
00077 array('log_params' => implode(',',$params) ),
00078 array('log_id' => $row->log_id ) );
00079 }
00080 }
00081 $items = explode(',',$params[1]);
00082 $log = new LogPage( $row->log_type );
00083
00084 $log->addRelations( $field, $items, $row->log_id );
00085
00086 $prefix = substr( $field, 0, strpos($field,'_') );
00087 if( !isset(self::$tableMap[$prefix]) )
00088 continue;
00089 $table = self::$tableMap[$prefix];
00090 $userField = $prefix.'_user';
00091 $userTextField = $prefix.'_user_text';
00092
00093 $userIds = $userIPs = array();
00094 $sres = $db->select( $table,
00095 array($userField,$userTextField),
00096 array($field => $items)
00097 );
00098 foreach( $sres as $srow ) {
00099 if( $srow->$userField > 0 )
00100 $userIds[] = intval($srow->$userField);
00101 else if( $srow->$userTextField != '' )
00102 $userIPs[] = $srow->$userTextField;
00103 }
00104
00105 $log->addRelations( 'target_author_id', $userIds, $row->log_id );
00106 $log->addRelations( 'target_author_ip', $userIPs, $row->log_id );
00107
00108 } else if( LogEventsList::typeAction( $row, $delTypes, 'event' ) ) {
00109 $params = LogPage::extractParams( $row->log_params );
00110
00111 if( count($params) < 1 ) continue;
00112 $items = explode( ',', $params[0] );
00113 $log = new LogPage( $row->log_type );
00114
00115 $log->addRelations( 'log_id', $items, $row->log_id );
00116
00117 $userIds = $userIPs = array();
00118 $sres = $db->select( 'logging',
00119 array('log_user','log_user_text'),
00120 array('log_id' => $items)
00121 );
00122 foreach( $sres as $srow ) {
00123 if( $srow->log_user > 0 )
00124 $userIds[] = intval($srow->log_user);
00125 else if( IP::isIPAddress($srow->log_user_text) )
00126 $userIPs[] = $srow->log_user_text;
00127 }
00128 $log->addRelations( 'target_author_id', $userIds, $row->log_id );
00129 $log->addRelations( 'target_author_ip', $userIPs, $row->log_id );
00130 }
00131 }
00132 $blockStart += self::LOG_SEARCH_BATCH_SIZE;
00133 $blockEnd += self::LOG_SEARCH_BATCH_SIZE;
00134 wfWaitForSlaves( 5 );
00135 }
00136 if( $db->insert(
00137 'updatelog',
00138 array( 'ul_key' => 'populate log_search' ),
00139 __FUNCTION__,
00140 'IGNORE'
00141 )
00142 ) {
00143 $this->output( "log_search population complete.\n" );
00144 return true;
00145 } else {
00146 $this->output( "Could not insert log_search population row.\n" );
00147 return false;
00148 }
00149 }
00150 }
00151
00152 $maintClass = "PopulateLogSearch";
00153 require_once( DO_MAINTENANCE );