Correct wildly inaccurate description of a change.
[lhc/web/wiklou.git] / maintenance / fixPageRestrictions.php
1 <?php
2 /**
3 * Maintenance script to fix the page restrictions which were added using an
4 * older version of MediaWiki.
5 *
6 * Background
7 *
8 * Before page_restrictions table was introduced in version 1.10
9 * (r19095\96r19703), page protection was handled by modifying the value of
10 * page_restrictions field of the page table. Initially, this field either was
11 * left empty (ie page was not protected) or filled with the value "sysop" or
12 * "autoconfirmed". Later, it was decided that edit protection and move
13 * protection should be handled separately, so again, either the field was
14 * left empty, or filled with something like "edit=x:move=y" where x and y were
15 * either "sysop" or "autoconfirmed" (without quotes) themselves. Often,
16 * the order of the two parts were reversed (ie "move=x:edit=y").
17 *
18 * When page_restrictions table was introduced, the code was touched in a way
19 * that if a page's protection level was changed, the old values in the page
20 * table were wiped out. The problem is, this part of code only runs when the
21 * protection level of the page *is changed*, and unfortunately, if an admin
22 * tries to unprotect the page, the code doesn't *sense* a change in protection
23 * level (because it compares the new value -- ie, nothing -- against the value
24 * of the page_restrictions table -- which is again nothing. This is reported on
25 * bugzilla as bug 13132.
26 *
27 * This maintenance script deals with the problem, by moving the protection data
28 * from page table to page_restrictions table.
29 *
30 * @addtogroup Maintenance
31 * @author Hojjat (aka Huji) <huji.huji@gmail.com>
32 */
33
34 require_once( 'commandLine.inc' );
35
36 $fname = 'fixPageRestrictions.php';
37
38 $dbw = wfGetDB( DB_MASTER );
39
40 // Get user IDs which need fixing
41 $res = $dbw->select( 'page', 'page_id, page_restrictions', 'page_restrictions <>\'\'', $fname );
42
43 while ( $row = $dbw->fetchObject( $res ) ) {
44 $id = $row->page_id;
45 $old_restrictions = $row->page_restrictions;
46 $edit_restriction = '';
47 $move_restriction = '';
48 if ( strpos( $old_restrictions, '=' ) !== false ) {
49 if ( strpos( $old_restrictions, ':' ) !== false ) {
50 # either "edit=x:move=y" or "move=x:edit=y"
51 if ( strpos( $old_restrictions, 'edit' ) == 0 ){
52 # "edit=x:move=y"
53 $edit_restriction = substr( $old_restrictions, 5, strlen( $old_restrictions) - strpos( $old_restrictions, ':') - 6);
54 $move_restriction = substr( $old_restrictions, 5, strlen( $old_restrictions) - strpos( $old_restrictions, ':') - 6);
55 } else {
56 # "move=x:edit=y"
57 $move_restriction = substr( $old_restrictions, 5, strlen( $old_restrictions) - strpos( $old_restrictions, ':') - 6);
58 $edit_restriction = substr( $old_restrictions, 5, strlen( $old_restrictions) - strpos( $old_restrictions, ':') - 6);
59 }
60 } else {
61 # either "edit=x" or "move=x"
62 if ( strpos( $old_restrictions, 'edit') !== false ) {
63 $edit_restriction = substr( $old_restrictions, 5);
64 } else {
65 $move_restriction = substr( $old_restrictions, 5);
66 }
67 }
68 } else {
69 # either "sysop" or "autoconfirmed" -- or an unexpected value
70 if ( $old_restrictions == 'sysop' ) {
71 $edit_restriction = 'sysop';
72 $move_restriction = 'sysop';
73 } elseif ( $old_restrictions == 'autoconfirmed' ) {
74 $edit_restriction = 'autoconfirmed';
75 $move_restriction = 'autoconfirmed';
76 } else {
77 #Shouldn't happen
78 print "WARNING: I found a record with old restriction set to '$old_restrictions' and I can't handle it!\n";
79 }
80 }
81
82 if ( $edit_restriction <> '' ) {
83 $dbw->replace( 'page_restrictions',
84 array(array('pr_page', 'pr_type')),
85 array( 'pr_page' => $id, 'pr_type' => 'edit'
86 , 'pr_level' => $edit_restriction, 'pr_cascade' => 0
87 , 'pr_expiry' => 'infinity' ),
88 $fname );
89 }
90 if ( $move_restriction <> '' ) {
91 $dbw->replace( 'page_restrictions',
92 array(array('pr_page', 'pr_type')),
93 array( 'pr_page' => $id, 'pr_type' => 'move'
94 , 'pr_level' => $move_restriction, 'pr_cascade' => 0
95 , 'pr_expiry' => 'infinity' ),
96 $fname );
97 }
98
99 $dbw->update( 'page', array( 'page_restrictions' => ''), array( 'page_id' => $id), $fname);
100
101 print "Fixed restrictions for page_id=$id\n from '$old_restrictions'\n to 'edit=$edit_restriction, move=$move_restriction'\n";
102 }
103 print "\n";