Merge "Call $wgContLang->findVariantLink() in {{PAGESINCATEGORY: }}"
[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 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 ) {
79 continue; // bad row?
80 }
81 $field = RevisionDeleter::getRelationType( $params[0] );
82 // B/C, the params may start with a title key (<title> <urlparam> <CSV>)
83 if ( $field == null ) {
84 array_shift( $params ); // remove title param
85 $field = RevisionDeleter::getRelationType( $params[0] );
86 if ( $field == null ) {
87 $this->output( "Invalid param type for {$row->log_id}\n" );
88 continue; // skip this row
89 } else {
90 // Clean up the row...
91 $db->update( 'logging',
92 array( 'log_params' => implode( ',', $params ) ),
93 array( 'log_id' => $row->log_id ) );
94 }
95 }
96 $items = explode( ',', $params[1] );
97 $log = new LogPage( $row->log_type );
98 // Add item relations...
99 $log->addRelations( $field, $items, $row->log_id );
100 // Determine what table to query...
101 $prefix = substr( $field, 0, strpos( $field, '_' ) ); // db prefix
102 if ( !isset( self::$tableMap[$prefix] ) ) {
103 continue; // bad row?
104 }
105 $table = self::$tableMap[$prefix];
106 $userField = $prefix . '_user';
107 $userTextField = $prefix . '_user_text';
108 // Add item author relations...
109 $userIds = $userIPs = array();
110 $sres = $db->select( $table,
111 array( $userField, $userTextField ),
112 array( $field => $items )
113 );
114 foreach ( $sres as $srow ) {
115 if ( $srow->$userField > 0 ) {
116 $userIds[] = intval( $srow->$userField );
117 } elseif ( $srow->$userTextField != '' ) {
118 $userIPs[] = $srow->$userTextField;
119 }
120 }
121 // Add item author relations...
122 $log->addRelations( 'target_author_id', $userIds, $row->log_id );
123 $log->addRelations( 'target_author_ip', $userIPs, $row->log_id );
124 // RevisionDelete logs - log events
125 } elseif ( LogEventsList::typeAction( $row, $delTypes, 'event' ) ) {
126 $params = LogPage::extractParams( $row->log_params );
127 // Param format: <item CSV> [<ofield> <nfield>]
128 if ( count( $params ) < 1 ) {
129 continue; // bad row
130 }
131 $items = explode( ',', $params[0] );
132 $log = new LogPage( $row->log_type );
133 // Add item relations...
134 $log->addRelations( 'log_id', $items, $row->log_id );
135 // Add item author relations...
136 $userIds = $userIPs = array();
137 $sres = $db->select( 'logging',
138 array( 'log_user', 'log_user_text' ),
139 array( 'log_id' => $items )
140 );
141 foreach ( $sres as $srow ) {
142 if ( $srow->log_user > 0 ) {
143 $userIds[] = intval( $srow->log_user );
144 } elseif ( IP::isIPAddress( $srow->log_user_text ) ) {
145 $userIPs[] = $srow->log_user_text;
146 }
147 }
148 $log->addRelations( 'target_author_id', $userIds, $row->log_id );
149 $log->addRelations( 'target_author_ip', $userIPs, $row->log_id );
150 }
151 }
152 $blockStart += $this->mBatchSize;
153 $blockEnd += $this->mBatchSize;
154 wfWaitForSlaves();
155 }
156 $this->output( "Done populating log_search table.\n" );
157 return true;
158 }
159 }
160
161 $maintClass = "PopulateLogSearch";
162 require_once RUN_MAINTENANCE_IF_MAIN;