User:RMCD bot/requestedmoves.php

From Wikipedia, the free encyclopedia
<?php
/** requestedmoves.php
 *
 *  (c) 2010 James Hare - http://en.wikipedia.org/wiki/User:Harej
 *  (c) 2012-2024 Bill  - http://en.wikipedia.org/wiki/User:wbm1058
 *
 *  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, WikiProject notifications (June 2015), Subject-space notifications (August 2016)
 **/
set_time_limit(1440);    # 24 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 bot_version = "8.51";    #version 8+ requires PHP 8.0 or higher (uses new function str_starts_with)
const ds = 86400;    #number of seconds in a day
const ditmax = 8;    #array of dates, then backlog
const botuser = "RMCD bot";
const p_hats = "ital.*|pp-.*|use dmy.*|use mdy.*|featured article|good article";
const hats = "short description|about|about2|about-distinguish|about-distinguish2|ambiguous link|for|for2|for timeline|dablink|" .
	"distinguish|distinguish2|distinguish-otheruses|distinguish-otheruses2|further|further2|hatnote|other|others|see also|this|" .
	"otherpeople|otherpeople1|otherpeople2|otherpeople3|other people|other people2|other people3|other persons|otherpersons|otherpersons2|" .
	"other hurricanes|otherplaces|other places|otherplaces3|other places3|otherships|other ships|" .
	"other uses|other uses2|otheruses|otheruses2|otheruses3|otheruses4|other uses of|otheruse|outline|disambig-acronym|" .
	"redirect-acronym|redirect-distinguish|redirect-distinguish2|redirect-multi|redirect-several|redirect|redirect2|redirect3|selfref";
const dabs = "disambiguation|disambig|dab|geodis|hndis|mil-unit-dis|numberdis";
const namespaces = "User|Wikipedia|File|MediaWiki|Template|Module|Help|Category|Portal|Book|Draft";
const reflists = "reflist-talk|reflist talk|talk-reflist|reftalk|talk reflist|talk ref|ref talk|reference talk|talk reference|talkref|tref|" .
	"talk page reference|ref-talk|reflisttalk";
const collapsed = "collapse bottom|cob|cbot|collapsed bottom|collapsebottom|colbot|collapse end|collapse bot|collapse-bottom";
const rms = "(Requested move\/dated|movereq)";
const ntc = "(Requested move notice|Title notice|User:RMCD bot\/subject notice)";

function mb_ucfirst (string $str, ?string $encoding = null): string {
	return mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding) . mb_substr($str, 1, null, $encoding);
}

function talkpagename ($pagename) {
	if (preg_match("/^((" . namespaces . "):)/i",$pagename,$tpcp)) {
		$talkname = str_replace($tpcp[1],$tpcp[2].' talk:',$pagename);
	}
	else {
		$talkname = "Talk:" . $pagename;
	}
	$talkname = str_replace("alk::", "alk:", $talkname);
	return $talkname;
}
function wikititle ($targettitle) {
	$basename = preg_replace("/^(" . namespaces . "|)( |)(talk|):\s*/i","",$targettitle);
	$ucbasename = mb_ucfirst($basename);
	$targettitle = str_replace($basename,$ucbasename,$targettitle);
	$namesp = str_replace($ucbasename, "", $targettitle);
	$trimname = trim($namesp);
	$targettitle = str_replace($namesp,$trimname,$targettitle);
	$targettitle = str_replace("_"," ",$targettitle);
	$targettitle = trim($targettitle);
	$targettitle = mb_ucfirst($targettitle);
	$targettitle = preg_replace('/\s+/', ' ', $targettitle); #remove multiple consecutive whitespace characters & convert them into single spaces
	return $targettitle;
}

echo "PHP version: " . PHP_VERSION . "\n";
#phpinfo();
echo "Bot version: " . bot_version . "\n";

$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);
$current_time = time();
$twenty_ago = $current_time - 1200;
$week_ago = $current_time - ds*7;
echo "Current time: ". $current_time . " (" . date("Y-m-d H:i:s", $current_time) . ")\n";
echo "20 mins. ago: ". $twenty_ago . " (" . date("Y-m-d H:i:s", $twenty_ago) . ")\n";
echo "One week ago: ". $week_ago . " (" . date("Y-m-d H:i:s", $week_ago) . ")\n\n";

echo "Logging in...\n";
$objwiki = new wikipedia();
$objwiki->http->useragent = '[[User:RMCD bot]] php wikibot classes';
$objwiki->login($rmuser, $rmpass);
echo "...done.\n";

$transcludes = array();
$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);

# First pass
$names = 0;
$conflicts = 0;
$conflict = array();
$contents = array();
$currentname = array();
$newname = array();
$malformed = "";

for ($i = 0; $i < count($transcludes); $i++) {
	$subjectpagename[$i] = preg_replace("/^(" . namespaces . "|)( |)talk:/i","$1:",$transcludes[$i]);
	echo "\n" . $i . " Retrieving $transcludes[$i] (" . $subjectpagename[$i] . ") contents...\n";
	$breakcounter = 0;
	do {
		if ($breakcounter == 5) {
			die("Error 2");
		}
		else {
			$contents[$i] = $objwiki->getpage($transcludes[$i]);
			$breakcounter += 1;
		}
	} while ($contents[$i] == "");

	# Parse parameters
	preg_match("/\{{2}\s?" . rms . "\s?[^}]*\h?\}{2}/iu", $contents[$i], $parameters);
	#echo "parameters ";
	#print_r($parameters);

	$meta = preg_replace("/\n+/", "", $parameters[0]);
	$meta = preg_replace("/ ?\| ?/", "|", $meta);
	$meta = preg_replace("/\{{2}\s?/", "", $meta);
	$meta = preg_replace("/\s?}{2}/", "", $meta);
	$components = explode("|", $meta);
	#echo "components ";
	#print_r($components);
	$newname[$transcludes[$i]][0] = "";

	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] == "" && $currentname[$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]);
	}
	if (!isset($currentname[$transcludes[$i]][0])) {
		$currentname[$transcludes[$i]][0] = "";
		echo "\n?! Current name specification error\n";
	}
	for ($nom = 0; $nom < count($currentname[$transcludes[$i]]); $nom++) {
		if (isset($currentname[$transcludes[$i]][$nom])) {
			$pagename = $currentname[$transcludes[$i]][$nom];
		}
		else {
			$pagename = "";
		}

		for ($ni = 1; $ni < $names; $ni++) {
			if ($currentnames[$ni] == $pagename or $currentnames[$ni] == ":" . $pagename or ":" . $currentnames[$ni] == $pagename) {
				if ($pagename != "") {
					echo "\n?! Conflicting discussion found! $pagename\n";
					$malformed .= "\n* [[" . $transcludes[$i] . "]] – Conflicting discussion found! [[" . $pagename . "]]\n";
					$conflicts += 1;
					$conflict[$conflicts] = $pagename;
					goto nextname;
				}
			}
		}

		$names += 1;
		$currentnames[$names] = $pagename;
		nextname:
	}
	unset($parameters);
	unset($meta);
	unset($components);
}

echo "\n\nPages requested to be moved:\n";
print_r($currentnames);
echo "\nConflicts: $conflicts\n";
if ($conflicts > 0) print_r($conflict);

# Second pass
$lua = 0;
$css = 0;
$relisted = 0;
$leftright = 0;
$inserted = 0;
$multisigned = 0;
$outsidenot = 0;
$incomplete = "";

