* Graceful behavior for updateRestrictions.php if a page already has records
[lhc/web/wiklou.git] / maintenance / updateRestrictions.php
1 <?php
2
3 /*
4 * Makes the required database updates for Special:ProtectedPages
5 * to show all protected pages, even ones before the page restrictions
6 * schema change. All remaining page_restriction column values are moved
7 * to the new table.
8 */
9
10 define( 'BATCH_SIZE', 100 );
11
12 require_once 'commandLine.inc';
13
14 $db =& wfGetDB( DB_MASTER );
15 if ( !$db->tableExists( 'page_restrictions' ) ) {
16 echo "page_restrictions does not exist\n";
17 exit( 1 );
18 }
19
20 migrate_page_restrictions( $db );
21
22 function migrate_page_restrictions( $db ) {
23
24 $start = $db->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
25 $end = $db->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
26 $blockStart = $start;
27 $blockEnd = $start + BATCH_SIZE - 1;
28 $encodedExpiry = 'infinity';
29 while ( $blockEnd <= $end ) {
30 $cond = "page_id BETWEEN $blockStart AND $blockEnd AND page_restrictions !='' AND page_restrictions !='edit=:move='";
31 $res = $db->select( 'page', array('page_id', 'page_restrictions'), $cond, __FUNCTION__ );
32 $batch = array();
33 while ( $row = $db->fetchObject( $res ) ) {
34 $oldRestrictions = array();
35 foreach( explode( ':', trim( $row->page_restrictions ) ) as $restrict ) {
36 $temp = explode( '=', trim( $restrict ) );
37 if(count($temp) == 1) {
38 // old old format should be treated as edit/move restriction
39 $oldRestrictions["edit"] = trim( $temp[0] );
40 $oldRestrictions["move"] = trim( $temp[0] );
41 } else {
42 $oldRestrictions[$temp[0]] = trim( $temp[1] );
43 }
44 }
45 # Update restrictions table
46 foreach( $oldRestrictions as $action => $restrictions ) {
47 $batch[] = array(
48 'pr_page' => $row->page_id,
49 'pr_type' => $action,
50 'pr_level' => $restrictions,
51 'pr_cascade' => 0,
52 'pr_expiry' => $encodedExpiry
53 );
54 }
55 }
56 # We use insert() and not replace() as Article.php replaces
57 # page_restrictions with '' when protected in the restrictions table
58 if ( count( $batch ) ) {
59 $db->insert( 'page_restrictions', $batch, __FUNCTION__, array( 'IGNORE' ) );
60 }
61 $blockStart += BATCH_SIZE;
62 $blockEnd += BATCH_SIZE;
63 wfWaitForSlaves( 5 );
64 }
65 }
66
67