User:Bot1058/refreshmainlinks.php

<?php
/** refreshmainlinks.php - Null-edit mainspace 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 August 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;

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/refreshmainlinks.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_title FROM page WHERE page_links_updated < $days_ago AND page_namespace = 0 LIMIT $querylimit");
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();

$namespace = ":";

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(
                $namespace . $titles[$a] . "|" . $namespace . $titles[$a+1] . "|" . $namespace . $titles[$a+2] . "|" .
                $namespace . $titles[$a+3] . "|" . $namespace . $titles[$a+4] . "|" . $namespace . $titles[$a+5] . "|" .
                $namespace . $titles[$a+6] . "|" . $namespace . $titles[$a+7] . "|" . $namespace . $titles[$a+8] . "|" .
                $namespace . $titles[$a+9] . "|" . $namespace . $titles[$a+10] . "|" . $namespace . $titles[$a+11] . "|" .
                $namespace . $titles[$a+12] . "|" . $namespace . $titles[$a+13] . "|" . $namespace . $titles[$a+14] . "|" .
                $namespace . $titles[$a+15] . "|" . $namespace . $titles[$a+16] . "|" . $namespace . $titles[$a+17] . "|" .
                $namespace . $titles[$a+18] . "|" . $namespace . $titles[$a+19]);
	}
	else {
	    $titleslist = $namespace . $titles[$a];
		for ($aa = 1; $aa < 18; $aa++) {
			if (isset($titles[$a+$aa])) $titleslist = $titleslist . "|" . $namespace . $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/refreshmainlinks.dar', $days_ago_to_refresh);
}
else if (count($titles) == querylimit) {
	$days_ago_to_refresh += 1;
	file_put_contents('./php/refreshmainlinks.dar', $days_ago_to_refresh);

}

sleep (120);
goto doit_again;
?>