User:Sambot/Code/Ships

From Wikipedia, the free encyclopedia
<?php
 
define ('PILLAR','PILLAR');
 
require('class.pillar.php');
 
$pillar = Pillar::ini_launch('pillar.ini');
 
// for ($i = 1984 ; $i < 2010 ; $i++) {
//     if (in_array($i,array(1850,1900, 1906, 1911, 1912, 1986))) {
//         continue;
//     }
//  
//     try {
//         $page = new Page ($pillar->cursite,"Category:$i ships");
//     } catch (PillarException $e) {
//         $i--; //try this number again
//         continue;
//     }
//  
//     if ( !$page->get_exists() ) {
//         try {
//             $page->put("{{cathead ship year|$i}}",'[[Wikipedia:Bots/Requests for approval/Sambot 6|BOT]]: Creating category',false,false);
//         } catch (PillarException $e) {
//             //
//         }
//     } else {
//         Pillar::report("Category:$i ships exists",PILLAR_ERROR);
//     }
//  
//     try {
//         $page = new Page ($pillar->cursite,"Category talk:$i ships");
//     } catch (PillarException $e) {
//         $i--; //try this number again
//         continue;
//     }
//  
//     if ( !$page->get_exists() ) {
//         try {
//             $page->put('{{WikiProject Ships}}','[[Wikipedia:Bots/Requests for approval/Sambot 6|BOT]]: Creating category',false,false);
//         } catch (PillarException $e) {
//             //
//         }
//     } else {
//         Pillar::report("Category talk:$i ships exists",PILLAR_ERROR);
//     }
// }

do {
    $titles = $pillar->cursite->get_embeddedin('Template:Infobox_Ship_Career',500,$continue,0);
 
    foreach ($titles as $title) {
        $page = new PageVerbose ($pillar->cursite,$title);
        $template = Template::separatetemplate($page->get_text(),'Infobox Ship Career');
 
        $list = Template::templatesplit($template['template']);
 
        $launched = str_ireplace("Ship launched=",'',trim($list['fieldnames']['Ship launched']));
 
        if (!preg_match ('/(\d{4})/',$launched,$match)) {
            $completed = str_ireplace("Ship completed=",'',trim($list['fieldnames']['Ship completed']));
 
            if (!preg_match ('/(\d{4})/',$completed,$match)) {
                $datenotfound .= "* [[$title]]\n";
                continue;
            }
        }
 
        $year = $match[1];
 
        if ($year <= 1599) {
            if ($year % 100 == 0) {
                $cat = "Category:" . floor($year / 100) . "th-century ships";
            } else {
                $cat = "Category:" . (floor($year / 100) + 1) . "th-century ships";
            }
        } elseif ($year < 1850) {
            $cat = "Category:" . floor($year / 10) . "0s ships";
        } elseif ($year <= 2009) {
            $cat = "Category:$year ships";
        } elseif ($year > 2009) {
            $cat = "Category:Proposed ships";
        }
        
        $text = $page->get_text() . "\n[[Category:1984 introductions]]";
        
        $text = preg_replace("/\\n?\[\[Category:$year introductions\]\]/",'',$text);
        
        if (($text == $page->get_text()) && in_array($cat,$page->get_categories())) {
            continue;
        }
 
        if (!in_array($cat,$page->get_categories())) {
            if (preg_match('/\[\[Category\:.*?\]\]/is',$text)) {
                $text = preg_replace("/(.*\[\[Category:.*?\]\])/is","$1\n[[$cat]]",$text); //add $cat to the last category link on the page
            } else {
                $text = $text . "\n\n[[$cat]]";
            }
        }
 
        try {
            $page->put($text,"[[Wikipedia:Bots/Requests for approval/Sambot 6|BOT]]: Adding to [[:$cat]]",true,false);
        } catch (PillarException $e) {
            //
        }
    }
} while ($continue);
 
$page = new Page ($pillar->cursite,'User:Sambot/Tasks/Ships');
try {
    $page->put ($datenotfound,"[[Wikipedia:Bots/Requests for approval/Sambot 6|BOT]]: Listing pages where launch date not found",true,false);
} catch (PillarException $e) {
    //
}