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_MASTER
);
44 $batchSize = $this->getBatchSize();
46 if ( !$dbw->tableExists( 'page_restrictions', __METHOD__
) ) {
47 $this->fatalError( "page_restrictions table does not exist" );
50 $encodedExpiry = $dbw->getInfinity();
52 $maxPageId = $dbw->selectField( 'page', 'MAX(page_id)', '', __METHOD__
);
53 $escapedEmptyBlobValue = $dbw->addQuotes( '' );
58 $batchMaxPageId = $batchMinPageId +
$batchSize;
60 $this->output( "...processing page IDs from $batchMinPageId to $batchMaxPageId.\n" );
64 [ 'page_id', 'page_restrictions' ],
66 "page_restrictions != $escapedEmptyBlobValue",
67 'page_id > ' . $dbw->addQuotes( $batchMinPageId ),
68 'page_id <= ' . $dbw->addQuotes( $batchMaxPageId ),
73 // No pages have legacy protection settings in the current batch
74 if ( !$res->numRows() ) {
75 $batchMinPageId = $batchMaxPageId;
82 foreach ( $res as $row ) {
83 $pageIds[] = $row->page_id
;
85 $restrictionsByAction = $this->mapLegacyRestrictionBlob( $row->page_restrictions
);
87 # Update restrictions table
88 foreach ( $restrictionsByAction as $action => $restrictions ) {
90 'pr_page' => $row->page_id
,
92 'pr_level' => $restrictions,
94 'pr_expiry' => $encodedExpiry
99 $this->beginTransaction( $dbw, __METHOD__
);
101 // Insert new format protection settings for the pages in the current batch.
102 // Use INSERT IGNORE to ignore conflicts with new format settings that might exist for the page
110 // Clear out the legacy page.page_restrictions blob for this batch
111 $dbw->update( 'page', [ 'page_restrictions' => '' ], [ 'page_id' => $pageIds ], __METHOD__
);
113 $this->commitTransaction( $dbw, __METHOD__
);
115 $batchMinPageId = $batchMaxPageId;
116 } while ( $batchMaxPageId < $maxPageId );
118 $this->output( "...Done!\n" );
122 * Convert a legacy restriction specification from the page.page_restrictions blob to
123 * a map of action names to restriction levels.
125 * @param string $legacyBlob Legacy page.page_restrictions blob,
126 * e.g. "sysop" or "edit=sysop:move=autoconfirmed"
127 * @return string[] array of restriction levels keyed by action names
129 private function mapLegacyRestrictionBlob( $legacyBlob ) {
130 $oldRestrictions = [];
132 foreach ( explode( ':', trim( $legacyBlob ) ) as $restrict ) {
133 $temp = explode( '=', trim( $restrict ) );
135 // Treat old old format without action name as edit/move restriction
136 if ( count( $temp ) == 1 ) {
137 $level = trim( $temp[0] );
139 $oldRestrictions['edit'] = $level;
140 $oldRestrictions['move'] = $level;
142 $restriction = trim( $temp[1] );
143 // Some old entries are empty
144 if ( $restriction != '' ) {
145 $oldRestrictions[$temp[0]] = $restriction;
150 return $oldRestrictions;
154 $maintClass = UpdateRestrictions
::class;
155 require_once RUN_MAINTENANCE_IF_MAIN
;