for ($i = 0; $i < count($transcludes); $i++) {
	if ($subjectpagename[$i] == $transcludes[$i]) {
		echo "\n__________\n" . $i . " Malformed request " . $transcludes[$i] . ", must be placed on a talk page\n";
		$malformed .= "\n* [[" . $transcludes[$i] . "]]\n";
		continue;
	}

	$elapsed_time = time() - $current_time;
	echo "\n__________\n" . $i . " Processing $transcludes[$i] (" . $subjectpagename[$i] . ") contents... (elapsed time: " .
	    date("i:s", $elapsed_time) . ")\n";

	#echo "contents:\n";
	#echo "$contents[$i]";
	#echo "\n";

	# Description and Timestamp
	$scontents = preg_replace('/\x{200e}/u', '', $contents[$i]); // strip left-to-right marks
	$ltrcontents = preg_replace('/\x{200e}/u', '&lrm;', $contents[$i]);
	if ($scontents != $contents[$i]) {
		echo "\n!! Left-to-right mark stripped!\n$ltrcontents\n\n";
		$leftright += 1;
	}
	if (preg_match("/\{{2}\s?" . rms . "\s?[^}]*\}{2}[\S\s]*?(?:(?:--|—|—&nbsp;)'''''Relist(?:ing|ed)\.'''''.*([0-2]\d):([0-5]\d),\s(\d{1,2})\s(\w*)\s(\d{4})\s\([A-Z]{3}\).*)/i",
	    $scontents, $m1)) {
		echo "\nRelisted\n";
	}
	else {
		echo "\nNot relisted\n";
		if (preg_match("/\{{2}\s?" . rms . "\s?[^}]*\}{2}[\S\s]*?(?:([0-2]\d):([0-5]\d),\s(\d{1,2})\s(\w*)\s(\d{4})\s\([A-Z]{3}\).*This is a contested technical request.*)/i",
		    $scontents, $m1)) {
			echo "\nContested technical request\n";
		}
		else {
			preg_match("/\{{2}\s?" . rms . "\s?[^}]*\}{2}[\S\s]*?(?:([0-2]\d):([0-5]\d),\s(\d{1,2})\s(\w*)\s(\d{4})\s\([A-Z]{3}\).*)/i", $scontents, $m1);
			if (preg_match("/([0-2]\d:[0-5]\d,\s\d{1,2}\s\w*\s\d{4}\s\([A-Z]{3}\).*){2,}/i", $m1[0], $dupchk)) {
				echo "\n!! Redundant signatures found!\n";
				echo "\n" . $m1[0] . "\n";
				print_r ($dupchk);
				$multisigned += 1;
			}
		}
	}
	#echo "m1";
	#print_r($m1);
	if ($m1[0] == "") {
		echo "\n!! Failure to parse Description and Timestamp!\n";
		$malformed .= "\n* [[" . $transcludes[$i] . "]] – Failure to parse Description and Timestamp\n";
		goto next_rm;
	}

	$dscr = preg_replace("/\{{2}\s?" . rms . "\s?[^}]*\}{2}\n*/i", "", $m1[0]);
	$dscr = preg_replace("/\n/", "  ", $dscr);    // replace newlines with two spaces
	$dscr = preg_replace("/<p>/", "  ", $dscr);   // replace html <p> (paragraph) tags with spaces
	$dscr = preg_replace("/<\/p>/", "  ", $dscr);
	$dscr = preg_replace("/<ol>/", "  ", $dscr);  // replace html <ol> (ordered list) tags with spaces
	$dscr = preg_replace("/<\/ol>/", "  ", $dscr);
	$dscr = preg_replace("/<ul>/", "  ", $dscr);  // replace html <ul> (unordered list) tags with spaces
	$dscr = preg_replace("/<\/ul>/", "  ", $dscr);
	$dscr = preg_replace("/<li>/", "  ", $dscr);  // replace html <li> (list) tags with spaces
	$dscr = preg_replace("/<\/li>/", "  ", $dscr);
	# newlines before and after {{reflist-talk}}, {{collapse bottom}}, and {{Search for}}
	$dscr = preg_replace("/\{{2}\s?(" . reflists . "|" . collapsed . "|Search for)\s?[^}]*\}{2}/iu", "\n$0\n:", $dscr);
	$description[$transcludes[$i]] = preg_replace("/\s*(\*\s)?\[{2}.*?\]{2}\s*?→\s*?(\{{2}|\[{2}|).*?(\}{2}|\]{2}|\?)\s*?/", "", $dscr,
	    count($currentname[$transcludes[$i]]));

	# Timestamp strings range in length from 24 bytes (May) to 30 bytes (September), so there may be up to 23 bytes following the timestamp
	# (24+23=47)
	preg_match("/([0-2]\d):([0-5]\d),\s(\d{1,2})\s(\w*)\s(\d{4})\s\([A-Z]{3}\)/i", $description[$transcludes[$i]], $ts, 0,
	    strlen($description[$transcludes[$i]])-47);
	#print_r($ts);
	$timestamp[$transcludes[$i]] = strtotime($ts[0]);

	if (preg_match("/(--|—|—&nbsp;)'''''Relist(ing|ed).'''''/i", $description[$transcludes[$i]]) === 1) {
		$relisted += 1;
		$dlink[$transcludes[$i]] = "Discuss";
		$olist = preg_replace("/\s*(<small>)?(--|—|—&nbsp;)'''''Relist(ing|ed).'''''.*/i", "", $description[$transcludes[$i]]);
		#echo "\nOrig. list: " . $olist . "\n";
		preg_match("/([0-2]\d):([0-5]\d),\s(\d{1,2})\s(\w*)\s(\d{4})\s\([A-Z]{3}\)/i", $olist, $ots, 0, strlen($olist)-47);
		#print_r($ots);
		$otimestamp[$transcludes[$i]] = strtotime($ots[0]);
	}
	else {
		$dlink[$transcludes[$i]] = "Discuss";
		$ots = $ts;
		$otimestamp[$transcludes[$i]] = $timestamp[$transcludes[$i]];
	}

	preg_match_all("/\{{2}\s?" . rms . "\s?/iu", $scontents, $mx);
	#echo "mx ";
	#print_r($mx);
	if (count($mx[0]) > 1) {
		echo "\nMultiple open requested moves on this page\n";
		$malformed .= "\n* [[" . $transcludes[$i] . "]] – Multiple open requested moves on this page\n";
	}
	else {
	    if (preg_match("/\[\[:?(.*)\]\]\s→\s(?=\?|\{\{[n|N]o redirect\|(.*?)\}\})/", $m1[0], $outside)) {
		#print_r($outside);
		if ($outside[1] != $currentname[$transcludes[$i]][0] and ":" . $outside[1] != $currentname[$transcludes[$i]][0]) {
			echo "\nName outside template: [[" . $outside[1] . "]] does not match name in template: [[" . $currentname[$transcludes[$i]][0] . "]]\n";
			$malformed .= "\n* [[" . $transcludes[$i] . "]] – Pagename to be moved listed below template: [[" . $outside[1] . "]] does not match name in template: [[" .
			    $currentname[$transcludes[$i]][0] . "]]. – Page may have been moved to the requested title.\n";
			$outsidenot += 1;
			goto next_rm;
		}
		if (count($outside) > 2) {
			$outside[2] = str_replace("1=", "", $outside[2]);
			if ($outside[2] != $newname[$transcludes[$i]][0] and ":" . $outside[2] != $newname[$transcludes[$i]][0] and $newname[$transcludes[$i]][0] != "?") {
				echo "\nRequested name outside template: [[" . $outside[2] . "]] does not match name in template: [[" . $newname[$transcludes[$i]][0] . "]]\n";
				$outsidenot += 1;
			}
		}
	    }
	    else {
		echo "\nName outside template does not match name in template: [[" . $currentname[$transcludes[$i]][0] . "]]\n";
		$malformed .= "\n* [[" . $transcludes[$i] . "]] – Pagename to be moved listed below template does not match name in template: [[" .
			    $currentname[$transcludes[$i]][0] . "]].\n";
		$outsidenot += 1;
		goto next_rm;
	    }
	}

	$rationale = preg_replace("/^((.*?)*\s?(&mdash;|—|&ndash;|–)\s)/", "", $description[$transcludes[$i]]);

	if ($rationale == "") {
		echo "No dash found\n";
	}
	else {
		$description[$transcludes[$i]] = $rationale;
	}

	if ($twenty_ago > $otimestamp[$transcludes[$i]] and $week_ago < $otimestamp[$transcludes[$i]]) {
		$delay_passed = true;
	}
	else {
		$delay_passed = false;
	}

	echo "Description: " . $description[$transcludes[$i]] . "\n";
	echo "Timestamp: " . $timestamp[$transcludes[$i]] . " - " . $ts[0] . "; Original timestamp: " . $otimestamp[$transcludes[$i]] . " - " .
	     $ots[0] . " Delay passed?: " . json_encode($delay_passed) . "\n";
	if ($otimestamp[$transcludes[$i]] == "" ) {
		echo "\nOriginal timestamp could not be ascertained\n";
		$malformed .= "\n* [[" . $transcludes[$i] . "]] – Original timestamp could not be ascertained; check relisting syntax\n";
	}

	# Section
	if (!preg_match("/==+\h?[^=].*\h?==+(?=\n*(.*)\n+.*\{{2}" . rms . "+[^}]*\}{2}+)/iu", $contents[$i], $m)) {
		if (preg_match("/\{{2}\s?" . rms . "\s?/iu", $contents[$i])) {
			echo "Malformed request, contents:\n";
			echo "$contents[$i]";
			echo "\n";
			$malformed .= "\n* [[" . $transcludes[$i] . "]]\n";
		}
		else {
			echo "A match was not found, contents:\n";
			echo "$contents[$i]";
			echo "\n";
		}
		unset($contents[$i]);
		continue;
	}

	print_r($m);
	if ($m[1] != "" and !str_starts_with($m[1], "===")) {
		echo "\nInserted text: " . $m[1] . "\n";
		$inserted += 1;
	}
	$section[$transcludes[$i]] = preg_replace("/=+\s*/", "", $m[0]);
	xyzzy: $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]] = trim(preg_replace("/\]\]/", "", $section[$transcludes[$i]]));
	echo "Section> ";
	if ($section[$transcludes[$i]] == "") {
		echo "It's NULL!!\n";
		if ($m[1] != "") {
			echo "\nMaking inserted text the section header\n";
			$section[$transcludes[$i]] = preg_replace("/=+\s*/", "", $m[1]);
			goto xyzzy;
		}
		$sectlink = "";
		$pipedlink = "";
	}
	else {
		$sectlink = $transcludes[$i] . "#" . $section[$transcludes[$i]];
		echo $sectlink . "\n";
		$pipedlink = $sectlink . "|" . $transcludes[$i];
	}

	# Newtitle(s) and notifications
	$nom1 = -1;
	$count = count($currentname[$transcludes[$i]]);

	for ($nom = 0; $nom < $count; $nom++) {
		skipblank: $nom1 += 1;
		if (isset($currentname[$transcludes[$i]][$nom1])) {
			$pagename = $currentname[$transcludes[$i]][$nom1];
		}
		else {
			$pagename = "";
		}
		if ($nom1 != 0 and ($pagename == $subjectpagename[$i] or ":" . $pagename == $subjectpagename[$i])) {
			if ($pagename == $currentname[$transcludes[$i]][0]) {
				echo "\n! Duplicate title detected (" . $pagename . "); cannot move the same page to two different places\n";
				$malformed .= "\n* [[" . $transcludes[$i] . "]] – Duplicate title detected (" . $pagename . "); cannot move the same page to two different places\n";
				goto next_rm;
			}
			echo "Current name: " . $nom1 . ": " . $pagename . " is hosting the discussion!\n";
			if ($firstlist === true) {
				echo "firstlist is true\n";
			}
		}
		else {
			echo "Current name: " . $nom1 . ": " . $pagename . "\n";
		}

		if ($pagename == "") {
			if ($nom1 < $count) {
				echo "*** Name is blank; skip to next *** $nom : $count\n";
				goto skipblank;
			}
			else continue;
		}

		$konflikt = false;
		if ($conflicts > 0) {
			for ($z = 1; $z <= $conflicts; $z++) {
				if ($conflict[$z] == $pagename) {
					echo "\n!! $pagename has conflicting discussions!\n\n";
					$malformed .= "\n* [[" . $transcludes[$i] . "]] – [[" . $pagename . "]] has a conflicting request for move on another page\n";
					$konflikt = true;
				}
			}
		}

		$talkname = wikititle(talkpagename($pagename));

		if (strpos($pagename, "Module:") === 0) {
			$getpagename = $pagename . "/doc";
			echo "\nNotices are placed on Lua module documentation pages: " . $getpagename . "\n";
			$lua += 1;
			$module = true;
		}
		else {
			$getpagename = $pagename;
			$module = false;
		}
		$break = 0;
		$pagecontents = "";
		while ($pagecontents == "") {
			if ($break == 5) {
				echo "PAGE IS BLANK OR DOES NOT EXIST\n";
				$malformed .= "\n* [[" . $transcludes[$i] . "]] – [[" . $getpagename . "]] is blank or does not exist\n";
				unset($pagecontents);
				goto aaa;
			}
			else {
				$pagecontents = $objwiki->getpage($getpagename);
				$break += 1;
			}
		}

		$good = true;
		if (preg_match("/^\n*\#REDIRECT(\s*|:\s*)\[{2}.*\]{2}/i", $pagecontents, $redirect)) {
			echo "\n*** PAGE " . $getpagename . " IS A REDIRECT!! ***\n";
			echo $pagecontents . "\n\n";
			$good = false;
			preg_match("/(?<=\[{2}).+(?=(\]{2}))/i", $redirect[0], $target);
			echo "Target: " . $target[0] . "\n";
			$target[0] = wikititle($target[0]);

			if ($target[0] == $getpagename) { // self-redirect, possibly from a page-mover swap
				echo $getpagename . " self-redirects to: " . $target[0] . "\n\n";
				$malformed .= "\n* [[" . $transcludes[$i] . "]] – [[" . $getpagename .
				    "]] self-redirects. May be in process of moving or closing.\n";
				goto next_rm;
			}
			else if ($target[0] == $newname[$transcludes[$i]][$nom1]) {
				echo $getpagename . " redirects to requested name: " . $target[0] . "\n\n";
				$malformed .= "\n* [[" . $transcludes[$i] . "]] – [[" . $getpagename . "]] redirects to requested name: [[" . $target[0] .
				    "]]. – May be in process of closing.\n";
				goto next_rm;
			}
			else {
				$malformed .= "\n* [[" . $transcludes[$i] . "]] – [[" . $getpagename . "]] redirects to [[" . $target[0] . "]]\n";
				if ($count == 1) {
					goto next_rm;
				}
			}
		}
		elseif (substr($pagename, -4) == ".css") {
			echo "\nNotices are not placed on CSS pages\n";
			$css += 1;
		}
		else {
			# Check for errors. In testing this I got a PREG_JIT_STACKLIMIT_ERROR in some cases,
			# which was solved by making the regex "unroll the loop"
			$retcode = preg_match("/\{{2}(?:" . hats . ")[^{]*(?:\{[^{]|\{\{[^{}]+\}\}[^{]*)*\}{2}/iu", $pagecontents, $m, PREG_OFFSET_CAPTURE);
			if ($retcode === 1) {
				#echo "-Hatnote found: " . $m[0][0] . " offset:" . $m[0][1] . "\n";
			}
			else if ($retcode === false) {
				echo "-ERROR: ";
				echo array_flip(get_defined_constants(true)['pcre'])[preg_last_error()] . "\n";
			}

			$pagecontents2 = $pagecontents;
			$hatnum = 0;

			# Match template {{...}} possibly with templates inside it, but no templates inside those:
			# [[Wikipedia:AutoWikiBrowser/Regular expression#Token matching]]
			while (preg_match("/\{{2}(" . hats . ")[^{]*(?:\{[^{]|\{\{[^{}]+\}\}[^{]*)*\}{2}/iu", $pagecontents2, $m, PREG_OFFSET_CAPTURE) === 1)
			{
				$hatnum += 1;
				echo ":Hatnote " . $hatnum . " found: " . $m[0][0] . " offset:" . $m[0][1] . "\n";
				$pagecontents2 = preg_replace("/(\{{2}(" . hats . ")[^{]*(?:\{[^{]|\{\{[^{}]+\}\}[^{]*)*\}{2}\n?)/iu","",$pagecontents2,1);
			}

			$subjectnotice = "{{User:RMCD bot/subject notice|1=" . $newname[$transcludes[$i]][$nom1] . "|2=" . $sectlink . "}}";
			$check = preg_match("/\{{2}" . ntc . ".*\}{2}/i", $pagecontents, $multitalk);

			if ($check === 1 && $konflikt == false) { // check for tampering
				if ($multitalk[0] != $subjectnotice) {
					echo "Subject page template: " . $multitalk[0] . "\n";
					echo "Expected: " . $subjectnotice . "\n";
					$pagecontents = str_replace($multitalk[0], $subjectnotice, $pagecontents);
					#echo $pagecontents;
					echo "\nSync modified subject page notice on " . $getpagename . "\n";
					$recent = $objwiki->recent_page_edits($getpagename,"RMCD bot");
					echo "==> RMCD bot recent edits to " . $getpagename .": " . $recent . "\n";
					if ($objwiki->nobots($getpagename,botuser,$pagecontents) == true and $recent < 3) {
						$objwiki->edit($getpagename,$pagecontents,"Sync modified notice of move discussion on [[" . $pipedlink . "]]",false,true);
					}
					else {
						echo "\n?! Edit failed due to {{nobots}} or recent-edits limit exceeded\n";					
					}
				}
			}
			if ($check === 0 && $objwiki->nobots($getpagename,botuser,$pagecontents) == true && $delay_passed == true && $konflikt == false) {
				$pagecontents2 = $pagecontents;
				$hatnum = 0;
				$hatnote = array_fill(1, 10, "");

				# Match template {{...}} possibly with templates inside it, but no templates inside those:
				# [[Wikipedia:AutoWikiBrowser/Regular expression#Token matching]]
				while (preg_match("/\n*(\{\{(" . p_hats . ")\}\})*\n*\{{2}(" . hats .
				    ")[^{]*(?:\{[^{]|\{\{[^{}]+\}\}[^{]*)*\}{2}/iu", $pagecontents2, $m, PREG_OFFSET_CAPTURE) === 1) {
					if ($m[0][1] == 0) {
						$hatnum += 1;
						$hatnote[$hatnum] = $m[0][0];
						echo ":Hatnote " . $hatnum . " found: " . $hatnote[$hatnum] . "\n";
						$pagecontents2 = preg_replace("/\n*(\{\{(" . p_hats . ")\}\})*\n*(\{{2}(" . hats .
							")[^{]*(?:\{[^{]|\{\{[^{}]+\}\}[^{]*)*\}{2}\n?)/iu","",$pagecontents2,1);
					}
					else {
						break;
					}
				}

				$pagecontents = "";
				echo "\nHatnum = " . $hatnum . "\n";
				print_r($hatnote);

				for ($ii = 1; $ii <= $hatnum; $ii++) {
					$pagecontents .= $hatnote[$ii] . "\n";
				}
				echo $pagecontents;
				if ($module == false) {
					$pagecontents .= "<noinclude>" . $subjectnotice . "\n</noinclude>" . $pagecontents2;
				}
				else if ($module == true) {
					$pagecontents .= $subjectnotice . "\n" . $pagecontents2;
				}

				#echo $pagecontents;
				echo "\nNotify subject page " . $getpagename . "\n";
				$recent = $objwiki->recent_page_edits($getpagename,"RMCD bot");
				echo "==> RMCD bot recent edits to " . $getpagename .": " . $recent . "\n";
				if ($recent < 1) {
					$objwiki->edit($getpagename,$pagecontents,"Notifying subject page of move discussion on [[" . $pipedlink . "]]",false,true);
				}
				else {
					echo "\n?! Edit failed due to a recent RMCD bot edit (0RR)\n";
				}
				if ($module == true) $objwiki->purgeCache($pagename);
			}
		}

		if ($nom1 == 0) {
			#echo "\n@* " . $subjectpagename[$i] . " +-+ " . $getpagename . "\n";
			$firstlist = false;
			if ($subjectpagename[$i] != $getpagename and $subjectpagename[$i] != ":" . $getpagename) {
				echo "\n" . $getpagename . " first-listed item in request on " . $subjectpagename[$i] . "\n";
				$firstlist = true;
			}
		}
		if (($nom1 != 0 or $firstlist === true) && $good === true) {
			$break = 0;
			$talkpage = "";
			while ($talkpage == "") {
				if ($break == 5) {
					break;
				}
				else {
					$talkpage = $objwiki->getpage($talkname);
					$break += 1;
				}
			}
			if (preg_match("/^\n*\#REDIRECT(\s*|:\s*)\[{2}.*\]{2}/i", $talkpage, $redirect)) {
				echo "\n" . $talkname . " REDIRECTS. May be a shared talk page.\n";
			}
			else {
				#$check = strpos($talkpage, "<!-- " . $sectlink . " crosspost -->");
				$check = strpos($talkpage, "{{User:RMCD bot/multimove");
				if ($check === false && $talkname != $transcludes[$i] && $objwiki->nobots($talkname,botuser,$talkpage) == true
				    && $delay_passed == true && $konflikt == false) {
					echo "\nNotify page " . $talkname . "\n";

					if (preg_match("/\=\= Move discussion in progress \=\=\n\nThere is a move discussion in progress on \[\[(.*)\#(.*)\|(.*)\]\] which/",
					    $talkpage,$crossnote)) {
					        echo "\nMatched notice!\n";
					        print_r($crossnote);
					        echo "**" . $transcludes[$i] . "**\n";
					        echo "**" . $section[$transcludes[$i]] . "**\n";
					        if ($crossnote[1] == $transcludes[$i] and $crossnote[2] == $section[$transcludes[$i]]) {
							echo "\nAdding template to existing notice\n";
							$talkpage = preg_replace(
							    "/\=\= Move discussion in progress \=\=\n\nThere is a move discussion in progress on \[\[(.*)\#(.*)\|(.*)\]\] which/",
							    "== Move discussion in progress ==\n\n{{User:RMCD bot/multimove|1=" . $newname[$transcludes[$i]][$nom1] .
							    "|2=" . $sectlink . "}}\n" . "There is a move discussion in progress on [[$1#$2|$3]] which", $talkpage);
							#echo "\n" . $talkpage . "\n\n";
							goto crossnoted;
					        }
					}
					if ($talkpage != "") {
						$talkpage .= "\n\n";
					}
					else if (preg_match("/\{{2}.*(" . dabs . ").*\}{2}/iu", $pagecontents)) {
						$talkpage = "{{WikiProject Disambiguation}}\n\n";
					}

					$talkpage .= "== Move discussion in progress ==\n\n{{User:RMCD bot/multimove|1=" . $newname[$transcludes[$i]][$nom1] .
					    "|2=" . $sectlink . "}}\n" . "There is a move discussion in progress on [[" . $pipedlink .
					    "]] which affects this page. Please participate on that page and not in this talk page section. Thank you. <!-- " .
			 	 	    $sectlink . " crosspost --> —[[User:RMCD bot|RMCD bot]] ~~~~~";
					crossnoted:$recent = $objwiki->recent_page_edits($talkname,"RMCD bot");
					echo "==> RMCD bot recent edits to " . $talkname .": " . $recent . "\n";
					if ($recent > 0) {
						echo "\n?! Edit failed due to a recent RMCD bot edit (0RR)\n";
						goto rrr;
					}
					$objwiki->edit($talkname,$talkpage,"Notifying of multimove discussion on [[" . $pipedlink . "]]",false,false);
					rrr:
				}
			}
			unset($talkpage);
		}
		unset($pagecontents);
	}
	aaa:

	if ($count != count($newname[$transcludes[$i]])) {
		echo "\n?? Counts aren't equal -- Current: " . $count . "  New: " . count($newname[$transcludes[$i]]) . "\n";
		$malformed .= "\n* [[" . $transcludes[$i] . "]] – Counts aren't equal -- Current: " . $count .
			"  New: " . count($newname[$transcludes[$i]]) . "\n";
	}

	$nom1 = -1;

	for ($nom = 0; $nom < $count; $nom++) {
		$nom1 += 1;
		while (!isset($currentname[$transcludes[$i]][$nom1]) && $nom1 < $count) {
			$nom1 += 1;
		}

		$konflikt = false;
		if ($conflicts > 0) {
			for ($z = 1; $z <= $conflicts; $z++) {
				if ($conflict[$z] == $currentname[$transcludes[$i]][$nom1]) {
				    echo "\n!!" . $currentname[$transcludes[$i]][$nom1] . " has conflicting discussions! (second loop)\n\n";
				    #$malformed .= "\n* [[" . $transcludes[$i] . "]] – [[" . $currentname[$transcludes[$i]][$nom1] .
				    #    "]] has a conflicting request for move on another page\n";
				    $konflikt = true;
				}
			}
		}

		$dabover[$transcludes[$i]][$nom1] = "";
		$talkname = talkpagename($newname[$transcludes[$i]][$nom1]);

		if ($newname[$transcludes[$i]][$nom1] == "?") {
			echo "New name: " . $nom1 . ": " . $newname[$transcludes[$i]][$nom1] . " (name to be decided)\n";
		}
		else if ($newname[$transcludes[$i]][$nom1] == "") {
			echo "? New name was not specified\n";
			$malformed .= "\n* [[" . $transcludes[$i] . "]] – [[" . $currentname[$transcludes[$i]][$nom1] . "]] new name was not specified\n";
			$newname[$transcludes[$i]][$nom1] = "?";
		}
		else if ($talkname == $transcludes[$i]) {
			echo "New name: " . $nom1 . ": " . $newname[$transcludes[$i]][$nom1] . " (moving over the current page)\n";
			$not_redir[$transcludes[$i]][$nom1] = "no";
		}
		else {
			aa:$break = 0;
			$talkpage = "";
			while ($talkpage == "") {
				if ($break == 5) {
					break;
				}
				else {
					$talkpage = $objwiki->getpage($talkname);
					$break += 1;
				}
			}
			if ($talkpage == "") {
				echo "New name: " . $nom1 . ": " . $newname[$transcludes[$i]][$nom1] . " - NO TALKPAGE\n";
				$contentpage = $objwiki->getpage($newname[$transcludes[$i]][$nom1]);
				if (preg_match("/^\n*\#REDIRECT(\s*|:\s*)\[{2}.*\]{2}/i", $contentpage, $redirect)) {
					preg_match("/(?<=\[{2}).+(?=(\]{2}))/i", $redirect[0], $target);
					$target[0] = wikititle($target[0]);
					echo $newname[$transcludes[$i]][$nom1] . " redirects to: " . $target[0];
					if ($subjectpagename[$i] != $target[0] and $subjectpagename[$i] != ":" . $target[0]) {
						$targettalkname = talkpagename($target[0]);
						if (strpos($targettalkname,"#")) {
							$len = strpos($targettalkname,"#");
							$targettalkname = substr($targettalkname,0,$len); // strip section links
						}
						if ($targettalkname != $transcludes[$i]) {
							echo " -- check $targettalkname\n";
							$break = 0;
							$targettalkpage = "";
							while ($targettalkpage == "") {
								if ($break == 5) {
									break;
								}
								else {
									$targettalkpage = $objwiki->getpage($targettalkname);
									$break += 1;
								}
							}

							if (preg_match("/^\n*\#REDIRECT(\s*|:\s*)\[{2}.*\]{2}/i", $targettalkpage, $redirect)) {
								echo " -- it's a REDIRECT!\n";
							}
							else {
								$check = strpos($targettalkpage, "<!-- " . $sectlink . " crosspost -->");
								if ($check === false && $objwiki->nobots($targettalkname,botuser,$targettalkpage) == true &&
								    $delay_passed == true &&$konflikt == false) {
								        echo " -- notify talk page\n";
								        if ($targettalkpage != "") {
								            $targettalkpage .= "\n\n";
								        }
								        else {
								            $targetcontent = $objwiki->getpage($target[0]);
								            if (preg_match("/\{{2}.*(" . dabs . ").*\}{2}/iu", $targetcontent)) {
								                $targettalkpage = "{{WikiProject Disambiguation}}\n\n";
								            }
								        }
								        $targettalkpage .= "== Move discussion in progress ==\n\nThere is a move discussion in progress on [[" .
				 	 			            $pipedlink . "]] which affects this page. Please participate on that page and not in this " .
				 	 			            "talk page section. Thank you. <!-- " . $sectlink . " crosspost --> —[[User:RMCD bot|RMCD bot]] ~~~~~";
								        $objwiki->edit($targettalkname,$targettalkpage,"Notifying target talkpage of move discussion on [[" .
				 	 			            $pipedlink . "]]",false,true);
								}
								else {
								        echo " -- already notified\n";
								}
							}
						}
						else {
							echo " -- hosting the discussion\n";
						}
					}
					else {
						echo " -- hosting discussion, count=" . $count . "\n";
						if (str_ends_with($newname[$transcludes[$i]][$nom1], "(disambiguation)") and $count == 1 and
						    !str_ends_with($currentname[$transcludes[$i]][$nom1], "(disambiguation)")) {
							echo $newname[$transcludes[$i]][$nom1] . " move is possibly MALPLACED\n";
							$incomplete .= "\n* [[" . $transcludes[$i] . "]] – [[" . $currentname[$transcludes[$i]][$nom1] . "]] is requested for move to [[" .
							    $newname[$transcludes[$i]][$nom1] . "]], which could make the disambiguation [[WP:MALPLACED|malplaced]]. " .
							        "Has a [[WP:PRIMARYREDIRECT|primary redirect]] been specified?\n";
						}
					}
					$not_redir[$transcludes[$i]][$nom1] = "no";
				}
				elseif ($contentpage != "" && $delay_passed == true && $konflikt == false) {
					echo "\nTarget page " . $newname[$transcludes[$i]][$nom1] . " has non-redirecting content.\n";
					$talkpage .= "== Move discussion in progress ==\n\nThere is a move discussion in progress on [[" . $pipedlink .
					    "]] which affects this page. Please participate on that page and not in this talk page section. Thank you. <!-- " .
					    $sectlink . " crosspost --> —[[User:RMCD bot|RMCD bot]] ~~~~~";
					echo "*** Post crosspost notice to $talkname ***\n";
					$objwiki->edit($talkname,$talkpage,"Notifying talkpage of move discussion on [[" . $pipedlink . "]]",false,true);
					$not_redir[$transcludes[$i]][$nom1] = "yes";
				}
				elseif ($contentpage == "") {
					#echo "\nTarget page " . $newname[$transcludes[$i]][$nom1] . " is a red link or blank page\n";
					$not_redir[$transcludes[$i]][$nom1] = "yes";
				}
			}
			else if (preg_match("/^\n*\#REDIRECT(\s*|:\s*)\[{2}.*\]{2}/i", $talkpage, $redirect)) {
				preg_match("/(?<=\[{2}).+(?=(\]{2}))/i", $redirect[0], $target);
				$target[0] = wikititle($target[0]);
				$not_redir[$transcludes[$i]][$nom1] = "no";

				if ($target[0] == str_ireplace("Talk:","",$target[0])) {
					echo "Talk page does not redirect to another talk page!!\n";
					$malformed .= "\n* [[" . $transcludes[$i] . "]] – [[" . $talkname . "]] Talk page does not redirect to another talk page!!\n";
				}
				else if ($target[0] == $transcludes[$i] or $target[0] == ":" . $transcludes[$i]) {
					echo "New name: " . $nom1 . ": " . $newname[$transcludes[$i]][$nom1] . " redirects :" . $redirect[0] . " → " . $target[0] . " (same)\n";
				}
				else {
					echo "New name: " . $nom1 . ": " . $newname[$transcludes[$i]][$nom1] . " redirects :" . $redirect[0] . " → " . $target[0] . " (different)\n";
					if ($talkname == $target[0]) {
						echo "Self-redirecting talk page!!\n";
						$malformed .= "\n* [[" . $transcludes[$i] . "]] – [[" . $talkname . "]] Self-redirecting talk page!!\n";
					}
					else {
						$talkname = $target[0];
						goto aa;
					}
				}
			}
			else {
				echo "New name: " . $nom1 . ": " . $newname[$transcludes[$i]][$nom1] . " (" . $talkname . " has non-redirecting content) ";

				$contentpage = $objwiki->getpage($newname[$transcludes[$i]][$nom1]);
				if (preg_match("/^\n*\#REDIRECT(\s*|:\s*)\[{2}.*\]{2}/i", $contentpage, $redirect)) {
					preg_match("/(?<=\[{2}).+(?=(\]{2}))/i", $redirect[0], $target);
					$target[0] = wikititle($target[0]);

					if ($currentname[$transcludes[$i]][$nom1] != $target[0] and $currentname[$transcludes[$i]][$nom1] != ":" . $target[0]) {
						echo "Target-page " . $newname[$transcludes[$i]][$nom1] . " is a REDIRECT to " . $target[0];
						$targettalkname = talkpagename($target[0]);
						if (strpos($targettalkname,"#")) {
							$len = strpos($targettalkname,"#");
							$targettalkname = substr($targettalkname,0,$len); // strip section links
						}
						if ($targettalkname != $transcludes[$i]) {
							echo " -- check $targettalkname\n";
							$break = 0;
							$targettalkpage = "";
							while ($targettalkpage == "") {
								if ($break == 5) {
									break;
								}
								else {
									$targettalkpage = $objwiki->getpage($targettalkname);
									$break += 1;
								}
							}

							if (preg_match("/^\n*\#REDIRECT(\s*|:\s*)\[{2}.*\]{2}/i", $targettalkpage, $redirect)) {
								echo " -- it's a REDIRECT!\n";
							}
							else {
								$check = strpos($targettalkpage, "<!-- " . $sectlink . " crosspost -->");
								if ($check === false && $objwiki->nobots($targettalkname,botuser,$targettalkpage) == true &&
								    $delay_passed == true &&$konflikt == false) {
								        echo " -- notify talk page\n";
								        if ($targettalkpage != "") {
								            $targettalkpage .= "\n\n";
								        }
								        else {
								            $targetcontent = $objwiki->getpage($target[0]);
								            if (preg_match("/\{{2}.*(" . dabs . ").*\}{2}/iu", $targetcontent)) {
								                $targettalkpage = "{{WikiProject Disambiguation}}\n\n";
								            }
								        }
								        $targettalkpage .= "== Move discussion in progress ==\n\nThere is a move discussion in progress on [[" .
				 	 			            $pipedlink . "]] which affects this page. Please participate on that page and not in this " .
				 	 			            "talk page section. Thank you. <!-- " . $sectlink . " crosspost --> —[[User:RMCD bot|RMCD bot]] ~~~~~";
								        $objwiki->edit($targettalkname,$targettalkpage,"Notified target talkpage of move discussion on [[" .
				 	 			            $pipedlink . "]]",false,true);
								}
								else {
								        echo " -- already notified\n";
								}
							}
						}
						else {
							echo " -- hosting the discussion\n";
						}
					}
					else {
						echo "Target-page " . $newname[$transcludes[$i]][$nom1] . " redirects back to " . $target[0] . "\n";
					}

					$not_redir[$transcludes[$i]][$nom1] = "no";
				}
				elseif ($contentpage != "") {
					echo "Target-page " . $newname[$transcludes[$i]][$nom1] . " has non-redirecting content\n";
					#print_r($currentname[$transcludes[$i]]);
					$found = false;
					for ($k = 0; $k < count($currentname[$transcludes[$i]]); $k++) {
						if ($currentname[$transcludes[$i]][$k] == $newname[$transcludes[$i]][$nom1]) {
							echo $currentname[$transcludes[$i]][$k] . " requested for move to " . $newname[$transcludes[$i]][$k] . "\n";
							$found = true;
						}
					}
					if ($found == false) {
						for ($ni = 1; $ni < $names; $ni++) {
							if ($currentnames[$ni] == $newname[$transcludes[$i]][$nom1] or $currentnames[$ni] == ":" . $newname[$transcludes[$i]][$nom1] or
							    ":" . $currentnames[$ni] == $newname[$transcludes[$i]][$nom1]) {
								echo $newname[$transcludes[$i]][$nom1] . " is requested for move in a conflicting RM discussion\n";
								$konflikt = true;
								$malformed .= "\n* [[" . $transcludes[$i] . "]] – [[" . $newname[$transcludes[$i]][$nom1] .
								    "]] is requested for move in a conflicting RM discussion\n";
								goto jumpconflict;
							}
						}
						echo $newname[$transcludes[$i]][$nom1] . " is not requested for move\n";
						echo $contentpage;
						if (preg_match("/\{{2}.*(" . dabs . ").*\}{2}/iu", $contentpage)) {
							echo "\n" . $newname[$transcludes[$i]][$nom1] . " is a disambiguation page\n";
							$r_to_dab = $newname[$transcludes[$i]][$nom1] . " (disambiguation)";
							$r_to_dab_content = $objwiki->getpage($r_to_dab);
							if ($r_to_dab_content != "") {
								echo "[[" . $r_to_dab . "]] contents:\n" . $r_to_dab_content . "\n";
								$dabover[$transcludes[$i]][$nom1] = "yes";
							}
							else {
								echo $r_to_dab . " not found\n";
							}
						}
						else {
							$incomplete .= "\n* [[" . $transcludes[$i] . "]] – [[" . $currentname[$transcludes[$i]][$nom1] . "]] is requested for move to [[" .
							    $newname[$transcludes[$i]][$nom1] . "]], which has non-redirecting content and is not requested for move\n";
						}
					}
					jumpconflict:$not_redir[$transcludes[$i]][$nom1] = "yes";
				}
				else {
					#echo "\nTarget page " . $newname[$transcludes[$i]][$nom1] . " is a red link or blank page\n";
					$not_redir[$transcludes[$i]][$nom1] = "yes";
				}

				$check = strpos($talkpage, "<!-- " . $sectlink . " crosspost -->");
				if ($check === false && $objwiki->nobots($talkname,botuser,$talkpage) == true && $delay_passed == true && $konflikt == false) {
				    if ($talkpage != "") {
				        $talkpage .= "\n\n";
				    }
				    $talkpage .= "== Move discussion in progress ==\n\nThere is a move discussion in progress on [[" . $pipedlink .
				        "]] which affects this page. Please participate on that page and not in this talk page section. Thank you. <!-- " .
			 	        $sectlink . " crosspost --> —[[User:RMCD bot|RMCD bot]] ~~~~~";
				    echo "*** Post cross-post notice to $talkname ***\n";
				    $objwiki->edit($talkname,$talkpage,"Notifying talk page of move discussion on [[" . $pipedlink . "]]",false,true);
				}
			}
			unset($talkpage);
		}
	}

	# Notify WikiProjects
	preg_match_all("/\{{2}WikiProject\s.*/i", $contents[$i], $projects);
	#print_r($projects[0]);
	for ($ii = 0; $ii < count($projects[0]); $ii++) {
		#echo "Projects> " . $projects[0][$ii] . "\n";

		if (stripos($projects[0][$ii], "banner") !== FALSE) {
			echo "\nBANNER> " . $projects[0][$ii] . "\n";
			goto b;
		}

		if (preg_match("/WikiProject\s.*?(?=(\||\}{2}))/i", $projects[0][$ii], $wikiproject)) {
			$wikiproject[0] = trim($wikiproject[0]);
			echo "\nProject> " . $wikiproject[0] . "\n";
		}
		else {
			preg_match("/WikiProject\s.*/i", $projects[0][$ii], $wikiproject);
			echo "\nPROJECT: " . $wikiproject[0] . "\n";
		}

		$projectname = "Wikipedia:" . $wikiproject[0];
		$templatename = "Template:" . $wikiproject[0];
		$break = 0;
		$projectpage = "";

		while ($projectpage == "") {
			if ($break == 5) {
				break;
			}
			elseif ($break == 1) {
				$projectpage = $objwiki->getpage($templatename);
				$break += 1;
			}
			else {
				$projectpage = $objwiki->getpage($projectname);
				$break += 1;
			}
		}

		if (preg_match("/^\n*\#REDIRECT(\s*|:\s*)\[{2}.*\]{2}/i", $projectpage, $redirect)) {
			if ($redirect[0] == "#REDIRECT [[Template:WikiProjectBannerShell]]") {
				echo "\nSkipping #REDIRECT [[Template:WikiProjectBannerShell]]\n";
				goto b;
			}
			if ($redirect[0] == "#REDIRECT [[Template:WikiProjectBanners]]") {
				echo "\nSkipping #REDIRECT [[Template:WikiProjectBanners]]\n";
				goto b;
			}
			if ($redirect[0] == "#REDIRECT [[Template:WPMILHIST VC migration]]") {
				echo "\nSkipping #REDIRECT [[Template:WPMILHIST VC migration]]\n";
				goto b;
			}
			echo "\nFollowing redirect :". $redirect[0] . "\n";
			preg_match("/(?<=\[{2}).+(?=(\]{2}))/i", $redirect[0], $redirect);
			#echo "\nFollowing redirect :". $redirect[0] . "\n";
			$alertname = $redirect[0] . "/Article alerts";
			$alertname = str_replace("Template:", "Wikipedia:", $alertname);
			$talkname = str_replace("Template:", "Wikipedia:", $redirect[0]);
			$talkname = str_replace("Wikipedia:", "Wikipedia talk:", $talkname);
		}
		else {
			$alertname = "Wikipedia:" . $wikiproject[0] . "/Article alerts";
			$talkname = "Wikipedia talk:" . $wikiproject[0];
		}

		$break = 0;
		$alertpage = "";

		while ($alertpage == "") {
			if ($break == 2) {
				break;
			}
			else {
				$alertpage = $objwiki->getpage($alertname);
				$break += 1;
			}
		}
		#print_r($alertpage);

		if ($alertpage != "") {
			echo $alertname . " exists. Skipping notification.";
		}
		else {
			a:$break = 0;
			$talkpage = "";

			while ($talkpage == "") {
				if ($break == 5) {
					echo "WIKIPROJECT PAGE IS BLANK OR DOES NOT EXIST\n";
					unset($talkpage);
					goto b;
				}
				else {
					$talkpage = $objwiki->getpage($talkname);
					$break += 1;
				}
			}
			#print_r($talkpage);
			if (preg_match("/^\n*\#REDIRECT(\s*|:\s*)\[{2}.*\]{2}/i", $talkpage, $redirect)) {
				echo "\nFollowing redirect:". $redirect[0] . "\n";
				preg_match("/(?<=\[{2}).+(?=(\]{2}))/i", $redirect[0], $redirect);
				#echo "\nFollowing redirect:". $redirect[0] . "\n";
				$talkname = $redirect[0];
				goto a;
			}

			$check = strpos($talkpage, "<!-- " . $sectlink . " crosspost -->");
			if ($check === false && $objwiki->nobots($talkname,botuser,$talkpage) == true && $talkname != $transcludes[$i]
			    && $delay_passed == true && $konflikt == false) {
				echo "Notify " . $talkname . " of discussion on " . $transcludes[$i] . ", Current name: " . $currentname[$transcludes[$i]][0] .
				 ", New name: " . $newname[$transcludes[$i]][0] . "\n";

				$talkpage .= "\n\n== [[" . $currentname[$transcludes[$i]][0] . "]] listed at Requested moves==\n[[File:Information.svg|30px|left]]" .
				    "A [[Wikipedia:Requested moves|requested move]] discussion has been initiated for [[" . $currentname[$transcludes[$i]][0] . "]] to be moved";

				if ($newname[$transcludes[$i]][0] != "?") {
				     $talkpage .= " to [[" . $newname[$transcludes[$i]][0] . "]]";
				}

				$talkpage .= ". This page is of interest to this WikiProject and interested members may want to participate in the discussion [[" .
				    $sectlink . "|here]].<!-- " . $sectlink . " crosspost --> —[[User:RMCD bot|RMCD bot]] ~~~~~" .
				    "\n:<small>To opt out of RM notifications on this page, transclude {{tlp|bots|2=deny=RMCD bot}}," .
				    " or set up [[Wikipedia:Article alerts|Article alerts]] for this WikiProject.</small>";

				#print_r($talkpage);
				$objwiki->edit($talkname,$talkpage,"Notifying WikiProject of move discussion on [[" . $pipedlink . "]]", false, true);
			}
			else {
				echo "Skipping WikiProject talk page\n";
			}
			unset($talkpage);
		}
		b:continue;
	}

	next_rm:unset($contents[$i]);
}

