User:SQLBot/tagem.php

From Wikipedia, the free encyclopedia

<?php
include('SxWiki.php');
include('config.php');
 
$charcats = file("char7");
foreach ($charcats as $pr_cat) {
        $pr_cat = rtrim($pr_cat);
        $searchfor = '/\{\{(WPMEXICO|WikiProject( |_)Mexico|ProjectMexico)/i';
        replace_ban($pr_cat, $searchfor, "WikiProject Mexico|class=|importance=");
}
 
function replace_ban($pr_cat, $searchfor, $templatetoplace) {
echo "Running $templatetoplace on $pr_cat\r\n";
$logfile = "User:SQLBot/tagem.log";
#$logfile = "User:SQLBot/tagem.log/test";
$insert = '{{' . $templatetoplace  . '}}';
$shell_insert = '{{' . $templatetoplace . '|nested=yes}}';
$replace = "
$shell_insert
}}
";
$shell_search = "/
\}\}
/";
$banshell = "/\{\{WikiProjectBannerShell( |\|)/i";
//End config
 
$in_cat = sxGetCat($pr_cat, "0");
$timenow = date("jMY H:i:s");
$log = "\r\n\r\n==Run initiated at $timenow==\r\n===Params===\r\n#Category: [[:Category:$pr_cat]]\r\n#Search string: <nowiki>$searchfor</nowiki>\r\n#Tagging with: <nowiki>$insert</nowiki>\r\n===Activity Log===\r\n";
$skipped = 0;
$changed = 0;
foreach($in_cat as $spage) {
        $stopnow = sxGetPage('User:SQLBot/tagem.run');
        if($stopnow != "go") {
                $sqloldtalk = sxGetPage('User talk:SQL');
                $leprop = sxLastEdited('User:SQLBot/tagem.run');
                $whostoppedme = $leprop[user];
                $whystopme = $leprop[editsum];
                $sqlnewtalk = "\r\n==Help! I've been thwarted!==\r\nHey, as instructed, I need to tell you that [[User:$whostoppedme|$whostoppedme]] has stopped me from running, with an edit summary of $whystopme. ~~~~\r\n";
                $sqlnewtalk = $sqloldtalk . $sqlnewtalk;
                sxPutPage('User talk:SQL', 'Emergency! Bot stopped!', $sqlnewtalk, $null);
                die("\r\nBot stopped by $whostoppedme\r\n");
        }
        $toch = 'Talk:' . $spage;
        $oldtext = sxGetPage($toch);
        if (preg_match($searchfor, $oldtext) != 1) {
                if (preg_match($banshell, $oldtext) != 0) {
                        $newtext = preg_replace($shell_search, $replace, $oldtext, 1);
                        $shelled = " '''Shelled'''";
                } else {
                        $newtext = $insert . "\r\n" . $oldtext;
                        $shelled = "";
                }
                $hitc = 0;
                $done = "n";
                while($done == "n") {
                        if($hitc >= 40) { die("Hit maxlag too many times, giving up.\r\n"); }
                        if (sxPutPage($toch, 'WikiProject tagging page using [[User:SQL/SxWiki|SxWiki]]', $newtext, $null)) {
                        #if ("o" == "o") {
                                $timenow = date("jMY H:i:s");
                                $log = $log . "* Changed [[$toch]] at ''$timenow''$shelled\t\n";
                                $done = "y";
                                echo ".";
                                $changed++;
                        } else {
                                $done = "n";
                                sleep(60);
                                echo "Maxlag hit, sleeping.\r\n";
                                $hitc++;
                        } 
                }
        } else {
                $timenow = date("jMY H:i:s");
                #$log = $log . "* Skipped [[$toch]] at ''$timenow''\r\n";
                echo "X";
                $skipped++;
        }
}
echo "\r\n";
$oldlog = sxGetPage($logfile);
$timenow = date("jMY H:i:s");
$log = $log . "===Results===\r\n*Changed: $changed articles\r\n*Skipped: $skipped articles\r\n*Run ended at '''$timenow'''";
$newlog = $oldlog . $log;
while($postlog == false) {
        if ($postlog == true) { die("\r\nComplete!\r\n"); }
        $postlog =sxPutPage($logfile, 'Uploading log using [[User:SQL/SxWiki|SxWiki]]', $newlog, $null);
        if ($postlog == false) { echo "Can't post log, Maxlag hit\r\n"; }
}
}
?>