User:Cyberbot II/Run/PC/source

<?php

ini_set('memory_limit','16M');

require_once( '/home/cyberpower678/Peachy/Init.php' );

$site = Peachy::newWiki( "cyberbotii" );

$site->set_runpage("User:Cyberbot II/Run/PC");

$request = $site->get_http()->get( 'https://en.wikipedia.org/w/index.php?title=Special:StablePages&offset=&limit=500000000' );

echo "Retrieving Protected Pages...\n";

//echo $request;
preg_match_all('/'.
			'\<li\>\<a href=(.*?) title=(.*?)\>(.*?)\<\/a\>(.*?)\[autoreview=(.*?)\]/i',$request,$list);
			
//print_r($list);

$names = $list[3];
$level = $list[5];

echo "Retrieved protected pages.\n";

$i = 0;

//Tag each page with a protection template
foreach($names as $page)	{
	$buffer = initPage($page)->get_text();
	echo "Processing ".$page."...\n";
	if( $level[$i] == "autoconfirmed" )	{
		$template = "";
	echo "Tagging ".$page." with ...\n";
		if( preg_match('/\{\{pp-pc1/i', $buffer ) )	{
			echo "Page already tagged with !\n";
		}	else	{
			if ($buffer == initPage($page)->get_text())	{
				initPage($page)->edit($template."\n".$buffer, "Tagging page with PC1 protection template.");
			}	else	{
				echo "Page is different from the analyzed buffer.  Aborting edit...";
			}
		}
	}	else	{
		$template = "{{pp-pc2}}";
		echo "Tagging ".$page." with {{pp-pc2}}...\n";
		if( preg_match('/\{\{pp-pc2/i', $buffer ) )	{
			echo "Page already tagged with {{pp-pc2}}!\n";
		}	else	{
			if ($buffer == initPage($page)->get_text())	{
				initPage($page)->edit($template."\n".$buffer, "Tagging page with PC2 protection template.");
			}	else	{
				echo "Page is different from the analyzed buffer.  Aborting edit...";
			}			
		}
	}
	$i = $i+1;
}

//Removing templates from unprotected pages
$templatelist = initPage("Template:Pp-pc1")->get_transclusions();
echo "\nVerifying that all pages are tagged correctly...\n";

foreach($templatelist as $page)	{
	$i = 0;
	$isprotected = false;
		foreach($names as $page2)	{
			if(htmlspecialchars ($page) == $page2)	{
				if($level[$i] == "autoconfirmed")	{
					echo $page." has a correct tag\n";
					$isprotected = true;
				}
			}
			$i = $i+1;
		}
		if($isprotected == false)	{
			echo $page." is not PC1 protected.  Removing template...\n";
			initPage($page)->edit(preg_replace('/\{\{(P)?(p)?p-pc1(.*?)\}\}/i','',initPage($page)->get_text()),"Removing .  Page is not protected.");
		}
	}
	
	$templatelist = initPage("Template:Pp-pc2")->get_transclusions();
	foreach($templatelist as $page)	{
		$i = 0;
		$isprotected = false;
		foreach($names as $page2)	{
			if($page == $page2)	{
				if($level[$i] == "review")	{
					echo $page." has a correct tag\n";
					$isprotected = true;
				}
			}
			$i = $i+1;
		}
		if($isprotected == false)	{
			echo $page." is not PC2 protected.  Removing template...\n";
			initPage($page)->edit(preg_replace('/\{\{(P)?(p)?p-pc2(.*?)\}\}/i','',initPage($page)->get_text()),"Removing {{Pp-pc2}}.  Page is not protected.");
		}
	}
	
	echo "Task completed!";