User:Leucosticte/Sandbox

<?php /** * EnhancedAutopromote MediaWiki extension. * * This extension allows system administrators to adds more criteria for autopromotion. * * Written by Leucosticte * http://www.mediawiki.org/wiki/User:Leucosticte * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * http://www.gnu.org/copyleft/gpl.html * * @file * @ingroup Extensions */

if ( !defined( 'MEDIAWIKI' ) ) { die( 'This file is a MediaWiki extension. It is not a valid entry point' ); } $wgExtensionCredits['antispam'][] = array(   'path' => __FILE__,    'name' => 'EnhancedAutopromote',    'descriptionmsg' => 'enhancedautopromote-desc',    'version' => '1.0.0',    'author' => 'Leucosticte',    'url' => 'https://www.mediawiki.org/wiki/Extension:EnhancedAutopromote', );

define( 'APCOND_AGE_FROM_EDITCOUNT', 30 ); define( 'APCOND_CAPTCHACOUNT', 31 ); define( 'APCOND_AGE_FROM_CAPTCHACOUNT', 32 );

// Number of edits an account requires before it is autopromoted $wgEnhancedAutopromoteCount = 0; // Required seconds since $wgEnhancedAutopromoteCount reached $wgEnhancedAutopromoteAgeSinceCount = 0; // Number of Captchas that need to be passed to autoconfirm $wgEnhancedAutopromoteCaptchaCount = 0; // Required seconds since $wgEnhancedAutopromoteCaptchaCount reached $wgEnhancedAutopromoteAgeSinceCaptchaCount = 0; $wgHooks['AutopromoteCondition'][] = 'EnhancedAutopromoteHooks::moreCriteria'; $wgHooks['CaptchaPassed'][] = 'EnhancedAutopromoteHooks::captchaPassed'; $wgHooks['LoadExtensionSchemaUpdates'][] = 'EnhancedAutopromoteHooks::enhAutopromoteTable';

