User:RMCD bot/requestedmoves.php: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
v 4.20 – fix formatting for inline usage of Template:reflist-talk
Line 2: Line 2:
<?php
<?php
/** requestedmoves.php - A fork of requestedmoves.php for improving on said script
/** requestedmoves.php - A fork of requestedmoves.php for improving on said script
* Version 4.19
* Version 4.20
*
*
* (c) 2010 James Hare - http://en.wikipedia.org/wiki/User:Harej
* (c) 2010 James Hare - http://en.wikipedia.org/wiki/User:Harej
Line 194: Line 194:
$description[$transcludes[$i]] = preg_replace("/<li>/", " ", $description[$transcludes[$i]]); // replace html <li> (list) tags with spaces
$description[$transcludes[$i]] = preg_replace("/<li>/", " ", $description[$transcludes[$i]]); // replace html <li> (list) tags with spaces
$description[$transcludes[$i]] = preg_replace("/<\/li>/", " ", $description[$transcludes[$i]]);
$description[$transcludes[$i]] = preg_replace("/<\/li>/", " ", $description[$transcludes[$i]]);
$description[$transcludes[$i]] = preg_replace("/\{{2}\s?(reflist-talk|Reflist talk)\s?[^}]*\}{2}/", "\n{{reflist-talk}}\n:", $description[$transcludes[$i]]);


#echo "$lim Description->" . $description[$transcludes[$i]] . "\n";
#echo "$lim Description->" . $description[$transcludes[$i]] . "\n";
Line 211: Line 212:
}
}
}
}
$description[$transcludes[$i]] = preg_replace("/^((.*?)*\s?(&mdash;|—|&ndash;|–)\s?)/", "", $description[$transcludes[$i]]);
echo "Description: " . $description[$transcludes[$i]] . "\n";
echo "Description: " . $description[$transcludes[$i]] . "\n";
echo "Timestamp: " . $timestamp[$transcludes[$i]] . " - " . $t[0] . "\n";
echo "Timestamp: " . $timestamp[$transcludes[$i]] . " - " . $t[0] . "\n";
Line 244: Line 246:
echo "Adding entries to different lists...\n";
echo "Adding entries to different lists...\n";
foreach ($timestamp as $title => $time) {
foreach ($timestamp as $title => $time) {
#echo "\nDescription= " . $description[$title] . "\n";
$description[$title] = preg_replace("/^((.*?)*\s?(&mdash;|—|&ndash;|–)\s?)/", "", $description[$title]);
#echo "Description> " . $description[$title] . "\n";
#echo "Description> " . $description[$title] . "\n";
$description[$title] = preg_replace("/\*{1,2}\s?\[{2}[^\]]*\]{2}\s?→\s?\[{2}[^\]]*\]{2}/", "", $description[$title]);
$description[$title] = preg_replace("/\*{1,2}\s?\[{2}[^\]]*\]{2}\s?→\s?\[{2}[^\]]*\]{2}/", "", $description[$title]);

Revision as of 14:05, 19 January 2015

<?php
/** requestedmoves.php - A fork of requestedmoves.php for improving on said script
 *  Version 4.20
 *
 *  (c) 2010 James Hare - http://en.wikipedia.org/wiki/User:Harej
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *   
 *  Developers (add your self here if you worked on the code):
 *    James Hare - [[User:Harej]] - Wrote everything
 *    WBM - [[User:Wbm1058]] - August 2012 updates
 **/
set_time_limit(360);    # 6 minutes
ini_set("display_errors", 1);
error_reporting(E_ALL ^ E_NOTICE);
include("botclasses.php");  // Botclasses.php was written by User:Chris_G and is available under the GNU General Public License
include("logininfo.php");

const ds = 86400;    #number of seconds in a day
const ditmax = 8;    #array of dates, then backlog
const maxmoves = 150;  #maximum number of allowed moves in a multiple move request

