User:Sambot/Code/Ships 2

From Wikipedia, the free encyclopedia
<?php

define ('PILLAR','PILLAR');

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

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

foreach (array ('Lists of ship commissionings','Lists of ship decommissionings','List of shipwrecks by year') as $cat) {
    do {
        $pages = $pillar->cursite->get_categorymembers("Category:$cat",100,$continue);
        
        foreach ($pages as $title) {
            $page = new Page ($title['title'],false,Page::TEMPLATES);
            
            preg_match('/\d{4}/',$title['title'],$year);
            
            if (in_array('Shipevents',$page->get_templates())) {
                continue;
            }
            
            $text = preg_replace('/\{\{(S|s)hipevents\d{4}\}\}/i',"{{\\1hipevents|{$year[0]}}}",$page->get_text(),1);
            $text = preg_replace('/\{\{shipevents\d{4}\}\}\n?/i','',$text);
            
            try {
                $page->put($text,'Format {{shipevents}}',false,false);
            } catch (PillarActionCancelled $e) {
                continue;
            } catch (PillarException $e) {
                continue;
            }
        }
    } while ($continue);
}

do {
    $pages = $pillar->cursite->get_categorymembers('Category:Lists of ship launches',100,$continue);
    
    foreach ($pages as $title) {
        $page = new Page ($title['title'],false,Page::TEMPLATES);
        
        preg_match('/\d{4}/',$title['title'],$year);
        
        $text = $page->get_text();
        
        if (!in_array('Shipevents',$page->get_templates())) {
            $text = preg_replace('/\{\{(S|s)hipevents\d{4}\}\}/i',"{{\\1hipevents|{$year[0]}}}",$page->get_text(),1);
            $text = preg_replace('/\{\{shipevents\d{4}\}\}\n?/i','',$text);
        }

        if (strpos($text,"[[Category:{$year[0]} ships]]")) {
            $text = preg_replace("/\[\[Category:{$year[0]} ships\]\]/","[[Category:{$year[0]} ships| ]]",$text);
        } elseif (!strpos($text,"[[Category:{$year[0]} ships")) {
            if (preg_match('/\[\[Category:.*?\]\]/',$text)) {
                $text = preg_replace('/(.*\[\[Category:.*?\]\])(.*)$/',"$1\n[[Category:{$year[0]} ships| ]]$2",$text);
            } else {
                $text .= "\n[[Category:{$year[0]}| ]]";
            }
        }
        
        try {
            $page->put($text,'Format {{shipevents}} and category link',false,false);
        } catch (PillarActionCancelled $e) {
            continue;
        } catch (PillarException $e) {
            continue;
        }
    }
} while ($continue);