$wgExtensionMessagesFiles['EnhancedAutopromote'] = dirname( __FILE__ ). '/EnhancedAutopromote.i18n.php'; class EnhancedAutopromoteHooks { // Main entry point public static function moreCriteria( $type, $args, $user, &$result ) { global $wgEnhancedAutopromoteCount, $wgEnhancedAutopromoteAgeSinceCount, $wgEnhancedAutopromoteCaptchaCount, $wgEnhancedAutopromoteAgeSinceCaptchaCount; // If none of these enhanced features are being used, abort if ( $type != APCOND_AGE_FROM_EDITCOUNT           && $type != APCOND_CAPTCHACOUNT            && $type != APCOND_AGE_FROM_CAPTCHACOUNT ) { return $result; }

// Don't bother checking if the globals are set so that anyone would pass if (           $wgEnhancedAutopromoteCount === 0            && $wgEnhancedAutopromoteAgeSinceCount === 0            && $wgEnhancedAutopromoteCaptchaCount === 0            && $wgEnhancedAutopromoteAgeSinceCaptchaCount === 0 ) { $result = true; return true; }       $dbw = wfGetDB( DB_MASTER ); $res = EnhancedAutopromoteHooks::getEnhRow ( $user ); if ( !$res ) { $res = EnhancedAutopromoteHooks::addEnhRow( $user ); }       switch ( $type ) { case APCOND_CAPTCHACOUNT: $enhCaptchaReached = $res->eha_captcha_count_reached; if ( $enhCaptchaReached ) { $result = true; return true; }               $result = false; return true; case APCOND_AGE_FROM_EDITCOUNT: // Get the count from the enh_autopromote table if possible; it's probably less // expensive $enhCountReached = $res->eha_count_reached; // If it's not recorded in enh_autopromote, check the user's edit count and query the // revision table to get that timestamp if ( $enhCountReached ) { $countReached = $enhCountReached; } else { $countReached = EnhancedAutopromoteHooks::whenReachedCount ( $user ); // Record that timestamp in the table for the next time if ( $countReached ) { $dbw->update ( 'enh_autopromote', array ( 'eha_count_reached' => $countReached ),                           array ( 'eha_id' => $res->eha_id ) ); }               }                // If the user has reached the required edit count, then compare the timestamp of                // when it was reached to the timestamp of now if ( $countReached ) { if ( wfTimestampNow - $res->eha_count_reached                       < $wgEnhancedAutopromoteAgeSinceCount ) { // The user has reached the required edit count, but it's too recently to                       // autopromote yet $result = false; return true; } else { // Yes, autopromote $result = true; return true; }               }                // No, the required edit count has not been reached $result = false; return true; case APCOND_AGE_FROM_CAPTCHACOUNT: // If the user has reached the required captcha count, then compare the timestamp of               // when it was reached to the timestamp of now if ( $res->eha_captcha_count_reached ) { if ( wfTimestampNow - $res->eha_captcha_count_reached                       < $wgEnhancedAutopromoteAgeSinceCount ) { // The user has reached the required captcha count, but it's too recently to                       // autopromote yet $result = false; return true; } else { // Yes, autopromote $result = true; return true; }               }                // No, the required captcha count has not been reached $result = false; return true; }   }    // Get the user's row from the enh_autopromote table public static function getEnhRow ( $user ) { $dbr = wfGetDB( DB_SLAVE ); $res = $dbr->selectRow ( 'enh_autopromote',           array ( 'eha_id', 'eha_user', 'eha_count_reached', 'eha_captchas_thus_far', 'eha_captcha_count_reached', ),           array ( 'eha_user' => $user->getId )        ); return $res; }   // Add a new row to the table private static function addEnhRow ( $user, $captchasThusFar = 0 ) { global $wgEnhancedAutopromoteCount, $wgEnhancedAutopromoteAgeSinceCount; $countReached = null; if ( $wgEnhancedAutopromoteCount > 0           && $user->getEditCount >= $wgEnhancedAutopromoteCount ) { $countReached = EnhancedAutopromoteHooks::whenReachedCount ( $user ); }       $row = array (            'eha_user' => $user->getId,            'eha_count_reached' => $countReached,            'eha_captchas_thus_far' => $captchasThusFar,            'eha_captcha_count_reached' => null,        ); $dbw = wfGetDB( DB_MASTER ); $dbw->insert ( 'enh_autopromote', $row ); return $row; }   // Check to see when the user's edit count became sufficient. Return null if it hasn't yet public static function whenReachedCount ( $user ) { global $wgEnhancedAutopromoteCount; // If the user hasn't reached the required number of edits, don't bother checking the table if ( $user->getEditCount < $wgEnhancedAutopromoteCount ) { return null; }       $dbr = wfGetDB( DB_SLAVE ); $revisionRow = $dbr->selectRow (           'revision',            array ( 'rev_user', 'rev_timestamp', ),           array ( 'rev_user' => $user->getId, ),           __METHOD__,            "OFFSET $wgEnhancedAutopromoteCount"        ); $result = null; if ( $revisionRow ) { $result = $revisionRow->rev_timestamp; }       return $result; }   // Upon passing a CAPTCHA, increment the eha_captchas_thus_far field public static function captchaPassed { global $wgUser, $wgEnhancedAutopromoteCaptchaCount, $wgEnhancedAutopromoteAgeSinceCaptchaCount; $user = $wgUser; $dbw = wfGetDB( DB_MASTER ); $res = EnhancedAutopromoteHooks::getEnhRow ( $user ); // Initialization check, since it's possible the sysadmin hasn't configured // the wiki to check before loading the CAPTCHA if ( !$res ) { $row = EnhancedAutopromoteHooks::addEnhRow( $user, 1 ); $captchasThusFar = $row[ 'eha_captchas_thus_far' ]; $captchaCountReached = $row[ 'eha_captcha_count_reached' ]; } else { $captchasThusFar = $res->eha_captchas_thus_far; $captchaCountReached = $res->eha_captcha_count_reached; }       // If we've reached the necessary number of CAPTCHAs, record the timestamp if ( $captchaCountReached == null           && $captchasThusFar >= $wgEnhancedAutopromoteCaptchaCount ) { $captchaCountReached = wfTimestampNow; }       $dbw->update( 'enh_autopromote',            array ( 'eha_captchas_thus_far' => $captchasThusFar + 1, 'eha_captcha_count_reached' => $captchaCountReached, ),           array ( 'eha_user' => $user->getId ), __METHOD__ ); return true; }   // Create table public static function enhAutopromoteTable ( DatabaseUpdater $updater ) { $updater->addExtensionUpdate( array( 'addTable', 'enh_autopromote', dirname( __FILE__ ). '/enh-autopromote.sql', true ) ); return true; } }