echo "\n__________\nLua modules: " . $lua;
echo "\nCSS pages: " . $css;
echo "\n" . $relisted . " items have been relisted";
echo "\n" . $leftright . " items with left-to-right marks";
echo "\n" . $inserted . " items with something inserted between header and template";
echo "\n" . $multisigned . " items with redundant signatures";
echo "\n" . $outsidenot . " items where requested name outside template doesn't match name inside\n";

echo "\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 "Sorting by original timestamp... ";
$keys = array_keys($otimestamp);
$values = array_values($otimestamp);
array_multisort($values, SORT_DESC, $keys);
$otimestamp = array_combine($keys, $values);
echo "done.\n";

foreach ($timestamp as $title => $time) {
	if (isset($currentname[$title][1]) and $currentname[$title][1] == $newname[$title][0]) {
		echo "\nswitch " . $currentname[$title][1];
		$tempc = $currentname[$title][0];
		$tempn = $newname[$title][0];
		$tempr = $not_redir[$title][0];
		$currentname[$title][0] = $currentname[$title][1];
		$newname[$title][0] = $newname[$title][1];
		if (isset($not_redir[$title][1])) $not_redir[$title][0] = $not_redir[$title][1];
		$currentname[$title][1] = $tempc;
		$newname[$title][1] = $tempn;
		$not_redir[$title][1] = $tempr;
	}
}

