Merge maintenance-work branch (now with less errors!):
[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( "Maintenance.php" );
25
26 class PopulateLogSearch extends Maintenance {
27
28 const LOG_SEARCH_BATCH_SIZE = 100;
29
30 public function __construct() {
31 parent::__construct();
32 $this->mDescription = "Migrate log params to new table and index for searching";
33 }
34
35 public function execute() {
36 $db = wfGetDB( DB_MASTER );
37 if ( !$db->tableExists( 'log_search' ) ) {
38 $this->error( "log_search does not exist\n", true );
39 }
40 $start = $db->selectField( 'logging', 'MIN(log_id)', false, __FUNCTION__ );
41 if( !$start ) {
42 $this->output( "Nothing to do.\n" );
43 return true;
44 }
45 $end = $db->selectField( 'logging', 'MAX(log_id)', false, __FUNCTION__ );
46
47 # Do remaining chunk
48 $end += self::LOG_SEARCH_BATCH_SIZE - 1;
49 $blockStart = $start;
50 $blockEnd = $start + self::LOG_SEARCH_BATCH_SIZE - 1;
51 while( $blockEnd <= $end ) {
52 $this->output( "...doing log_id from $blockStart to $blockEnd\n" );
53 $cond = "log_id BETWEEN $blockStart AND $blockEnd";
54 $res = $db->select( 'logging', '*', $cond, __FUNCTION__ );
55 $batch = array();
56 while( $row = $db->fetchObject( $res ) ) {
57 // RevisionDelete logs - revisions
58 if( LogEventsList::typeAction( $row, array('delete','suppress'), 'revision' ) ) {
59 $params = LogPage::extractParams( $row->log_params );
60 // Param format: <urlparam> <item CSV> [<ofield> <nfield>]
61 if( count($params) >= 2 ) {
62 $field = RevisionDeleter::getRelationType($params[0]);
63 // B/C, the params may start with a title key
64 if( $field == null ) {
65 array_shift($params);
66 $field = RevisionDeleter::getRelationType($params[0]);
67 }
68 if( $field == null ) {
69 $this->output( "Invalid param type for $row->log_id\n" );
70 continue; // skip this row
71 }
72 $items = explode(',',$params[1]);
73 $log = new LogPage( $row->log_type );
74 $log->addRelations( $field, $items, $row->log_id );
75 }
76 // RevisionDelete logs - log events
77 } else if( LogEventsList::typeAction( $row, array('delete','suppress'), 'event' ) ) {
78 $params = LogPage::extractParams( $row->log_params );
79 // Param format: <item CSV> [<ofield> <nfield>]
80 if( count($params) >= 1 ) {
81 $items = explode(',',$params[0]);
82 $log = new LogPage( $row->log_type );
83 $log->addRelations( 'log_id', $items, $row->log_id );
84 }
85 }
86 }
87 $blockStart += self::LOG_SEARCH_BATCH_SIZE;
88 $blockEnd += self::LOG_SEARCH_BATCH_SIZE;
89 wfWaitForSlaves( 5 );
90 }
91 if( $db->insert(
92 'updatelog',
93 array( 'ul_key' => 'populate log_search' ),
94 __FUNCTION__,
95 'IGNORE'
96 )
97 ) {
98 $this->output( "log_search population complete.\n" );
99 return true;
100 } else {
101 $this->output( "Could not insert log_search population row.\n" );
102 return false;
103 }
104 }
105 }
106
107 $maintClass = "PopulateLogSearch";
108 require_once( DO_MAINTENANCE );