User:Cocoaguy/wpdead source.js
(Redirected from User:Cocoaguy/WPDead Source.js)
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Cocoaguy/wpdead source. |
<?php
// Username of the bot (can be left blank)
define ('bot_username', 'Cocoabot');
// 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();
?>