Jump to content

Wikipedia:Bots/Requests for approval/MadmanBot 10/Source code

From Wikipedia, the free encyclopedia
#!/usr/bin/env php
<?php
set_include_path('./lib:' . get_include_path());
require_once 'api.inc.php';
require_once 'config.inc.php';

// Log in to the MediaWiki API
$login = api_request(array('action' => 'login', 'lgname' => LG_NAME), HTTP_Request2::METHOD_POST);
$login = api_request(array('action' => 'login', 'lgname' => LG_NAME, 'lgpassword' => LG_PASSWORD, 
	'lgtoken' => $login['login']['token']), HTTP_Request2::METHOD_POST);

// Load touched redirects
echo "Please wait, loading touched redirects...\n";
if (file_exists('imageusage_touched.dat'))
	$touched = unserialize(file_get_contents('imageusage_touched.dat'));
else
	$touched = array();
echo "Loading complete.\n";

// Perform info request: All file redirects
$parameters['info'] = array('action' => 'query', 'prop' => 'info', 'intoken' => 'edit',
	'generator' => 'allpages', 'gapnamespace' => 6, 'gapfilterredir' => 'redirects', 'gaplimit' => 5000);
do
{
	$results['info'] = api_request($parameters['info']);

	$pageids = array_keys($results['info']['query']['pages']);
	echo "Checking file redirects: " . $results['info']['query']['pages'][$pageids[0]]['title'] . " - " .
		$results['info']['query']['pages'][$pageids[count($pageids) - 1]]['title'] . ".\n";

	foreach ($results['info']['query']['pages'] as $pageid => $page)
	{
		// Skip touched redirects
		if (in_array($pageid, $touched))
		{
			echo "Skipped: $page[title] (touched redirect).\n";
			continue;
		}

		// Make null edit
		$results['edit'] = api_request(array('action' => 'edit', 'title' => $page['title'], 'appendtext' => '',
			'summary' => 'Automated edit: Performing null edit.', 'bot' => 1, 'minor' => 1, 
			'token' => $page['edittoken']),
			HTTP_Request2::METHOD_POST);
		echo $results['edit']['edit']['result'] . ": $page[title].\n";

		// Add pageid to touched redirects
		$touched[] = $pageid;
	}

	// Update touched redirects
	file_put_contents('imageusage_touched.dat', serialize($touched));

	// Continue info request
	if (isset($results['info']['query-continue']['allpages']))
		$parameters['info'] = array_merge($parameters['info'], $results['info']['query-continue']['allpages']);
} while (isset($results['info']['query-continue']['allpages']));
?>