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