r103134 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103133‎ | r103134 | r103135 >
Date:09:22, 15 November 2011
Author:aaron
Status:ok
Tags:
Comment:
Converted flagToSemiProtect script to the Maintenance class
Modified paths:
  • /trunk/extensions/FlaggedRevs/maintenance/flagToSemiProtect.inc (deleted) (history)
  • /trunk/extensions/FlaggedRevs/maintenance/flagToSemiProtect.php (replaced) (history)

Diff [purge]

Index: trunk/extensions/FlaggedRevs/maintenance/flagToSemiProtect.inc
@@ -1,91 +0,0 @@
2 -<?php
3 -
4 -function flag_to_semi_protect( $db, $reason ) {
5 - global $wgFlaggedRevsNamespaces, $wgFlaggedRevsProtection;
6 - echo "Semi-protecting all flag-protected pages...\n";
7 - if ( !$wgFlaggedRevsProtection ) {
8 - echo "\$wgFlaggedRevsProtection not enabled!\n";
9 - }
10 - $BATCH_SIZE = 500;
11 - $start = $db->selectField( 'flaggedpage_config', 'MIN(fpc_page_id)', false, __FUNCTION__ );
12 - $end = $db->selectField( 'flaggedpage_config', 'MAX(fpc_page_id)', false, __FUNCTION__ );
13 - if ( is_null( $start ) || is_null( $end ) ){
14 - echo "...flaggedpage_config table seems to be empty.\n";
15 - return;
16 - }
17 - # Do remaining chunk
18 - $end += $BATCH_SIZE - 1;
19 - $blockStart = $start;
20 - $blockEnd = $start + $BATCH_SIZE - 1;
21 - $count = 0;
22 - while ( $blockEnd <= $end ) {
23 - echo "...doing fpc_page_id from $blockStart to $blockEnd\n";
24 - $res = $db->select(
25 - array( 'flaggedpage_config', 'page' ),
26 - array( 'fpc_page_id', 'fpc_level', 'fpc_expiry' ),
27 - array( "fpc_page_id BETWEEN $blockStart AND $blockEnd",
28 - 'page_namespace' => $wgFlaggedRevsNamespaces,
29 - 'page_id = fpc_page_id',
30 - "fpc_level != ''" ),
31 - __FUNCTION__
32 - );
33 - # Go through and protect each page...
34 - foreach ( $res as $row ) {
35 - $title = Title::newFromId( $row->fpc_page_id );
36 - if ( $title->isProtected( 'edit' ) ) {
37 - continue; // This page already has edit protection - skip it
38 - }
39 - $article = new Article( $title );
40 - # Flagged protection settings
41 - $frLimit = trim( $row->fpc_level );
42 - $frExpiry = ( $row->fpc_expiry === 'infinity' )
43 - ? Block::infinity()
44 - : wfTimestamp( TS_MW, $row->fpc_expiry );
45 - # Build the new protection settings
46 - $cascade = 0;
47 - $limit = $expiry = array();
48 - $desc = array(); // for output
49 - foreach ( $title->getRestrictionTypes() as $type ) {
50 - # Get existing restrictions for this action
51 - $oldLimit = $title->getRestrictions( $type ); // array
52 - $oldExpiry = $title->getRestrictionExpiry( $type ); // MW_TS
53 - # Move or Edit rights - take highest of (flag,type) settings
54 - if ( $type == 'edit' || $type == 'move' ) {
55 - # Sysop flag-protect -> full protect
56 - if ( $frLimit == 'sysop' || in_array( 'sysop', $oldLimit ) ) {
57 - $newLimit = 'sysop';
58 - # Reviewer/autoconfirmed flag-protect -> semi-protect
59 - } else {
60 - $newLimit = 'autoconfirmed';
61 - }
62 - # Take highest expiry of (flag,type) settings
63 - $newExpiry = ( !$oldLimit || $frExpiry >= $oldExpiry )
64 - ? $frExpiry // note: 'infinity' > '99999999999999'
65 - : $oldExpiry;
66 - # Otherwise - maintain original limits
67 - } else {
68 - $newLimit = $oldLimit;
69 - $newExpiry = $oldExpiry;
70 - }
71 - $limit[$type] = $newLimit;
72 - $expiry[$type] = $newExpiry;
73 - $desc[] = "{$type}={$newLimit}: {$newExpiry}";
74 - }
75 -
76 - $db->begin();
77 - $ok = $article->updateRestrictions( $limit, $reason, $cascade, $expiry );
78 - if ( $ok ) {
79 - #echo $title->getPrefixedText() . "\n\t[" . implode( ', ', $desc ) . "]\n";
80 - $count++;
81 - } else {
82 - echo "Could not protect: " . $title->getPrefixedText() . "\n";
83 - }
84 - $db->commit();
85 - }
86 - $db->freeResult( $res );
87 - $blockStart += $BATCH_SIZE - 1;
88 - $blockEnd += $BATCH_SIZE - 1;
89 - wfWaitForSlaves( 5 );
90 - }
91 - echo "Protection of all flag-protected pages complete ... {$count} pages\n";
92 -}
Index: trunk/extensions/FlaggedRevs/maintenance/flagToSemiProtect.php
@@ -1,43 +1,139 @@
22 <?php
3 -
 3+/**
 4+ * @ingroup Maintenance
 5+ */