$d = array(date("F j, Y"), date("F j, Y", time()-ds), date("F j, Y", time()-ds*2), date("F j, Y", time()-ds*3),
 date("F j, Y", time()-ds*4), date("F j, Y", time()-ds*5), date("F j, Y", time()-ds*6), date("F j, Y", time()-ds*7));
print_r($d);

echo "Logging in...\n";
$objwiki = new wikipedia();
$objwiki->login($rmuser, $rmpass);
echo "...done.\n";

$attempts = 0;
while (count($transcludes) == 0) {
		if ($attempts == 5) {
			die("Error 1");
		}
		else {
			echo "Checking for transclusions...\n";
			$transcludes = $objwiki->getTransclusions("Template:Requested move/dated");
			$attempts += 1;
		}
}
print_r($transcludes);

$addition = "";

for ($i = 0; $i < count($transcludes); $i++) {
	echo "\n__________\n" . $i . " Retrieving $transcludes[$i] contents...\n";
	
	$breakcounter = 0;
	while ($contents == "") {
		if ($breakcounter == 5) {
			die("Error 2");
		}
		else {
			$contents = $objwiki->getpage($transcludes[$i]);
			$breakcounter += 1;
		}
	}
	$breakcounter = 0;
	#echo "contents:\n";
	#echo "$contents";
	#echo "\n";
	
	# Section
	if (preg_match("/=+\s?.*\s?=+(?=\n+.*\{{2}(Requested move\/dated|movereq)+[^}]*\}{2}+)/iu", $contents, $m)) {
		$section[$transcludes[$i]] = preg_replace("/=+\s*/", "", $m[0]);
		$section[$transcludes[$i]] = preg_replace("/\s*=+\n*/", "", $section[$transcludes[$i]]);
		#echo "Section: " . $section[$transcludes[$i]] . "\n";
		# remove links from section titles
		$section[$transcludes[$i]] = preg_replace("/\[\[/", "", $section[$transcludes[$i]]);
		$section[$transcludes[$i]] = preg_replace("/\]\]/", "", $section[$transcludes[$i]]);
		echo "Section> " . $section[$transcludes[$i]] . "\n";
		if ($section[$transcludes[$i]] == "") {
			echo "It's NULL!!\n";
		}
	
		# Newtitle
		$regexpart1 = "/\{{2}\s?(Requested move\/dated|movereq)\s?[^}]*";
		$regexpart2 = "\}{2}/iu";
	
		while ($nt[0] == "") {
			preg_match($regexpart1 . $regexpart2, $contents, $nt);
			$regexpart1 .= "\n";
			$breakcounter += 1;
			if ($breakcounter == 5) {
				echo "Breaking from regex loop!!\n";
				break;
			}
		}

		#echo "nt ";
		#print_r($nt);
		$breakcounter = 0;
		$newtitlemeta = preg_replace("/\n+/", "", $nt[0]);
		$newtitlemeta = preg_replace("/ ?\| ?/", "|", $newtitlemeta);
		$newtitlemeta = preg_replace("/\{{2}\s?/", "", $newtitlemeta);
		$newtitlemeta = preg_replace("/\s?}{2}/", "", $newtitlemeta);
		$components = explode("|", $newtitlemeta);
		#echo "components ";
		#print_r($components);

		for ($multi = 1; $multi < count($components); $multi++) {
			#echo "multi " . $multi . "-->" . $components[$multi] . "\n";
			if (preg_match("/^current\d+\s?=\s?/i", $components[$multi], $check)) {
				preg_match("/\d+/", $check[0], $number);
				$number = $number[0] - 1;
				$currentname[$transcludes[$i]][$number] = preg_replace("/^current\d+\s?=\s?/i", "", $components[$multi]);
				echo "Current name> " . $number . ": " . $currentname[$transcludes[$i]][$number] . "\n";
				continue;
			}
			elseif (preg_match("/^new\d+\s?=\s?/i", $components[$multi], $check)) {
				preg_match("/\d+/", $check[0], $number);
				$number = $number[0] - 1;
				$newname[$transcludes[$i]][$number] = preg_replace("/\s?new\d+\s?=\s?/i", "", $components[$multi]);
				echo "New name> " . $number . ": " . $newname[$transcludes[$i]][$number] . "\n";
				if ($newname[$transcludes[$i]][$number] == "") {
					$newname[$transcludes[$i]][$number] = "?";
					echo "\nSetting NULL newname to ?";
				}
				continue;
			}
		}
		if ($newname[$transcludes[$i]][0] == "") {
			$currentname[$transcludes[$i]][0] = preg_replace("/(\s|_)?talk:/i", ":", $transcludes[$i]);
			$newname[$transcludes[$i]][0] = str_replace("1=", "", $components[1]);
		}
		for ($nom = 0; $nom < count($currentname[$transcludes[$i]]); $nom++) {
			echo "Current name: " . $nom . ": " . $currentname[$transcludes[$i]][$nom] . "\n";
		
			if (preg_match("/^(User|Wikipedia|File|MediaWiki|Template|Help|Category|Portal):/i",$currentname[$transcludes[$i]][$nom],$tpcp)) {
				$talkname = str_replace($tpcp[1],$tpcp[1].' talk',$currentname[$transcludes[$i]][$nom]);
			}
			else {
				$talkname = "Talk:" . $currentname[$transcludes[$i]][$nom];
			}
		
			if ($nom != 0) {
				$break = 0;
				$talkpage = "";
				while ($talkpage == "") {
					if ($break == 5) {
						break;
					}
					else {
						$talkpage = $objwiki->getpage($talkname);
						$break += 1;
					}
				}
				$check = strpos($talkpage, "<!-- " . $transcludes[$i] . " crosspost -->");
				if ($check === false && $objwiki->nobots($talkname,$botuser,$talkpage) == true) {
					$talkpage .= "\n\n==Move discussion in progress==\nThere is a move discussion in progress on [[" . $transcludes[$i] . "#" . $section[$transcludes[$i]] .
					 "|" . $transcludes[$i] . "]] which affects this page. Please participate on that page and not in this talk page section. Thank you. <!-- " .
				 	$transcludes[$i] . " crosspost --> —[[User:RMCD bot|RMCD bot]] ~~~~~";
					$objwiki->edit($talkname,$talkpage,"Notifying of move discussion",false,true);
				}
				unset($talkpage);
			}
		}
		for ($nom = 0; $nom < count($newname[$transcludes[$i]]); $nom++) {
			echo "New name: " . $nom . ": " . $newname[$transcludes[$i]][$nom] . "\n";
		}

		# Description and Timestamp
		$regex1 = "/\{{2}\s?(Requested move\/dated|movereq)\s?[^}]*\}{2}";
		$regex2 = "([0-2]\d):([0-5]\d),\s(\d{1,2})\s(\w*)\s(\d{4})\s\(UTC\).*/i";

		for ($lim = 0; $lim < maxmoves; $lim++) {
			$regex1 .= "\n*.*";
			preg_match($regex1 . $regex2, $contents, $m);
			#echo "m ";
			#print_r($m);
			$description[$transcludes[$i]] = preg_replace("/\{{2}\s?(Requested move\/dated|movereq)\s?[^}]*\}{2}\n*/i", "", $m[0]);
			$description[$transcludes[$i]] = preg_replace("/\n/", "  ", $description[$transcludes[$i]]);    // replace newlines with two spaces
			$description[$transcludes[$i]] = preg_replace("/<p>/", "  ", $description[$transcludes[$i]]);   // replace html <p> (paragraph) tags with spaces
			$description[$transcludes[$i]] = preg_replace("/<\/p>/", "  ", $description[$transcludes[$i]]);
			$description[$transcludes[$i]] = preg_replace("/<ol>/", "  ", $description[$transcludes[$i]]);  // replace html <ol> (ordered list) tags with spaces
			$description[$transcludes[$i]] = preg_replace("/<\/ol>/", "  ", $description[$transcludes[$i]]);
			$description[$transcludes[$i]] = preg_replace("/<ul>/", "  ", $description[$transcludes[$i]]);  // replace html <ul> (unordered list) tags with spaces
			$description[$transcludes[$i]] = preg_replace("/<\/ul>/", "  ", $description[$transcludes[$i]]);
			$description[$transcludes[$i]] = preg_replace("/<li>/", "  ", $description[$transcludes[$i]]);  // replace html <li> (list) tags with spaces
			$description[$transcludes[$i]] = preg_replace("/<\/li>/", "  ", $description[$transcludes[$i]]);
			$description[$transcludes[$i]] = preg_replace("/\{{2}\s?(reflist-talk|Reflist talk)\s?[^}]*\}{2}/", "\n{{reflist-talk}}\n:", $description[$transcludes[$i]]);

			#echo "$lim Description->" . $description[$transcludes[$i]] . "\n";
	
			$description[$transcludes[$i]] = preg_replace("/\[{2}.*?\]{2}\s*?→\s*?(\{{2}|\[{2}|).*?(\}{2}|\]{2}|\?)\s*?/", "", $description[$transcludes[$i]]);

			if (preg_match("/Relisted/i", $description[$transcludes[$i]]) === 1) {
				preg_match("/([0-2]\d):([0-5]\d),\s(\d{1,2})\s(\w*)\s(\d{4})\s\(UTC\)/i", $description[$transcludes[$i]], $t);
			}
			else {
				preg_match("/([0-2]\d):([0-5]\d),\s(\d{1,2})\s(\w*)\s(\d{4})\s\(UTC\)/i", $description[$transcludes[$i]], $t, 0, strlen($description[$transcludes[$i]])-32);
			}
			#print_r($t);
			$timestamp[$transcludes[$i]] = strtotime($t[0]);
			if ($description[$transcludes[$i]] != "") {
				break;
			}
		}
		$description[$transcludes[$i]] = preg_replace("/^((.*?)*\s?(&mdash;|—|&ndash;|–)\s?)/", "", $description[$transcludes[$i]]);
		echo "Description: " . $description[$transcludes[$i]] . "\n";
		echo "Timestamp: " . $timestamp[$transcludes[$i]] . " - " . $t[0] . "\n";
	
		unset($nt);
		unset($newtitlemeta);
		unset($components);
	}
	else {
		if (preg_match("/\{{2}\s?(Requested move\/dated|movereq)\s?/iu", $contents)) {
			echo "Malformed request, contents:\n";
			echo "$contents";
			echo "\n";
			$malformed .= "\n* [[" . $transcludes[$i] . "]]\n";
		}
		else {
			echo "A match was not found, contents:\n";
			echo "$contents";
			echo "\n";
		}
	}
	unset($contents);
}

