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
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
24 * @ingroup Maintenance
27 require_once __DIR__
. '/Maintenance.php';
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" );
55 $encodedExpiry = $dbw->getInfinity();
57 $maxPageId = $dbw->selectField( 'page', 'MAX(page_id)', '', __METHOD__
);
58 $escapedEmptyBlobValue = $dbw->addQuotes( '' );
63 $batchMaxPageId = $batchMinPageId +
$batchSize;
65 $this->output( "...processing page IDs from $batchMinPageId to $batchMaxPageId.\n" );
69 [ 'page_id', 'page_restrictions' ],
71 "page_restrictions != $escapedEmptyBlobValue",
72 'page_id > ' . $dbw->addQuotes( $batchMinPageId ),
73 'page_id <= ' . $dbw->addQuotes( $batchMaxPageId ),
78 // No pages have legacy protection settings in the current batch
79 if ( !$res->numRows() ) {
80 $batchMinPageId = $batchMaxPageId;
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 ) {
95 'pr_page' => $row->page_id
,
97 'pr_level' => $restrictions,
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
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" );
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;
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
;