User:MadmanBot/Source/zerozero.php
Appearance
<?php
require_once '../config.php';
$log->setIdent('zerozero');
// Initialize Dementia
require_once '../dementia/Dementia.php';
Dementia::init('http://wiki.riteme.site', 'enwiki-p.rrdb.toolserver.org');
// Obtain login token
$login = DementiaAction::factory('login');
$result = $login->setName(BOT_NAME)->setPassword(BOT_PASSWORD)->execute();
if (!isset($result['token']))
{
$log->err('Unable to obtain login token');
exit;
}
// Log in and get authentication tokens
$login->setToken($result['token']);
$result = $login->execute();
if (!isset($result['result']) || $result['result'] !== 'Success')
{
$log->err('Unable to log in and get authentication tokens');
exit;
}
// Find last revision of all pages linking to www.zerozerofootball.com
$exturlusage = DementiaQuery::factory('exturlusage');
$exturlusage->setQuery('www.zerozerofootball.com')->setNamespace(0);
$revisions = DementiaQuery::factory('revisions');
$revisions->setGenerator($exturlusage)->setProp('content');
while (($pages = $revisions->execute()) !== false)
{
// Loop through pages
foreach ($pages as $page)
{
// Find bare links
$content = $page['revisions'][0]['*'];
preg_match_all($ExtLinkRegex, $content, $matches, PREG_OFFSET_CAPTURE);
// Loop through links
$offset = 0;
foreach ($matches[0] as $key => $match)
{
// Get line of content
$match[1] += $offset;
$start = strrpos($content, "\n", -strlen($content) + $match[1]) + 1;
$length = strpos($content, "\n", $match[1]) - $start;
$line = substr($content, $start, $length);
$link = $match[0];
$parsed_link = parse_url($link);
// Skip link if not within scope
if ($parsed_link['host'] != 'www.zerozerofootball.com')
continue;
// Replace host in link
$replacement = http_build_url(array_merge($parsed_link,
array('host' => 'www.footballzz.co.uk')));
// Replace link within line
$line = str_replace($link, $replacement, $line);
// Replace host in line
$line = str_ireplace('zerozerofootball.com', 'footballzz.co.uk', $line);
// Replace line within content
$content = substr_replace($content, $line, $start, $length);
$offset += strlen($line) - $length;
}
// Find bracketed links
preg_match_all($ExtLinkBracketedRegex, $content, $matches, PREG_OFFSET_CAPTURE);
// Loop through links
$offset = 0;
foreach ($matches[0] as $key => $match)
{
$link = $match[0];
$link_url = $matches[1][$key][0];
$link_text = $matches[3][$key][0];
$parsed_url = parse_url($link_url);
// Skip link if not within scope of task
if ($parsed_url['host'] != 'www.footballzz.co.uk')
continue;
if ($parsed_url['path'] == '/jogador.php')
{
// Replace link with template
parse_str($parsed_url['query'], &$query);
$replacement = "{{Zerozero profile|id=$query[id]}}";
}
else
{
// Replace host in link text
$new_text = str_ireplace('zerozerofootball.com', 'footballzz.co.uk', $link_text);
$replacement = str_replace($link_text, $new_text, $link);
}
// Replace link within content
$content = substr_replace($content, $replacement, $match[1], strlen($link));
$offset += strlen($replacement) - strlen($link);
}
if ($content == $page['revisions'][0]['*'])
{
$log->warning("[[$page[title]]]: No changes were made");
continue;
}
if (strpos($content, 'zerozerofootball.com') !== FALSE)
{
$log->warning("[[$page[title]]]: References to zerozerofootball.com remain");
continue;
}
// Edit content of page
$edit = DementiaAction::factory('edit')->setTitle($page['title']);
$edit->setMinor(true)->setBot(true);
$edit->text = $content;
$edit->summary = 'Replacing links to www.zerozerofootball.com [[Wikipedia:' .
'Bot requests#Bot needed to convert links to a template|by request]]';
$info = DementiaQuery::factory('info')->setTitles($page['title'])->setToken('edit');
$result = $info->execute();
$result = $edit->setToken($result['edittoken'])->execute();
if (!isset($result['result']) || $result['result'] !== 'Success')
$log->err("Unable to edit [[$page[title]]]: " . json_encode($result));
else
$log->info("Edited [[$page[title]]] successfully");
}
}