3 * Makes the required database updates for populating the
4 * log_search table retroactively
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
22 * @ingroup Maintenance
25 require_once __DIR__
. '/Maintenance.php';
28 * Maintenance script that makes the required database updates for populating the
29 * log_search table retroactively
31 * @ingroup Maintenance
33 class PopulateLogSearch
extends LoggedUpdateMaintenance
{
34 private static $tableMap = [
36 'fa' => 'filearchive',
41 public function __construct() {
42 parent
::__construct();
43 $this->addDescription( 'Migrate log params to new table and index for searching' );
44 $this->setBatchSize( 100 );
47 protected function getUpdateKey() {
48 return 'populate log_search';
51 protected function updateSkippedMessage() {
52 return 'log_search table already populated.';
55 protected function doDBUpdates() {
56 $db = $this->getDB( DB_MASTER
);
57 if ( !$db->tableExists( 'log_search' ) ) {
58 $this->error( "log_search does not exist" );
62 $start = $db->selectField( 'logging', 'MIN(log_id)', false, __FUNCTION__
);
64 $this->output( "Nothing to do.\n" );
68 $end = $db->selectField( 'logging', 'MAX(log_id)', false, __FUNCTION__
);
71 $end +
= $this->mBatchSize
- 1;
73 $blockEnd = $start +
$this->mBatchSize
- 1;
75 $delTypes = [ 'delete', 'suppress' ]; // revisiondelete types
76 while ( $blockEnd <= $end ) {
77 $this->output( "...doing log_id from $blockStart to $blockEnd\n" );
78 $cond = "log_id BETWEEN $blockStart AND $blockEnd";
80 'logging', [ 'log_id', 'log_type', 'log_action', 'log_params' ], $cond, __FUNCTION__
82 foreach ( $res as $row ) {
83 // RevisionDelete logs - revisions
84 if ( LogEventsList
::typeAction( $row, $delTypes, 'revision' ) ) {
85 $params = LogPage
::extractParams( $row->log_params
);
86 // Param format: <urlparam> <item CSV> [<ofield> <nfield>]
87 if ( count( $params ) < 2 ) {
90 $field = RevisionDeleter
::getRelationType( $params[0] );
91 // B/C, the params may start with a title key (<title> <urlparam> <CSV>)
92 if ( $field == null ) {
93 array_shift( $params ); // remove title param
94 $field = RevisionDeleter
::getRelationType( $params[0] );
95 if ( $field == null ) {
96 $this->output( "Invalid param type for {$row->log_id}\n" );
97 continue; // skip this row
99 // Clean up the row...
100 $db->update( 'logging',
101 [ 'log_params' => implode( ',', $params ) ],
102 [ 'log_id' => $row->log_id
] );
105 $items = explode( ',', $params[1] );
106 $log = new LogPage( $row->log_type
);
107 // Add item relations...
108 $log->addRelations( $field, $items, $row->log_id
);
109 // Determine what table to query...
110 $prefix = substr( $field, 0, strpos( $field, '_' ) ); // db prefix
111 if ( !isset( self
::$tableMap[$prefix] ) ) {
112 continue; // bad row?
114 $table = self
::$tableMap[$prefix];
115 $userField = $prefix . '_user';
116 $userTextField = $prefix . '_user_text';
117 // Add item author relations...
118 $userIds = $userIPs = [];
119 $sres = $db->select( $table,
120 [ $userField, $userTextField ],
123 foreach ( $sres as $srow ) {
124 if ( $srow->$userField > 0 ) {
125 $userIds[] = intval( $srow->$userField );
126 } elseif ( $srow->$userTextField != '' ) {
127 $userIPs[] = $srow->$userTextField;
130 // Add item author relations...
131 $log->addRelations( 'target_author_id', $userIds, $row->log_id
);
132 $log->addRelations( 'target_author_ip', $userIPs, $row->log_id
);
133 } elseif ( LogEventsList
::typeAction( $row, $delTypes, 'event' ) ) {
134 // RevisionDelete logs - log events
135 $params = LogPage
::extractParams( $row->log_params
);
136 // Param format: <item CSV> [<ofield> <nfield>]
137 if ( count( $params ) < 1 ) {
140 $items = explode( ',', $params[0] );
141 $log = new LogPage( $row->log_type
);
142 // Add item relations...
143 $log->addRelations( 'log_id', $items, $row->log_id
);
144 // Add item author relations...
145 $userIds = $userIPs = [];
146 $sres = $db->select( 'logging',
147 [ 'log_user', 'log_user_text' ],
148 [ 'log_id' => $items ]
150 foreach ( $sres as $srow ) {
151 if ( $srow->log_user
> 0 ) {
152 $userIds[] = intval( $srow->log_user
);
153 } elseif ( IP
::isIPAddress( $srow->log_user_text
) ) {
154 $userIPs[] = $srow->log_user_text
;
157 $log->addRelations( 'target_author_id', $userIds, $row->log_id
);
158 $log->addRelations( 'target_author_ip', $userIPs, $row->log_id
);
161 $blockStart +
= $this->mBatchSize
;
162 $blockEnd +
= $this->mBatchSize
;
165 $this->output( "Done populating log_search table.\n" );
171 $maintClass = "PopulateLogSearch";
172 require_once RUN_MAINTENANCE_IF_MAIN
;