The following is automatically generated by LoxyBot

templatemain.run.php (LoxyBot)

edit
#!/usr/bin/php 
<?php
/*********************************************************************\
* Template:Main script
*
* This file is part of LoxyBot, a bot created by User:Foxy Loxy
* at <http://en.wikipedia.org/wiki/User:LoxyBot>.
*
* LoxyBot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* LoxyBot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LoxyBot.  If not, see <http://www.gnu.org/licenses/>.
*
* This code is optimized to be run from the command line.
\*********************************************************************/
$Main_Version = '0.4';

/**
* = Files to include =
* pwiki.class.php; 	PHPediaWiki is the bot framework designed by Foxy Loxy (originally ported from SxWiki) for use with his bots.
**/
require_once 		"pwiki.class.php";

/**
* = Pwiki class stuff = 
* This varible opens the Pwiki class for use.
* Make sure to call it with every Pwiki based command, e.g. $pw->getPage instead of getPage.
**/
$configurationfile = 'configfaster.include.php';
$extensions = array(
		    'templatemanipulation.extends.php',
		    'categorymanipulation.extends.php',
		    'pagechecks.extends.php',
		    );
$verbosity = 0;
$logverbosity = 4;
$pwlogfile = 'templatemain.log';
$logerrors = true;
$pw			= new Pwiki($configurationfile,$extensions,$verbosity,$pwlogverbosity,$pwlogfile,$logerrors);
$username 		= $pw->username;

$stpage			= "User:$username/SourceTemplateGeneric";
$sourcecode		= str_replace('$$LoXyBoT$$', file_get_contents(__FILE__), $pw->getPage($stpage));
$sourcepage		= $pw->getPage("User:$username/TemplateMain/Source");
$whotoinform = $pw->getPage("User:$username/TemplateMain/WhoToInform");
$runpage = "User:$username/TemplateMain/run";
$sourcesum		= "Writing source code. Script $Main_Version. Pwiki $Pwiki_Version.";

if (preg_match('/(yes|go|run)/i',$runpage) === false) {
  $pw->_exit('Runpage is not \'yes\'');
 } else {
  $pw->putPage($runpage,'Now running bot, changing run value back to no.','no',true);
 }
  
if ($pw->getPage($sourcepage) != $sourcecode) {
	$pw->_debug(0,"Source code different at User:$username/Source, writing.");
	$pw->putPage($sourcepage,$sourcesum,$sourcecode);
	}


$logsize = $pw->getPage("User:$username/TemplateMain/Logsize");
$pw->_debug(0,"\$logsize = $logsize");
$page = 'Template:Main';
$backlinks = $pw->tm->getTransclusion($page, NULL, $logsize);
$outputsum = "Outputting result of Template:Main linking analysis to userspace.";
$outputpage = "User:$username/TemplateMain/Output";

$redirects = array():
$pw->_debug(2,"Pages to scan:");
$pw->_debug(2,print_r($backlinks, true));
foreach ($backlinks as $backlink) {
  $filteredpages['redlinks'][$backlink] = array();
  $filteredpages['redirects'][$backlink] = array();
  $filteredpages['dabs'][$backlink] = array();
  $pagesource = $pw->getPage($backlink);
  $regex = "/{{[mM]ain\|.*?}}/i";
  preg_match_all($regex,$pagesource,$matches);
  $checklist = array();
  foreach ($matches[0] as $match) {
    $result = $pw->tm->parseTemplate($match);
    $result = array_slice($result, 1);
    foreach ($result as $result_single) {
      $checklist[] = $result_single;
    }
  }
  foreach($checklist as $key => $value) {
    if($value == "" || $value == " " || is_null($value)) {
      unset($checklist[$key]);
    }
  }
  $pw->_debug(2,"$backlink - pages to check:");
  $pw->_debug(2,print_r($checklist, true));
  foreach ($checklist as $page) {
    $isRedirect = $pw->pc->isRedirect($page, true);
    $allpages[$backlink][] = $page;
    if ($isRedirect !== false) {
      $redarr = $filteredpages['redirects'][$backlink];
      if(!in_array($page, $redarr)) {
	$filteredpages['redirects'][$backlink][] = $page;
	$redirects[$backlink][]['old'] = $page;
	$redirects[$backlink][]['new'] = $isRedirect;
      }
    } elseif (!$pw->pc->doesPageExist($page)) {
      $linkarr = $filteredpages['redlinks'][$backlink];
      if(!in_array($page, $linkarr) || $check) {
	$filteredpages['redlinks'][$backlink][] = $page;
      }
    } elseif ($pw->pc->isDab($page)) {
      $dabarr = $filteredpages['dabs'][$backlink];
      if(!in_array($page,$dabarr)) {
	$filteredpages['dabs'][$backlink][] = $page;
      }
    }
  }
}

foreach($filteredpages as $fkey => $indivarrs) {
  foreach($indivarrs as $key => $value) {
    if(empty($value)) {
      unset($filteredpages[$fkey][$key]);
    }
  }
}

// This replaces redirects with their non-redirect counterparts
foreach($redirects as $backlink => $searchstrings) {
  $find = array();
  $replace = array();
  foreach($searchstrings as $searchstring) {
    $find_s = $searchstring['old'];
    $replace_s = $searchstring['new'];
    $find[] = "/{{[mM]ain\|(.*?)$find_s(.*?)}}/";
    $replace[] = "{{main|$1$replace_s$2}}";
  }
  $pagesource = $pw->getPage($backlink);
  $newsource = preg_replace($find, $replace, $pagesource);
  $pw->putPage($backlink,$redirReplaceSum,$newsource);
}

foreach($filteredpages as $type => $pages) {
  foreach($pages as $parentpage => $values) {
    ${$type . "list"} .= "* [[$parentpage]]\n";
    foreach ($values as $subpage) {
      ${$type . "list"} .= ":* [[$subpage]]\n";
    }
  }
}

if (!isset($redlinkslist)) {
  $redlinkslist = "\n * None in sample.";
 }
if (!isset($dabslist)) {
  $dabslist = "\n * None in sample.";
 }
if (!isset($redirectslist)) {
  $redirectslist = "\n * None in sample.";
 }

$genpage = <<<PAGE
This page is a generated list of Redlinks, Redirects and Disambiguation pages found in the usage of [[Template:Main]]. This page was generated by ~~~ on ~~~~~.



Links on this page are displayed in the format
* Parent
:* Articles linked from parent via Template:Main that fit criteria



'''Disambiguation pages''':
$dabslist



'''Redlinks''':
$redlinkslist



'''Redirects''':
$redirectslist

PAGE;

$pw->putPage($outputpage,$outputsum,$genpage);
$pw->putPage('User talk:'.$whotoinform, 'Template:Main computations completed', 'The result of these computations can be found at [[User:LoxyBot/TemplateMain/Output]]. Enjoy! ~~~~', false, false, true);
$pw->_exit("End of script.");
?>

pwiki.class.php

edit

LoxyBot now runs off the PHPediaWiki framework that I have ported from SxWiki and now develop myself. Pwiki can be downloaded from subversion at http://pwiki.svn.sourceforge.net/viewvc/pwiki/trunk/

config.include.php

edit

See http://pwiki.sf.net/docs/Configuration_file