Merge "[FileBackend] Bumped Swift auth caching TTL."
[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 * @file
24 * @ingroup Maintenance
25 */
26
27 require_once( __DIR__ . '/Maintenance.php' );
28
29 class UpdateRestrictions extends Maintenance {
30 public function __construct() {
31 parent::__construct();
32 $this->mDescription = "Updates page_restrictions table from old page_restriction column";
33 $this->setBatchSize( 100 );
34 }
35
36 public function execute() {
37 $db = wfGetDB( DB_MASTER );
38 if ( !$db->tableExists( 'page_restrictions' ) ) {
39 $this->error( "page_restrictions table does not exist", true );
40 }
41
42 $start = $db->selectField( 'page', 'MIN(page_id)', false, __METHOD__ );
43 if ( !$start ) {
44 $this->error( "Nothing to do.", true );
45 }
46 $end = $db->selectField( 'page', 'MAX(page_id)', false, __METHOD__ );
47
48 # Do remaining chunk
49 $end += $this->mBatchSize - 1;
50 $blockStart = $start;
51 $blockEnd = $start + $this->mBatchSize - 1;
52 $encodedExpiry = 'infinity';
53 while ( $blockEnd <= $end ) {
54 $this->output( "...doing page_id from $blockStart to $blockEnd\n" );
55 $cond = "page_id BETWEEN $blockStart AND $blockEnd AND page_restrictions !=''";
56 $res = $db->select( 'page', array( 'page_id', 'page_namespace', 'page_restrictions' ), $cond, __METHOD__ );
57 $batch = array();
58 foreach ( $res as $row ) {
59 $oldRestrictions = array();
60 foreach ( explode( ':', trim( $row->page_restrictions ) ) as $restrict ) {
61 $temp = explode( '=', trim( $restrict ) );
62 // Make sure we are not settings restrictions to ""
63 if ( count( $temp ) == 1 && $temp[0] ) {
64 // old old format should be treated as edit/move restriction
65 $oldRestrictions["edit"] = trim( $temp[0] );
66 $oldRestrictions["move"] = trim( $temp[0] );
67 } elseif ( $temp[1] ) {
68 $oldRestrictions[$temp[0]] = trim( $temp[1] );
69 }
70 }
71 # Clear invalid columns
72 if ( $row->page_namespace == NS_MEDIAWIKI ) {
73 $db->update( 'page', array( 'page_restrictions' => '' ),
74 array( 'page_id' => $row->page_id ), __FUNCTION__ );
75 $this->output( "...removed dead page_restrictions column for page {$row->page_id}\n" );
76 }
77 # Update restrictions table
78 foreach ( $oldRestrictions as $action => $restrictions ) {
79 $batch[] = array(
80 'pr_page' => $row->page_id,
81 'pr_type' => $action,
82 'pr_level' => $restrictions,
83 'pr_cascade' => 0,
84 'pr_expiry' => $encodedExpiry
85 );
86 }
87 }
88 # We use insert() and not replace() as Article.php replaces
89 # page_restrictions with '' when protected in the restrictions table
90 if ( count( $batch ) ) {
91 $ok = $db->deadlockLoop( array( $db, 'insert' ), 'page_restrictions',
92 $batch, __FUNCTION__, array( 'IGNORE' ) );
93 if ( !$ok ) {
94 throw new MWException( "Deadlock loop failed wtf :(" );
95 }
96 }
97 $blockStart += $this->mBatchSize - 1;
98 $blockEnd += $this->mBatchSize - 1;
99 wfWaitForSlaves();
100 }
101 $this->output( "...removing dead rows from page_restrictions\n" );
102 // Kill any broken rows from previous imports
103 $db->delete( 'page_restrictions', array( 'pr_level' => '' ) );
104 // Kill other invalid rows
105 $db->deleteJoin( 'page_restrictions', 'page', 'pr_page', 'page_id', array( 'page_namespace' => NS_MEDIAWIKI ) );
106 $this->output( "...Done!\n" );
107 }
108 }
109
110 $maintClass = "UpdateRestrictions";
111 require_once( RUN_MAINTENANCE_IF_MAIN );