Commit aaf71ee6 authored by Morten Haan's avatar Morten Haan

Merge branch '2-categoryrenamer' into 'develop'

2-categoryrenamer: Add categoryrenamer

See merge request !2
parents f0216f59 b66ac782
Pipeline #3931 failed with stage
in 6 seconds
# RenameCategory
* You need to provide a good summary. The bot will use it for all his actions. Remeber, include the Bot: prefix, and tell the other users what you are doing an why.
* WARNING: If you decide to run this script as normal user, it may be very noisy. But even if you run this as bot, make sure that the bot makes enough brakes between his edits. The bot will ask you, so you are responsible for the noise.
* The bot will start with renaming the cats to the subcats. If he does not succeed, he asks you to fix these problems manually.
* The bot will normalize the categorys on all pages he touches. It will happen due the replacement, so these are not cosmetic changes only. Most categorys are already normalized.
* Use this script only if there is a consensus for such an action.
* NOTE: This script is currently not tested, it may not work or break things. Be careful.
\ No newline at end of file
<?php
require_once __DIR__ . '/Cygnus/BotCore.php';
/** RenameCategory.php
* Renames a category
* @Author Luke081515 <luke081515@tools.wmflabs.org>
* @Version 0.1
* @Status Alpha
*/
class RenameCategory extends Core {
public function RenameCategory () {
echo 'Let\'s start... \033[01;31mIf you are new to this script, please read MANUAL.md first.\033[0m';
if (strtolower($this->askOperator('Do you want to run this with an account with botflag? [y/N]')) === "y")
$this->initCurl(null, 'RenameCategory', 'cli', 'bot');
else
$this->initCurl(null, 'RenameCategory', 'cli', 'user');
while (true) {
$originalCat = $this->askOperator('Enter the original category, without the \'Category:\' prefix:');
$newCat = $this->askOperator("Enter the new category, without the \'Category:\' prefix:");
echo '\nOriginal category: ' . $originalCat . '\nNew category: ' . $newCat;
if (strtolower($this->askOperator('Is this information correct? [y/N]')) === "y")
break;
}
$summary = $this->askOperator('Which summary do you want me to use for my edits?');
$sleep = $this->askOperator('How many seconds do you want me to wait between my edits?');
$prefix = $this->askOperator('Enter the local name for categorys in your language, e.g. category for en, Kategorie for de:');
$originalCat = $prefix . ':' . $originalCat;
$newCat = $prefix . ':' . $newCat;
$sleep = intval($sleep);
if (strtolower($this->askOperator('Do you want to use this information? [y/N]')) !== 'y')
throw new Exception ('User aborted workflow.');
$res = $this->movePage($originalCat, $newCat, $summary, true, true, false);
echo '\nTrying to move category...';
if ($res !== 'success') {
if ($this->askOperator('Error ' . $res . ' occured. Continue anyway? [y/N]') !== 'y')
throw new Exception ('User aborted workflow.');
}
echo '\nMoved category...';
echo '\nSearching for SubCats...';
$list = $this->getCatMembers($kat, true);
if ($list === null)
echo 'No subcategorys found.';
$listcount = count($list);
if (strtolower($this->askOperator('Found ' . $listcount . ' subcategorys to fix. Should I try to fix those now? [y/N]')) !== 'y')
throw new Exception ('User aborted workflow.');
$a=0;
while (isset($list[$a])) {
echo '\n\033[01;32mTrying to move the subcategory: ' . $list[$a] . ' \033[0m';
while (true) {
$result = $this->replace($list[$a], $originalCat, $newCat, $summary, $prefix);
if ($result !== 'conflict' && $result !== 'retry-fail')
break;
echo '\n\033[01;32mNon-critical error occured. Trying again....\033[0m';
}
if ($result === 'fail') {
if (strtolower($this->askOperator('\033[01;31mAn error occured, the edit failed. Continue anyway? [y/N] \033[0m')) !== 'y')
throw new Exception ('User aborted workflow.');
} else if ($result === 'nochange')
echo '\033[01;31mThe bot made an null-edit. Will check this later.\033[0m';
else {
echo '\n\033[01;32mSuccessfully edited the subcategory! \033[0m';
echo '\n\033[01;34mSleeping for ' . $sleep . ' seconds... \033[0m';
sleep($sleep);
}
$a++;
}
while (true) {
echo '\n\033[01;32mDone! Searching for SubCats...(again)...\033[0m';
$list = $this->getCatMembers($kat, true);
if ($list === null) {
echo ('\n\033[01;32mNo SubCats found that need to fixed. Nice!\033[0m');
break;
} else {
$a=0;
while (isset($list[$a])) {
echo ('\033[01;31mI was not able to fix the category at these pages. Please try yourself:\033[0m');
$this->askOperator('Page to fix: ' . $list[$a] . '\nPress enter to continue.');
$a++;
}
}
}
echo '\n\033[01;32mDone! Checking for categorys at other pages now...\033[0m';
$list = $this->getCatMembers($kat, false);
if ($list === null) {
echo ('\n\033[01;32mNo categorys found. Nice! I\'m done!\033[0m'); // ToDo
exit(0);
} else {
$a=0;
while (isset($list[$a])) {
echo '\n\033[01;32mTrying to fix links on page: ' . $list[$a] . ' \033[0m';
while (true) {
$result = $this->replace($list[$a], $originalCat, $newCat, $summary, $prefix);
if ($result !== 'conflict' && $result !== 'retry-fail')
break;
echo '\n\033[01;32mNon-critical error occured. Trying again....\033[0m';
}
if ($result === 'fail') {
if (strtolower($this->askOperator('\033[01;31mAn error occured, the edit failed. Continue anyway? [y/N] \033[0m')) !== 'y')
throw new Exception ('User aborted workflow.');
} else if ($result === 'nochange')
echo '\033[01;31mThe bot made an null-edit. Will check this later.\033[0m';
else {
echo '\n\033[01;32mSuccessfully edited the page! \033[0m';
echo '\n\033[01;34mSleeping for ' . $sleep . ' seconds... \033[0m';
sleep($sleep);
}
$a++;
}
}
while (true) {
echo '\n\033[01;32mDone! Checking for links at pages again....\033[0m';
$list = $this->getCatMembers($kat, false);
if ($list === null) {
echo ('\n\033[01;32mNo links at pages found. Nice!\033[0m');
break;
} else {
$a=0;
while (isset($list[$a])) {
echo ('\033[01;31mI was not able to fix a link at these pages. Please try yourself:\033[0m');
$this->askOperator('Page to fix: ' . $list[$a] . '\nPress enter to continue.');
$a++;
}
}
}
if (strtolower($this->askOperator('\033[01;31mWant to delete the original category? [y/N] \033[0m')) !== 'y') {
$this->deletePage($originalCat, $summary);
echo 'We are done now.';
} else {
$templ = $this->askOperator('Enter the template for deletion which I will preprend to the cat page.');
$this->editPage($originalCat, $templ . '\n' . $this-readPage($originalCat), $summary);
echo 'We are done now. Please make sure, that the deletion request was correct.';
}
}
private function replace($page, $set, $target, $summary, $prefix) {
$content = $this->normalizeAllLinks($page);
$cotent = str_ireplace('[[category:', '[[' . $prefix . ':', $content);
$content = str_replace('[[' . $set . ']]', '[[' . $target . ']]', $content);
$content = str_replace('[[' . $set . '|', '[[' . $target . '|', $content);
$content = str_replace ('[[' . $target . '|' . $target . ']]', '[[' . $target . ']]' , $content);
return $this->editPage($page, $replace, $summary);
}
private function normalizeAllLinks($page) {
$content = $this->readPage($page);
$links = $this->parseWikilinks ($content, true);
$a=0;
while (isset($links[0][$a])) {
str_replace($links[0][0][$a], $links[1][0][$a], $content);
$a++;
}
return $content;
}
}
$Bot = new RenameCategory ();
?>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment