Wikia

WoWWiki

Arcanedeath/bot

Talk1
102,281pages on
this wiki

< User:Arcanedeath

This is a bot I have developed to convert the deprecated mobox's into npcbox's. Yes, it is ugly. Please feel free to edit it, but don't run it. I will run it once consensus has been reached on the discussion page of this article and I have gotten permission. The source code:

<?php
set_time_limit(0);


	//Login
	$loginData = array(
		'wpName' => '',
		'wpPassword' => '',
		'wpRemember' => '1'
	);
	post('http://www.wowwiki.com/index.php?title=Special:Userlogin&action=submitlogin&type=login', $loginData);
	unset($loginData);
	
	

	//Get deprecated pages.
	$nextUrl = 'http://www.wowwiki.com/index.php?title=Category:Pages_using_deprecated_templates&action=render';
	$pages = array();
	$data = get($nextUrl);
	$nextRegex = '/\) \(<a href="(.*?)"/';
	$pageRegex = '/<li><a href="http:\/\/www.wowwiki.com\/(.*?)"/';
	$render = '&action=render';
	while(preg_match($nextRegex, $data, $next)) {
		$nextUrl = html_entity_decode($next[1]);
		if(preg_match_all($pageRegex, $data, $matches)) {
			$pages = array_merge($pages, $matches[1]);
		}
		$data = get($nextUrl);
	}
	//One more time!
	if(preg_match_all($pageRegex, $data, $matches)) {
		$pages = array_merge($pages, $matches[1]);
	}
	unset($nextUrl);
	unset($data);
	unset($nextRegex);
	unset($pageRegex);
	unset($render);
	
	
	
	//For testing purposes:
	$pages = array('User:Arcanebot/test');
	
	
	
	//Check whether a page has {{mobbox}} on it.
	$page1 = 'http://www.wowwiki.com/index.php?title=';
	$page2 = '&action=edit';
	$textRegex = '/<textarea.*?>(.*?)<\/textarea>/s';
	$timeRegex = '/<input type=\'hidden\' value="(.*?)" name="wpStarttime" \/>

<input type=\'hidden\' value="(.*?)" name="wpEdittime" \/>/';
	$sessRegex = '/<input type=\'hidden\' value="(.*?)" name="wpEditToken" \/>/';
	
	$baseOptions = array(
		'wpSummary' => 'Converting {{mobbox}} to {{npcbox}}.'
	);
	foreach($pages as $page) {
		$data = get($page1 . $page . $page2);
		if(preg_match($textRegex, $data, $text) && preg_match($timeRegex, $data, $time) && preg_match($sessRegex, $data, $sess)) {
			$post = $baseOptions;
			$post['wpStarttime'] = $time[1];
			$post['wpLogin'] = '';
			$post['wpScrolltop'] = '';
			$post['wpSave'] = 'Save Page';
			$post['wpEdittime'] = $time[2];
			$post['wpEditToken'] = $sess[1];
			$edit = $text[1];
			
			unset($text);
			unset($time);
			unset($sess);
			
			$stuff = magic($edit);
			
			//Start editing!
			if($stuff) {
				$options = $stuff[0];
				$before = $stuff[1];
				$after = $stuff[2];
				unset($stuff);
				$newoptions = array();
				foreach($options as $option) {
					$val = strtolower($option[1]);
					switch($option[0]) {
						case 'elite':
							if($val == 'elite' && in_array(array('type', 'Rare'), $newoptions)) {
								$newoptions[] = array('type', 'Rare Elite');
								$arrnum = array_search(array('type', 'Rare'), $newoptions);
								unset($newoptions[$arrnum]);
							} elseif($val == 'elite') {
								$newoptions[] = array('type', 'Elite');
							}
							break;
						case 'rare':
							if($val == 'rare' && in_array(array('type', 'Elite'), $newoptions)) {
								$newoptions[] = array('type', 'Rare Elite');
								$arrnum = array_search(array('type', 'Elite'), $newoptions);
								unset($newoptions[$arrnum]);
							} elseif($val == 'rare') {
								$newoptions[] = array('type', 'Rare');
							}
							break;
						case 'type':
							if($val != 'elite' && $val != 'rare' && $val != 'rare elite') {
								$newoptions[] = array('creature', $option[1]);
							} else {
								$newoptions[] = array('type', $option[1]);
							}
							break;
						case 'attitude':
							if(!isset($newoptions['aggro'])) {
								if($val == 'neutral') {
									$newoptions[] = array('aggro', '{{aggro|0|0}}');
								} else {
									$newoptions[] = array('aggro', '{{aggro|-1|-1}}');
								}
							}
							break;
						case 'faction':
							if($val == 'neutral') {
								$newoptions[] = array('faction', 'Neutral');
							} else {
								$newoptions[] = array('faction', 'Combat');
							}
							break;
						default:
							$newoptions[] = array($option[0], $option[1]);
							break;
					}
				}
			}
			foreach($newoptions as &$option) {
				$option = "\r\n | " . $option[0] . ' = ' . $option[1];
			}
			$options = implode('', $newoptions);
			$edit = $before . '{{npcbox' . $options . "\r\n}}" . $after;
			$post['wpTextbox1'] = $edit;
			post($page1 . $page . $page2, $post);
			die;
		}
	}	
	
	
