User:One bot/mfdarchiver.php

From Wikipedia, the free encyclopedia
<?php
/** mfdarchiver.php -- Moves MfD discussions to the archive.
 *  STABLE Version 2.2
 *
 *  (c) 2010 James Hare - http://en.wikipedia.org/wiki/User:Harej
 *
 *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *   
 *  Developers (add your self here if you worked on the code):
 *    James Hare - [[User:Harej]] - Wrote everything
 **/
ini_set("display_errors", 1);
error_reporting(E_ALL ^ E_NOTICE);
include("./public_html/botclasses.php");  // Botclasses.php was written by User:Chris_G and is available under the GNU General Public License
include("logininfo.php");

echo "Logging in...";
$objwiki = new wikipedia();
$objwiki->login($oneuser, $onepass);
echo " done.\n";

echo "Retrieving MFD contents... ";
$mfd = $objwiki->getpage("Wikipedia:Miscellany for deletion");
preg_match_all("/\{{2}Wikipedia:Miscellany for deletion\/(?!Front matter)(.*?)*\}{2}/i", $mfd, $m);
echo count($m[0]) . "\n";

for ($i = 0; $i < count($m[0]); $i++) {
	$title = preg_replace("/\{{2}/", "", $m[0][$i]);
	$title = preg_replace("/\}{2}/", "", $title);
	echo "Retrieving: " . $title . "\n";
	$mfdcontents = $objwiki->getpage($title);
	$testforclose = strpos($mfdcontents, "The following discussion is an archived debate");
	
	preg_match_all("/([0-2]\d):([0-5]\d),\s(\d{1,2})\s(\w*)\s(\d{4})\s\(UTC\).*/", $mfdcontents, $origtimeraw);
	if ($testforclose === false) {
		preg_match("/([0-2]\d):([0-5]\d),\s(\d{1,2})\s(\w*)\s(\d{4})\s\(UTC\)/i", $origtimeraw[0][0], $time);
		$origtime[$title] = date("F j, Y", strtotime($time[0]));
		unset($origtimeraw);
		echo "MFD started at " . $origtime[$title] . "\n";
		continue;
	}
	else {
		preg_match("/([0-2]\d):([0-5]\d),\s(\d{1,2})\s(\w*)\s(\d{4})\s\(UTC\)/i", $origtimeraw[0][1], $time);
		$origtime[$title] = date("F j, Y", strtotime($time[0]));
		unset($origtimeraw);
		echo "MFD started at " . $origtime[$title] . "\n";
		preg_match("/([0-2]\d):([0-5]\d),\s(\d{1,2})\s(\w*)\s(\d{4})\s\(UTC\)/i", $mfdcontents, $n); // This is based off the logic that the first time stamp found on the MFD would be that of the closing statement.
		$closetime = strtotime($n[0]);
		echo "MfD closed at " . $n[0] . "\n";
		if (time() - $closetime >= 64800) {
			echo "MFD has been closed for at least 18 hours. Proceeding to archive.\n";
			
			preg_match("/The result of the discussion was\s*[^']*'{3}[^']*'{3}/i", $mfdcontents, $o);
			$mfdresult = str_replace("'''", "", $o[0]);
			$mfdresult = preg_replace("/The result of the discussion was\s*/", "", $mfdresult);
			echo "MFD result: " . $mfdresult . "\n";
			
			$origtimeparts = explode(" ", $origtime[$title]);
			$origyear = $origtimeparts[2];
			$origmonth = $origtimeparts[0];
			
			$archivetitle = "Wikipedia:Miscellany for deletion/Archived debates/" . $origmonth . " " . $origyear;			
			echo "Retrieving " . $archivetitle . " contents\n";
			$archivecontents = $objwiki->getpage($archivetitle);
			$old = "/={3}\s?" . $origtime[$title] . "\s?={3}/i";
			$new = "=== " . $origtime[$title] . " ===\n* [[" . $title . "]] (" . $mfdresult . ")";
			$archivecontents = preg_replace($old, $new, $archivecontents);
			$archivesummary = "Archiving: [[" . $title . "]]";
			$archivecontents = str_replace("( ", "(", $archivecontents);
			$objwiki->edit($archivetitle,$archivecontents,$archivesummary,false,true);
			unset($origtime[$title]);
		}
	}
}

$keys = array_keys($origtime);
$values = array_values($origtime);
array_multisort($values, SORT_DESC, $keys);
$origtime = array_combine($keys, $values);

$expirationdate = time() - 691200;
foreach ($origtime as $pagetitle => $timestamp) {
	$timestamp = strtotime($timestamp);
	if ($timestamp > $expirationdate) {
		$submission[date("F j, Y", $timestamp)] .= "{{" . $pagetitle . "}}\n";
	}
	else {
		$oldsubmission[date("F j, Y", $timestamp)] .= "{{" . $pagetitle . "}}\n";
	}
}

$tobeposted = "";
foreach ($submission as $time => $stuff) {
	$stuff = "===" . date("F j, Y", strtotime($time)) . "===\n" . $stuff . "\n";
	$tobeposted .= $stuff;
}
if (count($oldsubmission) > 0) {
	$tobeposted .= "==Old business==\n{{mfdbacklog|backlog=yes}}\n";
	foreach ($oldsubmission as $time => $stuff) {
		$stuff = "===" . date("F j, Y", strtotime($time)) . "===\n" . $stuff . "\n";
		$tobeposted .= $stuff;
	}
}

$tobeposted = "<!-- PLEASE ADD your discussion BELOW this line, creating a new dated section where necessary. -->\n\n" . $tobeposted . "\n==Closed discussions==";

$regex1 = "/<!-- PLEASE ADD your discussion BELOW this line, creating a new dated section where necessary. -->";
$regex2 = "==Closed discussions==/";
do {
	$regex1 .= "\n*.*";
	preg_match($regex1 . $regex2, $mfd, $m);
} while ($m[0] == "");

$currentcontent = $m[0];
unset($m);

$mfd = str_replace($currentcontent, $tobeposted, $mfd);
$objwiki->edit("Wikipedia:Miscellany for deletion",$mfd,"Removing archived MfD debates",false,true);

?>