echo "Adding entries to different lists...\n";
$hatted = false;
$weekago = time()-ds*7;
$eightdays = time()-ds*8;
$add = array_fill(0, ditmax, "");
$oldadd = array_fill(0, ditmax, "");
$summ = array_fill(0, ditmax, "");
$BLadd = "";
$BLold = "";
$BLsumm = "";
$MALadd = "";
$MALold = "";
$MALsumm = "";
#print_r($not_redir);

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] . "|" . $dlink[$title] . "]])'' – '''[[" . $currentname[$title][0] . "]] → ?''' – " .
		    $description[$title] . "\n";
		$oldaddition = "* " . "'''[[" . $currentname[$title][0] . "]] → ?''' – (''[[" . $title . "#" . $section[$title] . "|" . $dlink[$title] . "]]'') – " .
		    $description[$title] . "\n";
		$summaddition = "*[[" . $currentname[$title][0] . "]] → ? – '''([[" . $title . "#" . $section[$title] . "|" . $dlink[$title] . "]])'''\n";
	}
	else if (!array_key_exists([$title][0], $not_redir)) {
		$theaddition = "* " . "''([[" . $title . "|" . $dlink[$title] . "]])'' – '''[[" . $currentname[$title][0] . "]] → {{no redirect|" .
		  $newname[$title][0] . "}}''' – " . $description[$title] . "\n";

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

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

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

		$summaddition = "*[[" . $currentname[$title][0] . "]] → [[" . $newname[$title][0] . "]] – '''([[" . $title . "#" . $section[$title] . "|" .
		  $dlink[$title] . "]])'''\n";

		if ($dabover[$title][0] == "yes") {
			$theaddition .= "** [[" . $newname[$title][0] . "]]  → {{no redirect|" . $newname[$title][0] . " (disambiguation)}} over redirect " .
			    "without leaving a redirect (implied), then [[WP:G14]] delete if unnecessary\n";
			$oldaddition .= "** [[" . $newname[$title][0] . "]]  → {{no redirect|" . $newname[$title][0] . " (disambiguation)}} over redirect " .
			    "without leaving a redirect (implied), then [[WP:G14]] delete if unnecessary\n";
			$currentname[$title][0] = $newname[$title][0]; // switch for the table, built below
			$newname[$title][0] = $newname[$title][0] . " (disambiguation)"; // switch for the table, built below
		}
	}
	else {
		$theaddition = "* " . "''([[" . $title . "#" . $section[$title] . "|" . $dlink[$title] . "]])'' – '''[[" . $currentname[$title][0] . "]] → {{no redirect|" .
		  $newname[$title][0] . "}}''' – " . $description[$title] . "\n";

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

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

	$indent = 0;

	for ($inden = 1; $inden < count($currentname[$title]); $inden++) {
		skipblank2: $indent += 1;
		if (!isset($newname[$title][$indent])) $newname[$title][$indent] = "";
		echo "\nindent: " . $inden . " | " . $indent . " > " . $newname[$title][$indent];

		if ($newname[$title][$indent] == "?") {
			$theaddition .= "** [[" . $currentname[$title][$indent] . "]]  → ?\n";
			$oldaddition .= "** [[" . $currentname[$title][$indent] . "]]  → ?\n";
		}
		else if ($newname[$title][$indent] != "") {
			if ($not_redir[$title][$indent] == "yes") {
				$theaddition .= "** [[" . $currentname[$title][$indent] . "]]  → [[" . $newname[$title][$indent] . "]]\n";
				$oldaddition .= "** [[" . $currentname[$title][$indent] . "]]  → [[" . $newname[$title][$indent] . "]]\n";

				if ($dabover[$title][$indent] == "yes") {
					$theaddition .= "** [[" . $newname[$title][$indent] . "]]  → {{no redirect|" . $newname[$title][$indent] . " (disambiguation)}} over redirect " .
					    "without leaving a redirect (implied), then [[WP:G14]] delete if unnecessary\n";
					$oldaddition .= "** [[" . $newname[$title][$indent] . "]]  → {{no redirect|" . $newname[$title][$indent] . " (disambiguation)}} over redirect " .
					    "without leaving a redirect (implied), then [[WP:G14]] delete if unnecessary\n";
				}
			}
			else {
				$theaddition .= "** [[" . $currentname[$title][$indent] . "]]  → {{no redirect|" . $newname[$title][$indent] . "}}\n";
				$oldaddition .= "** [[" . $currentname[$title][$indent] . "]]  → {{no redirect|" . $newname[$title][$indent] . "}}\n";
			}
		}
		else {
			if ($indent == count($currentname[$title])) break;
			goto skipblank2;
		}
	}

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

	#echo date("Y-m-d H:i:s", $time) . " (" . date("Y-m-d H:i:s", $weekago) . ")\n";

	for ($dit = 0; $dit < ditmax; $dit++) {
		if ($time > 0) {
			if (date("F j, Y", $time) == $d[$dit]) {
				
				if ($weekago > $time) {
				    #echo "Over a week ago\n";
				    if ($hatted == false) {
					$add[$dit] .= "===Elapsed listings===\n{{shortcut|WP:RME}}\n{{hatnote|The 7-day listing period has elapsed." .
					    " Items below may be closed if there's a consensus, or if discussion has run its course and consensus could not be achieved.}}\n";
					$oldadd[$dit] .= "===Elapsed listings===\n{{hatnote|The 7-day listing period has elapsed." .
					    " Items below may be closed if there's a consensus, or if discussion has run its course and consensus could not be achieved.}}\n";
					$summ[$dit] .= "{{end}}\n\n";
					$summ[$dit] .= "{{Dashboard grouping|c=#BDD8FF|'''[[Wikipedia:Requested moves#Elapsed listings|Elapsed listings]]'''}}\n";
					#echo "Adding hatnote\n";
					$hatted = true;
				    }
				}
				$add[$dit] .= $theaddition;
				$oldadd[$dit] .= $oldaddition;
				$summ[$dit] .= $summaddition;
			}
			else {
				continue;
			}
		}
	}
	
	if ($time < strtotime($d[ditmax-1]) && $time != "") {
		if ($time > $eightdays) {
			#echo "Previous day elapsed(" . date("Y-m-d H:i:s", $eightdays) . ")\n";
			if ($hatted == false) {
				$add[ditmax-1] .= "===Elapsed listings===\n{{shortcut|WP:RME}}\n{{hatnote|The 7-day listing period has elapsed." .
				    " Items below may be closed if there's a consensus, or if discussion has run its course and consensus could not be achieved.}}\n";
				$oldadd[ditmax-1] .= "===Elapsed listings===\n{{hatnote|The 7-day listing period has elapsed." .
				    " Items below may be closed if there's a consensus, or if discussion has run its course and consensus could not be achieved.}}\n";
				$summ[ditmax-1] .= "{{end}}\n\n";
				$summ[ditmax-1] .= "{{Dashboard grouping|c=#BDD8FF|'''[[Wikipedia:Requested moves#Elapsed listings|Elapsed listings]]'''}}\n";
				#echo "Adding hatnote\n";
				$hatted = true;
			}
			$add[ditmax-1] .= $theaddition;
			$oldadd[ditmax-1] .= $oldaddition;
			$summ[ditmax-1] .= $summaddition;
		}
		else {
			#echo "Backlog\n";
			$BLadd .= $theaddition;
			$BLold .= $oldaddition;
			$BLsumm .= $summaddition;
		}
	}
	elseif ($time == "") {
		$MALadd .= $theaddition;
		$MALold .= $oldaddition;
		$MALsumm .= $summaddition;
	}
}

