MediaWiki  master
updateRestrictions.php
Go to the documentation of this file.
1 <?php
27 require_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  $db = $this->getDB( DB_MASTER );
44  $batchSize = $this->getBatchSize();
45  if ( !$db->tableExists( 'page_restrictions' ) ) {
46  $this->fatalError( "page_restrictions table does not exist" );
47  }
48 
49  $start = $db->selectField( 'page', 'MIN(page_id)', '', __METHOD__ );
50  if ( !$start ) {
51  $this->fatalError( "Nothing to do." );
52  }
53  $end = $db->selectField( 'page', 'MAX(page_id)', '', __METHOD__ );
54 
55  # Do remaining chunk
56  $end += $batchSize - 1;
57  $blockStart = $start;
58  $blockEnd = $start + $batchSize - 1;
59  $encodedExpiry = 'infinity';
60  while ( $blockEnd <= $end ) {
61  $this->output( "...doing page_id from $blockStart to $blockEnd out of $end\n" );
62  $cond = "page_id BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd .
63  " AND page_restrictions !=''";
64  $res = $db->select(
65  'page',
66  [ 'page_id', 'page_namespace', 'page_restrictions' ],
67  $cond,
68  __METHOD__
69  );
70  $batch = [];
71  foreach ( $res as $row ) {
72  $oldRestrictions = [];
73  foreach ( explode( ':', trim( $row->page_restrictions ) ) as $restrict ) {
74  $temp = explode( '=', trim( $restrict ), 2 );
75  // Make sure we are not settings restrictions to ""
76  if ( count( $temp ) == 1 && $temp[0] ) {
77  // old old format should be treated as edit/move restriction
78  $oldRestrictions["edit"] = trim( $temp[0] );
79  $oldRestrictions["move"] = trim( $temp[0] );
80  } elseif ( $temp[1] ) {
81  $oldRestrictions[$temp[0]] = trim( $temp[1] );
82  }
83  }
84  # Clear invalid columns
85  if ( $row->page_namespace == NS_MEDIAWIKI ) {
86  $db->update( 'page', [ 'page_restrictions' => '' ],
87  [ 'page_id' => $row->page_id ], __FUNCTION__ );
88  $this->output( "...removed dead page_restrictions column for page {$row->page_id}\n" );
89  }
90  # Update restrictions table
91  foreach ( $oldRestrictions as $action => $restrictions ) {
92  $batch[] = [
93  'pr_page' => $row->page_id,
94  'pr_type' => $action,
95  'pr_level' => $restrictions,
96  'pr_cascade' => 0,
97  'pr_expiry' => $encodedExpiry
98  ];
99  }
100  }
101  # We use insert() and not replace() as Article.php replaces
102  # page_restrictions with '' when protected in the restrictions table
103  if ( count( $batch ) ) {
104  $ok = $db->deadlockLoop( [ $db, 'insert' ], 'page_restrictions',
105  $batch, __FUNCTION__, [ 'IGNORE' ] );
106  if ( !$ok ) {
107  throw new MWException( "Deadlock loop failed wtf :(" );
108  }
109  }
110  $blockStart += $batchSize - 1;
111  $blockEnd += $batchSize - 1;
112  wfWaitForSlaves();
113  }
114  $this->output( "...removing dead rows from page_restrictions\n" );
115  // Kill any broken rows from previous imports
116  $db->delete( 'page_restrictions', [ 'pr_level' => '' ] );
117  // Kill other invalid rows
118  $db->deleteJoin(
119  'page_restrictions',
120  'page',
121  'pr_page',
122  'page_id',
123  [ 'page_namespace' => NS_MEDIAWIKI ]
124  );
125  $this->output( "...Done!\n" );
126  }
127 }
128 
130 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance script that updates page_restrictions table from old page_restriction column...
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
setBatchSize( $s=0)
Set the batch size.
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
const DB_MASTER
Definition: defines.php:26
$batch
Definition: linkcache.txt:23
$res
Definition: database.txt:21
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
addDescription( $text)
Set the description text.
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
output( $out, $channel=null)
Throw some output to the user.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
const NS_MEDIAWIKI
Definition: Defines.php:68
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
getBatchSize()
Returns batch size.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.