User:Sambot/Code/Extlinkmigrate.php

From Wikipedia, the free encyclopedia
<?php

$opts = getopt('f:t:n::s::');

$from = $opts['f'];
$to = $opts['t'];

$namespace = (is_numeric($opts['n']) ? (int) $opts['n'] : false);
$site = (is_string($opts['s']) ? $opts['s'] : false);

unset($opts);

define ('PILLAR','PILLAR');

require('pillar/class.pillar.php');

$pillar = Pillar::ini_launch('pillar.ini');

if ($site) {
    $pillar->set_cursite($site);
}

$hostname = parse_url(preg_replace('/(?:http\:\/\/)?(.*)/i',"http://$1",$from),PHP_URL_HOST);

do {
    $pages = $pillar->cursite->get_extlinksto($from,100,$offset,$namespace);

    foreach ($pages as $title) {
        $page = new Page($pillar->cursite,$title['title']);
        
        $newtext = preg_replace('/' . preg_quote($from,'/') . '/i',$to,$page->get_text());
        
        try {
            $page->put($newtext,'BOT: External link correction (link to ' . $hostname . ')',1);
        } catch (PillarActionCancelled $e) {
            continue;
        }
    }
} while ($offset != null);

Pillar::report($pillar->cursite->get_edits_made() . ' edits made',PILLAR_ACTION);