46 if ( getenv( 'MW_INSTALL_PATH' ) ) {
5 - $IP = getenv( 'MW_INSTALL_PATH' );
 7+ $IP = getenv( 'MW_INSTALL_PATH' );
68 } else {
7 - $IP = dirname(__FILE__).'/../../..';
 9+ $IP = dirname(__FILE__).'/../../..';
810 }
9 -require "$IP/maintenance/commandLine.inc";
10 -require dirname(__FILE__) . '/flagToSemiProtect.inc';
1111
12 -if( isset( $options['help'] ) || empty( $args[0] ) ) {
13 - echo <<<TEXT
14 -Usage:
15 - php flagToSemiProtect.php --help
16 - php flagToSemiProtect.php <username> [<reason>]
 12+require_once( "$IP/maintenance/Maintenance.php" );
1713
18 - --help : This help message
19 - --<user> : The name of the admin user to use as the "protector"
 14+class FlagProtectToSemiProtect extends Maintenance {
2015
21 -TEXT;
22 - exit(0);
23 -}
 16+ public function __construct() {
 17+ $this->mDescription = 'Convert flag-protected pages to semi-protection.';
 18+ $this->addOption( 'user', 'The name of the admin user to use as the "protector"', true, true );
 19+ $this->addOption( 'reason', 'The reason for the conversion', false, true );
 20+ $this->setBatchSize( 500 );
 21+ }
2422
25 -error_reporting( E_ALL );
 23+ public function execute() {
 24+ global $wgFlaggedRevsProtection;
2625
27 -$wgUser = User::newFromName( $args[0] );
28 -if ( !$wgUser || !$wgUser->getID() ) {
29 - echo( "Invalid user specified!" );
30 - exit(0);
31 -}
 26+ if ( !$wgFlaggedRevsProtection ) {
 27+ $this->output( "\$wgFlaggedRevsProtection not enabled.\n" );
 28+ return;
 29+ }
3230
33 -echo "Protecter username: \"".$wgUser->getName()."\"\n";
34 -echo "Running in 5 seconds...Press ctrl-c to abort.\n";
35 -sleep( 5 );
 31+ $user = User::newFromName( $this->getOption( 'user' ) );
 32+ if ( !$user || !$user->getID() ) {
 33+ $this->error( "Invalid user specified!", true );
 34+ }
 35+ $reason = $this->getOption( 'reason',
 36+ "Converting flagged protection settings to edit protection settings." );
3637
37 -if ( isset( $args[1] ) ) {
38 - $reason = $args[1];
39 -} else {
40 - $reason = "Converting flagged protection settings to edit protection settings.";
 38+ $this->output( "Protecter username: \"" . $user->getName() . "\"\n" );
 39+ $this->output( "Running in 5 seconds...Press ctrl-c to abort.\n" );
 40+ sleep( 5 );
 41+
 42+ $this->flag_to_semi_protect( $user, $reason );
 43+ }
 44+
 45+ protected function flag_to_semi_protect( User $user, $reason ) {
 46+ global $wgFlaggedRevsNamespaces;
 47+
 48+ $this->output( "Semi-protecting all flag-protected pages...\n" );
 49+ if ( !$wgFlaggedRevsNamespaces ) {
 50+ $this->output( "\$wgFlaggedRevsNamespaces is empty.\n" );
 51+ return;
 52+ }
 53+
 54+ $db = wfGetDB( DB_MASTER );
 55+ $start = $db->selectField( 'flaggedpage_config', 'MIN(fpc_page_id)', false, __FUNCTION__ );
 56+ $end = $db->selectField( 'flaggedpage_config', 'MAX(fpc_page_id)', false, __FUNCTION__ );
 57+ if ( is_null( $start ) || is_null( $end ) ) {
 58+ $this->output( "...flaggedpage_config table seems to be empty.\n" );
 59+ return;
 60+ }
 61+ # Do remaining chunk
 62+ $end += $this->mBatchSize - 1;
 63+ $blockStart = $start;
 64+ $blockEnd = $start + $this->mBatchSize - 1;
 65+ $count = 0;
 66+ while ( $blockEnd <= $end ) {
 67+ $this->output( "...doing fpc_page_id from $blockStart to $blockEnd\n" );
 68+ $res = $db->select(
 69+ array( 'flaggedpage_config', 'page' ),
 70+ array( 'fpc_page_id', 'fpc_level', 'fpc_expiry' ),
 71+ array( "fpc_page_id BETWEEN $blockStart AND $blockEnd",
 72+ 'page_namespace' => $wgFlaggedRevsNamespaces,
 73+ 'page_id = fpc_page_id',
 74+ "fpc_level != ''" ),
 75+ __FUNCTION__
 76+ );
 77+ # Go through and protect each page...
 78+ foreach ( $res as $row ) {
 79+ $title = Title::newFromId( $row->fpc_page_id );
 80+ if ( $title->isProtected( 'edit' ) ) {
 81+ continue; // page already has edit protection - skip it
 82+ }
 83+ # Flagged protection settings
 84+ $frLimit = trim( $row->fpc_level );
 85+ $frExpiry = ( $row->fpc_expiry === $db->getInfinity() )
 86+ ? 'infinity'
 87+ : wfTimestamp( TS_MW, $row->fpc_expiry );
 88+ # Build the new protection settings
 89+ $cascade = 0;
 90+ $limit = $expiry = array();
 91+ $desc = array(); // for output
 92+ foreach ( $title->getRestrictionTypes() as $type ) {
 93+ # Get existing restrictions for this action
 94+ $oldLimit = $title->getRestrictions( $type ); // array
 95+ $oldExpiry = $title->getRestrictionExpiry( $type ); // MW_TS
 96+ # Move or Edit rights - take highest of (flag,type) settings
 97+ if ( $type == 'edit' || $type == 'move' ) {
 98+ # Sysop flag-protect -> full protect
 99+ if ( $frLimit == 'sysop' || in_array( 'sysop', $oldLimit ) ) {
 100+ $newLimit = 'sysop';
 101+ # Reviewer/autoconfirmed flag-protect -> semi-protect
 102+ } else {
 103+ $newLimit = 'autoconfirmed';
 104+ }
 105+ # Take highest expiry of (flag,type) settings
 106+ $newExpiry = ( !$oldLimit || $frExpiry >= $oldExpiry )
 107+ ? $frExpiry // note: 'infinity' > '99999999999999'
 108+ : $oldExpiry;
 109+ # Otherwise - maintain original limits
 110+ } else {
 111+ $newLimit = $oldLimit;
 112+ $newExpiry = $oldExpiry;
 113+ }
 114+ $limit[$type] = $newLimit;
 115+ $expiry[$type] = $newExpiry;
 116+ $desc[] = "{$type}={$newLimit}: {$newExpiry}";
 117+ }
 118+
 119+ $db->begin();
 120+ $article = new WikiPage( $title );
 121+ $ok = $article->updateRestrictions( $limit, $reason, $cascade, $expiry, $user );
 122+ if ( $ok ) {
 123+ #echo $title->getPrefixedText() . "\n\t[" . implode( ', ', $desc ) . "]\n";
 124+ $count++;
 125+ } else {
 126+ $this->output( "Could not protect: " . $title->getPrefixedText() . "\n" );
 127+ }
 128+ $db->commit();
 129+ }
 130+ $db->freeResult( $res );
 131+ $blockStart += $this->mBatchSize - 1;
 132+ $blockEnd += $this->mBatchSize - 1;
 133+ wfWaitForSlaves( 5 );
 134+ }
 135+ $this->output( "Protection of all flag-protected pages complete ... {$count} pages\n" );
 136+ }
41137 }
42138
43 -$db = wfGetDB( DB_MASTER );
44 -flag_to_semi_protect( $db, $reason );
 139+$maintClass = "FlagProtectToSemiProtect";
 140+require_once( RUN_MAINTENANCE_IF_MAIN );

Status & tagging log