MediaWiki master
updateRestrictions.php
Go to the documentation of this file.
1<?php
28
29// @codeCoverageIgnoreStart
30require_once __DIR__ . '/Maintenance.php';
31// @codeCoverageIgnoreEnd
32
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription( 'Updates page_restrictions table from old page_restriction column' );
43 $this->setBatchSize( 1000 );
44 }
45
47 public function execute() {
48 $dbw = $this->getDB( DB_PRIMARY );
49 $batchSize = $this->getBatchSize();
50
51 if ( !$dbw->tableExists( 'page_restrictions', __METHOD__ ) ) {
52 $this->fatalError( "page_restrictions table does not exist" );
53 }
54
55 if ( !$dbw->fieldExists( 'page', 'page_restrictions', __METHOD__ ) ) {
56 $this->output( "Migration is not needed.\n" );
57 return true;
58 }
59
60 $encodedExpiry = $dbw->getInfinity();
61
62 $maxPageId = $dbw->newSelectQueryBuilder()
63 ->select( 'MAX(page_id)' )
64 ->from( 'page' )
65 ->caller( __METHOD__ )->fetchField();
66
67 $batchMinPageId = 0;
68
69 do {
70 $batchMaxPageId = $batchMinPageId + $batchSize;
71
72 $this->output( "...processing page IDs from $batchMinPageId to $batchMaxPageId.\n" );
73
74 $res = $dbw->newSelectQueryBuilder()
75 ->select( [ 'page_id', 'page_restrictions' ] )
76 ->from( 'page' )
77 ->where( [
78 $dbw->expr( 'page_restrictions', '!=', '' ),
79 $dbw->expr( 'page_id', '>', $batchMinPageId ),
80 $dbw->expr( 'page_id', '<=', $batchMaxPageId ),
81 ] )
82 ->caller( __METHOD__ )->fetchResultSet();
83
84 // No pages have legacy protection settings in the current batch
85 if ( !$res->numRows() ) {
86 $batchMinPageId = $batchMaxPageId;
87 continue;
88 }
89
90 $batch = [];
91 $pageIds = [];
92
93 foreach ( $res as $row ) {
94 $pageIds[] = $row->page_id;
95
96 $restrictionsByAction = $this->mapLegacyRestrictionBlob( $row->page_restrictions );
97
98 # Update restrictions table
99 foreach ( $restrictionsByAction as $action => $restrictions ) {
100 $batch[] = [
101 'pr_page' => $row->page_id,
102 'pr_type' => $action,
103 'pr_level' => $restrictions,
104 'pr_cascade' => 0,
105 'pr_expiry' => $encodedExpiry
106 ];
107 }
108 }
109
110 $this->beginTransaction( $dbw, __METHOD__ );
111
112 // Insert new format protection settings for the pages in the current batch.
113 // Use INSERT IGNORE to ignore conflicts with new format settings that might exist for the page
114 $dbw->newInsertQueryBuilder()
115 ->insertInto( 'page_restrictions' )
116 ->ignore()
117 ->rows( $batch )
118 ->caller( __METHOD__ )->execute();
119
120 // Clear out the legacy page.page_restrictions blob for this batch
121 $dbw->newUpdateQueryBuilder()
122 ->update( 'page' )
123 ->set( [ 'page_restrictions' => '' ] )
124 ->where( [ 'page_id' => $pageIds ] )
125 ->caller( __METHOD__ )
126 ->execute();
127
128 $this->commitTransaction( $dbw, __METHOD__ );
129
130 $batchMinPageId = $batchMaxPageId;
131 } while ( $batchMaxPageId < $maxPageId );
132
133 $this->output( "...Done!\n" );
134 return true;
135 }
136
145 private function mapLegacyRestrictionBlob( $legacyBlob ) {
146 $oldRestrictions = [];
147
148 foreach ( explode( ':', trim( $legacyBlob ) ) as $restrict ) {
149 $temp = explode( '=', trim( $restrict ) );
150
151 // Treat old old format without action name as edit/move restriction
152 if ( count( $temp ) == 1 ) {
153 $level = trim( $temp[0] );
154
155 $oldRestrictions['edit'] = $level;
156 $oldRestrictions['move'] = $level;
157 } else {
158 $restriction = trim( $temp[1] );
159 // Some old entries are empty
160 if ( $restriction != '' ) {
161 $oldRestrictions[$temp[0]] = $restriction;
162 }
163 }
164 }
165
166 return $oldRestrictions;
167 }
168}
169
170// @codeCoverageIgnoreStart
171$maintClass = UpdateRestrictions::class;
172require_once RUN_MAINTENANCE_IF_MAIN;
173// @codeCoverageIgnoreEnd
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DB servers to catch up.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB handle.
addDescription( $text)
Set the description text.
Maintenance script that updates page_restrictions table from old page_restriction column.
__construct()
Default constructor.
execute()
Do the actual work.All child classes will need to implement thisbool|null|void True for success,...
const DB_PRIMARY
Definition defines.php:28