Add more error handling
[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
26 $start = $db->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
27 $end = $db->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
28 # Do remaining chunk
29 $end += BATCH_SIZE - 1;
30 $blockStart = $start;
31 $blockEnd = $start + BATCH_SIZE - 1;
32 $encodedExpiry = 'infinity';
33 while ( $blockEnd <= $end ) {
34 echo "...doing page_id from $blockStart to $blockEnd\n";
35 $cond = "page_id BETWEEN $blockStart AND $blockEnd AND page_restrictions !='' AND page_restrictions !='edit=:move='";
36 $res = $db->select( 'page', array('page_id', 'page_restrictions'), $cond, __FUNCTION__ );
37 $batch = array();
38 while ( $row = $db->fetchObject( $res ) ) {
39 $oldRestrictions = array();
40 foreach( explode( ':', trim( $row->page_restrictions ) ) as $restrict ) {
41 $temp = explode( '=', trim( $restrict ) );
42 if(count($temp) == 1) {
43 // old old format should be treated as edit/move restriction
44 $oldRestrictions["edit"] = trim( $temp[0] );
45 $oldRestrictions["move"] = trim( $temp[0] );
46 } else {
47 $oldRestrictions[$temp[0]] = trim( $temp[1] );
48 }
49 }
50 # Update restrictions table
51 foreach( $oldRestrictions as $action => $restrictions ) {
52 $batch[] = array(
53 'pr_page' => $row->page_id,
54 'pr_type' => $action,
55 'pr_level' => $restrictions,
56 'pr_cascade' => 0,
57 'pr_expiry' => $encodedExpiry
58 );
59 }
60 }
61 # We use insert() and not replace() as Article.php replaces
62 # page_restrictions with '' when protected in the restrictions table
63 if ( count( $batch ) ) {
64 $db->insert( 'page_restrictions', $batch, __FUNCTION__, array( 'IGNORE' ) );
65 }
66 $blockStart += BATCH_SIZE - 1;
67 $blockEnd += BATCH_SIZE - 1;
68 wfWaitForSlaves( 5 );
69 }
70 }
71
72