User:ClueBot II/Source

The following is automatically generated by ClueBot II.


 * wikibot.classes.php is here.

ClueBot Script (cluebot2.cbsfunctions.php)
&lt;?PHP $debug = true; function getarguments ($data,&amp;$return,&amp;$session,$conditionals = true,$delimiter = ' ') { $pos = 0; $depthp = 0; $depthb = 0; $quotes1 = 0; $quotes2 = 0; $tmp = ''; $chr = ''; $data = trim($data); $array = array;

while ($pos &lt; strlen($data)) { $chr = $data{$pos}; if (($quotes1 == 1) and ($chr == '&quot;')) { $quotes1 = 0; } elseif (($quotes2 == 1) and ($chr == &quot;'&quot;)) { $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 == '&quot;') $quotes1 = 1; if ($chr == &quot;'&quot;) $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[] = &amp;$session['vars'][substr($arg,1)]; } elseif ($arg{0} == '{') { $return[] = $arg; } elseif (($conditionals == false) and ($arg{0} == '(')) {				$return[] = $arg;			} else {				$return[] = evalconditional($arg,&amp;$session);			}		}	}

function evalfunction ($function,&amp;$session) { if (preg_match('/^\$([^(]+)\((.*)\)$/',trim($function),$m)) {			$func = strtolower($m[1]);			$args = array;			getarguments($m[2],&amp;$args,&amp;$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) &gt;= 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&lt;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,&amp;$session) { global $debug; if ($debug) echo 'evalconditional(&quot;'.$conditional.'&quot;,'.serialize($session).');'.&quot;\n&quot;; $pos = 0; $depth = 0; $part = 1; $quotes1 = 0; $quotes2 = 0; $c1 = ''; $op = ''; $c2 = '';

while ($pos &lt; strlen($conditional)) { $chr = $conditional{$pos}; $chr2 = $conditional{$pos+1}; $chr3 = $conditional{$pos+2}; if (($quotes1 == 1) and ($chr == '&quot;')) { $quotes1 = 0; } elseif (($quotes2 == 1) and ($chr == &quot;'&quot;)) { $quotes2 = 0; } elseif (($quotes1 == 0) and ($quotes2 == 0)) { if ($chr == '(') $depth++;				if ($chr == ')') $depth--; if ($chr == '&quot;') $quotes1 = 1; if ($chr == &quot;'&quot;) $quotes2 = 1; }			if (($depth == 0) and (preg_match('/^(\&lt;=|\&gt;=|\&lt;|\&gt;|==|or|\|\||and|&amp;&amp;|!=|\+|\-| \% |\^|\=|\||\&amp;)/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 = &amp;$session['vars'][substr($c1,1)]; } elseif (substr($c1,0,1) == '$') { $d1 = 'evalfunction($c1,&amp;$session)'; } elseif ((substr($c1,0,1) == '(') and (substr($c1,-1,1) == ')')) { $d1 = 'evalconditional($e1,&amp;$session)'; $e1 = substr($c1,1,-1); } elseif (((substr($c1,0,1) == '&quot;') and (substr($c1,-1,1) == '&quot;')) or ((substr($c1,0,1) == &quot;'&quot;) and (substr($c1,-1,1) == &quot;'&quot;))) { $d1 = '$e1'; if (substr($c1,0,1) == '&quot;') { $c1 = str_replace(array('\n', '\r'),array(&quot;\n&quot;, &quot;\r&quot;),$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 = &amp;$session['vars'][substr($c2,1)]; } elseif (substr($c2,0,1) == '$') { $d2 = 'evalfunction($c2,&amp;$session)'; } elseif ((substr($c2,0,1) == '(') and (substr($c2,-1,1) == ')')) { $d2 = 'evalconditional($e2,&amp;$session)'; $e2 = substr($c2,1,-1); } elseif (((substr($c2,0,1) == '&quot;') and (substr($c2,-1,1) == '&quot;')) or ((substr($c2,0,1) == &quot;'&quot;) and (substr($c2,-1,1) == &quot;'&quot;))) { $d2 = '$e2'; if (substr($c2,0,1) == '&quot;') { $c2 = str_replace(array('\n', '\r'),array(&quot;\n&quot;, &quot;\r&quot;),$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.');\');'.&quot;\n&quot;; eval('$ret = ('.$d1.' '.$op.' '.$d2.');'); } else { if ($debug) echo 'eval(\'$ret = '.$d1.';\');'.&quot;\n&quot;; eval('$ret = '.$d1.';'); }		return $ret; }

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

if ($debug) echo 'evalstatement(&quot;'.$statement.'&quot;,'.serialize($session).');'.&quot;\n&quot;; $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,&amp;$tmp,&amp;$session,false); //						print_r($tmp); if ($s == 'if') { if (evalconditional($tmp[0],&amp;$session)) { evalscript($tmp[1],&amp;$session); }						} elseif ($s == 'while') { while (evalconditional($tmp[0],&amp;$session)) { evalscript($tmp[1],&amp;$session); }						}						break; case 'set': $tmp = array; getarguments($rest,&amp;$tmp,&amp;$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,&amp;$tmp,&amp;$session); $page = $tmp[0]; $search = $tmp[1]; $replace = $tmp[2]; if (isset($tmp[3])) { $editsummary = $tmp[3]; } else { $editsummary = 'Replacing &quot;'.$search.'&quot; with &quot;'.$replace.'&quot;. (bot)'; }						if ($s == 'pagereplace') { $wpi-&gt;post($page,str_replace($search,$replace,$wpq-&gt;getpage($page)),$editsummary); } elseif ($s == 'pagepregreplace') { $wpi-&gt;post($page,preg_replace($search,$replace,$wpq-&gt;getpage($page)),$editsummary); }						break; case 'pageappend': case 'pageprepend': case 'pageset': $tmp = array; getarguments($rest,&amp;$tmp,&amp;$session); $page = $tmp[0]; $data = $tmp[1]; if (isset($tmp[2])) { $editsummary = $tmp[2]; } else { if ($s == 'pageappend') { $editsummary = 'Appending &quot;'.((strlen($data) &gt; 150)?substr($data,0,150).'...':$data).'&quot;. (bot)'; } elseif ($s == 'pageset') { $editsummary = 'Setting page to &quot;'.((strlen($data) &gt; 100)?substr($data,0,100).'...':$data).'&quot;. (bot)'; }						}						if ($s == 'pageappend') { $data = $wpq-&gt;getpage($page).$data; } elseif ($s == 'pageprepend') { $data = $data.$wpq-&gt;getpage($page); }						$wpi-&gt;post($page,$data,$editsummary); break; case 'pageget': $tmp = array; getarguments($rest,&amp;$tmp,&amp;$session); $page = $tmp[0]; $tmp[1] = $wpq-&gt;getpage($page); break; case 'varappend': case 'varprepend': $tmp = array; getarguments($rest,&amp;$tmp,&amp;$session); if ($s == 'varprepend') { $tmp[0] = $tmp[1].$tmp[0]; } elseif ($s == 'varappend') { $tmp[0] .= $tmp[1]; }						break; case 'foreach': $tmp = array; getarguments($rest,&amp;$tmp,&amp;$session); foreach (explode($tmp[1],$tmp[0]) as $tmp[2]) { evalscript(trim($tmp[3]),&amp;$session); }						break; case 'getrecentchanges': $tmp = array; getarguments($rest,&amp;$tmp,&amp;$session); if (count($tmp) == 2) { $x = $wpapi-&gt;recentchanges; } elseif (count($tmp) == 3) { $x = $wpapi-&gt;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,&amp;$tmp,&amp;$session); if (count($tmp) == 3) { $x = $wpapi-&gt;categorymembers($tmp[2]); } elseif (count($tmp) == 4) { $x = $wpapi-&gt;categorymembers($tmp[2],$tmp[3]); } elseif (count($tmp) == 5) { $x = $wpapi-&gt;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,&amp;$tmp,&amp;$session); if (count($tmp) == 3) { $x = $wpapi-&gt;backlinks($tmp[2]); } elseif (count($tmp) == 4) { $x = $wpapi-&gt;backlinks($tmp[2],$tmp[3]); } elseif (count($tmp) == 5) { $x = $wpapi-&gt;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,&amp;$tmp,&amp;$session); if (count($tmp) == 3) { $x = $wpapi-&gt;embeddedin($tmp[2]); } elseif (count($tmp) == 4) { $x = $wpapi-&gt;embeddedin($tmp[2],$tmp[3]); } elseif (count($tmp) == 5) { $x = $wpapi-&gt;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,&amp;$tmp,&amp;$session); if (count($tmp) == 2) { $x = $wpapi-&gt;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,&amp;$tmp,&amp;$session); if (count($tmp) == 3) { $x = $wpapi-&gt;listprefix($tmp[2]); } elseif (count($tmp) == 4) { $x = $wpapi-&gt;listprefix($tmp[2],$tmp[3]); } elseif (count($tmp) == 5) { $x = $wpapi-&gt;listprefix($tmp[2],$tmp[3],$tmp[4]); } elseif (count($tmp) == 6) { $x = $wpapi-&gt;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,&amp;$tmp,&amp;$session); evalscript($tmp[0],&amp;$session); break; }				break; }		}	}

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

$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 &lt; strlen($script)) { if (($quotes1 == 1) and ($script{$pos} == '&quot;')) $quotes1 = 0; elseif (($quotes2 == 1) and ($script{$pos} == &quot;'&quot;)) $quotes2 = 0; elseif (($quotes1 == 0) and ($quotes2 == 0)) { if ($script{$pos} == '&quot;') $quotes1 = 1; if ($script{$pos} == &quot;'&quot;) $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),&amp;$session); $tmp = ''; $pos++; continue; }			}			$tmp .= $script{$pos}; $pos++; }	} ?&gt;