$submission = "<noinclude>{{shortcut|WP:RMC|WP:RM/C|WP:RMCD}}</noinclude><includeonly>{{shortcut|WP:RM#C}}</includeonly>\n" .
    ":''This <noinclude>page</noinclude><includeonly>section</includeonly> 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]]''' and" .
    " in '''[[Wikipedia:Requested moves/Current discussions (table)|table format]].''' " . $relisted . " discussions have been relisted.''\n\n";

$oldsubmission = ":''This <noinclude>page</noinclude><includeonly>section</includeonly> 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]]''' and" .
    " in '''[[Wikipedia:Requested moves/Current discussions (table)|table format]].''' " . $relisted . " discussions have been relisted.''\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 .= $summ[$dit] . "{{end}}\n\n";
}

if ($BLadd != "") {
	$BLhatnote = "{{hatnote|Elapsed listings fall into the backlog after 24 hours. Consider relisting 8-day-old discussions with minimal participation.}}";
	$submission .= "===Backlog===\n{{shortcut|WP:RMB}}\n" . $BLhatnote . "\n";
	$submission .= $BLadd;
	$oldsubmission .= "===Backlog===\n" . $BLhatnote . "\n";
	$oldsubmission .= $BLold;
	$summsubmission .= "{{Dashboard grouping|c=#BDD8FF|'''[[Wikipedia:Requested moves#Backlog|Backlog]]'''}}\n";
	$summsubmission .= $BLsumm . "{{end}}\n\n";
	echo "\n\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 . "{{end}}\n\n";
}

if ($malformed != "") {
	$submission .= "===Malformed requests===\n" .
	  "{{hatnote|See [[Wikipedia:Requested moves/Closing instructions#Bot considerations|\"Bot considerations\"]]}}\n";
	$submission .= $malformed;
	$oldsubmission .= "===Malformed requests===\n" .
	  "{{hatnote|See [[Wikipedia:Requested moves/Closing instructions#Bot considerations|\"Bot considerations\"]]}}\n";
	$oldsubmission .= $malformed;
}

if ($incomplete != "") {
	$submission .= "===Possibly incomplete requests===\n" .
	    "{{hatnote|See [[Wikipedia:Requested moves#Request all associated moves explicitly|\"Request all associated moves explicitly\"]]}}\n";
	$submission .= $incomplete;
	$oldsubmission .= "===Possibly incomplete requests===\n" .
	    "{{hatnote|See [[Wikipedia:Requested moves#Request all associated moves explicitly|\"Request all associated moves explicitly\"]]}}\n";
	$oldsubmission .= $incomplete;
}

$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" .
    "{{notelist}}\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";

$tablesubmission = "{{shortcut|WP:RMTABLE}}\n:''This table lists all move requests filed or identified as potentially controversial which are" .
    " currently under discussion.''\n\n{{ombox|text=Do not attempt to edit this table 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 table is also available''' as a list including rationales and multi-moves in a '''[[Wikipedia:Requested moves/Current discussions|discussion-link-first format]]'''" .
    " and in a '''[[Wikipedia:Requested moves/Current discussions (alt)|page-link-first format]].''' " . $relisted . " ''discussions'' have been " .
    "{{Background color|#ffebeb|relisted}}.\n\n<sup>*</sup> The far-right column shows the number of revisions of the requested move target. " .
    "Page-movers are encouraged to prioritize working those with red links or one revision." .
    "\n\n{|class=\"wikitable sortable\"\n!scope=\"col\" style=\"width: 100px;\" | Original list date\n!data-sort-type=number | Days<br/>open" .
    "\n!scope=\"col\" style=\"width: 100px;\" | Current list date\n!Talk<br/>link\n!Current title → New title\n!data-sort-type=number | *\n";

