MediaWiki  master
populateLogSearch.php
Go to the documentation of this file.
1 <?php
25 require_once __DIR__ . '/Maintenance.php';
26 
28 
36  private static $tableMap = [
37  'rev' => 'revision',
38  'fa' => 'filearchive',
39  'oi' => 'oldimage',
40  'ar' => 'archive'
41  ];
42 
43  public function __construct() {
44  parent::__construct();
45  $this->addDescription( 'Migrate log params to new table and index for searching' );
46  $this->setBatchSize( 100 );
47  }
48 
49  protected function getUpdateKey() {
50  return 'populate log_search';
51  }
52 
53  protected function updateSkippedMessage() {
54  return 'log_search table already populated.';
55  }
56 
57  protected function doDBUpdates() {
58  $batchSize = $this->getBatchSize();
59  $db = $this->getDB( DB_MASTER );
60  if ( !$db->tableExists( 'log_search' ) ) {
61  $this->error( "log_search does not exist" );
62 
63  return false;
64  }
65  $start = $db->selectField( 'logging', 'MIN(log_id)', '', __FUNCTION__ );
66  if ( !$start ) {
67  $this->output( "Nothing to do.\n" );
68 
69  return true;
70  }
71  $end = $db->selectField( 'logging', 'MAX(log_id)', '', __FUNCTION__ );
72 
73  // This maintenance script is for updating pre-1.16 to 1.16. The target_author_id and
74  // target_author_ip relations it adds will later be migrated to target_author_actor by
75  // migrateActors.php. If the schema is already 1.34, we should have nothing to do.
76  if ( !$db->fieldExists( 'logging', 'log_user' ) ) {
77  $this->output(
78  "This does not appear to be an upgrade from MediaWiki pre-1.16 "
79  . "(logging.log_user does not exist).\n"
80  );
81  $this->output( "Nothing to do.\n" );
82 
83  return true;
84  }
85 
86  # Do remaining chunk
87  $end += $batchSize - 1;
88  $blockStart = $start;
89  $blockEnd = $start + $batchSize - 1;
90  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
91 
92  $delTypes = [ 'delete', 'suppress' ]; // revisiondelete types
93  while ( $blockEnd <= $end ) {
94  $this->output( "...doing log_id from $blockStart to $blockEnd\n" );
95  $cond = "log_id BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd;
96  $res = $db->select(
97  'logging', [ 'log_id', 'log_type', 'log_action', 'log_params' ], $cond, __FUNCTION__
98  );
99  foreach ( $res as $row ) {
100  // RevisionDelete logs - revisions
101  if ( LogEventsList::typeAction( $row, $delTypes, 'revision' ) ) {
102  $params = LogPage::extractParams( $row->log_params );
103  // Param format: <urlparam> <item CSV> [<ofield> <nfield>]
104  if ( count( $params ) < 2 ) {
105  continue; // bad row?
106  }
107  $field = RevisionDeleter::getRelationType( $params[0] );
108  // B/C, the params may start with a title key (<title> <urlparam> <CSV>)
109  if ( $field == null ) {
110  array_shift( $params ); // remove title param
111  $field = RevisionDeleter::getRelationType( $params[0] );
112  if ( $field == null ) {
113  $this->output( "Invalid param type for {$row->log_id}\n" );
114  continue; // skip this row
115  } else {
116  // Clean up the row...
117  $db->update( 'logging',
118  [ 'log_params' => implode( ',', $params ) ],
119  [ 'log_id' => $row->log_id ] );
120  }
121  }
122  $items = explode( ',', $params[1] );
123  $log = new LogPage( $row->log_type );
124  // Add item relations...
125  $log->addRelations( $field, $items, $row->log_id );
126  // Determine what table to query...
127  $prefix = substr( $field, 0, strpos( $field, '_' ) ); // db prefix
128  if ( !isset( self::$tableMap[$prefix] ) ) {
129  continue; // bad row?
130  }
131  $table = self::$tableMap[$prefix];
132  $userField = $prefix . '_user';
133  $userTextField = $prefix . '_user_text';
134  // Add item author relations...
135  $userIds = $userIPs = [];
136  $sres = $db->select( $table,
137  [ $userField, $userTextField ],
138  [ $field => $items ]
139  );
140  foreach ( $sres as $srow ) {
141  if ( $srow->$userField > 0 ) {
142  $userIds[] = intval( $srow->$userField );
143  } elseif ( $srow->$userTextField != '' ) {
144  $userIPs[] = $srow->$userTextField;
145  }
146  }
147  // Add item author relations...
148  $log->addRelations( 'target_author_id', $userIds, $row->log_id );
149  $log->addRelations( 'target_author_ip', $userIPs, $row->log_id );
150  } elseif ( LogEventsList::typeAction( $row, $delTypes, 'event' ) ) {
151  // RevisionDelete logs - log events
152  $params = LogPage::extractParams( $row->log_params );
153  // Param format: <item CSV> [<ofield> <nfield>]
154  if ( count( $params ) < 1 ) {
155  continue; // bad row
156  }
157  $items = explode( ',', $params[0] );
158  $log = new LogPage( $row->log_type );
159  // Add item relations...
160  $log->addRelations( 'log_id', $items, $row->log_id );
161  // Add item author relations...
162  $userIds = $userIPs = [];
163  $sres = $db->select( 'logging',
164  [ 'log_user', 'log_user_text' ],
165  [ 'log_id' => $items ]
166  );
167  foreach ( $sres as $srow ) {
168  if ( $srow->log_user > 0 ) {
169  $userIds[] = intval( $srow->log_user );
170  } elseif ( Wikimedia\IPUtils::isIPAddress( $srow->log_user_text ) ) {
171  $userIPs[] = $srow->log_user_text;
172  }
173  }
174  $log->addRelations( 'target_author_id', $userIds, $row->log_id );
175  $log->addRelations( 'target_author_ip', $userIPs, $row->log_id );
176  }
177  }
178  $blockStart += $batchSize;
179  $blockEnd += $batchSize;
180  $lbFactory->waitForReplication();
181  }
182  $this->output( "Done populating log_search table.\n" );
183 
184  return true;
185  }
186 }
187 
188 $maintClass = PopulateLogSearch::class;
189 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:146
LogPage\extractParams
static extractParams( $blob)
Extract a parameter array from a blob.
Definition: LogPage.php:421
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:323
$res
$res
Definition: testCompression.php:57
PopulateLogSearch\__construct
__construct()
Default constructor.
Definition: populateLogSearch.php:43
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: LoggedUpdateMaintenance.php:26
$maintClass
$maintClass
Definition: populateLogSearch.php:188
LogEventsList\typeAction
static typeAction( $row, $type, $action, $right='')
Definition: LogEventsList.php:512
LogPage
Class to simplify the use of log pages.
Definition: LogPage.php:33
DB_MASTER
const DB_MASTER
Definition: defines.php:26
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1350
PopulateLogSearch\updateSkippedMessage
updateSkippedMessage()
Message to show that the update was done already and was just skipped.
Definition: populateLogSearch.php:53
PopulateLogSearch
Maintenance script that makes the required database updates for populating the log_search table retro...
Definition: populateLogSearch.php:35
RevisionDeleter\getRelationType
static getRelationType( $typeName)
Get DB field name for URL param...
Definition: RevisionDeleter.php:156
PopulateLogSearch\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: populateLogSearch.php:57
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:362
PopulateLogSearch\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: populateLogSearch.php:49
Wikimedia
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:457
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:429
PopulateLogSearch\$tableMap
static $tableMap
Definition: populateLogSearch.php:36
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:370