Jump to content

User:NoomBot/scripts/AFCDel.php

From Wikipedia, the free encyclopedia

Uses framework based off of botclasses.php (bug fixes + extra functions). VertigoScript just provides some meta to a wrapper script which isn't being used to run this task. It is scheduled through cronie every 30 minutes to the toolserver job queue.

<?php

date_default_timezone_set('UTC');

require '../api/wapi.php';
require '../api/vertigo/vertigoscript.php';

class AFCDel extends VertigoScript {

	private static function clean_old_cache($a) {
		$nc = $a;
		foreach ($a as $art => $info) {
			if ($info['ts'] <= time()-60*60*24*31) unset($nc[$art]); # 31 day unset
		}
		return $nc;
	}

	public function run() {
		$this->setAPI('wapi');
		$this->setLogging('events.txt', 'actions.txt');
		require '../en.php';
		
		$cache = array();
		if (!is_file('cache.dat')) {
			$f = fopen('cache.dat', 'w'); fwrite($f, serialize($cache)); fclose($f);
		}
		$cache = unserialize(trim(file_get_contents('cache.dat')));
		AFCDel::clean_old_cache($cache);

		$b = new wikibot(false, 3);
		$b->login($user, $password)or die("Failed to login");

		$afd = $b->categorymembers("Category:Articles_for_deletion", false);
		$afc = array();
		foreach ($afd as $page) {
			if ($b->shutoff("User:NoomBot/Shutoff/AFCDel")) die("Emergency stopped!!");
			
			if (substr($page, 0, 9) == "Category:") continue;
			
			# straight up continue if we saw the page in the last 3 days
			if (isset($cache[$page]) && ((isset($cache[$page]['notafc'])) || ($cache[$page]['ts'] >= time()-60*60*24*3))) continue; 
						
			# fetch talk, check if has afc template
			$t = $b->getPage("Talk:".$page);
			$this->logEvent("Inspecting ".$page);
			if (preg_match('${{(?:WikiProject Articles for creation|WPAFC)\|.*reviewer=(.+?)(?:\|.*)*}}$i', $t, $r)) {
				# skip api request, find out ourselves
				$rev = $r[1];
				$this->logEvent($page." is an article for creation, reviewer: ".$rev);
			}
			else { # not afc article
				$cache[$page] = array('notafc' => true, 'nom' => 1, 'ts' => time());
				continue;
			}
			
			$p = $b->getPage($page);
			if (preg_match('${{(?:afd|article for deletion|afdm|article for deletion/dated)\|page=(.+?)(?:\|.*)*}}$i', $p, $m)) {
				$nom = 1;
				if (preg_match('/.+\(([0-9])(?:th|nd|rd) nomination\)/', $m[1], $n)) {
					$nom = $n[1];
				}
				if (isset($cache[$page]) && $cache[$page]['nom'] == $nom) {
					$this->logEvent("Skipped page that's cached");
					continue;
				}
				
				$info = array('retname' => $page, 'nom' => $nom, 'link' => "Wikipedia:Articles for deletion/".$m[1], 'rev' => $rev);
				# okay, ready
				$afc[$page] = $info;
			}
			else {
				$this->logEvent("[WARNING] Article listed at AfD but could not match it's template!");
				continue;
			}
		}

		foreach ($afc as $article => $info) {
			$reviewer = $info['rev'];
			echo "Notifying ".$reviewer.", ".$article." listed at AfD... ";
			if (isset($cache[$article])) {
				echo "failed; reviewer already notified.".PHP_EOL;
				continue;
			}
			$talk = $b->getPage("User talk:".$reviewer);
			if (!$b->excluded($talk, "NoomBot")) {
				echo "failed; nobots denied.".PHP_EOL;
				continue;
			}
			$regex = str_replace(" ", ".", $info['link'])."|".str_replace(" ", ".", $article).".*delet[e|ed|ion].*";
			$regex = "/".str_replace("/", "\/", $regex)."/i";
			if (preg_match($regex, $talk)) {
				echo "failed; user most likely aware of deletion, r1 match.".PHP_EOL;
				continue;
			}
			if (preg_match('/{{retired(}}|\|.+}})/i', $talk)) {
				echo 'failed; user likely retired, r2 match.'.PHP_EOL;
				continue;
			}
			$message  = "== Nomination of ".$article." for deletion ==";
			$message .= PHP_EOL.'<div class="floatleft" style="margin-bottom:0">[[File:Ambox warning orange.svg|48px|alt=|link=]]</div>A discussion is taking place as to whether the article \'\'\'[['.$article.']]\'\'\', which you recently reviewed as part of the [[WP:AFC|Articles for creation]] process, is suitable for inclusion in Wikipedia according to [[Wikipedia:List of policies and guidelines|Wikipedia\'s policies and guidelines]] or whether it should be [[Wikipedia:Deletion policy|deleted]].';
			$message .= PHP_EOL."The discussion is occuring '''[[".$info['link']."|here]]'''. As the reviewer, your contribution to the discussion will be helpful in reaching a consensus. ~~~~";
			$talk = rtrim($talk).PHP_EOL.PHP_EOL.$message;

			$b->edit("User talk:".$reviewer, $talk, "([[Wikipedia:Bots/Requests_for_approval/NoomBot_8|BOT]]) Notifying AfC reviewer of [[".$info['link']."]]", null, false, true);
			#$b->edit("User:Noommos/tests", $message, "Notifying AfC reviewer of [[".$info['link']."]]", null, false, true);
			$this->logAction("Notified ".$reviewer." about ".$article." at User talk:".$reviewer);
			$info['ts'] = time();
			$cache[$article] = $info;
			echo "success.".PHP_EOL;
		}

		$f = fopen('cache.dat', 'w');
		fwrite($f, serialize($cache));
		fclose($f);
	}
}

$task = new AFCDel();
$task->run();
?>