<?php
error_reporting(E_STRICT|E_ALL);
include('trunk/class.pillar.php');
$pillar = Pillar::ini_launch('images.ini');
for ($i = 1 ; ; $i++) {
try {
$ffdroot = new Page ('Wikipedia:Files for deletion',false,Page::TEMPLATES);
} catch (PillarException $e) {
if ($i == 5) {
Pillar::report('[[WP:FFD]] inaccessible; quitting',PILLAR_ACTION);
die();
}
continue;
}
break;
}
$ffdpages = array_filter($ffdroot->get_templates(),'ffd'); //array of all pages included in this page that appear to be daily subpages
preg_match('/==\s*Old discussions\s*==\s(.*?)\s*==/is',$ffdroot->get_text(),$match);
$holdingcell = $match[1];
preg_match_all('/\*\[\[\/(.*?)\]\]/',$holdingcell,$matches);
foreach ($matches[1] as $page) {
$ffdpages[] = 'Wikipedia:Files for deletion/'. $page;
}
$ffdpages = array_values($ffdpages); //sequentially numeric-indexed array
for ($j = 0 ; $j < count($ffdpages) ; ++$j) {
$title =& $ffdpages[$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 );
array_shift ($array); //first index is the description at the top of the page
for ($i = 0 ; $i < count($array) ; $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 {
for ($j = 0 ; ; $j++) {
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 {
for ($j = 0 ; ; $j++) {
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) {
for ($i = 1 ; ; $i++) {
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,'ffdc')) {
$template = new Template ($imageuse,'ffdc');
} elseif (false !== stripos($imageuse,'ifdc')) {
$template = new Template ($imageuse,'ifdc');
} else {
return;
}
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 12|BOT]]: removing {{ffdc}} from non-deleted image',0);
} catch (PillarActionCancelled $e) {
return;
} catch (PillarException $e) {
kept($title,$imagename);
}
}
function open ($title,$imagename,$puiname) {
for ($i = 1 ; ; $i++) {
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*[if]fdc/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 .= ' {{ffdc|' . preg_replace('/^\s*(?:File|Image):/i','',$imagename) . '|' . $puiname . '}}';
$added = true;
}
$addlink = '|' . $bit . $addlink;
}
}
if (!$added) {
$addlink .= '| {{ffdc|' . 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 12|BOT]]: tagging [[:' . $imagename . ']] as {{[[Template:ffdc|ffdc]]}}',false);
} catch (PillarActionCancelled $e) {
return;
} catch (PillarException $e) {
open ($title,$imagename,$puiname);
}
}
function ffd($name) {
return (bool)preg_match('/^Wikipedia\:Files for deletion\/\d{4}/',$name);
}