echo "\n__________\nSorting by timestamp... ";
$keys = array_keys($timestamp);
$values = array_values($timestamp);
array_multisort($values, SORT_DESC, $keys);
$timestamp = array_combine($keys, $values);
echo "done.\n";


echo "Adding entries to different lists...\n";
foreach ($timestamp as $title => $time) {
	#echo "Description> " . $description[$title] . "\n";
	$description[$title] = preg_replace("/\*{1,2}\s?\[{2}[^\]]*\]{2}\s?→\s?\[{2}[^\]]*\]{2}/", "", $description[$title]);

	if ($newname[$title][0] == "?") {
		$theaddition = "* " . "''([[" . $title . "#" . $section[$title] . "|Discuss]])'' – '''[[" . $currentname[$title][0] . "]] → ?''' – " . $description[$title] . "\n";
		$oldaddition = "* " . "'''[[" . $currentname[$title][0] . "]] → ?''' – (''[[" . $title . "#" . $section[$title] . "|Discuss]]'') – " . $description[$title] . "\n";
		$summaddition = "*[[" . $currentname[$title][0] . "]] → ? – '''([[" . $title . "#" . $section[$title] . "|Discuss]])'''\n";
	}
	else {
		$theaddition = "* " . "''([[" . $title . "#" . $section[$title] . "|Discuss]])'' – '''[[" . $currentname[$title][0] . "]] → {{no redirect|" . $newname[$title][0] . "}}''' – " .
		 $description[$title] . "\n";

		$oldaddition = "* " . "'''[[" . $currentname[$title][0] . "]] → {{no redirect|" . $newname[$title][0] . "}}''' – (''[[" . $title . "#" . $section[$title] . "|Discuss]]'') – " .
		 $description[$title] . "\n";

		$summaddition = "*[[" . $currentname[$title][0] . "]] → {{no redirect|" . $newname[$title][0] . "}} – '''([[" . $title . "#" . $section[$title] . "|Discuss]])'''\n";
	}

	for ($indent = 1; $indent < count($currentname[$title]); $indent++) {
		if ($newname[$title][$indent] == "?") {
			$theaddition .= "** [[" . $currentname[$title][$indent] . "]]  → ?\n";
			$oldaddition .= "** [[" . $currentname[$title][$indent] . "]]  → ?\n";
		}
		else {
			$theaddition .= "** [[" . $currentname[$title][$indent] . "]]  → {{no redirect|" . $newname[$title][$indent] . "}}\n";
			$oldaddition .= "** [[" . $currentname[$title][$indent] . "]]  → {{no redirect|" . $newname[$title][$indent] . "}}\n";
		}
	}

	$theaddition .= "\n";
	$oldaddition .= "\n";

	for ($dit = 0; $dit < ditmax; $dit++) {
		if ($time > 0) {
			if (date("F j, Y", $time) == $d[$dit]) {
				$add[$dit] .= $theaddition;
				$oldadd[$dit] .= $oldaddition;
				$summ[$dit] .= $summaddition;
			}
			else {
				continue;
			}
		}
	}
	
	if ($time < strtotime($d[ditmax-1]) && $time != "") {
		$BLadd .= $theaddition;
		$BLold .= $oldaddition;
		$BLsumm .= $summaddition;
	}
	elseif ($time == "") {
		$MALadd .= $theaddition;
		$MALold .= $oldaddition;
		$MALsumm .= $summaddition;
	}
}

