Jump to content

User:Sambot/Code/Assessment

From Wikipedia, the free encyclopedia
<?php

error_reporting(E_STRICT | E_ALL);

include('trunk/class.pillar.php');

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

$consecerrors = 0;

do {
    $pages = $pillar->cursite->get_categorymembers('Category:Unassessed college football articles',500,$continue);
    
    for ($i = 0 ; $i < count($pages) ; $i++) {
        $p = $pages[$i];
        
        $title = $p['title'];
        $classes = array();
        $class = false;
        
        try {
            $page = new Page ($title,false, Page::TEMPLATES);
        } catch (PillarException $e) {
            if ($consecerrors++ == 10) {
                Pillar::report('10 consecutive failures on page ' . $title . ': quitting',Pillar::ACTION);
                die();
            }
            --$i;
            continue;
        }
        $consecerrors = 0;
        
        foreach ($page->get_templates() as $tname) {
            $tname = preg_replace('/^' . preg_quote($pillar->cursite->get_namespacename(10) . ':','/') . '/','',$tname);
            $template = new Template($page,$tname);
            
            if ($template->fieldvalue('class')) {
                $classes[] = $template->fieldvalue('class');
            }
        }
        
        $classes = array_map('strtolower',array_unique($classes));
        
        if (empty($classes)) {
            try {
                $article = new Page($pillar->cursite->get_mainname($p['title']),false,Page::NOCONTENT | Page::CATEGORIES);
            } catch (PillarException $e) {
                --$i;
                continue;
            }
            
            foreach ($article->get_categories() as $cname) {
                if ($article->get_redirect()) {
                    $classes[] = 'redirect';
                    break;
                }
                if (preg_match('/^Category:.* stubs$/i',$cname)) {
                    $classes[] = 'stub';
                    break;
                }
                if ($cname == 'Category:All disambiguation pages') {
                    $classes[] = 'disambig';
                    break;
                }
            }
            
            if (empty($classes)) {
                continue;
            }
        }
        
        if (in_array('fa',$classes)) {
            $class = 'fa';
        } elseif (in_array('ga',$classes)) {
            $class = 'ga';
        } elseif (in_array('a',$classes)) {
            $class = 'a';
        } elseif (in_array('b',$classes)) {
            $class = 'b';
        } elseif (in_array('c',$classes)) {
            $class = 'c';
        } elseif (in_array('start',$classes)) {
            $class = 'start';
        } elseif (in_array('stub',$classes)) {
            $class = 'stub';
        } elseif (in_array('redirect',$classes)) {
            $class = 'redirect';
        } elseif (in_array('disambig',$classes)) {
            $class = 'disambig';
        }
        
        $template = new Template ($page,'WikiProject College football');
        
        $template->updatefield('class',$class);
        $template->updatefield('auto','yes');
        
        $text = $template->wholePage();
        
        try {
            $page->put($text,'[[Wikipedia:Bots/Requests for approval/Sambot 13|BOT]]: WikiProject College football auto-assessment',false,false);
        } catch (PillarActionCancelled $e) {
            continue;
        } catch (PillarException $e) {
            --$i;
            continue;
        }
    }
} while ($continue);

Pillar::report($pillar->cursite->get_edits_made() . ' pages edited',Pillar::ACTION);