Jump to content

User:ClueBot II/Source

From Wikipedia, the free encyclopedia

The following is automatically generated by ClueBot II.


  • wikibot.classes.php is here.


ClueBot Script (cluebot2.cbsfunctions.php)

[edit]
<?PHP
	$debug = true;
	function getarguments ($data,&$return,&$session,$conditionals = true,$delimiter = ' ') {
		$pos = 0;
		$depthp = 0;
		$depthb = 0;
		$quotes1 = 0;
		$quotes2 = 0;
		$tmp = '';
		$chr = '';
		$data = trim($data);
		$array = array();

		while ($pos < strlen($data)) {
			$chr = $data{$pos};
			if (($quotes1 == 1) and ($chr == '"')) { $quotes1 = 0; }
			elseif (($quotes2 == 1) and ($chr == "'")) { $quotes2 = 0; }
			elseif (($quotes1 == 0) and ($quotes2 == 0)) {
				if ($depthb == 0) {
					if ($chr == '(') $depthp++;
					if ($chr == ')') $depthp--;
				}
				if ($depthp == 0) {
					if ($chr == '{') $depthb++;
					if ($chr == '}') $depthb--;
				}
				if ($chr == '"') $quotes1 = 1;
				if ($chr == "'") $quotes2 = 1;
				if (($chr == $delimiter) and ($depthp == 0) and ($depthb == 0)) { $array[] = $tmp; $tmp = ''; $pos++; continue; }
			}
			$tmp .= $chr;
			$pos++;
		}
		$array[] = $tmp;

		foreach ($array as $arg) {
			if ($arg{0} == '%') {
				$return[] = &$session['vars'][substr($arg,1)];
			} elseif ($arg{0} == '{') {
				$return[] = $arg;
			} elseif (($conditionals == false) and ($arg{0} == '(')) {
				$return[] = $arg;
			} else {
				$return[] = evalconditional($arg,&$session);
			}
		}
	}

	function evalfunction ($function,&$session) {
		if (preg_match('/^\$([^(]+)\((.*)\)$/',trim($function),$m)) {
			$func = strtolower($m[1]);
			$args = array();
			getarguments($m[2],&$args,&$session,true,',');
			switch ($func) {
				case '+':
				case 'cat':
					$ret = '';
					foreach ($args as $arg) {
						$ret .= $arg;
					}
					return $ret;
					break;
				case 'mid':
				case 'substr':
					if (count($args) == 2) {
						return substr($args[0],$args[1]);
					} elseif (count($args) == 3) {
						return substr($args[0],$args[1],$args[2]);
					}
					break;
				case 'gettok':
					if (count($args) >= 3) {
						if (count($args) == 4) {
							$x = ($args[0]?explode($args[1],$args[0],$args[3]):array());
						} elseif (count($args) == 3) {
							$x = ($args[0]?explode($args[1],$args[0]):array());
						}
						if ($args[2] == 0) {
							return count($x);
						} else {
							return $x[$args[2] - 1];
						}
					}
					break;
				case 'settok':
					if (count($args) == 4) {
						$x = ($args[0]?explode($args[1],$args[0]):array());
						$x[$args[2] - 1] = $args[3];
						return implode($args[1],$x);
					}
					break;
				case 'addtok':
					if (count($args) == 3) {
						$x = ($args[0]?explode($args[1],$args[0]):array());
						$x[] = $args[2];
						return implode($args[1],$x);
					}
					break;
				case 'deltok':
					if (count($args) == 3) {
						$x = ($args[0]?explode($args[1],$args[0]):array());
						unset($x[$args[2] - 1]);
						return implode($args[1],$x);
					}
					break;
				case 'strpos':
					if (count($args) == 2) {
						return ((($x = strpos($args[0],$args[1])) === false)?-1:$x);
					} elseif (count($args) == 3) {
						return ((($x = strpos($args[0],$args[1],$args[2])) === false)?-1:$x);
					}
					break;
				case 'stripos':
					if (count($args) == 2) {
						return ((($x = stripos($args[0],$args[1])) === false)?-1:$x);
					} elseif (count($args) == 3) {
						return ((($x = stripos($args[0],$args[1],$args[2])) === false)?-1:$x);
					}
					break;
				case 'replace':
					if ((count($args) % 2) == 1) {
						$search = array();
						$replace = array();
						$data = $args[0];
						for ($i=1;$i<count($args);$i+=2) {
							$search[] = $args[$i];
							$replace[] = $args[$i + 1];
						}
						return str_replace($search,$replace,$data);
					}
					break;
				case 'pregreplace':
					if (count($args) == 3) {
						return preg_replace($args[1],$args[2],$args[0]);
					}
					break;
				case 'time':
					return time();
					break;
			}
		}
	}

	function evalconditional ($conditional,&$session) {
		global $debug;
		if ($debug) echo 'evalconditional("'.$conditional.'",'.serialize($session).');'."\n";
		$pos = 0;
		$depth = 0;
		$part = 1;
		$quotes1 = 0;
		$quotes2 = 0;
		$c1 = '';
		$op = '';
		$c2 = '';

		while ($pos < strlen($conditional)) {
			$chr = $conditional{$pos};
			$chr2 = $conditional{$pos+1};
			$chr3 = $conditional{$pos+2};
			if (($quotes1 == 1) and ($chr == '"')) { $quotes1 = 0; }
			elseif (($quotes2 == 1) and ($chr == "'")) { $quotes2 = 0; }
			elseif (($quotes1 == 0) and ($quotes2 == 0)) {
				if ($chr == '(') $depth++;
				if ($chr == ')') $depth--;
				if ($chr == '"') $quotes1 = 1;
				if ($chr == "'") $quotes2 = 1;
			}
			if (($depth == 0) and (preg_match('/^(\<=|\>=|\<|\>|==|or|\|\||and|&&|!=|\+|\-| \% |\^|\=|\||\&)/i',$chr.$chr2.$chr3,$m)) and ($quotes1 == 0) and ($quotes2 == 0)) {
				$op = $m[1];
				$part = 2;
				$pos += (strlen($op) - 1);
			} elseif ($part == 1) {
				$c1 .= $chr;
			} elseif ($part == 2) {
				$c2 .= $chr;
			}
			$pos++;
		}
		$c1 = trim($c1);
		$op = trim($op);
		$c2 = trim($c2);

		if (substr($c1,0,1) == '%') {
			$d1 = '$e1';
			$e1 = &$session['vars'][substr($c1,1)];
		} elseif (substr($c1,0,1) == '$') {
			$d1 = 'evalfunction($c1,&$session)';
		} elseif ((substr($c1,0,1) == '(') and (substr($c1,-1,1) == ')')) {
			$d1 = 'evalconditional($e1,&$session)';
			$e1 = substr($c1,1,-1);
		} elseif (((substr($c1,0,1) == '"') and (substr($c1,-1,1) == '"')) or ((substr($c1,0,1) == "'") and (substr($c1,-1,1) == "'"))) {
			$d1 = '$e1';
			if (substr($c1,0,1) == '"') { $c1 = str_replace(array('\n', '\r'),array("\n", "\r"),$c1); }
			$e1 = substr($c1,1,-1);
		} elseif (preg_match('/^[0-9+-]*$/',$c1)) {
			$d1 = $c1;
		} else {
			$d1 = 'false';
		}


		if (($op) and ($c2)) {
			if (substr($c2,0,1) == '%') {
				$d2 = '$e2';
				$e2 = &$session['vars'][substr($c2,1)];
			} elseif (substr($c2,0,1) == '$') {
				$d2 = 'evalfunction($c2,&$session)';
			} elseif ((substr($c2,0,1) == '(') and (substr($c2,-1,1) == ')')) {
				$d2 = 'evalconditional($e2,&$session)';
				$e2 = substr($c2,1,-1);
			} elseif (((substr($c2,0,1) == '"') and (substr($c2,-1,1) == '"')) or ((substr($c2,0,1) == "'") and (substr($c2,-1,1) == "'"))) {
				$d2 = '$e2';
				if (substr($c2,0,1) == '"') { $c2 = str_replace(array('\n', '\r'),array("\n", "\r"),$c2); }
				$e2 = substr($c2,1,-1);
			} elseif (preg_match('/^[0-9+-]*$/',$c2)) {
				$d2 = $c2;
			} else {
				$d2 = 'false';
			}

			if ($debug) echo 'eval(\'$ret = ('.$d1.' '.$op.' '.$d2.');\');'."\n";
			eval('$ret = ('.$d1.' '.$op.' '.$d2.');');
		} else {
			if ($debug) echo 'eval(\'$ret = '.$d1.';\');'."\n";
			eval('$ret = '.$d1.';');
		}
		return $ret;
	}

	function evalstatement ($statement,&$session) {
		global $wpq;
		global $wpapi;
		global $wpi;
		global $debug;

		if ($debug) echo 'evalstatement("'.$statement.'",'.serialize($session).');'."\n";
		$statements = array (
			'if',			'set',
			'unset',		'while',
			'foreach',		'pagereplace',
			'pageappend',		'pagepregreplace',
			'pageget',		'pageset',
			'pageprepend',		'varappend',
			'varprepend',		'getrecentchanges',
			'getcategorymembers',	'eval',
			'getmodifiedtime',	'getbacklinks',
			'getembeddedin',	'getprefix'
			);

		foreach ($statements as $s) {
			if (strtolower(substr($statement,0,strlen($s))) == $s) {
				$rest = substr($statement,strlen($s));
				switch ($s) {
					case 'while':
					case 'if':
						$tmp = array();
						getarguments($rest,&$tmp,&$session,false);
//						print_r($tmp);
						if ($s == 'if') {
							if (evalconditional($tmp[0],&$session)) {
								evalscript($tmp[1],&$session);
							}
						} elseif ($s == 'while') {
							while (evalconditional($tmp[0],&$session)) {
								evalscript($tmp[1],&$session);
							}
						}
						break;
					case 'set':
						$tmp = array();
						getarguments($rest,&$tmp,&$session);
						$tmp[0] = $tmp[1];
						break;
					case 'unset':
						$rest = trim($rest);
						if ($rest{0} == '%') {
							unset($session['vars'][substr($rest,1)]);
						}
						break;
					case 'pagereplace':
					case 'pagepregreplace':
						$tmp = array();
						getarguments($rest,&$tmp,&$session);
						$page = $tmp[0];
						$search = $tmp[1];
						$replace = $tmp[2];
						if (isset($tmp[3])) {
							$editsummary = $tmp[3];
						} else {
							$editsummary = 'Replacing "'.$search.'" with "'.$replace.'". (bot)';
						}
						if ($s == 'pagereplace') {
							$wpi->post($page,str_replace($search,$replace,$wpq->getpage($page)),$editsummary);
						} elseif ($s == 'pagepregreplace') {
							$wpi->post($page,preg_replace($search,$replace,$wpq->getpage($page)),$editsummary);
						}
						break;
					case 'pageappend':
					case 'pageprepend':
					case 'pageset':
						$tmp = array();
						getarguments($rest,&$tmp,&$session);
						$page = $tmp[0];
						$data = $tmp[1];
						if (isset($tmp[2])) {
							$editsummary = $tmp[2];
						} else {
							if ($s == 'pageappend') {
								$editsummary = 'Appending "'.((strlen($data) > 150)?substr($data,0,150).'...':$data).'". (bot)';
							} elseif ($s == 'pageset') {
								$editsummary = 'Setting page to "'.((strlen($data) > 100)?substr($data,0,100).'...':$data).'". (bot)';
							}
						}
						if ($s == 'pageappend') {
							$data = $wpq->getpage($page).$data;
						} elseif ($s == 'pageprepend') {
							$data = $data.$wpq->getpage($page);
						}
						$wpi->post($page,$data,$editsummary);
						break;
					case 'pageget':
						$tmp = array();
						getarguments($rest,&$tmp,&$session);
						$page = $tmp[0];
						$tmp[1] = $wpq->getpage($page);
						break;
					case 'varappend':
					case 'varprepend':
						$tmp = array();
						getarguments($rest,&$tmp,&$session);
						if ($s == 'varprepend') {
							$tmp[0] = $tmp[1].$tmp[0];
						} elseif ($s == 'varappend') {
							$tmp[0] .= $tmp[1];
						}
						break;
					case 'foreach':
						$tmp = array();
						getarguments($rest,&$tmp,&$session);
						foreach (explode($tmp[1],$tmp[0]) as $tmp[2]) {
							evalscript(trim($tmp[3]),&$session);
						}
						break;
					case 'getrecentchanges':
						$tmp = array();
						getarguments($rest,&$tmp,&$session);
						if (count($tmp) == 2) {
							$x = $wpapi->recentchanges();
						} elseif (count($tmp) == 3) {
							$x = $wpapi->recentchanges($tmp[2]);
						}
						$z = array();
						foreach ($x as $y) {
							$z[] = $y['title'];
						}
						$tmp[0] = implode($tmp[1],$z);
						break;
					case 'getcategorymembers':
						$tmp = array();
						getarguments($rest,&$tmp,&$session);
						if (count($tmp) == 3) {
							$x = $wpapi->categorymembers($tmp[2]);
						} elseif (count($tmp) == 4) {
							$x = $wpapi->categorymembers($tmp[2],$tmp[3]);
						} elseif (count($tmp) == 5) {
							$x = $wpapi->categorymembers($tmp[2],$tmp[3],$tmp[4]);
						}
						$z = array();
						foreach ($x as $y) { $z[] = $y['title']; }
						$tmp[0] = implode($tmp[1],$z);
						break;
					case 'getbacklinks':
						$tmp = array();
						getarguments($rest,&$tmp,&$session);
						if (count($tmp) == 3) {
							$x = $wpapi->backlinks($tmp[2]);
						} elseif (count($tmp) == 4) {
							$x = $wpapi->backlinks($tmp[2],$tmp[3]);
						} elseif (count($tmp) == 5) {
							$x = $wpapi->backlinks($tmp[2],$tmp[3],$tmp[4]);
						}
						$z = array();
						if (isset($x) and is_array($x)) { foreach ($x as $y) { $z[] = $y['title']; } }
						$tmp[0] = implode($tmp[1],$z);
						break;
					case 'getembeddedin':
						$tmp = array();
						getarguments($rest,&$tmp,&$session);
						if (count($tmp) == 3) {
							$x = $wpapi->embeddedin($tmp[2]);
						} elseif (count($tmp) == 4) {
							$x = $wpapi->embeddedin($tmp[2],$tmp[3]);
						} elseif (count($tmp) == 5) {
							$x = $wpapi->embeddedin($tmp[2],$tmp[3],$tmp[4]);
						}
						$z = array();
						if (isset($x) and is_array($x)) { foreach ($x as $y) { $z[] = $y['title']; } }
						$tmp[0] = implode($tmp[1],$z);
						break;
					case 'getmodifiedtime':
						$tmp = array();
						getarguments($rest,&$tmp,&$session);
						if (count($tmp) == 2) {
							$x = $wpapi->revisions($tmp[1]);
							if (preg_match('/(\d+)\-(\d+)\-(\d+)T(\d+):(\d+):(\d+)/',$x[0]['timestamp'],$m)) {
								$tmp[0] = gmmktime($m[4],$m[5],$m[6],$m[2],$m[3],$m[1]);
							}
						}
						break;
					case 'getprefix':
						$tmp = array();
						getarguments($rest,&$tmp,&$session);
						if (count($tmp) == 3) {
							$x = $wpapi->listprefix($tmp[2]);
						} elseif (count($tmp) == 4) {
							$x = $wpapi->listprefix($tmp[2],$tmp[3]);
						} elseif (count($tmp) == 5) {
							$x = $wpapi->listprefix($tmp[2],$tmp[3],$tmp[4]);
						} elseif (count($tmp) == 6) {
							$x = $wpapi->listprefix($tmp[2],$tmp[3],$tmp[4],$tmp[5]);
						}
						$z = array();
						foreach ($x as $y) { $z[] = $y['title']; }
						$tmp[0] = implode($tmp[1],$z);
						break;
					case 'eval':
						$tmp = array();
						getarguments($rest,&$tmp,&$session);
						evalscript($tmp[0],&$session);
						break;
				}
				break;
			}
		}
	}

	function evalscript ($script,&$session = array()) {
		/* Ok, we need to evaluate a ClueBot Script. */
		global $debug;
		if ($debug) echo 'evalscript("'.$script.'",'.serialize($session).');'."\n";

		$pos = 0;
		$quotes1 = 0;
		$quotes2 = 0;
		$depth = 0;
		$script = trim($script);
		$tmp = '';

		if ((substr($script,0,1) == '{') and (substr($script,-1,1) == '}')) {
			$script = trim(substr($script,1,-1));
		}

		while ($pos < strlen($script)) {
			if (($quotes1 == 1) and ($script{$pos} == '"')) $quotes1 = 0;
			elseif (($quotes2 == 1) and ($script{$pos} == "'")) $quotes2 = 0;
			elseif (($quotes1 == 0) and ($quotes2 == 0)) {
				if ($script{$pos} == '"') $quotes1 = 1;
				if ($script{$pos} == "'") $quotes2 = 1;
				if ($script{$pos} == '{') $depth++;
				if ($script{$pos} == '}') $depth--;
				if ((($script{$pos} == ';') or ($script{$pos} == '}')) and ($depth == 0)) {
					if ($script{$pos} == '}') $tmp .= $script{$pos};
					evalstatement(trim($tmp),&$session);
					$tmp = '';
					$pos++;
					continue;
				}
			}
			$tmp .= $script{$pos};
			$pos++;
		}
	}
