Personal tools
Share This Page
Facebook
del.icio.us
StumbleUpon

UMassWiki:Bb2ext.php

From UMassWiki

Jump to: navigation, search
<?php
if (!defined('MEDIAWIKI')) die();
/**
 * Bad Behavior 2 Extended Functionality v0.1
 *
 * Add a new Special page that displays Bad Behavior block list in reverse
 * chronological order along with aggregate information.
 *
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 */

// already defined in bad-behavior-mediawiki.php
//define('BB2_CWD', dirname(__FILE__));

$table = $wgDBprefix . 'bad_behavior';

require_once("bb2ext.i18n.php");	// language internationalization
require_once(BB2_CWD . "/bad-behavior/core.inc.php");		// core BB2 (needed for responses)
require_once(BB2_CWD . "/bad-behavior/responses.inc.php");	// human-readable keys
require_once('DatabaseFunctions.php');				// for wfQuery()

$wgExtensionFunctions[] = 'bb2ext_BadBehaviorlog';
$wgExtensionCredits['other'][] = array(
	'name' => 'Bad Behavior log',
	'version' => '0.1',
	'description' => 'Adds a log of Bad Behavior activity at [[Special:BadBehavior]]',
	'author' => 'MagicHour',
	'url' => 'http://wiki.evernex.com/index.php?title=Bad_Behavior_2_Extended'
);

// intialize the special page
function bb2ext_BadBehaviorlog() {
   global $IP, $wgMessageCache, $wgOut;
   
   // import internationalization messages.
   global $bb2ext_BadBehaviorlogMessages;
   foreach( $bb2ext_BadBehaviorlogMessages as $key => $value ) {
	$wgMessageCache->addMessages( $bb2ext_BadBehaviorlogMessages[$key], $key );
   }

   // add the special page.  note that 'block' permission is required to even see this page.  that's sort
   // of a cheap hack since I couldn't figure out how to just say "sysops only" using SpecialPage::addPage().
   require_once($IP . '/includes/SpecialPage.php');
   SpecialPage::addPage(new SpecialPage('BadBehavior', 'block', true, 'bb2ext_BadBehaviorOutput', false));
   $wgMessageCache->addMessage('badbehavior', wfMsg('bb2ext_logpage'));
}

// produce output when loaded
function bb2ext_BadBehaviorOutput() {
   global $wgOut, $table;
   
   $wikitext = wfMsg('bb2ext_logpagetext');	// top of page blurb
   
   /*
    * This part produces a sorted list of blocked IPs and the number of block incidents.
    *
    * In English wikitext:
    *
    * * '''[[User:IP|IP]]''' ([[Special:Contributions/IP|contribs]]) blocked NUMBER times, last was DATE
    */

   $wikitext .= wfMsg('bb2ext_aggregatetitle');

   $query = "SELECT ip, MAX(date) AS lastdate, COUNT(*) AS count FROM $table GROUP BY ip ORDER BY count DESC;";
   $result = wfQuery($query, DB_SLAVE, 'bb2ext_BadBehaviorOutput');
   
   while($ipentry = wfFetchRow($result)) {
	   
	   $ip = $ipentry['ip'];
	   $count = $ipentry['count'];
	   $date = $ipentry['lastdate'];
	   
	   // i18n
	   $contribs = wfMsg('bb2ext_contribs');
	   
	   // FIXME contribs, user page names different in other languages
	   $wikitext .= "* '''[[User:$ip|$ip]]''' ([[Special:Contributions/$ip|$contribs]]) " . sprintf(wfMsg('bb2ext_aggregateblocked'), $count, $date);
   }


   /*
    * This part produces a detailed log of block incidents in descending chronological order.
    *
    * In English wikitext:
    *
    * * '''DATE: [[User:IP|IP]]''' ([[Special:Contributions/IP|contribs]])
    * ** Action: HTTP_REQUEST of REQUEST_URI
    * ** User-agent: USER_AGENT
    * ** Reason: [http://www.ioerror.us/bb2-support-key?key=KEY KEY].
    */
    
   $wikitext .= "\n" . wfMsg('bb2ext_detailedtitle');

   $query = "SELECT * FROM $table ORDER BY date DESC;";   
   $result = wfQuery($query, DB_SLAVE, 'bb2ext_BadBehaviorOutput');
   
   while($logentry = wfFetchRow($result)) {
	   
	   // we do this to make the string concatenation statements readable by human coders.
	   $date = $logentry['date'];
	   $reqtype = $logentry['request_method'];
	   $requri = $logentry['request_uri'];
	   $ip = $logentry['ip'];
	   $ua = $logentry['user_agent'];
	   $key = $logentry['key'];
	   $keyglob = bb2_get_response($key);
	   $keytext = $keyglob['log'];
	   
	   // i18n
	   $contribs = wfMsg('bb2ext_contribs');
	   
	   //FIXME i18n page names
	   $wikitext .= "* '''$date: [[User:$ip|$ip]]''' ([[Special:Contributions/$ip|$contribs]])\n";
	   $wikitext .= "** " . sprintf(wfMsg('bb2ext_action'), $reqtype, $requri);
	   $wikitext .= "** " . wfMsg('bb2ext_useragent') . "$ua\n";
	   $wikitext .= "** " . wfMsg('bb2ext_reason') . "$keytext ([http://www.ioerror.us/bb2-support-key?key=$key $key])\n";
   }

   $wgOut->addWikiText($wikitext);
}


?>

Academics
Student Life
Food
Recreation
Campus
Local
fb Was this article useful? Please spread the word and share on Facebook!
Site Sponsors
Your Ad Here
10¢/day - full time