3 * Makes the required database updates for Special:ProtectedPages
4 * to show all protected pages, even ones before the page restrictions
5 * schema change. All remaining page_restriction column values are moved
12 define( 'BATCH_SIZE', 100 );
14 require_once 'commandLine.inc';
16 $db =& wfGetDB( DB_MASTER
);
17 if ( !$db->tableExists( 'page_restrictions' ) ) {
18 echo "page_restrictions does not exist\n";
22 migrate_page_restrictions( $db );
24 function migrate_page_restrictions( $db ) {
25 $start = $db->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__
);
27 die("Nothing to do.\n");
29 $end = $db->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__
);
32 $end +
= BATCH_SIZE
- 1;
34 $blockEnd = $start + BATCH_SIZE
- 1;
35 $encodedExpiry = 'infinity';
36 while( $blockEnd <= $end ) {
37 echo "...doing page_id from $blockStart to $blockEnd\n";
38 $cond = "page_id BETWEEN $blockStart AND $blockEnd AND page_restrictions !=''";
39 $res = $db->select( 'page', array('page_id','page_namespace','page_restrictions'), $cond, __FUNCTION__
);
41 while( $row = $db->fetchObject( $res ) ) {
42 $oldRestrictions = array();
43 foreach( explode( ':', trim( $row->page_restrictions
) ) as $restrict ) {
44 $temp = explode( '=', trim( $restrict ) );
45 // Make sure we are not settings restrictions to ""
46 if( count($temp) == 1 && $temp[0] ) {
47 // old old format should be treated as edit/move restriction
48 $oldRestrictions["edit"] = trim( $temp[0] );
49 $oldRestrictions["move"] = trim( $temp[0] );
50 } else if( $temp[1] ) {
51 $oldRestrictions[$temp[0]] = trim( $temp[1] );
54 # Clear invalid columns
55 if( $row->page_namespace
== NS_MEDIAWIKI
) {
56 $db->update( 'page', array( 'page_restrictions' => '' ),
57 array( 'page_id' => $row->page_id
), __FUNCTION__
);
58 echo "...removed dead page_restrictions column for page {$row->page_id}\n";
60 # Update restrictions table
61 foreach( $oldRestrictions as $action => $restrictions ) {
63 'pr_page' => $row->page_id
,
65 'pr_level' => $restrictions,
67 'pr_expiry' => $encodedExpiry
71 # We use insert() and not replace() as Article.php replaces
72 # page_restrictions with '' when protected in the restrictions table
73 if ( count( $batch ) ) {
74 $ok = $db->deadlockLoop( array( $db, 'insert' ), 'page_restrictions',
75 $batch, __FUNCTION__
, array( 'IGNORE' ) );
77 throw new MWException( "Deadlock loop failed wtf :(" );
80 $blockStart +
= BATCH_SIZE
- 1;
81 $blockEnd +
= BATCH_SIZE
- 1;
84 echo "...removing dead rows from page_restrictions\n";
85 // Kill any broken rows from previous imports
86 $db->delete( 'page_restrictions', array( 'pr_level' => '' ) );
87 // Kill other invalid rows
88 $db->deleteJoin( 'page_restrictions', 'page', 'pr_page', 'page_id', array('page_namespace' => NS_MEDIAWIKI
) );