Fix regression probably from r50975 - link was not parsed
[lhc/web/wiklou.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 $start = $db->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
26 if( !$start ) {
27 die("Nothing to do.\n");
28 }
29 $end = $db->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
30
31 # Do remaining chunk
32 $end += BATCH_SIZE - 1;
33 $blockStart = $start;
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__ );
40 $batch = array();
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] );
52 }
53 }
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";
59 }
60 # Update restrictions table
61 foreach( $oldRestrictions as $action => $restrictions ) {
62 $batch[] = array(
63 'pr_page' => $row->page_id,
64 'pr_type' => $action,
65 'pr_level' => $restrictions,
66 'pr_cascade' => 0,
67 'pr_expiry' => $encodedExpiry
68 );
69 }
70 }
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' ) );
76 if( !$ok ) {
77 throw new MWException( "Deadlock loop failed wtf :(" );
78 }
79 }
80 $blockStart += BATCH_SIZE - 1;
81 $blockEnd += BATCH_SIZE - 1;
82 wfWaitForSlaves( 5 );
83 }
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) );
89 echo "...Done!\n";
90 }
91
92