<?php

# ################# #
# Automatic Delete  #
# ################# #

# We're on the clock.
$time = time();

# Configuration

$config['username'] = "";
$config['password'] = "";
$config['project']  = "en.wikipedia.org";
$deleteReason = "[[CAT:TEMP|Temporary]] userpage";

$debug = true;
$delete = true;
$delay = 30;

# Configuration Ends Here

# Program invocation - php delete.php "Date Last Edited"
# delete.txt - Article titles, one on each line.

$deleteTime   = $argv[1];
# Grab a valid session.
login();

$deleteCount = 0;

$toDelete = explode( "\n", file_get_contents( "delete.txt" ) );
$tdc = count( $toDelete );
for ( $i=0; $i<$tdc; $i++ ) {
	if ( $toDelete[$i] == "" ) {
		debug( "Ignored white line" );
	} else {
		debug( "Deleting " . $toDelete[$i] );
		if ( checkValidDate( urlencode( $toDelete[$i] ), $deleteTime ) ) {
			delete( urlencode( $toDelete[$i] ), urlencode( $deleteReason ) ); 
		} else { debug( ">>Article did not meet date criteria." ); }
		debug( $toDelete[$i] . " deletion process completed." );
		debug( "<>Sleeping " . $delay );
		sleep( $delay );
	}
	if ( time() % 600 == 0 ) {	
		debug( "Reloading session" );
		login();
	}
}

debug( "Process completed in " . ( time() - $time ) . " seconds." );
exit;

# Functions begin here.

function checkValidDate( $article, $date ) {
	global $config;

	if ( $date == "0" ) { return true; }
	$queryURL = '"http://' . $config['project'] . '/w/api.php?action=query&prop=revisions&titles=' . $article . '&rvprop=timestamp&format=php"';
	debug( ">>QueryURL " . $queryURL );
	
	exec( 'wget -q -O check.tmp ' . $queryURL );
	$timestamp = unserialize( file_get_contents( "check.tmp" ) );
	unlink( 'check.tmp' );
	$tsarr = array_keys( $timestamp['query']['pages'] );
	$check1 = strtotime( $date );
	$check2 = strtotime( $timestamp['query']['pages'][$tsarr[0]]['revisions'][0]['timestamp'] );
	debug( ">>" . $check1 . " < " . $check2 );
        if ( !$check1 || !$check2 ) {
		return false;
	}
	return ( $check2 < $check1 );
}

function delete( $article, $reason ) {
	global $delete, $config, $deleteCount;
	
	exec( 'wget -q --load-cookies=cookie.txt "http://' . $config['project'] . '/w/api.php?action=query&prop=info&intoken=delete&titles=' . rawurlencode( $article ) . '&format=php" -O token.tmp' );
	$et2 = unserialize( file_get_contents( 'token.tmp' ) );
	$key = array_keys( $et2['query']['pages'] );
	$delt = $et2['query']['pages'][$key[0]]['deletetoken'];
	$delt2 = urlencode( substr( $delt, 0, 32 ) );
	debug( ">>Delete token is " . $delt2 );
	if ( $delete ) {
		exec( 'wget -q --post-data "action=delete&reason=' . $reason . '&token=' . $delt2 . '%2B%5C&title=' . $article . '" --load-cookies=cookie.txt "http://' . $config['project'] . '/w/api.php?action=delete" -O delete.tmp' );
		unlink( 'delete.tmp' );
		debug( ">>**Article Deleted.**" );
		$deleteCount++;
	} else { debug( ">>Article Not Deleted, variable override in effect." ); }
	unlink( 'token.tmp' );
	
}

function login() { 
	global $config;
	unlink( "cookie.txt" );
	debug( "Logging in" );
	exec( 'wget -q --post-data "action=login&lgname=' . $config['username'] . '&lgpassword=' . $config['password'] . '" --save-cookies=cookie.txt --keep-session-cookies -O login.tmp "http://' . $config['project'] . '/w/api.php?action=login"' );
	unlink( "login.tmp" );
	debug( "Session opened" );
}

function debug( $msg ) {
	global $debug;
	( $debug ) ? print $msg . "\n" : print "*";
}