MediaWiki master
updateRestrictions.php
Go to the documentation of this file.
1<?php
27// @codeCoverageIgnoreStart
28require_once __DIR__ . '/Maintenance.php';
29// @codeCoverageIgnoreEnd
30
38 public function __construct() {
39 parent::__construct();
40 $this->addDescription( 'Updates page_restrictions table from old page_restriction column' );
41 $this->setBatchSize( 1000 );
42 }
43
44 public function execute() {
45 $dbw = $this->getDB( DB_PRIMARY );
46 $batchSize = $this->getBatchSize();
47
48 if ( !$dbw->tableExists( 'page_restrictions', __METHOD__ ) ) {
49 $this->fatalError( "page_restrictions table does not exist" );
50 }
51
52 if ( !$dbw->fieldExists( 'page', 'page_restrictions', __METHOD__ ) ) {
53 $this->output( "Migration is not needed.\n" );
54 return true;
55 }
56
57 $encodedExpiry = $dbw->getInfinity();
58
59 $maxPageId = $dbw->newSelectQueryBuilder()
60 ->select( 'MAX(page_id)' )
61 ->from( 'page' )
62 ->caller( __METHOD__ )->fetchField();
63
64 $batchMinPageId = 0;
65
66 do {
67 $batchMaxPageId = $batchMinPageId + $batchSize;
68
69 $this->output( "...processing page IDs from $batchMinPageId to $batchMaxPageId.\n" );
70
71 $res = $dbw->newSelectQueryBuilder()
72 ->select( [ 'page_id', 'page_restrictions' ] )
73 ->from( 'page' )
74 ->where( [
75 $dbw->expr( 'page_restrictions', '!=', '' ),
76 $dbw->expr( 'page_id', '>', $batchMinPageId ),
77 $dbw->expr( 'page_id', '<=', $batchMaxPageId ),
78 ] )
79 ->caller( __METHOD__ )->fetchResultSet();
80
81 // No pages have legacy protection settings in the current batch
82 if ( !$res->numRows() ) {
83 $batchMinPageId = $batchMaxPageId;
84 continue;
85 }
86
87 $batch = [];
88 $pageIds = [];
89
90 foreach ( $res as $row ) {
91 $pageIds[] = $row->page_id;
92
93 $restrictionsByAction = $this->mapLegacyRestrictionBlob( $row->page_restrictions );
94
95 # Update restrictions table
96 foreach ( $restrictionsByAction as $action => $restrictions ) {
97 $batch[] = [
98 'pr_page' => $row->page_id,
99 'pr_type' => $action,
100 'pr_level' => $restrictions,
101 'pr_cascade' => 0,
102 'pr_expiry' => $encodedExpiry
103 ];
104 }
105 }
106
107 $this->beginTransaction( $dbw, __METHOD__ );
108
109 // Insert new format protection settings for the pages in the current batch.
110 // Use INSERT IGNORE to ignore conflicts with new format settings that might exist for the page
111 $dbw->newInsertQueryBuilder()
112 ->insertInto( 'page_restrictions' )
113 ->ignore()
114 ->rows( $batch )
115 ->caller( __METHOD__ )->execute();
116
117 // Clear out the legacy page.page_restrictions blob for this batch
118 $dbw->newUpdateQueryBuilder()
119 ->update( 'page' )
120 ->set( [ 'page_restrictions' => '' ] )
121 ->where( [ 'page_id' => $pageIds ] )
122 ->caller( __METHOD__ )
123 ->execute();
124
125 $this->commitTransaction( $dbw, __METHOD__ );
126
127 $batchMinPageId = $batchMaxPageId;
128 } while ( $batchMaxPageId < $maxPageId );
129
130 $this->output( "...Done!\n" );
131 return true;
132 }
133
142 private function mapLegacyRestrictionBlob( $legacyBlob ) {
143 $oldRestrictions = [];
144
145 foreach ( explode( ':', trim( $legacyBlob ) ) as $restrict ) {
146 $temp = explode( '=', trim( $restrict ) );
147
148 // Treat old old format without action name as edit/move restriction
149 if ( count( $temp ) == 1 ) {
150 $level = trim( $temp[0] );
151
152 $oldRestrictions['edit'] = $level;
153 $oldRestrictions['move'] = $level;
154 } else {
155 $restriction = trim( $temp[1] );
156 // Some old entries are empty
157 if ( $restriction != '' ) {
158 $oldRestrictions[$temp[0]] = $restriction;
159 }
160 }
161 }
162
163 return $oldRestrictions;
164 }
165}
166
167// @codeCoverageIgnoreStart
168$maintClass = UpdateRestrictions::class;
169require_once RUN_MAINTENANCE_IF_MAIN;
170// @codeCoverageIgnoreEnd
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
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.
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