//Get and post request functions.
function post($url, $send) {
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $send);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_COOKIEFILE, './npcboxcookie.txt');
	curl_setopt($ch, CURLOPT_COOKIEJAR, './npcboxcookie.txt');
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}
function get($url, $send = false) {
	if(is_array($send) && !empty($send)) {
		$sends = array();
		foreach($send as $key => $val) {
			$sends[] = $key . '=' . $val;
		}
		$send = implode('&', $sends);
		unset($sends);
		$url = $url . '?' . $send;
	}
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_COOKIEFILE, './npcboxcookie.txt');
	curl_setopt($ch, CURLOPT_COOKIEJAR, './npcboxcookie.txt');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}
//Do not question it. It works.
function magic($str) {
	if(preg_match('/(.*?){{mobbox(.*)/s', $str, $matches)) {
		$before = $matches[1];
		$str = $matches[2];
		$ex = str_split($str);
		$options = array();
		$optnum = 0;
		$states = array(
		'inopen' => false,
		'inclose' => false,
		'npcbox' => 0,
		'tempdepth' => 0,
		'done' => false,
		);
		foreach($ex as $num => $x) {
			if($states['done']) {
				$after = implode('', $ex);
				break;
			}
			switch($x) {
				case '{':
					if($states['inclose']) {
						$states['inclose'] = false;
					}
					if(!$states['inopen']) {
						$states['inopen'] = true;
						$states['tempdepth']++;
						if(!isset($options[$optnum])) $options[$optnum] = '';$options[$optnum] .= $x;
					} else {
						if(!isset($options[$optnum])) $options[$optnum] = '';$options[$optnum] .= $x;
					}
					break;
				case '}':
					if($states['inopen']) {
						$states['inopen'] = false;
					}
					if($states['tempdepth'] == 0 && !$states['inclose']) {
						$states['done'] = true;
						foreach($ex as $nnum => $nx) {
							if($nx == '}') {
								unset($ex[$nnum]);
							} else {
								break;
							}
						}
					}
					if(!$states['inclose'] && !$states['done']) {
						$states['inclose'] = true;
						$states['tempdepth']--;
						if(!isset($options[$optnum])) $options[$optnum] = '';$options[$optnum] .= $x;
					} elseif(!$states['done']) {
						if(!isset($options[$optnum])) $options[$optnum] = '';$options[$optnum] .= $x;
					}
					break;
				case '|':
					if($states['tempdepth'] == 0) {
						$optnum++;
					} else {
						if(!isset($options[$optnum])) $options[$optnum] = '';$options[$optnum] .= $x;
					}
					break;
				default:
					$states['inclose'] = false;
					$states['inopen'] = false;
					if(!isset($options[$optnum])) $options[$optnum] = '';$options[$optnum] .= $x;
					break;
			}
			unset($ex[$num]);
		}
		foreach($options as &$option) {
			if(preg_match('/(.*?)=(.*)/s', $option, $matches)) {
				$option = array();
				$option[0] = strtolower(trim($matches[1]));
				$option[1] = trim($matches[2]);
			} else {
				$oldoption = $option;
				$option = array();
				$option[0] = strtolower(trim($oldoption));
				$option[1] = '';
			}
		}
		foreach($options as $num => $opt) {
			if(empty($opt[0])) {
				unset($options[$num]);
			}
		}
		return array($options, $before, $after);
	} else return false;
}
?>

Around Wikia's network

Random Wiki