foreach ($otimestamp as $title => $time) {
	$secondsopen = time()-$time;
	$daysopen = 0;
	while ($secondsopen > ds) {
		$secondsopen = $secondsopen-ds;
		$daysopen += 1;
	}

	#echo "\n" . date("Y-m-d H:i", $time) . " (" . date("Y-m-d H:i", $timestamp[$title]) . ") " . " [" . $daysopen . "] " . $currentname[$title][0] . " → " . $newname[$title][0] .
	#   " " . $dlink[$title] . "\n";

	if ($daysopen == 0) {
		$tablesubmission .= "|-\n| " . date("Y-m-d H:i", $time) . " || style=\"text-align: center\" | < 1 || ";
	}
	else {
		$tablesubmission .= "|-\n| " . date("Y-m-d H:i", $time) . " || style=\"text-align: center\" |" . sprintf('%2d',$daysopen) . " || ";
	}
	if ($time != $timestamp[$title]) $tablesubmission .= "style=\"background: #ffebeb;\" | ";

	if (array_key_exists($title, $section)) {
		$tablesubmission .= date("Y-m-d H:i", $timestamp[$title]) . " || [[" . $title . "#" . $section[$title] . "|" .
			$dlink[$title] . "]] || [[" . $currentname[$title][0] . "]] → ";
	}
	else {
		$tablesubmission .= date("Y-m-d H:i", $timestamp[$title]) . " || [[" . $title . "|" .
			$dlink[$title] . "]] || [[" . $currentname[$title][0] . "]] → ";
	}

	if ($newname[$title][0] == "?") {
		$tablesubmission .= "? || style=\"text-align: center\" data-sort-value=-1| ?\n";
	}
	else {
		$latest = $objwiki->ten_latest_edits($newname[$title][0]);
		#echo "\n" . $newname[$title][0] . " ==> latest edits: " . $latest . "\n";

		if ($latest > 9) {
			$tablesubmission .= "{{no redirect|" . $newname[$title][0] . "}} || style=\"text-align: center\" data-sort-value=10| > 9\n";
		}
		else {
			$tablesubmission .= "{{no redirect|" . $newname[$title][0] . "}} || style=\"text-align: center\" | " . $latest . "\n";
		}
	}

	#echo "Description> " . $description[$title] . "\n";
}

$tablesubmission .= "|}\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);
$objwiki->edit("Wikipedia:Requested moves/Current discussions (table)",$tablesubmission,"Updating requested pagemoves table",false,true);
echo "done.\n";
$elapsed_time = time() - $current_time;
echo "Elapsed time: ". $elapsed_time . " (" . date("i:s", $elapsed_time) . ")\n\n";

# Remove closed subject notices
$subjecttranscludes = array();
$attempts = 0;
while (count($subjecttranscludes) == 0) {
		if ($attempts == 5) {
			die("Error 1");
		}
		else {
			echo "Checking for subject transclusions...\n";
			$subjecttranscludes = $objwiki->getTransclusions("Template:Requested move notice");
			$attempts += 1;
		}
}

echo "\nPages transcluding subject notices: ";
print_r($subjecttranscludes);
$hosting = 0;
$hosted = 0;
$lua = 0;
$nf = 0;
$requestcount = array_fill(0, count($transcludes), 0);

