*Add a batch updater to move remaining page_restrictions values in page to the page_r...
[lhc/web/wiklou.git] / maintenance / updateRestrictions.php
1 <?php
2
3 /*
4 * Makes the required database changes for the CheckUser extension
5 */
6
7 define( 'BATCH_SIZE', 100 );
8
9 require_once 'commandLine.inc';
10
11 $db =& wfGetDB( DB_MASTER );
12 if ( !$db->tableExists( 'page_restrictions' ) ) {
13 echo "page_restrictions does not exist\n";
14 exit( 1 );
15 }
16
17 migrate_page_restrictions( $db );
18
19 function migrate_page_restrictions( $db ) {
20
21 $start = $db->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
22 $end = $db->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
23 $blockStart = $start;
24 $blockEnd = $start + BATCH_SIZE - 1;
25 $encodedExpiry = Block::decodeExpiry('');
26 while ( $blockEnd <= $end ) {
27 $cond = "page_id BETWEEN $blockStart AND $blockEnd AND page_rescrictions !=''";
28 $res = $db->select( 'page', array('page_id', 'page_restrictions'), $cond, __FUNCTION__ );
29 $batch = array();
30 while ( $row = $db->fetchObject( $res ) ) {
31 $oldRestrictions = array();
32 foreach( explode( ':', trim( $row->page_restrictions ) ) as $restrict ) {
33 $temp = explode( '=', trim( $restrict ) );
34 if(count($temp) == 1) {
35 // old old format should be treated as edit/move restriction
36 $oldRestrictions["edit"] = explode( ',', trim( $temp[0] ) );
37 $oldRestrictions["move"] = explode( ',', trim( $temp[0] ) );
38 } else {
39 $oldRestrictions[$temp[0]] = explode( ',', trim( $temp[1] ) );
40 }
41 }
42 # Update restrictions table
43 foreach( $oldRestrictions as $action => $restrictions ) {
44 $batch[] = array(
45 'pr_page' => $row->page_id,
46 'pr_type' => $action,
47 'pr_level' => $restrictions,
48 'pr_cascade' => 0,
49 'pr_expiry' => $encodedExpiry
50 );
51 }
52 # Update page record
53 $db->update( 'page',
54 array( /* SET */
55 'page_restrictions' => ''
56 ), array( /* WHERE */
57 'page_id' => $row->page_id
58 ), 'migrate_restrictions'
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_restictions', $batch, __FUNCTION__ );
65 }
66 $blockStart += BATCH_SIZE;
67 $blockEnd += BATCH_SIZE;
68 wfWaitForSlaves( 5 );
69 }
70 }
71
72 ?>