Jump to content

User:HotArticlesBot/Source code

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by HotArticlesBot (talk | contribs) at 00:29, 11 January 2011 (source). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

/* Setup the mediawiki classes. */
require_once dirname(__FILE__) . '/../../config.inc.php';
require_once dirname(__FILE__) . '/botclasses.php';
$wikipedia = new wikipedia();
 
/* Log in to wikipedia. */
$username = "HotArticlesBot";
$password = "******";
$wikipedia->login($username,$password);

$query = "SELECT * FROM hotarticles";
$result = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_array ($result)) {
	$source = str_replace(" ", "+", $row['source']);
	$spanNumber = $row['span_days'];
	$url = false;
	if ($row['method'] == 'category') {
		$count = $wikipedia->categorypagecount('Category:'.$row['source']);
		if ($count < 2000) {
			$url = "http://toolserver.org/~tim1357/cgi-bin/HotArticles/?category=1&name=".$source."&num=".$spanNumber."&unit=DAY&xml=1";
		} else {
			echo "Category ".$row['source']." is too large. Skipping.<br/>";
		}
	} else {
		$url = "http://toolserver.org/~tim1357/cgi-bin/HotArticles/?name=".$source."&num=".$spanNumber."&unit=DAY&xml=1";
	}
	if ($url) {
		$p = xml_parser_create();
		xml_parse_into_struct($p,file_get_contents($url),$results,$index);
		xml_parser_free($p);
		
		$hotArticles = array();
		
		$spanDay = $results[$index['SPANDAY'][0]]['value'];
		
		for ( $x = 0; $x < 5; $x++ ) {
			$title = $results[$index['TITLE'][$x]]['value'];
			$fixedTitle = str_replace("_", " ", $title);
			$hotArticles[$fixedTitle] = $results[$index['EDITCOUNT'][$x]]['value'];
		}
		
		$output = "{|\r";
		
		foreach ($hotArticles as $key => $value) {
			switch (true) {
				case ($value >= $row['red'] ):
					$color = '#c60d27';
					break;
				case ($value >= $row['orange'] ):
					$color = '#f75a0d';
					break;
				case ($value > 0 ):
					$color = '#ff9900';
					break;
			}
			
			$output .= <<<WIKITEXT
|-
| style="text-align:center; font-size:130%; color:white; background:$color; padding: 0 0.2em" | '''$value''' <span 
style="font-size:60%">edits</span>
| style="padding: 0.4em;" | [[$key]]
WIKITEXT;
			$output .= "\r";
		}
		$wordarray = array('zero','one','two','three','four','five','six','seven','eight','nine','ten');
		$output .= <<<WIKITEXT
|-
| style="padding: 0.1em;" |
|}
WIKITEXT;
		
		$output .= "\rThese are the articles that have been edited the most within the last ".$wordarray[$spanNumber]." days. This section is updated daily.\r";
		
		$edit = $wikipedia->edit($row['target_page'],$output,'Updating for '.date('F j, Y'));
		
		if ($edit) echo "Completed update for ".$row['source'].".<br/>";
	}
}
echo "Done.";
?>