MediaWiki REL1_39
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_PRIMARY );
44 $batchSize = $this->getBatchSize();
45
46 if ( !$dbw->tableExists( 'page_restrictions', __METHOD__ ) ) {
47 $this->fatalError( "page_restrictions table does not exist" );
48 }
49
50 if ( !$dbw->fieldExists( 'page', 'page_restrictions' ) ) {
51 $this->output( "Migration is not needed.\n" );
52 return true;
53 }
54
55 $encodedExpiry = $dbw->getInfinity();
56
57 $maxPageId = $dbw->selectField( 'page', 'MAX(page_id)', '', __METHOD__ );
58 $escapedEmptyBlobValue = $dbw->addQuotes( '' );
59
60 $batchMinPageId = 0;
61
62 do {
63 $batchMaxPageId = $batchMinPageId + $batchSize;
64
65 $this->output( "...processing page IDs from $batchMinPageId to $batchMaxPageId.\n" );
66
67 $res = $dbw->select(
68 'page',
69 [ 'page_id', 'page_restrictions' ],
70 [
71 "page_restrictions != $escapedEmptyBlobValue",
72 'page_id > ' . $dbw->addQuotes( $batchMinPageId ),
73 'page_id <= ' . $dbw->addQuotes( $batchMaxPageId ),
74 ],
75 __METHOD__
76 );
77
78 // No pages have legacy protection settings in the current batch
79 if ( !$res->numRows() ) {
80 $batchMinPageId = $batchMaxPageId;
81 continue;
82 }
83
84 $batch = [];
85 $pageIds = [];
86
87 foreach ( $res as $row ) {
88 $pageIds[] = $row->page_id;
89
90 $restrictionsByAction = $this->mapLegacyRestrictionBlob( $row->page_restrictions );
91
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
100 ];
101 }
102 }
103
104 $this->beginTransaction( $dbw, __METHOD__ );
105
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' ]
113 );
114
115 // Clear out the legacy page.page_restrictions blob for this batch
116 $dbw->update( 'page', [ 'page_restrictions' => '' ], [ 'page_id' => $pageIds ], __METHOD__ );
117
118 $this->commitTransaction( $dbw, __METHOD__ );
119
120 $batchMinPageId = $batchMaxPageId;
121 } while ( $batchMaxPageId < $maxPageId );
122
123 $this->output( "...Done!\n" );
124 return true;
125 }
126
135 private function mapLegacyRestrictionBlob( $legacyBlob ) {
136 $oldRestrictions = [];
137
138 foreach ( explode( ':', trim( $legacyBlob ) ) as $restrict ) {
139 $temp = explode( '=', trim( $restrict ) );
140
141 // Treat old old format without action name as edit/move restriction
142 if ( count( $temp ) == 1 ) {
143 $level = trim( $temp[0] );
144
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;
152 }
153 }
154 }
155
156 return $oldRestrictions;
157 }
158}
159
160$maintClass = UpdateRestrictions::class;
161require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction 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)
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.
const DB_PRIMARY
Definition defines.php:28