Merge "Remove 6 unused watchlist messages"
[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 = array(
35 'rev' => 'revision',
36 'fa' => 'filearchive',
37 'oi' => 'oldimage',
38 'ar' => 'archive'
39 );
40
41 public function __construct() {
42 parent::__construct();
43 $this->mDescription = "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 return false;
60 }
61 $start = $db->selectField( 'logging', 'MIN(log_id)', false, __FUNCTION__ );
62 if ( !$start ) {
63 $this->output( "Nothing to do.\n" );
64 return true;
65 }
66 $end = $db->selectField( 'logging', 'MAX(log_id)', false, __FUNCTION__ );
67
68 # Do remaining chunk
69 $end += $this->mBatchSize - 1;
70 $blockStart = $start;
71 $blockEnd = $start + $this->mBatchSize - 1;
72
73 $delTypes = array( 'delete', 'suppress' ); // revisiondelete types
74 while ( $blockEnd <= $end ) {
75 $this->output( "...doing log_id from $blockStart to $blockEnd\n" );
76 $cond = "log_id BETWEEN $blockStart AND $blockEnd";
77 $res = $db->select( 'logging', '*', $cond, __FUNCTION__ );
78 foreach ( $res as $row ) {
79 // RevisionDelete logs - revisions
80 if ( LogEventsList::typeAction( $row, $delTypes, 'revision' ) ) {
81 $params = LogPage::extractParams( $row->log_params );
82 // Param format: <urlparam> <item CSV> [<ofield> <nfield>]
83 if ( count( $params ) < 2 ) {
84 continue; // bad row?
85 }
86 $field = RevisionDeleter::getRelationType( $params[0] );
87 // B/C, the params may start with a title key (<title> <urlparam> <CSV>)
88 if ( $field == null ) {
89 array_shift( $params ); // remove title param
90 $field = RevisionDeleter::getRelationType( $params[0] );
91 if ( $field == null ) {
92 $this->output( "Invalid param type for {$row->log_id}\n" );
93 continue; // skip this row
94 } else {
95 // Clean up the row...
96 $db->update( 'logging',
97 array( 'log_params' => implode( ',', $params ) ),
98 array( 'log_id' => $row->log_id ) );
99 }
100 }
101 $items = explode( ',', $params[1] );
102 $log = new LogPage( $row->log_type );
103 // Add item relations...
104 $log->addRelations( $field, $items, $row->log_id );
105 // Determine what table to query...
106 $prefix = substr( $field, 0, strpos( $field, '_' ) ); // db prefix
107 if ( !isset( self::$tableMap[$prefix] ) ) {
108 continue; // bad row?
109 }
110 $table = self::$tableMap[$prefix];
111 $userField = $prefix . '_user';
112 $userTextField = $prefix . '_user_text';
113 // Add item author relations...
114 $userIds = $userIPs = array();
115 $sres = $db->select( $table,
116 array( $userField, $userTextField ),
117 array( $field => $items )
118 );
119 foreach ( $sres as $srow ) {
120 if ( $srow->$userField > 0 ) {
121 $userIds[] = intval( $srow->$userField );
122 } elseif ( $srow->$userTextField != '' ) {
123 $userIPs[] = $srow->$userTextField;
124 }
125 }
126 // Add item author relations...
127 $log->addRelations( 'target_author_id', $userIds, $row->log_id );
128 $log->addRelations( 'target_author_ip', $userIPs, $row->log_id );
129 // RevisionDelete logs - log events
130 } elseif ( LogEventsList::typeAction( $row, $delTypes, 'event' ) ) {
131 $params = LogPage::extractParams( $row->log_params );
132 // Param format: <item CSV> [<ofield> <nfield>]
133 if ( count( $params ) < 1 ) {
134 continue; // bad row
135 }
136 $items = explode( ',', $params[0] );
137 $log = new LogPage( $row->log_type );
138 // Add item relations...
139 $log->addRelations( 'log_id', $items, $row->log_id );
140 // Add item author relations...
141 $userIds = $userIPs = array();
142 $sres = $db->select( 'logging',
143 array( 'log_user', 'log_user_text' ),
144 array( 'log_id' => $items )
145 );
146 foreach ( $sres as $srow ) {
147 if ( $srow->log_user > 0 ) {
148 $userIds[] = intval( $srow->log_user );
149 } elseif ( IP::isIPAddress( $srow->log_user_text ) ) {
150 $userIPs[] = $srow->log_user_text;
151 }
152 }
153 $log->addRelations( 'target_author_id', $userIds, $row->log_id );
154 $log->addRelations( 'target_author_ip', $userIPs, $row->log_id );
155 }
156 }
157 $blockStart += $this->mBatchSize;
158 $blockEnd += $this->mBatchSize;
159 wfWaitForSlaves();
160 }
161 $this->output( "Done populating log_search table.\n" );
162 return true;
163 }
164 }
165
166 $maintClass = "PopulateLogSearch";
167 require_once RUN_MAINTENANCE_IF_MAIN;