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