User:Bot1058/bypasspipe.php
Appearance
<?php
/** bypasspipe.php - Bypass bad (e.g., misspelled) piped links to link directly to the title displayed to readers
* Version 3.0
*
* (c) 2026 WBM - 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):
* WBM - [[User:Wbm1058]] - December 2024, August 2025, June 2026
**/
ini_set("display_errors", 1);
error_reporting(E_ALL ^ E_NOTICE);
require_once 'botclasses.php'; // Botclasses.php was written by User:Chris_G and is available under the GNU General Public License
include("logininfo.php");
const namespaces = "User|Wikipedia|File|MediaWiki|Template|Module|Help|Category|Portal|Book|Draft";
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 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 "Logging in...\n";
$objwiki = new wikipedia();
$objwiki->login($user, $pass);
echo "...done.\n";
$templateredirects = array();
$tm[1] = "R from misspelling";
$tm[2] = "R from incorrect hyphenation";
$tm[3] = "R from miscapitalisation";
$cat[1] = "'Redirects_from_misspellings'";
$cat[2] = "'Redirects_from_incorrect_hyphenation'";
$cat[3] = "'Redirects_from_miscapitalisations'";
$edited = 0;
for ($ii = 1; $ii <= count($cat); $ii++) {
echo "\nii: ". $ii . "\n";
$templateredirects = $objwiki->whatredirectshere ("Template:" . $tm[$ii], 10);
$typos = $tm[$ii];
for ($jj = 0; $jj < count($templateredirects); $jj++) {
$templateredirects[$jj] = substr($templateredirects[$jj], 9);
$typos = $typos . "|" . $templateredirects[$jj];
}
echo "\n" . $typos . "\n\n";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("enwiki.analytics.db.svc.wikimedia.cloud", $toolforgeuser, $toolforgepass, "enwiki_p");
/* Tunnel the Toolforge database to local port 4711 – use when not on the cloud */
#$mysqli = new mysqli("127.0.0.1:4711", $toolforgeuser, $toolforgepass, "enwiki_p");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
echo "Connected to database\n\n" . $cat[$ii] . "\n";
$result = $mysqli->query("SELECT p1.page_title FROM page AS p1
JOIN redirect AS r1 ON p1.page_id=r1.rd_from
JOIN categorylinks
ON p1.page_id = cl_from
JOIN linktarget AS l1
ON p1.page_title = l1.lt_title AND l1.lt_namespace = 0
JOIN pagelinks
ON l1.lt_id = pl_target_id
JOIN page AS p2
ON pl_from = p2.page_id AND p2.page_namespace = 0
LEFT JOIN redirect AS r2 ON p2.page_id=r2.rd_from
JOIN linktarget AS l2 ON l2.lt_id = cl_target_id
WHERE p1.page_namespace = 0
AND p1.page_is_redirect = 1
AND l2.lt_namespace = 14
AND l2.lt_title = $cat[$ii]
AND NOT ( r1.rd_namespace <=> r2.rd_namespace AND r1.rd_title <=> r2.rd_title )
GROUP BY 1 LIMIT 1000");
print_r($result);
$rows = $result->fetch_all(MYSQLI_ASSOC);
#print_r($rows);
$titles = array_column($rows, 'page_title');
print_r($titles);
/* close connection */
$mysqli->close();
for ($a = 0; $a < count($titles); $a++) {
echo "\n\n";
$titles[$a] = str_replace("_"," ",$titles[$a]);
$pagecontents = $objwiki->getpage($titles[$a]);
if (preg_match("/^\n*\#REDIRECT(\s*|:\s*)\[{2}.*\]{2}/i", $pagecontents, $redirect)) {
#echo $pagecontents . "\n\n";
preg_match("/(?<=\[{2}).+(?=(\]{2}))/i", $redirect[0], $target);
#echo "Target: " . $target[0] . "\n";
$target[0] = wikititle($target[0]);
}
else {
die("Not a redirect!");
}
echo "\n" . $a . "> " . $titles[$a] . " redirects to " . $target[0] . "\n";
$links = $objwiki->whatlinkshere($titles[$a],null,0);
#print_r($links);
for ($b = 0; $b < count($links); $b++) {
echo "\n " . $b . ">> " . $links[$b] . "\n ";
$contents = $objwiki->getpage($links[$b]);
$title = str_replace("/","\/",$titles[$a]);
$targt = str_replace("/","\/",$target[0]);
if (preg_match("/\[\[" . $title . "\|.*\]\]/", $contents, $mmx)) {
#print_r($mmx);
$new = preg_replace("/\[\[" . $title . "\|" . $targt . "\]\]/", "[[" . $target[0] . "]]" , $contents);
if ($new == $contents) {
if (preg_match("/\{{2}.*(" . $typos . ").*\|(.*)\}{2}/iu", $pagecontents, $mx)) {
#echo "mx ";
#print_r($mx);
echo " ";
$mxcontents = $objwiki->getpage($mx[2]);
if (preg_match("/^\n*\#REDIRECT(\s*|:\s*)\[{2}.*\]{2}/i", $mxcontents, $redirect)) {
#echo $mxcontents . "\n\n";
preg_match("/(?<=\[{2}).+(?=(\]{2}))/i", $redirect[0], $mxtarget);
$mxtarget[0] = wikititle($mxtarget[0]);
echo " " . $mx[2] . " redirects to " . $mxtarget[0] . "\n";
$mx2 = str_replace("/","\/",$mx[2]);
if ($mxtarget[0] == $target[0]) {
$new = preg_replace("/\[\[" . $title . "\|" . $mx2 . "\]\]/", "[[" . $mx[2] . "]]" , $contents);
$new = preg_replace("/\[\[" . $title . "\]\]/", "[[" . $mx[2] . "]]" , $new);
if ($new != $contents) {
$edited += 1;
echo " Edit " . $edited . " (A): [[" . $titles[$a] . "|" . $mx[2] . "]] ==> [[" . $mx[2] . "]]\n";
sleep(3);
#echo $new;
sleep(6);
$objwiki->edit($links[$b],$new,"[[User:Bot1058|Task 9]] – Bypass piped link: [[" . $titles[$a] . "|" . $mx[2] . "]] → [[" . $mx[2] . "]]",false,false);
#die;
}
}
}
}
else if (preg_match("/\{{2}.*(" . $typos . ").*\}{2}/iu", $pagecontents, $mx)) {
echo " ** no correct spelling was specified for [[". $titles[$a] . "]]: " . $mx[0] . " **";
#print_r($mx);
}
else {
echo " ** " . $titles[$a] . " is not tagged {{R from misspelling}} **";
echo "\n\n" . substr($pagecontents, 0, 180) . "\n\n";
}
}
else {
$edited += 1;
echo " Edit " . $edited . " (B): [[" . $titles[$a] . "|" . $target[0] . "]] ==> [[" . $target[0] . "]]\n";
sleep(3);
$newer = preg_replace("/\[\[" . $title . "\|(.*)\]\]/", "[[" . $target[0] . "|$1]]" , $new);
$newer = preg_replace("/\[\[" . $title . "\]\]/", "[[" . $target[0] . "]]" , $newer);
if ($newer != $new) {
echo " Edit " . $edited . " (B+): [[" . $titles[$a] . "|<x>]] ==> [[" . $target[0] . "|<x>]]\n";
sleep(3);
}
#echo $newer;
sleep(6);
$objwiki->edit($links[$b],$newer,"[[User:Bot1058|Task 9]] – Bypass piped link: [[" . $titles[$a] . "|" . $target[0] . "]] → [[" . $target[0] . "]]",false,false);
#die;
}
}
else {
echo " ** not piped **";
}
}
}
echo "\nDone: " . $cat[$ii] . "\n";
}
echo "\n\nPages edited: " . $edited;
echo "\nMission accomplished.\n\n";