Cannabis Ruderalis

<?php
/** refreshlinks.php - Null-edit pages in order to refresh links which are old.
 *  Version 3.2
 *
 *  (c) 2022–23 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 yourself here if you worked on the code):
 *    WBM - [[User:Wbm1058]] - v 2.0 May 2022 began operating from my desktop PC
 *                           - v 3.0 February 2023 began operating on the Toolforge jobs framework
 **/
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
$AssumeHTTPFailuresAreJustTimeoutsAndShouldBeSuppressed = true;
include("logininfo.php");

const ds = 86400;    #number of seconds in a day
const querylimit = 10000;

$querylimit = querylimit;
$ns = array_fill(0, 2301, "undefined");
$ns[0] = ":";
$ns[1] = "Talk:";
$ns[2] = "User:";
$ns[3] = "User talk:";
$ns[4] = "Wikipedia:";
$ns[5] = "Wikipedia talk:";
$ns[6] = "File:";
$ns[7] = "File talk:";
$ns[8] = "MediaWiki:";
$ns[9] = "MediaWiki talk:";
$ns[10] = "Template:";
$ns[11] = "Template talk:";
$ns[12] = "Help:";
$ns[13] = "Help talk:";
$ns[14] = "Category:";
$ns[15] = "Category talk:";
$ns[100] = "Portal:";
$ns[101] = "Portal talk:";
$ns[118] = "Draft:";
$ns[119] = "Draft talk:";
$ns[710] = "TimedText:";
$ns[711] = "TimedText talk:";
$ns[828] = "Module:";
$ns[829] = "Module talk:";
$ns[2300] = "Gadget:";
$ns[2301] = "Gadget talk:";
#print_r($ns);

echo "\nPHP version: " . PHP_VERSION . "\n";

echo "\nLogging in...\n";
$objwiki = new wikipedia();
$objwiki->login($user, $pass);
echo "...done.\n";

doit_again:
$days_ago_to_refresh = file_get_contents('./php/refreshlinks.dar');
echo "\nDays ago to refresh: " . $days_ago_to_refresh . "\n";
$current_time = time();
$days_ago = $current_time - ds*$days_ago_to_refresh;
echo "Current time: " . $current_time . " (" . date("Y-m-d H:i:s", $current_time) . ")\n";
echo "Refresh time: " . $days_ago . " (" . date("Y-m-d H:i:s", $days_ago) . ")";
$days_ago = date("YmdHis", $days_ago);
echo " ==> " . $days_ago . "\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";
$result = $mysqli->query("SELECT page_namespace, page_title FROM page WHERE page_links_updated < $days_ago LIMIT $querylimit");
print_r($result);

$rows = $result->fetch_all(MYSQLI_ASSOC);
#print_r($rows);
$namespaces = array_column($rows, 'page_namespace');
$titles = array_column($rows, 'page_title');
echo "\n" . count($titles) . " pages found needing links refreshed\n";
for ($i = 0; $i < count($titles); $i++) {
    if ($ns[$namespaces[$i]] != "undefined") {
        $nspaces[$i] = $ns[$namespaces[$i]];
        $titles[$i] = $nspaces[$i] . $titles[$i];
        #echo "\n" . $i . "> " . $titles[$i];
    }
    else {
        echo "\n\nUndefined namespace: " . $namespaces[$i] . "\n";
        $mysqli->close();
        die;
    }
}

/* close connection */
$mysqli->close();

for ($a = 0; $a < count($titles); $a+=20) {
	echo "\n" . $a . "> " . $titles[$a] . "\n";

	# . " | " . $titles[$a+1] . " | " . $titles[$a+2] . " | " . $titles[$a+3] . " | " . $titles[$a+4];
	#echo "\n" . ($a+5) . "> " . $titles[$a+5]
	# . " | " . $titles[$a+6] . " | " . $titles[$a+7] . " | " . $titles[$a+8] . " | " . $titles[$a+9];
	#echo "\n" . ($a+10) . "> " . $titles[$a+10]
	# . " | " . $titles[$a+11] . " | " . $titles[$a+12] . " | " . $titles[$a+13] . " | " . $titles[$a+14];
	#echo "\n" . ($a+15) . "> " . $titles[$a+15]
	# . " | " . $titles[$a+16] . " | " . $titles[$a+17] . " | " . $titles[$a+18] . " | " . $titles[$a+19] . "\n";

	if (isset($titles[$a+19])) {
		$result = $objwiki->purgeCache($titles[$a] . "|" . $titles[$a+1] . "|" . $titles[$a+2] . "|" . $titles[$a+3] . "|" .
                                     $titles[$a+4] . "|" . $titles[$a+5] . "|" . $titles[$a+6] . "|" . $titles[$a+7] . "|" .
                                     $titles[$a+8] . "|" . $titles[$a+9] . "|" . $titles[$a+10] . "|" . $titles[$a+11] . "|" .
                                     $titles[$a+12] . "|" . $titles[$a+13] . "|" . $titles[$a+14] . "|" . $titles[$a+15] . "|" .
                                     $titles[$a+16] . "|" . $titles[$a+17] . "|" . $titles[$a+18] . "|" . $titles[$a+19]);
	}
	else {
		$titleslist = $titles[$a];
		for ($aa = 1; $aa < 18; $aa++) {
			if (isset($titles[$a+$aa])) $titleslist = $titleslist . "|" . $titles[$a+$aa];
		}
		$result = $objwiki->purgeCache($titleslist);
	}

	#print_r($result);
	sleep (1);
	#usleep (750000);
}

$finish_time = time();
$elapsed = $finish_time - $current_time;
echo "\nMission accomplished at " . date("Y-m-d H:i:s", $finish_time) . " in " . date("H:i:s", $elapsed) . ".\n\n";

if (count($titles) < 250) {
	$days_ago_to_refresh = $days_ago_to_refresh - 1;
	file_put_contents('./php/refreshlinks.dar', $days_ago_to_refresh);
}
else if (count($titles) == querylimit) {
	$days_ago_to_refresh += 1;
	file_put_contents('./php/refreshlinks.dar', $days_ago_to_refresh);

}

sleep (120);
goto doit_again;
?>

Leave a Reply