<?php

error_reporting(E_STRICT|E_ALL);
include('trunk/class.pillar.php');
 
$pillar = Pillar::ini_launch('images.ini');
 
$i = 0;
while ($i++ < 5) {
    try {
        $puiroot = new Page ('Wikipedia:Possibly unfree files',false,Page::TEMPLATES);
    } catch (PillarException $e) {
        if ($i == 5) {
            Pillar::report('[[WP:PUI]] inaccessible; quitting',PILLAR_ACTION);
            die();
        }
        continue;
    }
    break;
}


$puipages = array_filter($puiroot->get_templates(),'pui'); //array of all pages included in this page that appear to be daily subpages
 
preg_match('/==Holding cell==\s(.*?)\s*==/is',$puiroot->get_text(),$match);
$holdingcell = $match[1];
 
preg_match_all('/\*\[\[\/(.*?)\]\]/',$holdingcell,$matches);
 
foreach ($matches[1] as $page) {
    $puipages[] = 'Wikipedia:Possibly unfree files/'. $page;
}
 
$puipages = array_values($puipages); //sequentially numeric-indexed array
 
 
for ($j = 0 ; $j < count($puipages) ; ++$j) {
    $title =& $puipages[$j];
 
    try {
        $page = new Page($title);
    } catch (PillarException $e) {
        --$j;
        continue;
    }
 
    $date = substr($title,32);
 
    $array = preg_split('/==== ?\[\[:?(File:.*)\]\] ?====/',$page,-1, PREG_SPLIT_DELIM_CAPTURE  );
    unset ($array[0]); //first index is the description at the top of the page
    $array = array_values($array);
    for ($i = 0 ; $i < count($array) ; $i = $i + 2) { //even indices are image names; odd indices are discussions
        if (strpos($array[$i + 1],'xfd-closed')) {
            try {
                $page = new Page ($array[$i]);
            } catch (PillarException $e) {
                --$i;
                continue;
            }
 
            if ($page->get_exists()) { //page was kept
                do {
                    $j = 0;
                    while ($j < 10) {
                        try {
                            $pages = $pillar->cursite->get_imageuse($array[$i],500,$continue);
                        } catch (PillarException $e) {
                            if ($j == 10) {
                                Pillar::report('10 consecutive failures getting pages using ' . $array[$i] . ': quitting',Pillar::ACTION);
                                die();
                            }
                            continue;
                        }
                        break;
                    }
 
                    foreach ($pages as $title) {
                        kept($title,$array[$i]);
                    }
                } while ($continue);
            }
        } else { //PUI still open
            do {
                $j = 0;
                while ($j < 10) {
                    try {
                        $pages = $pillar->cursite->get_imageuse($array[$i],500,$continue);
                    } catch (PillarException $e) {
                        if ($j == 10) {
                            Pillar::report('10 consecutive failures getting pages using ' . $array[$i] . ': quitting',Pillar::ACTION);
                            die();
                        }
                        continue;
                    }
                    break;
                }
 
                foreach ($pages as $title) {
                    open($title,$array[$i],$date);
                }
            } while ($continue);
        }
    }
}
 
function kept($title,$imagename) {
    $i = 0;
    while ($i++ < 10) {
        try {
            $page = new Page($title);
        } catch (PillarException $e) {
            if ($i == 10) {
                Pillar::report($title . ' is inaccessible on 10 consecutive attempts: quitting');
                die();
            }
            continue;
        }
        break;
    }
    $text = $page->get_text();
 
    $regex = '/\[\[' . preg_replace ('/^File/i','(?:(?:File)|(?:Image))',preg_quote($imagename,'/')) . '.*/is';
 
    preg_match_all($regex,$page->get_text(),$matches);
 
    foreach ($matches[0] as $match) {
        $counter = 2;
        $position = 2;
 
        while ($position < strlen($match)) {
            if ($match[$position] == '[') {
                ++$counter;
            } elseif ($match[$position] == ']') {
                --$counter;
            }
 
            ++$position;
 
            if ($counter == 0) {
                break 1; //the while loop only
            }
        }
 
        $imageuse = substr($match,0,$position);
 
        if (false !== stripos($imageuse,'puic')) {
            $template = new Template ($imageuse,'puic');
        } elseif (false !== stripos($imageuse,'pufc')) {
            $template = new Template ($imageuse,'pufc');
        }
 
        if (isset($template)) {
            $newimageuse = preg_replace('/[\s\|]*' . preg_quote($template->templatestring,'/') . '/','',$imageuse);
            $text = str_replace($imageuse,$newimageuse,$text);
        }
    }
 
    try {
        $page->put($text,'[[Wikipedia:Bots/Requests for approval/Sambot 11|BOT]]: removing {{puic}} from non-deleted image',0);
    } catch (PillarActionCancelled $e) {
        return;
    } catch (PillarException $e) {
        kept($title,$imagename);
    }
}
 
