User:Sambot/Code/EOL

From Wikipedia, the free encyclopedia
<?php

define ('PILLAR','PILLAR');

require('class.pillar.php');

$pillar = Pillar::ini_launch('pillar.ini');
$pillar->cursite->set_checkusertalk(true);

$settings = parse_ini_file('templates.ini');

$pillar->add_site('eol','www.eol.org','');

do {
    $articles = $pillar->cursite->get_embeddedin('Template:Eol',100,$continue,0);
    foreach ($articles as $article) {
        $page = new Page ($pillar->cursite,$article);
        
        preg_match_all('/\{\{eol\|(.*?)(?:\|.*?)?\}\}/i',$page->get_text(),$matches);
        
        $newtext = $page->get_text();
        
        for ($i = 0 ; $i < count($matches[0]) ; $i++) {
            $eolpage = new Request($pillar->get_site('eol'),array(),false,'/taxa/' . $matches[1][$i]);
            
            $headers = $eolpage->get_headers();
            
            if (substr($headers['Location'],25)) {
                $newtemplate = str_replace($matches[1][$i],substr($headers['Location'],25),$matches[0][$i]);
            
                $newtext = str_replace($matches[0][$i],$newtemplate,$newtext);
            }
        }
        
        try {
            $page->put($newtext,'Migrating {{[[Template:Eol|Eol]]}} to new URL scheme',1,false);
        } catch (PillarActionCancelled $e) {
            continue;
        }
    }
} while ($continue);