$submission = "{{#ifeq: {{FULLPAGENAME}} | Wikipedia:Requested moves/Current discussions | {{shortcut|WP:RMC|WP:RM/C|WP:RMCD}} | {{shortcut|WP:RM#C}} }}\n" .
    ":''This {{#ifeq: {{FULLPAGENAME}} | Wikipedia:Requested moves/Current discussions | page | section }} lists all requests filed or identified as potentially controversial which are" .
    " currently under discussion.''\n\n" . "{{ombox|text=Do not attempt to edit this list manually; [[User:RMCD bot|a bot]] will automatically update the page soon after the" .
    " {{tls|Requested move}} template is added to the discussion on" .
    " the relevant talk page. The entry is removed automatically soon after the discussion is closed.<br />'''To make a change to an entry, make the change on the linked talk page.'''}}\n\n" .
    "'''This list is also available in a [[Wikipedia:Requested moves/Current discussions (alt)|page-link-first format]].'''\n\n";

$oldsubmission = ":''This {{#ifeq: {{FULLPAGENAME}} | Wikipedia:Requested moves/Current discussions (alt) | page | section }} lists all requests filed or identified as potentially controversial which are" .
    " currently under discussion.''\n\n" . "{{ombox|text=Do not attempt to edit this list manually; [[User:RMCD bot|a bot]] will automatically update the page soon after the" .
    " {{tls|Requested move}} template is added to the discussion on" .
    " the relevant talk page. The entry is removed automatically soon after the discussion is closed.<br />'''To make a change to an entry, make the change on the linked talk page.'''}}\n\n" .
    "'''This list is also available in a [[Wikipedia:Requested moves/Current discussions|discussion-link-first format]].'''\n\n";

