Utilisateur:Amgine/mwApiXmlParse.php

Une nouvelle de Wikinews, la source d'informations que vous pouvez écrire.
<?php
/**
 * mwApiXmlParse class
 **
 * Class to hold parsing methods re: Mediawiki API
 * 
 * This class has several dependencies/assumptions
 * 		* PHP xml module is enabled (which, in turn, relies on expat
 * 		  by James Clark.
 * 		* mwApiXmlItem (class in mwApiXmlParse.php)
 **
 *  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 2010 by Amgine <amgine.saewyc@gmail.com>
 */
class mwApiXmlParse{
	/**
	 * @var	$tag	string	xml tag name	
	 **/
	public $tag = '';
	
	/**
	 * @var $query	bool	Within a query xml tag
	 **/
	public $query = false;
	/**
	 * @var $pages	bool	Within a pages xml tag
	 **/
	public $pages = false;
	/**
	 * @var $revisions	bool	Within a revisions xml tag
	 **/
	public $revisions = false;
	
	/**
	 * @var $depth	int		xml element nested depth
	 **/
	public $depth = array();
	
	/**
	 * @var	$allElements	string	text map
	 **/
	public $allElements = '';
	
	/**
	 * @var	$item	object	mwApiXmlItem
	 **/
	public $item;
	
	/**
	 * @var $values
	 **/
	
	/**
	 * __construct method
	 **/
	function __construct(){
		$this->item = new mwApiXmlItem;
	}
	
	/**
	 * startElement method
	 **
	 * Process new xml tags, as called by xml_parse in an event-driven
	 * xml parser.
	 **
	 * @param	$parser	string	resource handler
	 * @param	$tag	string	name of xml element
	 * @param	$attrib	array	associative array of element attributes
	 **/
	function startElement( $parser, $tag, $attrib ){
		for ($i = 0; $i < $this->depth[$parser]; $i++) {
	        $this->allElements .= '  ';
	    }
	    $this->allElements .= "$tag\n";
	    $this->depth[$parser]++;
	    
	    //debug lines
	    // echo "Processing $tag\n";
	    // print_r( $attrib );
	    
		switch( $tag ){
			case 'login':
				if( 'Success' == $attrib['result'] ){
					$this->item->userid = $attrib['lguserid'];
					$this->item->username = $attrib['lgusername'];
					$this->item->loginToken = $attrib['lgtoken'];
					$this->item->cookiePrefix = $attrib['cookieprefix'];
					$this->item->sessionId = $attrib['sessionid'];
				}
				break;
			case 'query':
				// Beginning of query tag
				$this->tag = $tag;
				$this->query = true;
				break;
			case 'pages':
				//beginning of pages tag
				$this->tag = $tag;
				$this->pages = true;
				break;
			case 'page':
				$this->item->pageid = $attrib['pageid'];
				$this->item->ns = $attrib['ns'];
				$this->item->title = $attrib['title'];
				$this->item->touched = $attrib['touched'];
				$this->item->lastrevid = $attrib['lastrevid'];
				$this->item->counter = $attrib['counter'];
				$this->item->length = $attrib['length'];
				$this->item->edittoken = $attrib['edittoken'];
				break;
			case 'revisions':
				// beginning of revisions tag
				$this->tag = $tag;
				$this->revisions = true;
				break;
			case 'rev':
				$this->item->revid = $attrib['revid'];
				$this->item->user = $attrib['user'];
				$this->item->timestamp = $attrib['timestamp'];
				$this->item->comment = $attrib['comment'];
				break;
			case 'edit':
				if( 'Success' == $attrib['result'] ){
					$this->setItems( $attrib );
					if( false != isset( $attrib['new'] ) ){
						$this->item->new = true;
					}
				}else{
					$this->item->result = $attrib['result'];
				}
				break;
			case 'error':
				$this->item->error = true;
				setItems( $attrib );
				break;
			case 'api':
				// default L1 tag
				
		}
	}
	
	/**
	 * endElement method
	 **
	 * Process at the end of xml tags, as called by xml_parse in an
	 * event-driven xml parser.
	 **
	 * @param	$pasers	string	Resource handler
	 * @param	$tag	string	xml element name
	 **/
	function endElement( $parser, $tag ) {
	    $this->depth[$parser]--;
	    
	    switch( $tag ){
	    	case 'query':
	    		$this->query = false;
	    		break;
	    	case 'pages':
	    		$this->pages = false;
	    		break;
	    	case 'revisions':
	    		$this->revisions = false;
	    		break;
	    }
	}
	
	/**
	 * setItems method
	 **
	 * Set multiple item values via array
	 **
	 * @param	$attrib	array	Attribution values from an xml tag.
	 **/
	public function setItems( $attrib ){
		foreach( $attrib AS $key => $val ){
			$this->item->$key = $val;
		}
	}
}

/**
 * mwApiXmlItem class
 **
 * Class to hold returned values and tokens re: Mediawiki API
 **
 *  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 2010 by Amgine <amgine.saewyc@gmail.com>
 */
class mwApiXmlItem{
	/**
	 * @var $comment	string	revision summary
	 **/
	public $comment = '';
	
	/**
	 * @var	$cookiePrefix	string	prefix for cookie names
	 **/
	public $cookiePrefix = '';
	
	/**
	 * @var $counter	string	representation of integer (page hits counter?)
	 **/
	public $counter = '';
	
	/**
	 * @var $edittoken	string	random session edit token
	 **/
	public $edittoken = '';
	
	/**
	 * @var	$lastrevid	string	representation of integer revid
	 **/
	public $lastrevid = '';
	
	/**
	 * @var	$length	string	representation of integer, length of text (in bytes?)
	 **/
	public $length = '';
	
	/**
	 * @var $loginToken	string	Login token
	 **/
	public $loginToken = '';
	
	/**
	 * @var $new	string/bool	Empty string, if set true the page edited was a new creation.
	 */
	public $new = '';
	
	/**
	 * @var	$newrevid	string	representation of integer revision id
	 **/
	public $newrevid = '';
	
	/**
	 * @var $newtimestamp	string	newest revision timestamp
	 **/
	public $newtimestamp = '';
	
	/**
	 * @var $ns		string	represenation of integer namespace
	 **/
	public $ns = '';
	
	/**
	 * @var $oldrevid	string	representation of integer revision value, 0 = new page creation
	 **/
	public $oldrevid = '';
	
	/**
	 * @var $pageid		string	representation of integer page id (unique)
	 **/
	public $pageid = '';
	
	/**
	 * @var	$result	string	'Success' = last api query was successful.
	 **/
	public $result = '';
	
	/**
	 * @var $revid	string	representation of integer.
	 **/
	public $revid = '';
	
	/**
	 * @var $sessionId	string	session id hash
	 **/
	public $sessionId = '';
	
	/**
	 * @var	$title	string	unique page title
	 **/
	public $title = '';
	
	/**
	 * @var $touched	string	timestamp of last revision
	 **/
	public $touched = '';
	
	/**
	 * @var $timestamp	string	revision timestamp.
	 **/
	public $timestamp = '';
	
	/**
	 * @var	$user	string	revision submitted by this user.
	 **/
	public $user = '';
	
	/**
	 * @var	$userid	string	User id
	 **/
	public $userid = '';
	
	/**
	 * @var $username	string	User name
	 **/
	public $username = '';
		
}