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