$summsubmission = "";

for ($dit = 0; $dit < ditmax; $dit++) {
	$submission .= "===" . $d[$dit] . "===\n";
	$submission .= $add[$dit];
	$oldsubmission .= "===" . $d[$dit] . "===\n";
	$oldsubmission .= $oldadd[$dit];
	$summsubmission .= "{{Dashboard grouping|c=#BDD8FF|'''[[Wikipedia:Requested moves#" . $d[$dit] . "|" . $d[$dit] . "]]}}\n";
	#$summsubmission .= "| group" . $counter . " = [[Wikipedia:Requested moves#" . $d[$dit] . "|" . $d[$dit] . "]]\n";
	$summsubmission .= $summ[$dit] . "\n{{collapse bottom}}\n";
}

if ($BLadd != "") {
	$submission .= "===Backlog===\n" . "{{shortcut|WP:RMB}}\n";
	$submission .= $BLadd;
	$oldsubmission .= "===Backlog===\n";
	$oldsubmission .= $BLold;
	$summsubmission .= "{{Dashboard grouping|c=#BDD8FF|'''[[Wikipedia:Requested moves#Backlog|Backlog]]'''}}\n";
	$summsubmission .= $BLsumm . "\n{{collapse bottom}}\n";
	$wprm = $objwiki->getpage("Wikipedia:Requested moves");
	$wprm = str_replace("{{admin backlog|bot=RMCD bot|disabled=yes}}", "{{admin backlog|bot=RMCD bot|backloglink=#Backlog}}", $wprm);
	echo "\nAdding backlog notice...\n";
	$objwiki->edit("Wikipedia:Requested moves",$wprm,"Adding backlog notice",false,true);
}
else {
	$wprm = $objwiki->getpage("Wikipedia:Requested moves");
	$wprm = str_replace("{{admin backlog|bot=RMCD bot|backloglink=#Backlog}}", "{{admin backlog|bot=RMCD bot|disabled=yes}}", $wprm);
	echo "\nRemoving backlog notice...\n";
	$objwiki->edit("Wikipedia:Requested moves",$wprm,"Removing backlog notice",false,true);
}

