]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - maintenance/updateRestrictions.php
MediaWiki 1.14.0
[autoinstallsdev/mediawiki.git] / maintenance / updateRestrictions.php
1 <?php
2 /**
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
6  * to the new table.
7  *
8  * @file
9  * @ingroup Maintenance
10  */
11
12 define( 'BATCH_SIZE', 100 );
13
14 require_once 'commandLine.inc';
15         
16 $db =& wfGetDB( DB_MASTER );
17 if ( !$db->tableExists( 'page_restrictions' ) ) {
18         echo "page_restrictions does not exist\n";
19         exit( 1 );
20 }
21
22 migrate_page_restrictions( $db );
23
24 function migrate_page_restrictions( $db ) {
25         
26         $start = $db->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
27         $end = $db->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
28         
29         if( !$start ) {
30                 die("Nothing to do.\n");
31         }
32         
33         # Do remaining chunk
34         $end += BATCH_SIZE - 1;
35         $blockStart = $start;
36         $blockEnd = $start + BATCH_SIZE - 1;
37         $encodedExpiry = 'infinity';
38         while ( $blockEnd <= $end ) {
39                 echo "...doing page_id from $blockStart to $blockEnd\n";
40                 $cond = "page_id BETWEEN $blockStart AND $blockEnd AND page_restrictions !=''";
41                 $res = $db->select( 'page', array('page_id', 'page_restrictions'), $cond, __FUNCTION__ );
42                 $batch = array();
43                 while ( $row = $db->fetchObject( $res ) ) {
44                         $oldRestrictions = array();
45                         foreach( explode( ':', trim( $row->page_restrictions ) ) as $restrict ) {
46                                 $temp = explode( '=', trim( $restrict ) );
47                                 // Make sure we are not settings restrictions to ""
48                                 if( count($temp) == 1 && $temp[0] ) {
49                                         // old old format should be treated as edit/move restriction
50                                         $oldRestrictions["edit"] = trim( $temp[0] );
51                                         $oldRestrictions["move"] = trim( $temp[0] );
52                                 } else if( $temp[1] ) {
53                                         $oldRestrictions[$temp[0]] = trim( $temp[1] );
54                                 }
55                         }
56                         # Update restrictions table
57                         foreach( $oldRestrictions as $action => $restrictions ) {
58                                 $batch[] = array( 
59                                         'pr_page' => $row->page_id,
60                                         'pr_type' => $action,
61                                         'pr_level' => $restrictions,
62                                         'pr_cascade' => 0,
63                                         'pr_expiry' => $encodedExpiry
64                                 );
65                         }
66                 }
67                 # We use insert() and not replace() as Article.php replaces
68                 # page_restrictions with '' when protected in the restrictions table
69                 if ( count( $batch ) ) {
70                         $ok = $db->deadlockLoop(
71                                 array( $db, 'insert' ),
72                                 'page_restrictions', $batch, __FUNCTION__, array( 'IGNORE' ) );
73                         if( !$ok ) {
74                                 throw new MWException( "Deadlock loop failed wtf :(" );
75                         }
76                 }
77                 $blockStart += BATCH_SIZE - 1;
78                 $blockEnd += BATCH_SIZE - 1;
79                 wfWaitForSlaves( 5 );
80         }
81         echo "...removing dead rows from page_restrictions\n";
82         // Kill any broken rows from previous imports
83         $db->delete( 'page_restrictions', array( 'pr_level' => '' ) );
84         echo "...Done!\n";
85 }
86
87