?>


Source to ClueBot II

[edit]
<?PHP
	declare(ticks = 1);

	pcntl_signal(SIGALRM, "docron", true);

	include 'cluebot2.cbsfunctions.php';

	function pub_evalscript ($script) { return evalscript($script); } /* Enable it to be called from docron() */
	function pub_doatq () { return doatq(); } /* Enable it to be called from docron() */
	function doatq () {
		global $user;
		global $wpq;
		global $wpi;
		global $wpapi;

		$header = explode("\n#---\n",$wpq->getpage('User:'.$user.'/atq.js'),2);
		$atq = explode("\nEOF.\n\n",$header[1]);
		$header = $header[0];
		$changed = false;
		if ((count($atq) > 0) and (isset($atq[0])) and ($atq[0])) {
			foreach ($atq as $key => $data) {
				$data = explode("\n",$data,2);
				$time = $data[0];
				$script = $data[1];
				if (time() >= $time) {
					evalscript($script);
					unset($atq[$key]);
					$changed = true;
				}
			}
		}
		if ($changed == true) {
			$wpapi->edit('User:'.$user.'/atq.js',$header."\n#---\n".implode("\nEOF.\n\n",$atq),'Updating atq.');
//			echo 'Ugg...'."\n";
		}
	}

	function docron () {
		global $user;
		global $wpq;
		global $wpapi;
		global $wpi;

		pcntl_alarm(60);
		$cron = explode("\n",$wpq->getpage('User:'.$user.'/crontab.js'));
		$m = gmdate('i');
		$h = gmdate('H');
		$dm= gmdate('d');
		$mo= gmdate('m');
		$dw= gmdate('w');
		foreach ($cron as $key => $data) {
			if (substr($data,0,1) == '#') continue;
			$data = explode(' ',$data,7);
			$min = $data[0];
			$hour = $data[1];
			$dom = $data[2];
			$mon = $data[3];
			$dow = $data[4];
			$func = $data[5];
			$args = (isset($data[6])?unserialize(base64_decode($data[6])):array());
			if (
				(
					($min == '*')
					or ($min == $m)
					or ((substr($min,0,2) == '*/') and ($tmp = explode('/',$min,2)) and (($m % $tmp[1]) == 0))
					or (in_array($m,explode(',',$min)))
				)
				and (
					($hour == '*')
					or ($hour == $h)
					or ((substr($hour,0,2) == '*/') and ($tmp = explode('/',$hour,2)) and (($h % $tmp[1]) == 0))
					or (in_array($h,explode(',',$hour)))
				)
				and (
					($dom == '*')
					or ($dom == $dm)
					or ((substr($dom,0,2) == '*/') and ($tmp = explode('/',$dom,2)) and (($dm % $tmp[1]) == 0))
					or (in_array($dm,explode(',',$dom)))
				)
				and (
					($mon == '*')
					or ($mon == $mo)
					or ((substr($mon,0,2) == '*/') and ($tmp = explode('/',$mon,2)) and (($mo % $tmp[1]) == 0))
					or (in_array($mo,explode(',',$mon)))
				)
				and (
					($dow == '*')
					or ($dow == $dw)
					or ((substr($dow,0,2) == '*/') and ($tmp = explode('/',$dow,2)) and (($dw % $tmp[1]) == 0))
					or (in_array($dw,explode(',',$dow)))
				)
			) {
				call_user_func_array('pub_'.$func,$args);
			}
		}
	}


	include '../wikibot.classes.php';
	include 'cluebot2.config.php';

	$wpq = new wikipediaquery;
	$wpi = new wikipediaindex;
	$wpapi = new wikipediaapi;

	$wpapi->login($user,$pass);

	$wpapi->edit('User:'.$user.'/Source',
		'The following is automatically generated by [[User:'.$user.'|'.$user."]].\n\n\n\n" .
		'* wikibot.classes.php is [[User:ClueBot/Source|here]].' .
		"\n\n\n\n==ClueBot Script (cluebot2.cbsfunctions.php)==\n\n<pre>" .
		htmlentities(file_get_contents('cluebot2.cbsfunctions.php'))."</pre>\n\n\n\n==Source to ".$user .
		"==\n\n".'<pre>'.htmlentities(file_get_contents(__FILE__))."</pre>\n\n\n\n" .
		'==Source to '.$user." (task 2)==\n\n<pre>".htmlentities(file_get_contents('cluebot2.task2.php')) .
		"</pre>\n\n\n\n~~~~",
		'Automated source upload.'); /* Our source code, we force post this because this is *our* page, and it triggers the nobots. */

