Use TOCData methods to process new headings
[mediawiki.git] / maintenance / updateRestrictions.php
blobe5a9c206d2ccbcad4d1c866572acf7569519a73d
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.
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.
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.
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
23 * @file
24 * @ingroup Maintenance
27 require_once __DIR__ . '/Maintenance.php';
29 /**
30 * Maintenance script that updates page_restrictions table from
31 * old page_restriction column.
33 * @ingroup Maintenance
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 );
42 public function execute() {
43 $dbw = $this->getDB( DB_PRIMARY );
44 $batchSize = $this->getBatchSize();
46 if ( !$dbw->tableExists( 'page_restrictions', __METHOD__ ) ) {
47 $this->fatalError( "page_restrictions table does not exist" );
50 if ( !$dbw->fieldExists( 'page', 'page_restrictions' ) ) {
51 $this->output( "Migration is not needed.\n" );
52 return true;
55 $encodedExpiry = $dbw->getInfinity();
57 $maxPageId = $dbw->selectField( 'page', 'MAX(page_id)', '', __METHOD__ );
58 $escapedEmptyBlobValue = $dbw->addQuotes( '' );
60 $batchMinPageId = 0;
62 do {
63 $batchMaxPageId = $batchMinPageId + $batchSize;
65 $this->output( "...processing page IDs from $batchMinPageId to $batchMaxPageId.\n" );
67 $res = $dbw->select(
68 'page',
69 [ 'page_id', 'page_restrictions' ],
71 "page_restrictions != $escapedEmptyBlobValue",
72 'page_id > ' . $dbw->addQuotes( $batchMinPageId ),
73 'page_id <= ' . $dbw->addQuotes( $batchMaxPageId ),
75 __METHOD__
78 // No pages have legacy protection settings in the current batch
79 if ( !$res->numRows() ) {
80 $batchMinPageId = $batchMaxPageId;
81 continue;
84 $batch = [];
85 $pageIds = [];
87 foreach ( $res as $row ) {
88 $pageIds[] = $row->page_id;
90 $restrictionsByAction = $this->mapLegacyRestrictionBlob( $row->page_restrictions );
92 # Update restrictions table
93 foreach ( $restrictionsByAction as $action => $restrictions ) {
94 $batch[] = [
95 'pr_page' => $row->page_id,
96 'pr_type' => $action,
97 'pr_level' => $restrictions,
98 'pr_cascade' => 0,
99 'pr_expiry' => $encodedExpiry
104 $this->beginTransaction( $dbw, __METHOD__ );
106 // Insert new format protection settings for the pages in the current batch.
107 // Use INSERT IGNORE to ignore conflicts with new format settings that might exist for the page
108 $dbw->insert(
109 'page_restrictions',
110 $batch,
111 __METHOD__,
112 [ 'IGNORE' ]
115 // Clear out the legacy page.page_restrictions blob for this batch
116 $dbw->update( 'page', [ 'page_restrictions' => '' ], [ 'page_id' => $pageIds ], __METHOD__ );
118 $this->commitTransaction( $dbw, __METHOD__ );
120 $batchMinPageId = $batchMaxPageId;
121 } while ( $batchMaxPageId < $maxPageId );
123 $this->output( "...Done!\n" );
124 return true;
128 * Convert a legacy restriction specification from the page.page_restrictions blob to
129 * a map of action names to restriction levels.
131 * @param string $legacyBlob Legacy page.page_restrictions blob,
132 * e.g. "sysop" or "edit=sysop:move=autoconfirmed"
133 * @return string[] array of restriction levels keyed by action names
135 private function mapLegacyRestrictionBlob( $legacyBlob ) {
136 $oldRestrictions = [];
138 foreach ( explode( ':', trim( $legacyBlob ) ) as $restrict ) {
139 $temp = explode( '=', trim( $restrict ) );
141 // Treat old old format without action name as edit/move restriction
142 if ( count( $temp ) == 1 ) {
143 $level = trim( $temp[0] );
145 $oldRestrictions['edit'] = $level;
146 $oldRestrictions['move'] = $level;
147 } else {
148 $restriction = trim( $temp[1] );
149 // Some old entries are empty
150 if ( $restriction != '' ) {
151 $oldRestrictions[$temp[0]] = $restriction;
156 return $oldRestrictions;
160 $maintClass = UpdateRestrictions::class;
161 require_once RUN_MAINTENANCE_IF_MAIN;