MediaWiki master
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->newSelectQueryBuilder()
58 ->select( 'MAX(page_id)' )
59 ->from( 'page' )
60 ->caller( __METHOD__ )->fetchField();
61
62 $batchMinPageId = 0;
63
64 do {
65 $batchMaxPageId = $batchMinPageId + $batchSize;
66
67 $this->output( "...processing page IDs from $batchMinPageId to $batchMaxPageId.\n" );
68
69 $res = $dbw->newSelectQueryBuilder()
70 ->select( [ 'page_id', 'page_restrictions' ] )
71 ->from( 'page' )
72 ->where( [
73 $dbw->expr( 'page_restrictions', '!=', '' ),
74 $dbw->expr( 'page_id', '>', $batchMinPageId ),
75 $dbw->expr( 'page_id', '<=', $batchMaxPageId ),
76 ] )
77 ->caller( __METHOD__ )->fetchResultSet();
78
79 // No pages have legacy protection settings in the current batch
80 if ( !$res->numRows() ) {
81 $batchMinPageId = $batchMaxPageId;
82 continue;
83 }
84
85 $batch = [];
86 $pageIds = [];
87
88 foreach ( $res as $row ) {
89 $pageIds[] = $row->page_id;
90
91 $restrictionsByAction = $this->mapLegacyRestrictionBlob( $row->page_restrictions );
92
93 # Update restrictions table
94 foreach ( $restrictionsByAction as $action => $restrictions ) {
95 $batch[] = [
96 'pr_page' => $row->page_id,
97 'pr_type' => $action,
98 'pr_level' => $restrictions,
99 'pr_cascade' => 0,
100 'pr_expiry' => $encodedExpiry
101 ];
102 }
103 }
104
105 $this->beginTransaction( $dbw, __METHOD__ );
106
107 // Insert new format protection settings for the pages in the current batch.
108 // Use INSERT IGNORE to ignore conflicts with new format settings that might exist for the page
109 $dbw->newInsertQueryBuilder()
110 ->insertInto( 'page_restrictions' )
111 ->ignore()
112 ->rows( $batch )
113 ->caller( __METHOD__ )->execute();
114
115 // Clear out the legacy page.page_restrictions blob for this batch
116 $dbw->newUpdateQueryBuilder()
117 ->update( 'page' )
118 ->set( [ 'page_restrictions' => '' ] )
119 ->where( [ 'page_id' => $pageIds ] )
120 ->caller( __METHOD__ )
121 ->execute();
122
123 $this->commitTransaction( $dbw, __METHOD__ );
124
125 $batchMinPageId = $batchMaxPageId;
126 } while ( $batchMaxPageId < $maxPageId );
127
128 $this->output( "...Done!\n" );
129 return true;
130 }
131
140 private function mapLegacyRestrictionBlob( $legacyBlob ) {
141 $oldRestrictions = [];
142
143 foreach ( explode( ':', trim( $legacyBlob ) ) as $restrict ) {
144 $temp = explode( '=', trim( $restrict ) );
145
146 // Treat old old format without action name as edit/move restriction
147 if ( count( $temp ) == 1 ) {
148 $level = trim( $temp[0] );
149
150 $oldRestrictions['edit'] = $level;
151 $oldRestrictions['move'] = $level;
152 } else {
153 $restriction = trim( $temp[1] );
154 // Some old entries are empty
155 if ( $restriction != '' ) {
156 $oldRestrictions[$temp[0]] = $restriction;
157 }
158 }
159 }
160
161 return $oldRestrictions;
162 }
163}
164
165$maintClass = UpdateRestrictions::class;
166require_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