//	$session = array();
//	evalscript('set %cm ""; getcategorymembers %cm "\n" "Wikipedia bots" "500";',&$session);
//	print_r($session);
//	foreach ($session['vars'] as $var => $value) {
//		echo '%'.$var.' = '.$value."\n";
//	}
	docron();
	while (1) sleep(2);
?>


Source to ClueBot II (task 2)

[edit]
<?PHP
	include '../wikibot.classes.php';
	include 'cluebot2.config.php';

	$wpq = new wikipediaquery;
	$wpapi = new wikipediaapi;
	$wpi = new wikipediaindex;

//	$wpapi->login($user,$pass);

	while (true) {
		$wpapi->login($user,$pass);

		$content = $wpq->getpage('Wikipedia:Suspected copyright violations');

		$content = str_replace("\n\n","\n",$content);

		$comment = '<!-- When removing items from this list, please only edit below this comment. -->'."\n";

		$tmp = explode($comment,$content,2);

		$header = $tmp[0].$comment;
		$body = $tmp[1];
	
		$body = preg_replace('/===.*===\n/iU','',$body);

//		echo $body."\n";

		preg_match_all('/(^|\n)\*\s*([^ ][^\n]*\s*)?\[\[([^|\x5d]+)\]\]\s*([^ ][^\n]*\s*)?(--|&mdash;)\s*([^ ](.|\n)*)(?=(\n\*\s*([^ ][^\n]*\s*)?\[\[[^|\x5d]+\]\]\s*([^ ][^\n]*\s*)?(--|&mdash;)|$))/iU',$body,$matches,PREG_SET_ORDER);
//		print_r($matches);

		$redlinks = false;

//		$unsetrls = unserialize(file_get_contents('unsetredlinks.txt'));
//		foreach ($unsetrls as $key => $time) if ((time() - $time) > (24*60*60)) unset($unsetrls[$key]);

		foreach ($matches as $key => $match) {
			if ($wpq->getpageid($match[3]) <= 0) {
//				$unsetrls[$match[3]] = time();
				$redlinks = true;
//				print_r($match);
				unset($matches[$key]);
			}
		}

//		file_put_contents('unsetredlinks.txt',serialize($unsetrls));

		$date = '';

		$data = $header;
		foreach ($matches as $match) {
			preg_match('/(\d+) (January|February|March|April|May|June|July|August|September|November|December) (\d+)/i',$match[5],$m);
			$tmpdate = $m[2].' '.$m[1].', '.$m[3];
//			if ($date != $tmpdate) { $date = $tmpdate; $data .= '==='.$date.'==='."\n"; } /* Uncomment this line if you want headers */
			$data .= '* '.($match[2]?$match[2].' ':'').'[['.$match[3].']] '.($match[4]?$match[4].' ':'').'&mdash; '.$match[6]."\n";
		}

//		echo $data;
		if ($redlinks == true) {
			$wpi->post('Wikipedia:Suspected copyright violations',$data,'Removing redlinks.');
		}

		sleep(600);
	}
?>


ClueBot II (talk) 16:58, 8 January 2011 (UTC)