Merge "RCFilters: define consistent interface in ChangesListFilterGroup"
[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 $db = $this->getDB( DB_MASTER );
57 if ( !$db->tableExists( 'log_search' ) ) {
58 $this->error( "log_search does not exist" );
59
60 return false;
61 }
62 $start = $db->selectField( 'logging', 'MIN(log_id)', false, __FUNCTION__ );
63 if ( !$start ) {
64 $this->output( "Nothing to do.\n" );
65
66 return true;
67 }
68 $end = $db->selectField( 'logging', 'MAX(log_id)', false, __FUNCTION__ );
69
70 # Do remaining chunk
71 $end += $this->mBatchSize - 1;
72 $blockStart = $start;
73 $blockEnd = $start + $this->mBatchSize - 1;
74
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";
79 $res = $db->select(
80 'logging', [ 'log_id', 'log_type', 'log_action', 'log_params' ], $cond, __FUNCTION__
81 );
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 ) {
88 continue; // bad row?
89 }
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
98 } else {
99 // Clean up the row...
100 $db->update( 'logging',
101 [ 'log_params' => implode( ',', $params ) ],
102 [ 'log_id' => $row->log_id ] );
103 }
104 }
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?
113 }
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 ],
121 [ $field => $items ]
122 );
123 foreach ( $sres as $srow ) {
124 if ( $srow->$userField > 0 ) {
125 $userIds[] = intval( $srow->$userField );
126 } elseif ( $srow->$userTextField != '' ) {
127 $userIPs[] = $srow->$userTextField;
128 }
129 }
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 ) {
138 continue; // bad row
139 }
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 ]
149 );
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;
155 }
156 }
157 $log->addRelations( 'target_author_id', $userIds, $row->log_id );
158 $log->addRelations( 'target_author_ip', $userIPs, $row->log_id );
159 }
160 }
161 $blockStart += $this->mBatchSize;
162 $blockEnd += $this->mBatchSize;
163 wfWaitForSlaves();
164 }
165 $this->output( "Done populating log_search table.\n" );
166
167 return true;
168 }
169 }
170
171 $maintClass = "PopulateLogSearch";
172 require_once RUN_MAINTENANCE_IF_MAIN;