echo = $echoresults; $this->loglevel = $loglevel; $this->log('Initializing...', 0); $this->ch = curl_init(); //curl_setopt($this->ch,CURLOPT_FILE,''); $this->user = ""; $this->fileid = rand(1,1000); $this->api = "http://en.wikipedia.org/w/api.php"; curl_setopt($this->ch,CURLOPT_USERAGENT,'WikiBot/1.1'); curl_setopt($this->ch,CURLOPT_COOKIEFILE,'curl/wp.bot-'.$this->fileid.'.cookie'); curl_setopt($this->ch,CURLOPT_COOKIEJAR,'curl/wp.bot-'.$this->fileid.'.cookie'); } /** * Destroys the class * * @return void */ function __destruct() { curl_close($this->ch); if (file_exists('curl/wp.bot-'.$this->fileid.'.cookie')) { @unlink('/tmp/wp.bot-'.$this->fileid.'.cookie'); } } function log($string, $num = 1) { $pre = array(0 => '[MSG] ', 1 => '[ERROR] ', 2 => '[WARN] ', 3 => '[DEBUG] '); if ($this->echo == 1 && $this->loglevel >= $num) { echo $pre[$num].$string.''.PHP_EOL; } } function get($to, $format = 'php') { $reqtime = microtime(1); $to = $to.'&format='.$format; curl_setopt($this->ch,CURLOPT_URL,$to); curl_setopt($this->ch,CURLOPT_TIMEOUT,120); curl_setopt($this->ch,CURLOPT_CONNECTTIMEOUT,30); curl_setopt($this->ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($this->ch,CURLOPT_HTTPGET,1); $ret = curl_exec($this->ch); $this->log('GET: '.$to.' ('.(microtime(1) - $reqtime).'s)', 3); return unserialize(trim($ret)); } function post($to, $array, $format = 'php') { $reqtime = microtime(1); $to = $to.'?format='.$format; curl_setopt($this->ch,CURLOPT_URL,$to); curl_setopt($this->ch,CURLOPT_TIMEOUT,120); curl_setopt($this->ch,CURLOPT_CONNECTTIMEOUT,30); curl_setopt($this->ch,CURLOPT_POST,1); curl_setopt($this->ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($this->ch,CURLOPT_POSTFIELDS, $array); curl_setopt($this->ch,CURLOPT_HTTPHEADER, array('Expect:')); $ret = curl_exec($this->ch); $this->log('POST: '.$to.' ('.(microtime(1) - $reqtime).'s)', 3); return unserialize(trim($ret)); } function sanitize($string) { return urlencode($string); } function excluded($string) { $user = $this->user; if (preg_match('/\{\{(nobots|bots\|allow=none|bots\|deny=all|bots\|optout=all|bots\|deny=.*?'.preg_quote($user,'/').'.*?)\}\}/iS',$string)) { return false; } else return true; } #back-wards compatible function editable($string) { return $this->excluded($string); } function query($params, $is_post = false) { //sanitize input if (!is_array($params)) { $params = str_replace(' ', '%20', $params); } if ($is_post == false) { $ret = $this->get($this->api.$params); } if ($is_post == true ) { $ret = $this->post($this->api, $params); } return $ret; } function login($user, $pass) { $array = array('action' => 'login', 'lgname' => $user, 'lgpassword' => $pass); $cookie = $this->query($array, true); if ($cookie['login']['result'] == 'Success') { return true; print_r($cookie); } else { if ($cookie['login']['result'] == 'NeedToken') { $array['lgtoken'] = $cookie['login']['token']; $tokenpost = $this->query($array, true); if ($tokenpost['login']['result'] == 'Success') { $this->user = $tokenpost['login']['lgusername']; $this->userid = $tokenpost['login']['lguserid']; $this->userflags = $this->getuserflags($this->user); if (array_search('bot', $this->userflags) == FALSE) { $this->log('Your account does not have the \'BOT\' flag!', 2); } return true; } else { return false; print_r($cookie); } } else { return FALSE; } } } function getuserflags($user) { if ($user == $this->user) { $flags = $this->query('?action=query&meta=userinfo&uiprop=groups'); return $flags['query']['userinfo']['groups']; } else { $flags = $this->query('?action=query&list=users&ususers='.$user.'&usprop=groups'); return $flags['query']['users']['groups']; } } function isuserblocked($user) { $x = $this->query('?action=query&list=blocks&bkusers='.$user); foreach ($x['query']['blocks'] as $xn) { if ($xn['user'] == $user) { return true; } } return false; } function getpagelimit() { $ret = 500; foreach($this->userflags as $flag) { if ($flag = 'bot' || $flag = 'sysop' || $flag = 'researcher') { $ret = 5000; } } return $ret; } function getToken($page, $type = 'edit') { $api = $this->query('?action=query&prop=info&titles='.$page.'&intoken='.$type); foreach($api['query']['pages'] as $page) { if (!$page[$type.'token']) return false; else return $page[$type.'token']; } } function siteMatrix($site = NULL,$convert = FALSE,$sitecode = NULL) { $matrix = $this->query('?action=sitematrix'); ob_start(); print_r($matrix); $out = ob_get_clean(); $fh=fopen('test.txt', 'a'); fwrite($fh,$out); echo $out; if ($site) { foreach($matrix['sitematrix'] as $sitelist) { if ($sitelist['code'] == $site) { if ($convert == TRUE) { if ($sitecode) { foreach($sitelist['site'] as $web) { if ($web['code'] == $sitecode) return $web['url']; else return false; } } else return false; } else return true; } if ($sitelist['name'] == $site) { return 1; } foreach($sitelist['site'] as $web) { if ($web['url'] == $site) { return 1; } } } } else { return $matrix; } } function logout() { $api = $this->query('?action=logout'); print_r($api); } function edit($page, $content, $summary, $section = NULL, $minor = 0, $bot = 1, $noEC = 0, $recreate = 0, $createonly = 0, $nocreate = 0) { $params = array('token' => '', 'action' => 'edit', 'title' => str_replace(' ', '_', $page), 'text' => $content, 'summary' => $summary, ($section?'section':'nosection') => ($section?$section:''), ($minor == 1?'minor':'notminor') => 1, ($bot == 1?'bot':'nobot') => 1, ($recreate == 1?'recreate':'norecreate') => 1, ($createonly == 1?'createonly':'nocreateonly') => 1, ($nocreate == 1?'nocreate':'nonocreate') => 1); if ($noEC == 1) { $parmas['basetimestamp'] = time(); } else { $gettoken = $this->getToken($page, 'edit'); $params['token'] = $gettoken; } $api = $this->query($params, 1); if ($api['edit']['result'] != 'Success') { return $api['edit']['result']; } else return true; } function getPage($page) { $api = $this->query('?action=query&prop=revisions&titles='.$page.'&rvprop=content&rvlimit=1'); foreach($api['query']['pages'] as $page) { if (isset($page['missing'])) { return false; } else { foreach($page['revisions'] as $rev) { return $rev['*']; } } } } function getpagens($page) { $x = $this->query('?action=query&format=php&titles='.urlencode($page).''.$append); foreach ($x['query']['pages'] as $ns) { return $ns['ns']; } } function checkDisable() { $runpage = $this->getPage($this->user."/Disable"); if (trim($runpage) == 'TRUE') { return TRUE; } else { return FALSE; } } function delete($title, $reason = NULL) { $params = array( 'action' => 'delete', 'title' => $title, 'token' => $this->getToken($title, 'delete'), ($reason != NULL?'reason':'noreason') => $reason); $api = $this->query($params, TRUE); return $api; } function categorymembers ($category,$subcat=false) { $continue = ''; $pages = array(); while (true) { $res = $this->query('?action=query&list=categorymembers&cmtitle='.urlencode($category).'&cmlimit='.$this->getpagelimit().$continue); if (isset($x['error'])) { return false; } foreach ($res['query']['categorymembers'] as $x) { $pages[] = $x['title']; } if (empty($res['query-continue']['categorymembers']['cmcontinue'])) { if ($subcat) { foreach ($pages as $p) { if (substr($p,0,9)=='Category:') { $pages2 = $this->categorymembers($p,true); $pages = array_merge($pages,$pages2); } } } return $pages; } else { $continue = '&cmcontinue='.urlencode($res['query-continue']['categorymembers']['cmcontinue']); } } } function subcats($cat) { $r = array(); $x = $this->categorymembers($cat, TRUE); foreach ($x as $subcat) { if (substr($subcat,0,9)=='Category:') { $r[] = $subcat; } } return $r; } #built-in shutdown function for you #specify your own bots shutoff check page #to shut the bot down, stop=true should be in the page # are stripped function shutoff($page) { $sp = $this->getpage($page); $sp = preg_replace('/\<\!\-\-.*\-\-\>/', '', $sp); $sp = trim($sp); $sp = explode('=', $sp); if ($sp[0] == 'stop' && $sp[1] == 'true') { return TRUE; } else { return FALSE; } } function whatlinkshere ($page,$extra=null) { $continue = ''; $pages = array(); while (true) { $res = $this->query('?action=query&list=backlinks&bltitle='.urlencode($page).'&bllimit=500&format=php'.$continue.$extra); if (isset($res['error'])) { return false; } foreach ($res['query']['backlinks'] as $x) { $pages[] = $x['title']; } if (empty($res['query-continue']['backlinks']['blcontinue'])) { return $pages; } else { $continue = '&blcontinue='.urlencode($res['query-continue']['backlinks']['blcontinue']); } } } function transclusions($page, $extra='') { $continue = ''; $pages = array(); while (true) { $res = $this->query('?action=query&list=embeddedin&eititle='.$page.'&eilimit='.$this->getpagelimit().$continue.$extra); if (isset($res['error'])) { return false; } foreach($res['query']['embeddedin'] as $x) { $pages[] = $x['title']; } if (empty($res['query-continue']['embeddedin']['eicontinue'])) { return $pages; } else { $continue = '&eicontinue='.urlencode($res['query-continue']['embeddedin']['eicontinue']); } } } function expandTemplate($text, $template) { $template = str_replace('/', '\/', $template); # + is not used in templates, thus is a valid delimiter # how taxing is this regex? #$reg = '/{{'.$template.'((\n\|( *).*)+\n( *)}}|( *)}}|(\|[^=\n]+=[^|\n]*)+}})/'; $parse = array(); /*$reg = '{{'.$template.'(\n\|( *).*)+\n( *)}}'; echo $reg.PHP_EOL; if (preg_match_all('#'.$reg.'#', $text, $m)) { foreach ($m[0] as $match) { $parse[] = $match; } } $reg = print_r($m);*/ $reg = '/{{'.$template.'((\n( *)\|[^=}]+=[^|}]+)+}}|}}|(( *)\|[^=]+=[^|\n]+)+)/'; preg_match_all($reg, $text, $m); print_r($m); } function solveredirect($page, $text = NULL) { if ($text == NULL) { $page = $this->getPage($page); } else { $page = $text; } if (preg_match('/^#REDIRECT \[\[.*\]\]/', $page, $m)) { preg_match('/\[\[.*\]\]/', $m[0], $a); return str_replace(']', '', str_replace('[', '', $a[0])); } else return FALSE; } function noomlog($str) { //pass by reference $ts = date('H:i:s'); $date = date('F j'); $mylog = $this->getpage('User:NoomBot/Log'); if (!preg_match('/== '.$date.' ==/', $mylog)) { $str = '== '.$date.' =='.PHP_EOL.'*('.$ts.') '.$str; $mylog = $str.PHP_EOL.$mylog; } else { $mylog = preg_replace('/== '.$date.' ==/', '== '.$date.' =='.PHP_EOL.'*('.$ts.') '.$str, $mylog); } $this->edit('User:NoomBot/Log', $mylog, 'Logging message', TRUE, TRUE); } } ?>