« Utilisateur:Amgine/mwAPI.php » : différence entre les versions

Contenu supprimé Contenu ajouté
Amgine (discuter | contributions)
stuck at the xml parsing stage for the moment
(Aucune différence)

Version du 2 janvier 2010 à 20:12

<?php
/**
 * mwAPI class
 **
 * Class to hold methods re: Mediawiki API
 * 
 * This class has several dependencies/assumptions
 * 		* PHP libcurl module is enabled
 * 		* script has read/write privileges in the folder where it is run.
 * 		  (allowing it to store cookies)
 **
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the Free
 *  Software Foundation; either version 2 of the License, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 *  more details.
 *
 *  You should have received a copy of the GNU General Public License along with
 *  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 *  Place - Suite 330, Boston, MA 02111-1307, USA.
 *  http://www.gnu.org/copyleft/gpl.html
 **
 * @author Amgine <amgine.saewyc@gmail.com>
 * @copyright 2009 by Amgine <amgine.saewyc@gmail.com>
 */
class mwAPI{
	/**
	 * @var	string	qualified domain/path to api
	 **/
	protected $address = '';
	
	/**
	 * @var	array	Cookie management/storage
	 **/
	public $cookieJar = array();
	
	/**
	 * @var	string	cURL handle
	 **/
	private $ch = '';
	
	/**
	 * @var	string	Contents of the output buffer from cURL action.
	 **
	 * WARNING:	this buffer is over-written with every execCurl.
	 **/
	public $curlBuffer = '';
	
	/**
	 * __construct method
	 **
	 * The construction method is called when the class is instantiated.
	 **
	 * @param	$mwUser	string	Mediawiki username
	 * @param	$mwPass	string	Mediawiki user password
	 * @param	$site	string	qualified url/path to api.php
	 **/
	private function __construct( $mwUser, $mwPass, $mwSite ){
		
		// $mwSite should be validated here.
		// it *must* be in the form http://domain.com/path/, including
		// the trailing /.
		$this->ch = curl_init( $site );
		if( mwLogin( $mwUser, $mwPass, $mwSite ) ){
			// successfully logged into API
		}
	}
	
	/**
	 * __destruct method
	 **
	 * To insure resources are released.
	 **/
	private function __destruct(){
		curl_close( $this->ch );
	}
	
	/**
	 * mwLogin method
	 **
	 * Establishes connection/session with mwAPI server
	 **
	 * @param $mwUser	string	Mediawiki account name
	 * @param $mwPass	string	Mediawiki account password
	 * @param $mwSite	string	Qualified url/path
	 **/
	private function mwLogin( $mwUser, $mwPass, $mwSite ){
		$postvars = array(
			'action' 		=> 'login',
			'lgname' 		=> $mwUser,
			'lgpassword'	=> $mwPass,
		);
		
		curl_setopt( $this->ch, CURLOPT_POST, true );
		curl_setopt( $this->ch, CURLOPT_POSTFIELDS, $postvars );
		curl_setopt( $this->ch, CURLOPT_COOKIESESSION, true );
		curl_setopt( $this->ch, CURLOPT_COOKIEJAR, 'cookies.txt' );
		
		$val = execCurl();
		
		// Parse xml? grab cookies?
	}
	
	/**
	 * execCurl method
	 **
	 * Executes a curl instance after turning off buffers, retrieves
	 * buffer into $this->curlBuffer, and returns result of execution
	 * as value.
	 * 
	 * Examples of use:
	 * 		$val = execCurl();
	 **
	 * @return	bool	Return value of curl_exec();
	 **/
	public function execCurl(){
		ob_start();
		$val = curl_exec( $this->ch );
		$this->curlBuffer = ob_get_contents();
		ob_end_clean();
		return $val;
	}
}