Source to ClueBot II
&lt;?PHP declare(ticks = 1);

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

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

pcntl_alarm(60); $cron = explode(&quot;\n&quot;,$wpq-&gt;getpage('User:'.$user.'/crontab.js')); $m = gmdate('i'); $h = gmdate('H'); $dm= gmdate('d'); $mo= gmdate('m'); $dw= gmdate('w'); foreach ($cron as $key =&gt; $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-&gt;login($user,$pass);

$wpapi-&gt;edit('User:'.$user.'/Source',		'The following is automatically generated by '.$user.&quot;.\n\n\n\n&quot; .		'* wikibot.classes.php is here.' .		&quot;\n\n\n\n==ClueBot Script (cluebot2.cbsfunctions.php)==\n\n&lt;pre&gt;&quot; .		htmlentities(file_get_contents('cluebot2.cbsfunctions.php')).&quot;&lt;/pre&gt;\n\n\n\n==Source to &quot;.$user .		&quot;==\n\n&quot;.'&lt;pre&gt;'.htmlentities(file_get_contents(__FILE__)).&quot;&lt;/pre&gt;\n\n\n\n&quot; .		'==Source to '.$user.&quot; (task 2)==\n\n&lt;pre&gt;&quot;.htmlentities(file_get_contents('cluebot2.task2.php')) .		&quot;&lt;/pre&gt;\n\n\n\n~&quot;,		'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 &quot;&quot;; getcategorymembers %cm &quot;\n&quot; &quot;Wikipedia bots&quot; &quot;500&quot;;',&amp;$session); //	print_r($session); //	foreach ($session['vars'] as $var =&gt; $value) { //		echo '%'.$var.' = '.$value.&quot;\n&quot;; //	}	docron; while (1) sleep(2); ?&gt;

Source to ClueBot II (task 2)
&lt;?PHP include '../wikibot.classes.php'; include 'cluebot2.config.php';

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

//	$wpapi-&gt;login($user,$pass);

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

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

$content = str_replace(&quot;\n\n&quot;,&quot;\n&quot;,$content);

$comment = '&lt;!-- When removing items from this list, please only edit below this comment. --&gt;'.&quot;\n&quot;;

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

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

//		echo $body.&quot;\n&quot;;

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

$redlinks = false;

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

foreach ($matches as $key =&gt; $match) { if ($wpq-&gt;getpageid($match[3]) &lt;= 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.'==='.&quot;\n&quot;; } /* Uncomment this line if you want headers */ $data .= '* '.($match[2]?$match[2].' ':)..$match[3].' '.($match[4]?$match[4].' ':'').'&amp;mdash; '.$match[6].&quot;\n&quot;; }

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

sleep(600); } ?&gt;

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