MediaWiki REL1_35
updateRestrictions.php
Go to the documentation of this file.
1<?php
27require_once __DIR__ . '/Maintenance.php';
28
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 $dbw = $this->getDB( DB_MASTER );
44 $batchSize = $this->getBatchSize();
45
46 if ( !$dbw->tableExists( 'page_restrictions', __METHOD__ ) ) {
47 $this->fatalError( "page_restrictions table does not exist" );
48 }
49
50 $encodedExpiry = $dbw->getInfinity();
51
52 $maxPageId = $dbw->selectField( 'page', 'MAX(page_id)', '', __METHOD__ );
53 $escapedEmptyBlobValue = $dbw->addQuotes( '' );
54
55 $batchMinPageId = 0;
56
57 do {
58 $batchMaxPageId = $batchMinPageId + $batchSize;
59
60 $this->output( "...processing page IDs from $batchMinPageId to $batchMaxPageId.\n" );
61
62 $res = $dbw->select(
63 'page',
64 [ 'page_id', 'page_restrictions' ],
65 [
66 "page_restrictions != $escapedEmptyBlobValue",
67 'page_id > ' . $dbw->addQuotes( $batchMinPageId ),
68 'page_id <= ' . $dbw->addQuotes( $batchMaxPageId ),
69 ],
70 __METHOD__
71 );
72
73 // No pages have legacy protection settings in the current batch
74 if ( !$res->numRows() ) {
75 $batchMinPageId = $batchMaxPageId;
76 continue;
77 }
78
79 $batch = [];
80 $pageIds = [];
81
82 foreach ( $res as $row ) {
83 $pageIds[] = $row->page_id;
84
85 $restrictionsByAction = $this->mapLegacyRestrictionBlob( $row->page_restrictions );
86
87 # Update restrictions table
88 foreach ( $restrictionsByAction as $action => $restrictions ) {
89 $batch[] = [
90 'pr_page' => $row->page_id,
91 'pr_type' => $action,
92 'pr_level' => $restrictions,
93 'pr_cascade' => 0,
94 'pr_expiry' => $encodedExpiry
95 ];
96 }
97 }
98
99 $this->beginTransaction( $dbw, __METHOD__ );
100
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
103 $dbw->insert(
104 'page_restrictions',
105 $batch,
106 __METHOD__,
107 [ 'IGNORE' ]
108 );
109
110 // Clear out the legacy page.page_restrictions blob for this batch
111 $dbw->update( 'page', [ 'page_restrictions' => '' ], [ 'page_id' => $pageIds ], __METHOD__ );
112
113 $this->commitTransaction( $dbw, __METHOD__ );
114
115 $batchMinPageId = $batchMaxPageId;
116 } while ( $batchMaxPageId < $maxPageId );
117
118 $this->output( "...Done!\n" );
119 }
120
129 private function mapLegacyRestrictionBlob( $legacyBlob ) {
130 $oldRestrictions = [];
131
132 foreach ( explode( ':', trim( $legacyBlob ) ) as $restrict ) {
133 $temp = explode( '=', trim( $restrict ) );
134
135 // Treat old old format without action name as edit/move restriction
136 if ( count( $temp ) == 1 ) {
137 $level = trim( $temp[0] );
138
139 $oldRestrictions['edit'] = $level;
140 $oldRestrictions['move'] = $level;
141 } else {
142 $restriction = trim( $temp[1] );
143 // Some old entries are empty
144 if ( $restriction != '' ) {
145 $oldRestrictions[$temp[0]] = $restriction;
146 }
147 }
148 }
149
150 return $oldRestrictions;
151 }
152}
153
154$maintClass = UpdateRestrictions::class;
155require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
const RUN_MAINTENANCE_IF_MAIN
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
Set the batch size.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Maintenance script that updates page_restrictions table from old page_restriction column.
__construct()
Default constructor.
execute()
Do the actual work.
mapLegacyRestrictionBlob( $legacyBlob)
Convert a legacy restriction specification from the page.page_restrictions blob to a map of action na...
const DB_MASTER
Definition defines.php:29