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