Advertisement
JoshG

Untitled

Aug 2nd, 2011
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.57 KB | None | 0 0
  1. <?PHP
  2.  
  3. /**
  4.   * @file
  5.   * API interface
  6.   */
  7. class wikibot {
  8.     /**
  9.       * Curl handle
  10.       *
  11.       * @var ch
  12.       * @access private
  13.       */
  14.     private $ch;
  15.    
  16.     /**
  17.       * Username
  18.       *
  19.       * @var username
  20.       * @access public
  21.       */
  22.     public $user;
  23.    
  24.     /**
  25.       * Quiet setting
  26.       *
  27.       * @var echo
  28.       * @access private
  29.       */
  30.     private $echo;
  31.     private $fileid;
  32.     public $userflags;
  33.     public $userid;
  34.    
  35.     /**
  36.       * Bot construct
  37.       *
  38.       * @params boolean $echoresults Determines whether or not the script should output information.
  39.       * @params integer $loglevel What level of output should be displayed. 0 = none, 1 = errors, 2 = warnings, 3 = debug
  40.       * @return void
  41.       */
  42.     function __construct($echoresults = FALSE, $loglevel = 2) {
  43.         date_default_timezone_set('UTC');
  44.         $this->echo = $echoresults;
  45.         $this->loglevel = $loglevel;
  46.         $this->log('Initializing...', 0);
  47.         $this->ch   = curl_init();
  48.         //curl_setopt($this->ch,CURLOPT_FILE,'');
  49.         $this->user   = "";
  50.         $this->fileid = rand(1,1000);
  51.         $this->api  = "http://en.wikipedia.org/w/api.php";
  52.         curl_setopt($this->ch,CURLOPT_USERAGENT,'WikiBot/1.1');
  53.         curl_setopt($this->ch,CURLOPT_COOKIEFILE,'curl/wp.bot-'.$this->fileid.'.cookie');
  54.         curl_setopt($this->ch,CURLOPT_COOKIEJAR,'curl/wp.bot-'.$this->fileid.'.cookie');
  55.     }
  56.    
  57.     /**
  58.       * Destroys the class
  59.       *
  60.       * @return void
  61.       */
  62.     function __destruct() {
  63.         curl_close($this->ch);
  64.         if (file_exists('curl/wp.bot-'.$this->fileid.'.cookie')) {
  65.             @unlink('/tmp/wp.bot-'.$this->fileid.'.cookie');
  66.         }
  67.     }
  68.    
  69.     function log($string, $num = 1) {
  70.         $pre = array(0 => '[MSG]   ', 1 => '[ERROR] ', 2 => '[WARN]  ', 3 => '[DEBUG] ');
  71.         if ($this->echo == 1 && $this->loglevel >= $num) {
  72.             echo $pre[$num].$string.''.PHP_EOL;
  73.         }
  74.     }
  75.    
  76.     function get($to, $format = 'php') {
  77.         $reqtime = microtime(1);
  78.         $to = $to.'&format='.$format;
  79.         curl_setopt($this->ch,CURLOPT_URL,$to);
  80.         curl_setopt($this->ch,CURLOPT_TIMEOUT,120);
  81.         curl_setopt($this->ch,CURLOPT_CONNECTTIMEOUT,30);
  82.         curl_setopt($this->ch,CURLOPT_RETURNTRANSFER,1);
  83.         curl_setopt($this->ch,CURLOPT_HTTPGET,1);
  84.         $ret = curl_exec($this->ch);
  85.         $this->log('GET: '.$to.' ('.(microtime(1) - $reqtime).'s)', 3);
  86.         return unserialize(trim($ret));
  87.     }
  88.    
  89.     function post($to, $array, $format = 'php') {
  90.         $reqtime = microtime(1);
  91.         $to = $to.'?format='.$format;
  92.         curl_setopt($this->ch,CURLOPT_URL,$to);
  93.         curl_setopt($this->ch,CURLOPT_TIMEOUT,120);
  94.         curl_setopt($this->ch,CURLOPT_CONNECTTIMEOUT,30);
  95.         curl_setopt($this->ch,CURLOPT_POST,1);
  96.         curl_setopt($this->ch,CURLOPT_RETURNTRANSFER,1);
  97.         curl_setopt($this->ch,CURLOPT_POSTFIELDS, $array);
  98.         curl_setopt($this->ch,CURLOPT_HTTPHEADER, array('Expect:'));
  99.         $ret = curl_exec($this->ch);
  100.         $this->log('POST: '.$to.' ('.(microtime(1) - $reqtime).'s)', 3);
  101.         return unserialize(trim($ret));
  102.     }
  103.    
  104.     function sanitize($string) {
  105.         return urlencode($string); 
  106.     }
  107.    
  108.     function excluded($string) {
  109.         $user = $this->user;
  110.         if (preg_match('/\{\{(nobots|bots\|allow=none|bots\|deny=all|bots\|optout=all|bots\|deny=.*?'.preg_quote($user,'/').'.*?)\}\}/iS',$string)) { return false; }  
  111.         else return true;
  112.     }
  113.     #back-wards compatible
  114.     function editable($string) {
  115.         return $this->excluded($string);
  116.     }
  117.    
  118.     function query($params, $is_post = false) {
  119.         //sanitize input
  120.         if (!is_array($params)) { $params = str_replace(' ', '%20', $params); }
  121.         if ($is_post == false) { $ret = $this->get($this->api.$params);   }
  122.         if ($is_post == true ) { $ret = $this->post($this->api, $params); }
  123.         return $ret;
  124.     }
  125.    
  126.     function login($user, $pass) {
  127.         $array = array('action' => 'login', 'lgname' => $user, 'lgpassword' => $pass);
  128.         $cookie = $this->query($array, true);
  129.         if ($cookie['login']['result'] == 'Success') { return true; print_r($cookie); }
  130.         else {
  131.             if ($cookie['login']['result'] == 'NeedToken') {
  132.                 $array['lgtoken'] = $cookie['login']['token'];
  133.                 $tokenpost = $this->query($array, true);
  134.                 if ($tokenpost['login']['result'] == 'Success') {
  135.                     $this->user   = $tokenpost['login']['lgusername'];
  136.                     $this->userid = $tokenpost['login']['lguserid'];
  137.                     $this->userflags = $this->getuserflags($this->user);
  138.                     if (array_search('bot', $this->userflags) == FALSE) {
  139.                         $this->log('Your account does not have the \'BOT\' flag!', 2);
  140.                     }
  141.                     return true;
  142.                 }
  143.                 else { return false; print_r($cookie); }
  144.             }
  145.             else { return FALSE; }
  146.         }
  147.     }
  148.    
  149.     function getuserflags($user) {
  150.         if ($user == $this->user) {
  151.             $flags = $this->query('?action=query&meta=userinfo&uiprop=groups');
  152.             return $flags['query']['userinfo']['groups'];
  153.         }
  154.         else {
  155.             $flags = $this->query('?action=query&list=users&ususers='.$user.'&usprop=groups');
  156.             return $flags['query']['users']['groups'];
  157.         }
  158.     }
  159.    
  160.     function isuserblocked($user) {
  161.         $x = $this->query('?action=query&list=blocks&bkusers='.$user);
  162.         foreach ($x['query']['blocks'] as $xn) {
  163.             if ($xn['user'] == $user) { return true; }
  164.         }
  165.         return false;
  166.     }
  167.    
  168.     function getpagelimit() {
  169.         $ret = 500;
  170.         foreach($this->userflags as $flag) {
  171.             if ($flag = 'bot' || $flag = 'sysop' || $flag = 'researcher') {
  172.                 $ret = 5000;   
  173.             }
  174.         }
  175.         return $ret;
  176.     }
  177.    
  178.     function getToken($page, $type = 'edit') {
  179.         $api = $this->query('?action=query&prop=info&titles='.$page.'&intoken='.$type);
  180.         foreach($api['query']['pages'] as $page) {
  181.             if (!$page[$type.'token']) return false;
  182.             else return $page[$type.'token'];
  183.         }
  184.     }
  185.    
  186.     function siteMatrix($site = NULL,$convert = FALSE,$sitecode = NULL) {
  187.         $matrix = $this->query('?action=sitematrix');
  188.         ob_start();
  189.         print_r($matrix);
  190.         $out = ob_get_clean();
  191.         $fh=fopen('test.txt', 'a');
  192.         fwrite($fh,$out);
  193.         echo $out;
  194.         if ($site) {
  195.             foreach($matrix['sitematrix'] as $sitelist) {
  196.                 if ($sitelist['code'] == $site) {
  197.                     if ($convert == TRUE) {
  198.                         if ($sitecode) {
  199.                             foreach($sitelist['site'] as $web) {
  200.                                 if ($web['code'] == $sitecode) return $web['url'];
  201.                                 else return false;                         
  202.                             }
  203.                         }
  204.                         else return false;
  205.                     }
  206.                     else return true;
  207.                 }
  208.                 if ($sitelist['name'] == $site) {
  209.                     return 1;
  210.                 }
  211.                 foreach($sitelist['site'] as $web) {
  212.                     if ($web['url'] == $site) {
  213.                         return 1;
  214.                     }
  215.                 }
  216.             }
  217.         }
  218.         else { return $matrix; }
  219.     }
  220.    
  221.     function logout() {
  222.         $api = $this->query('?action=logout');
  223.         print_r($api);
  224.     }
  225.    
  226.     function edit($page, $content, $summary, $section = NULL, $minor = 0, $bot = 1, $noEC = 0, $recreate = 0, $createonly = 0, $nocreate = 0) {
  227.         $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);
  228.        
  229.         if ($noEC == 1) {
  230.             $parmas['basetimestamp'] = time();
  231.         }
  232.         else {
  233.             $gettoken = $this->getToken($page, 'edit');
  234.             $params['token'] = $gettoken;
  235.         }
  236.        
  237.         $api = $this->query($params, 1);
  238.         if ($api['edit']['result'] != 'Success') { return $api['edit']['result']; }
  239.         else return true;
  240.     }
  241.    
  242.     function getPage($page) {
  243.         $api = $this->query('?action=query&prop=revisions&titles='.$page.'&rvprop=content&rvlimit=1');
  244.         foreach($api['query']['pages'] as $page) {
  245.             if (isset($page['missing'])) { return false; }
  246.             else {
  247.                 foreach($page['revisions'] as $rev) {
  248.                     return $rev['*'];  
  249.                 }
  250.             }
  251.         }
  252.     }
  253.    
  254.     function getpagens($page) {
  255.         $x = $this->query('?action=query&format=php&titles='.urlencode($page).''.$append);
  256.         foreach ($x['query']['pages'] as $ns) {
  257.             return $ns['ns'];
  258.         }
  259.     }
  260.  
  261.    
  262.     function checkDisable() {
  263.         $runpage = $this->getPage($this->user."/Disable"); 
  264.         if (trim($runpage) == 'TRUE') { return TRUE; }
  265.         else { return FALSE; }
  266.     }
  267.    
  268.     function delete($title, $reason = NULL) {
  269.         $params = array( 'action' => 'delete', 'title' => $title, 'token' => $this->getToken($title, 'delete'), ($reason != NULL?'reason':'noreason') => $reason);
  270.         $api = $this->query($params, TRUE);
  271.         return $api;
  272.     }
  273.    
  274.     function categorymembers ($category,$subcat=false) {
  275.         $continue = '';
  276.         $pages = array();
  277.         while (true) {
  278.             $res = $this->query('?action=query&list=categorymembers&cmtitle='.urlencode($category).'&cmlimit='.$this->getpagelimit().$continue);
  279.             if (isset($x['error'])) {
  280.                 return false;
  281.             }
  282.             foreach ($res['query']['categorymembers'] as $x) {
  283.                 $pages[] = $x['title'];
  284.             }
  285.             if (empty($res['query-continue']['categorymembers']['cmcontinue'])) {
  286.                 if ($subcat) {
  287.                     foreach ($pages as $p) {
  288.                         if (substr($p,0,9)=='Category:') {
  289.                             $pages2 = $this->categorymembers($p,true);
  290.                             $pages = array_merge($pages,$pages2);
  291.                         }
  292.                     }
  293.                 }
  294.                 return $pages;
  295.             }
  296.             else {
  297.                 $continue = '&cmcontinue='.urlencode($res['query-continue']['categorymembers']['cmcontinue']);
  298.             }
  299.         }
  300.     }
  301.    
  302.     function subcats($cat) {
  303.         $r = array();
  304.         $x = $this->categorymembers($cat, TRUE);
  305.         foreach ($x as $subcat) {
  306.             if (substr($subcat,0,9)=='Category:') {
  307.                 $r[] = $subcat;
  308.             }
  309.         }
  310.         return $r;
  311.     }
  312.    
  313.     #built-in shutdown function for you
  314.     #specify your own bots shutoff check page
  315.     #to shut the bot down, stop=true should be in the page
  316.     #<!-- comments --> are stripped
  317.     function shutoff($page) {
  318.         $sp = $this->getpage($page);
  319.         $sp = preg_replace('/\<\!\-\-.*\-\-\>/', '', $sp);
  320.         $sp = trim($sp);
  321.         $sp = explode('=', $sp);
  322.         if ($sp[0] == 'stop' && $sp[1] == 'true') {
  323.             return TRUE;
  324.         }
  325.         else { return FALSE; }
  326.     }
  327.    
  328.     function whatlinkshere ($page,$extra=null) {
  329.         $continue = '';
  330.         $pages = array();
  331.         while (true) {
  332.             $res = $this->query('?action=query&list=backlinks&bltitle='.urlencode($page).'&bllimit=500&format=php'.$continue.$extra);
  333.             if (isset($res['error'])) {
  334.                 return false;
  335.             }
  336.             foreach ($res['query']['backlinks'] as $x) {
  337.                 $pages[] = $x['title'];
  338.             }
  339.             if (empty($res['query-continue']['backlinks']['blcontinue'])) {
  340.                 return $pages;
  341.             } else {
  342.                 $continue = '&blcontinue='.urlencode($res['query-continue']['backlinks']['blcontinue']);
  343.             }
  344.         }
  345.     }
  346.  
  347.     function transclusions($page, $extra='') {
  348.         $continue = '';
  349.         $pages = array();
  350.         while (true) {
  351.             $res = $this->query('?action=query&list=embeddedin&eititle='.$page.'&eilimit='.$this->getpagelimit().$continue.$extra);
  352.             if (isset($res['error'])) {
  353.                 return false;
  354.             }
  355.             foreach($res['query']['embeddedin'] as $x) {
  356.                 $pages[] = $x['title'];
  357.             }
  358.             if (empty($res['query-continue']['embeddedin']['eicontinue'])) {
  359.                 return $pages;
  360.             }
  361.             else {
  362.                 $continue = '&eicontinue='.urlencode($res['query-continue']['embeddedin']['eicontinue']);
  363.             }
  364.         }
  365.     }
  366.    
  367.     function expandTemplate($text, $template) {
  368.         $template = str_replace('/', '\/', $template);
  369.         # + is not used in templates, thus is a valid delimiter
  370.         # how taxing is this regex?
  371.         #$reg = '/{{'.$template.'((\n\|( *).*)+\n( *)}}|( *)}}|(\|[^=\n]+=[^|\n]*)+}})/';
  372.         $parse = array();
  373.        
  374.         /*$reg = '{{'.$template.'(\n\|( *).*)+\n( *)}}';
  375.         echo $reg.PHP_EOL;
  376.         if (preg_match_all('#'.$reg.'#', $text, $m)) {
  377.             foreach ($m[0] as $match) {
  378.                 $parse[] = $match; 
  379.             }
  380.         }
  381.         $reg =
  382.         print_r($m);*/
  383.         $reg = '/{{'.$template.'((\n( *)\|[^=}]+=[^|}]+)+}}|}}|(( *)\|[^=]+=[^|\n]+)+)/';
  384.         preg_match_all($reg, $text, $m);
  385.         print_r($m);
  386.     }
  387.    
  388.     function solveredirect($page, $text = NULL) {
  389.         if ($text == NULL) {
  390.             $page = $this->getPage($page); 
  391.         }
  392.         else {
  393.             $page = $text; 
  394.         }
  395.         if (preg_match('/^#REDIRECT \[\[.*\]\]/', $page, $m)) {
  396.             preg_match('/\[\[.*\]\]/', $m[0], $a);
  397.             return str_replace(']', '', str_replace('[', '', $a[0]));  
  398.         }
  399.         else return FALSE;
  400.     }
  401.    
  402.     function noomlog($str) {
  403.         //pass by reference
  404.         $ts = date('H:i:s');
  405.         $date = date('F j');
  406.         $mylog = $this->getpage('User:NoomBot/Log');
  407.         if (!preg_match('/== '.$date.' ==/', $mylog)) {
  408.             $str = '== '.$date.' =='.PHP_EOL.'*('.$ts.') '.$str;
  409.             $mylog = $str.PHP_EOL.$mylog;
  410.         }
  411.         else {
  412.             $mylog = preg_replace('/== '.$date.' ==/', '== '.$date.' =='.PHP_EOL.'*('.$ts.') '.$str, $mylog);
  413.         }
  414.         $this->edit('User:NoomBot/Log', $mylog, 'Logging message', TRUE, TRUE);
  415.     }
  416. }
  417.  
  418. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement