Stylize maintenance folder..
[lhc/web/wiklou.git] / maintenance / updateRestrictions.php
1 <?php
2 /**
3 * Makes the required database updates for Special:ProtectedPages
4 * to show all protected pages, even ones before the page restrictions
5 * schema change. All remaining page_restriction column values are moved
6 * to the new table.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @ingroup Maintenance
24 */
25
26 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
27
28 class UpdateRestrictions extends Maintenance {
29 public function __construct() {
30 parent::__construct();
31 $this->mDescription = "Updates page_restrictions table from old page_restriction column";
32 $this->setBatchSize( 100 );
33 }
34
35 public function execute() {
36 $db = wfGetDB( DB_MASTER );
37 if ( !$db->tableExists( 'page_restrictions' ) ) {
38 $this->error( "page_restrictions table does not exist", true );
39 }
40
41 $start = $db->selectField( 'page', 'MIN(page_id)', false, __METHOD__ );
42 if ( !$start ) {
43 $this->error( "Nothing to do.", true );
44 }
45 $end = $db->selectField( 'page', 'MAX(page_id)', false, __METHOD__ );
46
47 # Do remaining chunk
48 $end += $this->mBatchSize - 1;
49 $blockStart = $start;
50 $blockEnd = $start + $this->mBatchSize - 1;
51 $encodedExpiry = 'infinity';
52 while ( $blockEnd <= $end ) {
53 $this->output( "...doing page_id from $blockStart to $blockEnd\n" );
54 $cond = "page_id BETWEEN $blockStart AND $blockEnd AND page_restrictions !=''";
55 $res = $db->select( 'page', array( 'page_id', 'page_namespace', 'page_restrictions' ), $cond, __METHOD__ );
56 $batch = array();
57 foreach ( $res as $row ) {
58 $oldRestrictions = array();
59 foreach ( explode( ':', trim( $row->page_restrictions ) ) as $restrict ) {
60 $temp = explode( '=', trim( $restrict ) );
61 // Make sure we are not settings restrictions to ""
62 if ( count( $temp ) == 1 && $temp[0] ) {
63 // old old format should be treated as edit/move restriction
64 $oldRestrictions["edit"] = trim( $temp[0] );
65 $oldRestrictions["move"] = trim( $temp[0] );
66 } else if ( $temp[1] ) {
67 $oldRestrictions[$temp[0]] = trim( $temp[1] );
68 }
69 }
70 # Clear invalid columns
71 if ( $row->page_namespace == NS_MEDIAWIKI ) {
72 $db->update( 'page', array( 'page_restrictions' => '' ),
73 array( 'page_id' => $row->page_id ), __FUNCTION__ );
74 $this->output( "...removed dead page_restrictions column for page {$row->page_id}\n" );
75 }
76 # Update restrictions table
77 foreach ( $oldRestrictions as $action => $restrictions ) {
78 $batch[] = array(
79 'pr_page' => $row->page_id,
80 'pr_type' => $action,
81 'pr_level' => $restrictions,
82 'pr_cascade' => 0,
83 'pr_expiry' => $encodedExpiry
84 );
85 }
86 }
87 # We use insert() and not replace() as Article.php replaces
88 # page_restrictions with '' when protected in the restrictions table
89 if ( count( $batch ) ) {
90 $ok = $db->deadlockLoop( array( $db, 'insert' ), 'page_restrictions',
91 $batch, __FUNCTION__, array( 'IGNORE' ) );
92 if ( !$ok ) {
93 throw new MWException( "Deadlock loop failed wtf :(" );
94 }
95 }
96 $blockStart += $this->mBatchSize - 1;
97 $blockEnd += $this->mBatchSize - 1;
98 wfWaitForSlaves( 5 );
99 }
100 $this->output( "...removing dead rows from page_restrictions\n" );
101 // Kill any broken rows from previous imports
102 $db->delete( 'page_restrictions', array( 'pr_level' => '' ) );
103 // Kill other invalid rows
104 $db->deleteJoin( 'page_restrictions', 'page', 'pr_page', 'page_id', array( 'page_namespace' => NS_MEDIAWIKI ) );
105 $this->output( "...Done!\n" );
106 }
107 }
108
109 $maintClass = "UpdateRestrictions";
110 require_once( DO_MAINTENANCE );