User:Jayden54/WPDead Source.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
<?php

// Username of the bot (can be left blank)
define ('bot_username', 'Jayden54Bot');

// Password of the bot (can be left blank)
define ('bot_password', '');

// Save the action log? (true/false)
define ('save_log', true);

define ('cutoff', 1);

$lists = array(	'Wikipedia:Dead-end_pages/A-K',
				'Wikipedia:Dead-end_pages/L-Z',
			  );

/* =========================== DO NOT EDIT BELOW THIS LINE ==================================== */

$base = dirname(__FILE__) . DIRECTORY_SEPARATOR;
chdir($base);

include '../../wikipediaphp/lib/wikipedia.php';

// Load Wikipedia object
$wp = new Wikipedia();
WP_STATUS::addCallback(array('WP_FUNCTIONS', 'printStatus'));

// save to log file?
if (defined('save_log') == true && save_log == true) {
	WP_STATUS::addCallback(array('WP_FUNCTIONS', 'saveLog'));
}

WP_FUNCTIONS::clearCli();
?>
Welcome to the WP:DEAD link clearing Program.

This bot will remove all the deleted articles from
the WP:DEAD lists.

Action Log:
<?php

// login
$wp->loadScript('login', array('site' => 'en'));

// confirm start
if (WP_FUNCTIONS::confirmYesNo('Bot ready to check lists.') == false) {
	WP_FUNCTIONS::stopBot();
}

// Check each list
foreach ($lists as $list) {
	// Get list HTML
	$list = $wp->en->getArticle($list);
	$html = $list->getHTML();

	// Find links that point to an invalid article (class="new")
	$regex = '/<li>([^"]*?)<a([^>]*?)class="new"([^>]*?)title="(?P<article>.*?)"(.*?)>(.*?)<\/a>(.*?)<\/li>/is';
	if (preg_match_all($regex, $html, $matches) == false) { 
		WP_STATUS::addMessage ('Found no articles in page. Skipping list.');
		continue; 
	}
	$articles = $matches['article'];

	WP_STATUS::addMessage(sprintf('Found %s articles in list.', count($articles)));

	if (count($articles) < cutoff) {
		WP_STATUS::addMessage('Not enough articles to match cutoff. Skipping list.');
		continue;
	}

	// Get list contents (wiki)
	$content = $list->get();

	// Find each article in content, and delete line
	$removed = 0;
	foreach ($articles as $article) {
		$article = html_entity_decode($article, ENT_QUOTES);
		$find = '[[' . $article . ']]';

		// find position of article
		$pos = strpos($content, $find);
		if ($pos === false) { continue; }

		// find start of line
		$line_start = strrpos(substr($content, 0, $pos), "#");

		// find end of line
		$line_end = strpos($content, "\n", $pos);

		// make sure to remove all newlines
		while(true) {
			$next_char = substr($content, $line_end, 1);

			if (in_array($next_char, array("\r", "\n")) == true) {
				$line_end++;
			} else {
				break;
			}
		}

		// remove line from content
		$content = substr($content, 0, $line_start) . substr($content, $line_end);
		$removed++;
	}

	WP_STATUS::addMessage(sprintf('Removed %s articles from list', $removed));

	// Put new article
	$editSummary = sprintf('Bot clearing deleted articles: %s removed', $removed);
	$list->put ($content, $editSummary, false, true);
}

// Finished
WP_STATUS::addMessage ('All Done!', 'Info');
WP_FUNCTIONS::stopBot();

?>