if ($MALadd != "") {
	$submission .= "===Time could not be ascertained===\n";
	$submission .= $MALadd;
	$oldsubmission .= "===Time could not be ascertained===\n";
	$oldsubmission .= $MALold;
	$summsubmission .= "{{Dashboard grouping|c=#BDD8FF|'''[[Wikipedia:Requested moves#Time could not be ascertained|Time could not be ascertained]]'''}}\n";
	$summsubmission .= $MALsumm . "\n{{collapse bottom}}\n";
}

if ($malformed != "") {
	$submission .= "===Malformed requests===\n{{hatnote|Did you remember to submit your request by using {{tlxs|Requested move}}?" .
	  " See [[Wikipedia:Requested moves/Closing instructions#Bot considerations|\"Bot considerations\"]]}}\n";
	$submission .= $malformed;
	$oldsubmission .= "===Malformed requests===\n{{hatnote|Did you remember to submit your request by using {{tlxs|Requested move}}?" .
	  " See [[Wikipedia:Requested moves/Closing instructions#Bot considerations|\"Bot considerations\"]]}}\n";
	$oldsubmission .= $malformed;
}

$submission .= "===References===\n" .
    "{{hatnote|References generally should not appear here. Use {{tlx|reflist-talk}} in the talk page section with the requested move to show references there.}}\n" .
    "<references/>\n\n[[Category:Requested moves| ]]\n";

$oldsubmission .= "===References===\n" .
    "{{hatnote|References generally should not appear here. Use {{tlx|reflist-talk}} in the talk page section with the requested move to show references there.}}\n" .
    "<references/>\n\n[[Category:Requested moves| ]]\n";

echo "\nPosting the new requested pagemoves...\n";
$objwiki->edit("Wikipedia:Requested moves/Current discussions",$submission,"Updating requested pagemoves list",false,true);
$objwiki->edit("Wikipedia:Requested moves/Current discussions (alt)",$oldsubmission,"Updating requested pagemoves list",false,true);
$objwiki->edit("Wikipedia:Dashboard/Requested moves",$summsubmission,"Updating requested pagemoves list",false,true);
echo "done.\n";

echo "\nMission accomplished.\n\n";

?>