User:Mutex

From MU Online Help Wiki

Jump to: navigation, search

Mutex is our work in progress homebrew media wiki bot. Full with bugs but still happy to serve.

Feel free to use its code:

 <?php
 class mediawiki{
   private $api_url = ;
   private $format = 'php';
   private $cookie_file = 'cookie.txt';
   function __construct($api_url){
     $this->api_url = $api_url;
   }
   private function get($url){
     echo $url;
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file);
     curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file);
     $result = curl_exec($ch);
     curl_close($ch);
     return $result;
   }
   private function post($url, $post){
     echo $url;
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
     curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file);
     curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file);
     $result = curl_exec($ch);
     curl_close($ch);
     return $result;
   }
   private function q($text){
     return htmlentities($text);
   }
   private function us($text){
     return unserialize($text);
   }
   private function createURL($params){
     $url = $this->api_url.'?'.$this->createParams($params);
     return $url;
   }
   private function createParams($params){
     return http_build_query($params, , '&');
   }
   function sitematrix(){
     $params = array('action' => 'sitematrix', 'format' => $this->format);
     return $this->get($this->createURL($params));
   }
   function query($params){
     $params['action'] = 'query';
     $params['format'] = $this->format;
     return $this->us($this->get($this->createURL($params)));
   }
   function getPage($titles){
     $params = array('action' => 'query',
                     'prop'   => 'revisions',
                     'titles' => $this->q($titles),
                     'rvprop' => 'content',
                     'format' => $this->format);
     return $this->us($this->get($this->createURL($params)));
   }
   function parse($title){
     $params = array('action' => 'parse',
                     'page' => $this->q($title),
                     'format' => $this->format);
     return $this->us($this->get($this->createURL($params)));
   }
   private function _getTokenTS($title){
     $r = array('action'=>'query',
                'prop'=>'info|revisions',
                'intoken'=>'edit',
                'titles'=>$this->q($title),
                'format' => $this->format);
     $ary = $this->query($r);
     $t = current(current(current($ary)));
     $res = array();
     $res['edittoken'] = $t['edittoken'];
     $t = current($t['revisions']);
     $res['timestamp'] = $t['timestamp'];
     return $res;
   }
   function editPage($title, $text, $replace = true){
     $tt = $this->_getTokenTS($title);
     $r1 = array( 'action' => 'edit');
     $r = array( 'action' => 'edit',
                 'title' => $this->q($title),
                 'section' => '0',
                 'summary' => 'Changed by Mutex',
                 'text' => $this->q($text),
                 'basetimestamp' => $tt['timestamp'],
                 'format' => $this->format,
                 'token' => $tt['edittoken']);
     return $this->us($this->post($this->createURL($r1), $this->createParams($r)));
   }
   function login($user, $pass){
     $r = array( 'action'=>'login',
                 'lgname' => $this->q($user),
                 'lgpassword' => $this->q($pass),
                 'format' => $this->format
     );
     $r1 = array( 'action' => 'login');
     return $this->us($this->post($this->createURL($r1), $this->createParams($r)));
   }
 }
 ?>
Personal tools
ads