using .append() instead of innerHTML+=. The latter replaces the html and causes all...
[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 foreach ( $res as $row ) {
60 // RevisionDelete logs - revisions
61 if ( LogEventsList::typeAction( $row, $delTypes, 'revision' ) ) {
62 $params = LogPage::extractParams( $row->log_params );
63 // Param format: <urlparam> <item CSV> [<ofield> <nfield>]
64 if ( count( $params ) < 2 ) continue; // bad row?
65 $field = RevisionDeleter::getRelationType( $params[0] );
66 // B/C, the params may start with a title key (<title> <urlparam> <CSV>)
67 if ( $field == null ) {
68 array_shift( $params ); // remove title param
69 $field = RevisionDeleter::getRelationType( $params[0] );
70 if ( $field == null ) {
71 $this->output( "Invalid param type for {$row->log_id}\n" );
72 continue; // skip this row
73 } else {
74 // Clean up the row...
75 $db->update( 'logging',
76 array( 'log_params' => implode( ',', $params ) ),
77 array( 'log_id' => $row->log_id ) );
78 }
79 }
80 $items = explode( ',', $params[1] );
81 $log = new LogPage( $row->log_type );
82 // Add item relations...
83 $log->addRelations( $field, $items, $row->log_id );
84 // Determine what table to query...
85 $prefix = substr( $field, 0, strpos( $field, '_' ) ); // db prefix
86 if ( !isset( self::$tableMap[$prefix] ) )
87 continue; // bad row?
88 $table = self::$tableMap[$prefix];
89 $userField = $prefix . '_user';
90 $userTextField = $prefix . '_user_text';
91 // Add item author relations...
92 $userIds = $userIPs = array();
93 $sres = $db->select( $table,
94 array( $userField, $userTextField ),
95 array( $field => $items )
96 );
97 foreach ( $sres as $srow ) {
98 if ( $srow->$userField > 0 )
99 $userIds[] = intval( $srow->$userField );
100 else if ( $srow->$userTextField != '' )
101 $userIPs[] = $srow->$userTextField;
102 }
103 // Add item author relations...
104 $log->addRelations( 'target_author_id', $userIds, $row->log_id );
105 $log->addRelations( 'target_author_ip', $userIPs, $row->log_id );
106 // RevisionDelete logs - log events
107 } else if ( LogEventsList::typeAction( $row, $delTypes, 'event' ) ) {
108 $params = LogPage::extractParams( $row->log_params );
109 // Param format: <item CSV> [<ofield> <nfield>]
110 if ( count( $params ) < 1 ) continue; // bad row
111 $items = explode( ',', $params[0] );
112 $log = new LogPage( $row->log_type );
113 // Add item relations...
114 $log->addRelations( 'log_id', $items, $row->log_id );
115 // Add item author relations...
116 $userIds = $userIPs = array();
117 $sres = $db->select( 'logging',
118 array( 'log_user', 'log_user_text' ),
119 array( 'log_id' => $items )
120 );
121 foreach ( $sres as $srow ) {
122 if ( $srow->log_user > 0 )
123 $userIds[] = intval( $srow->log_user );
124 else if ( IP::isIPAddress( $srow->log_user_text ) )
125 $userIPs[] = $srow->log_user_text;
126 }
127 $log->addRelations( 'target_author_id', $userIds, $row->log_id );
128 $log->addRelations( 'target_author_ip', $userIPs, $row->log_id );
129 }
130 }
131 $blockStart += self::LOG_SEARCH_BATCH_SIZE;
132 $blockEnd += self::LOG_SEARCH_BATCH_SIZE;
133 wfWaitForSlaves( 5 );
134 }
135 if ( $db->insert(
136 'updatelog',
137 array( 'ul_key' => 'populate log_search' ),
138 __FUNCTION__,
139 'IGNORE'
140 )
141 ) {
142 $this->output( "log_search population complete.\n" );
143 return true;
144 } else {
145 $this->output( "Could not insert log_search population row.\n" );
146 return false;
147 }
148 }
149 }
150
151 $maintClass = "PopulateLogSearch";
152 require_once( DO_MAINTENANCE );