function open ($title,$imagename,$puiname) {
    $i = 0;
    while ($i++ < 10) {
        try {
            $page = new Page($title);
        } catch (PillarException $e) {
            if ($i == 10) {
                Pillar::report($title . ' is inaccessible on 10 consecutive attempts: quitting');
                die();
            }
            continue;
        }
        break;
    }
    
    $text = $page->get_text();
 
    $regex = '/\[\[' . preg_replace ('/^File/i','(?:(?:File)|(?:Image))',preg_quote($imagename,'/')) . '.*/is';
 
    preg_match_all($regex,$page->get_text(),$matches);
 
    foreach ($matches[0] as $match) {
        $counter = 2;
        $position = 2;
 
        while ($position < strlen($match)) {
            if ($match[$position] == '[') {
                ++$counter;
            } elseif ($match[$position] == ']') {
                --$counter;
            }
 
            ++$position;
 
            if ($counter == 0) {
                break 1; //the while loop only
            }
        }
 
        $imageuse = substr($match,0,$position);
 
        if (preg_match('/[\|]?\{\{\s*pu[fi]c/i',$imageuse)) {
            continue;
        }
 
        $current = '';
        $template = 0;
        $link = false;
 
        for ($i = 2 ; $i < strlen($imageuse) ; $i++) {
            if ($current && !$template && !$link && ($imageuse[$i] == '|')) {
                $bits[] = $current;
                $current = '';
                continue;
            }
 
            if (!$template && !$link && ($imageuse[$i] == ']') && ($imageuse[$i + 1] == ']')) {
                if ($current) {
                    $bits[] = $current;
                } elseif ($imageuse[$i - 1] == '|') {
                    $bits[] = '';
                }
 
                break;
            }
 
            if (($imageuse[$i] == '{') && ($imageuse[$i + 1] == '{')) {
                $template++;
            }
 
            if (($imageuse[$i] == '}') && ($imageuse[$i + 1] == '}')) {
                $template--;
            }
 
            if (!$link && ($imageuse[$i] == '[') && ($imageuse[$i + 1] == '[')) {
                $link = true;
            }
 
            if ($link && ($imageuse[$i] == ']') && ($imageuse[$i + 1] == ']')) {
                $link = false;
            }
 
            $current .= $imageuse[$i];
        }
 
        $link = '[[' . $bits[0];
 
        array_shift($bits);
 
        $added = false;
        $addlink = '';
        $regex = '/^\s*(?:(?:thumb)|(?:frame)|(?:border)|(?:right)|(?:left)|(?:center)|(?:none)|(?:link=)|(?:\d+\s*px)|(?:\d+x\d+\s*px(?:px)?)\s*$)/i';
 
        for ($i = count($bits) - 1 ; $i >= 0 ; --$i) {
            $bit = $bits[$i];
            
            if (preg_match($regex,$bit)) {
                $addlink = '|' . $bit . $addlink;
            } else {
                if (!$added) {
                    $bit .= ' {{puic|' . preg_replace('/^\s*(?:(?:File)|(?:Image)):/i','',$imagename) . '|' . $puiname . '}}';
                    $added = true;
                }
                $addlink = '|' . $bit . $addlink;
            }
        }
        
        if (!$added) {
            $addlink .= '| {{puic|' . preg_replace('/^\s*(?:(?:File)|(?:Image)):/i','',$imagename) . '|' . $puiname . '}}';
        }
 
        $link .= $addlink . ']]';
 
        $text = str_replace($imageuse,$link,$text);
    }
 
    try {
        $page->put($text,'[[Wikipedia:Bots/Requests for approval/Sambot 11|BOT]]: tagging [[:' . $imagename . ']] as {{[[Template:puic|puic]]}}',false);
    } catch (PillarActionCancelled $e) {
        return;
    } catch (PillarException $e) {
        open ($title,$imagename,$puiname);
    }
}
 
function pui($name) {
    if (preg_match('/^Wikipedia\:Possibly unfree files\/\d{4}/',$name)) {
        return true;
    } else {
        return false;
    }
}