for ($i = 0; $i < count($subjecttranscludes); $i++) {
	# Subject notices may transclude to Modules if they are placed on the module's documentation page
	if (strpos($subjecttranscludes[$i], "Module:") === 0) {
		if (!str_ends_with($subjecttranscludes[$i], "/doc")) {
			echo $i . " : " . $subjecttranscludes[$i] . " => Lua module\n";
			$lua += 1;
			continue;
		}
	}

	for ($j = 0; $j < count($transcludes); $j++) {
		$pagename = wikititle($currentname[$transcludes[$j]][0]);
		#echo "\n" . $i . " " . $subjecttranscludes[$i] . " <==> " . $j . " " . $pagename . " || " . $subjectpagename[$j];

		if (":" . $subjecttranscludes[$i] == $pagename or $subjecttranscludes[$i] == $pagename) {
			$firstlist = false;
			if ($pagename != $subjectpagename[$j] and ":" . $pagename != $subjectpagename[$j]) {
				echo "\n" . $i . " : " . $subjecttranscludes[$i] . " first-listed in request on " . $subjectpagename[$j] . "\n";
				$firstlist = true;
				$firstlistsubjectpagename = $subjectpagename[$j];
			}
			else {
				echo $i . " : " . $subjecttranscludes[$i] . " => " . $j . "\n";
				$hosting += 1;
				$requestcount[$j] += 1;
				continue 2;
			}
		}
		else if (($pagename != $subjectpagename[$j] and ":" . $pagename != $subjectpagename[$j]) and
			 ($subjecttranscludes[$i] == $subjectpagename[$j] or ":" . $subjecttranscludes[$i] == $subjectpagename[$j])) {
			echo "\n" . $i . " : " . $subjecttranscludes[$i] . " => " . $j . " – hosting the discussion?!\n";
			$hosting += 1;
			$requestcount[$j] += 1;
			continue 2;
		}
	}

	$break = 0;
	$pagecontents = "";
	while ($pagecontents == "") {
		if ($break == 5) {
			break;
		}
		else {
			$pagecontents = $objwiki->getpage($subjecttranscludes[$i]);
			$break += 1;
		}
	}

	preg_match("/\{{2}" . ntc . ".*\}{2}/i", $pagecontents, $multitalk);
	#echo "Template: ". $multitalk[0] . "\n";
	preg_match("/\|2\=.*\#/i", $multitalk[0], $multitalk);
	$crosstalk = preg_replace("/\|2\=/", "", $multitalk[0]);
	$crosstalk = preg_replace("/\#/", "", $crosstalk);
	echo "Multi-move discussion at: " . $crosstalk;

	$subjectcrosstalk = preg_replace("/Talk:/", ":", $crosstalk);
	$subjectcrosstalk = preg_replace("/talk:/", ":", $subjectcrosstalk);
	$subjectcrosstalk = preg_replace("/\s:/", ":", $subjectcrosstalk);
	echo "\nMulti-move discussion at talk page for: " . $subjectcrosstalk . "\n";
	if ($firstlist === true) {
		echo "firstlist is true: " . $firstlistsubjectpagename . "\n";
	}
	if (":" . $subjecttranscludes[$i] == $subjectcrosstalk or $subjecttranscludes[$i] == $subjectcrosstalk) {
		echo "\nSAME NAME!\n";
	}

	for ($j = 0; $j < count($transcludes); $j++) {
		$pagename = wikititle($currentname[$transcludes[$j]][0]);
		#echo "\n" . $subjecttranscludes[$i] . " → " . $subjectcrosstalk . " <==> " . $pagename . " (" . $j . ")";
		if ($subjecttranscludes[$i] == $pagename) {
			echo "\nSAME!!\n";
			echo $i . " : " . $subjecttranscludes[$i] . " → " . $subjectcrosstalk . " => " . $j . "\n";
			$hosted += 1;
			$requestcount[$j] += 1;
			$hosted_talkname[$hosted] = talkpagename($subjecttranscludes[$i]);
			if (str_starts_with($hosted_talkname[$hosted], "Module talk:"))
			    $hosted_talkname[$hosted] = str_replace("/doc", "", $hosted_talkname[$hosted]);
			continue 2;
		}
		else if ($subjectcrosstalk == $pagename or $subjectcrosstalk == ":" . $pagename) {
			echo $i . " : " . $subjecttranscludes[$i] . " → " . $subjectcrosstalk . " => " . $j . "\n";
			$hosted += 1;
			$requestcount[$j] += 1;
			$hosted_talkname[$hosted] = talkpagename($subjecttranscludes[$i]);
			if (str_starts_with($hosted_talkname[$hosted], "Module talk:"))
			    $hosted_talkname[$hosted] = str_replace("/doc", "", $hosted_talkname[$hosted]);
			continue 2;
		}
	}

	$crosscontents = "";
	if ($crosstalk != "") $crosscontents = $objwiki->getpage($crosstalk);
	if (preg_match("/\{{2}\s?" . rms . "\s?/iu", $crosscontents)) {
		echo "\nCentrally-hosted discussion on " . $crosstalk . "\n";
		echo $i . " : " . $subjecttranscludes[$i] . " → " . $subjectcrosstalk . "\n";
		$hosted += 1;
		#$requestcount[$j] += 1;
		$hosted_talkname[$hosted] = talkpagename($subjecttranscludes[$i]);
		if (str_starts_with($hosted_talkname[$hosted], "Module talk:"))
		    $hosted_talkname[$hosted] = str_replace("/doc", "", $hosted_talkname[$hosted]);
	}
	else {
		echo "\n" . $i . " : " . $subjecttranscludes[$i] . " => NOT FOUND\n";
		$nf += 1;

		$newpagecontents = preg_replace("/<noinclude>\{{2}" . ntc . ".*\}{2}\s*\n<\/noinclude>/", "", $pagecontents);
		$newpagecontents = preg_replace("/<noinclude>\{{2}" . ntc . ".*\}{2}<\/noinclude>(\n|)/", "", $newpagecontents);
		$newpagecontents = preg_replace("/\{{2}" . ntc . ".*\}{2}(\n|)/", "", $newpagecontents);

		if ($newpagecontents == $pagecontents) {
			echo "!!! Failed to remove subject notice\n\n";
		}
		else {
			$objwiki->edit($subjecttranscludes[$i],$newpagecontents,"Removing notice of move discussion",false,true);
		}
	}
}

echo "\nHosting pages: " . $hosting;
echo "\nHosted pages: " . $hosted;
echo "\nLua modules (transcluded from documentation): " . $lua;
echo "\nNOT FOUND: " . $nf;
$total = $hosting + $hosted + $lua + $nf;
echo "\nTotal: " . $total . "\n\n";
$totalrequests = 0;

for ($j = 0; $j < count($requestcount); $j++) {
	echo "\n[" . $j . "] => " . $requestcount[$j] . "  " . $transcludes[$j];
}
echo "\n";
for ($j = 0; $j < count($requestcount); $j++) {
	if ($requestcount[$j] == 0) echo "\nNotice is not posted on " . $transcludes[$j];
	$totalrequests += $requestcount[$j];
}
echo "\n\nTotal requests: " . $totalrequests . "\n\n";

# Remove closed multimove notices
$multitranscludes = array();
$attempts = 0;
while (count($multitranscludes) == 0) {
		if ($attempts == 5) {
			die("Error 1");
		}
		else {
			echo "Checking for multimove transclusions...\n";
			$multitranscludes = $objwiki->getTransclusions("User:RMCD bot/multimove");
			$attempts += 1;
		}
}
echo "\nSubject-space notifications of hosted multimoves: ";
print_r($hosted_talkname);
echo "\nTalk pages with {{User:RMCD bot/multimove}} transclusions: ";
print_r($multitranscludes);

for ($i = 1; $i <= $hosted; $i++) {
	for ($j = 0; $j < count($multitranscludes); $j++) {
		if ($multitranscludes[$j] == $hosted_talkname[$i]) {
			continue 2;
		}
	}
	if (!preg_match("/^\n*\#REDIRECT(\s*|:\s*)\[{2}.*\]{2}/i", $objwiki->getpage($hosted_talkname[$i]), $redirect)) {
		echo "\nNotice not found: " . $hosted_talkname[$i] . "\n";
	}
}

for ($i = 0; $i < count($multitranscludes); $i++) {
	for ($j = 1; $j <= $hosted; $j++) {
		#echo "\n" . $i . "-->" . $multitranscludes[$i] . "  " . $j .  "-->" . $hosted_talkname[$j];
		if ($multitranscludes[$i] == $hosted_talkname[$j]) {
			#echo "\nFound " . $multitranscludes[$i];
			continue 2;
		}
	}

	echo "\nNot found: " . $multitranscludes[$i] . "\n";

	$konflikt = false;
	if ($conflicts > 0) {
		for ($z = 1; $z <= $conflicts; $z++) {
			if (talkpagename($conflict[$z]) == $multitranscludes[$i]) {
				echo "\n!!" . $multitranscludes[$i] . " has conflicting discussions! (removal loop)\n\n";
				$konflikt = true;
			}
		}
	}

	$break = 0;
	$pagecontents = "";
	while ($pagecontents == "") {
		if ($break == 5) {
			break;
		}
		else {
			$pagecontents = $objwiki->getpage($multitranscludes[$i]);
			$break += 1;
		}
	}

	preg_match("/\{{2}User:RMCD bot\/multimove.*\}{2}/i", $pagecontents, $multitalk);
	echo "Template: ". $multitalk[0] . "\n";
	$pagecontents = preg_replace("/\{{2}User:RMCD bot\/multimove.*\}{2}\n/", "", $pagecontents);
	#echo $pagecontents;
	if ($objwiki->nobots($multitranscludes[$i],botuser,$pagecontents) == true && $konflikt == false) {
		$objwiki->edit($multitranscludes[$i],$pagecontents,"Removing transcluded notice of move discussion",false,true);
	}
}

echo "\n\nMission accomplished.\n";
$elapsed_time = time() - $current_time;
echo "Elapsed time: ". $elapsed_time . " (" . date("i:s", $elapsed_time) . ")\n";

?>