Update IPSet use statements
[lhc/web/wiklou.git] / maintenance / populateLogSearch.php
1 <?php
2 /**
3 * Makes the required database updates for populating the
4 * log_search table retroactively
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 * @ingroup Maintenance
23 */
24
25 require_once __DIR__ . '/Maintenance.php';
26
27 /**
28 * Maintenance script that makes the required database updates for populating the
29 * log_search table retroactively
30 *
31 * @ingroup Maintenance
32 */
33 class PopulateLogSearch extends LoggedUpdateMaintenance {
34 private static $tableMap = [
35 'rev' => 'revision',
36 'fa' => 'filearchive',
37 'oi' => 'oldimage',
38 'ar' => 'archive'
39 ];
40
41 public function __construct() {
42 parent::__construct();
43 $this->addDescription( 'Migrate log params to new table and index for searching' );
44 $this->setBatchSize( 100 );
45 }
46
47 protected function getUpdateKey() {
48 return 'populate log_search';
49 }
50
51 protected function updateSkippedMessage() {
52 return 'log_search table already populated.';
53 }
54
55 protected function doDBUpdates() {
56 $batchSize = $this->getBatchSize();
57 $db = $this->getDB( DB_MASTER );
58 if ( !$db->tableExists( 'log_search' ) ) {
59 $this->error( "log_search does not exist" );
60
61 return false;
62 }
63 $start = $db->selectField( 'logging', 'MIN(log_id)', false, __FUNCTION__ );
64 if ( !$start ) {
65 $this->output( "Nothing to do.\n" );
66
67 return true;
68 }
69 $end = $db->selectField( 'logging', 'MAX(log_id)', false, __FUNCTION__ );
70
71 # Do remaining chunk
72 $end += $batchSize - 1;
73 $blockStart = $start;
74 $blockEnd = $start + $batchSize - 1;
75
76 $delTypes = [ 'delete', 'suppress' ]; // revisiondelete types
77 while ( $blockEnd <= $end ) {
78 $this->output( "...doing log_id from $blockStart to $blockEnd\n" );
79 $cond = "log_id BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd;
80 $res = $db->select(
81 'logging', [ 'log_id', 'log_type', 'log_action', 'log_params' ], $cond, __FUNCTION__
82 );
83 foreach ( $res as $row ) {
84 // RevisionDelete logs - revisions
85 if ( LogEventsList::typeAction( $row, $delTypes, 'revision' ) ) {
86 $params = LogPage::extractParams( $row->log_params );
87 // Param format: <urlparam> <item CSV> [<ofield> <nfield>]
88 if ( count( $params ) < 2 ) {
89 continue; // bad row?
90 }
91 $field = RevisionDeleter::getRelationType( $params[0] );
92 // B/C, the params may start with a title key (<title> <urlparam> <CSV>)
93 if ( $field == null ) {
94 array_shift( $params ); // remove title param
95 $field = RevisionDeleter::getRelationType( $params[0] );
96 if ( $field == null ) {
97 $this->output( "Invalid param type for {$row->log_id}\n" );
98 continue; // skip this row
99 } else {
100 // Clean up the row...
101 $db->update( 'logging',
102 [ 'log_params' => implode( ',', $params ) ],
103 [ 'log_id' => $row->log_id ] );
104 }
105 }
106 $items = explode( ',', $params[1] );
107 $log = new LogPage( $row->log_type );
108 // Add item relations...
109 $log->addRelations( $field, $items, $row->log_id );
110 // Determine what table to query...
111 $prefix = substr( $field, 0, strpos( $field, '_' ) ); // db prefix
112 if ( !isset( self::$tableMap[$prefix] ) ) {
113 continue; // bad row?
114 }
115 $table = self::$tableMap[$prefix];
116 $userField = $prefix . '_user';
117 $userTextField = $prefix . '_user_text';
118 // Add item author relations...
119 $userIds = $userIPs = [];
120 $sres = $db->select( $table,
121 [ $userField, $userTextField ],
122 [ $field => $items ]
123 );
124 foreach ( $sres as $srow ) {
125 if ( $srow->$userField > 0 ) {
126 $userIds[] = intval( $srow->$userField );
127 } elseif ( $srow->$userTextField != '' ) {
128 $userIPs[] = $srow->$userTextField;
129 }
130 }
131 // Add item author relations...
132 $log->addRelations( 'target_author_id', $userIds, $row->log_id );
133 $log->addRelations( 'target_author_ip', $userIPs, $row->log_id );
134 } elseif ( LogEventsList::typeAction( $row, $delTypes, 'event' ) ) {
135 // RevisionDelete logs - log events
136 $params = LogPage::extractParams( $row->log_params );
137 // Param format: <item CSV> [<ofield> <nfield>]
138 if ( count( $params ) < 1 ) {
139 continue; // bad row
140 }
141 $items = explode( ',', $params[0] );
142 $log = new LogPage( $row->log_type );
143 // Add item relations...
144 $log->addRelations( 'log_id', $items, $row->log_id );
145 // Add item author relations...
146 $userIds = $userIPs = [];
147 $sres = $db->select( 'logging',
148 [ 'log_user', 'log_user_text' ],
149 [ 'log_id' => $items ]
150 );
151 foreach ( $sres as $srow ) {
152 if ( $srow->log_user > 0 ) {
153 $userIds[] = intval( $srow->log_user );
154 } elseif ( IP::isIPAddress( $srow->log_user_text ) ) {
155 $userIPs[] = $srow->log_user_text;
156 }
157 }
158 $log->addRelations( 'target_author_id', $userIds, $row->log_id );
159 $log->addRelations( 'target_author_ip', $userIPs, $row->log_id );
160 }
161 }
162 $blockStart += $batchSize;
163 $blockEnd += $batchSize;
164 wfWaitForSlaves();
165 }
166 $this->output( "Done populating log_search table.\n" );
167
168 return true;
169 }
170 }
171
172 $maintClass = "PopulateLogSearch";
173 require_once RUN_MAINTENANCE_IF_MAIN;