« 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
 
Amgine (discuter | contributions)
Aucun résumé des modifications
Ligne 10 :
* * script has read/write privileges in the folder where it is run.
* (allowing it to store cookies)
* * mwApiXmlParse class (in mwApiXmlParse.php)
**
* This program is free software; you can redistribute it and/or modify it
Ligne 51 ⟶ 52 :
**/
public $curlBuffer = '';
/**
* @var string xml parser reference
**/
public $xml_parser = '';
/**
* @var object mwApiXmlItem
* /
public $item = array();
/**
Ligne 62 ⟶ 73 :
**/
private function __construct( $mwUser, $mwPass, $mwSite ){
// Insure mwApiXmlParse is available
require_once( 'mwApiXmlParse.php' );
// $mwSite should be validated here.
// it *must* be in the form http://domain.com/path/, including
// the trailing /.
$this->ch = curl_init( $sitemwSite );
if( mwLogin( $mwUser, $mwPass, $mwSite ) ){
// successfully logged into API, parse the return
$getLoginVals();
}
}
Ligne 79 ⟶ 92 :
private function __destruct(){
curl_close( $this->ch );
xml_parser_free( $this->xml_parser );
}
 
/**
* destroyXmlParser method
**
* Free xml parser resources.
**/
public function execCurldestroyXmlParser(){
xml_parser_free( $this->xml_parser );
}
/**
* 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;
}
Ligne 90 ⟶ 133 :
* @param $mwSite string Qualified url/path
**/
private function mwLogin( $mwUser, $mwPass, $mwSite ){
$postvars = array(
'action' => 'login',
'lgname' => $mwUser,
'lgpassword' => $mwPass,
'format' => 'xml',
);
Ligne 104 ⟶ 148 :
$val = execCurl();
if( false != $val ){
// Parse xml? grab cookies?
$this->mwXmlFunctions = new mwApiXmlParse;
doParse();
}
}
/**
* execCurldoParse method
**
*/
* Executes a curl instance after turning off buffers, retrieves
private function doParse(){
* buffer into $this->curlBuffer, and returns result of execution
$xml_parser = xml_parser_create();
* as value.
xml_set_object( $xml_parser, $this->mwXmlFunctions );
*
xml_set_element_handler( $xml_parser, 'startElement', 'endElement' );
* Examples of use:
xml_parser_set_option( $xml_parser, XML_OPTION_CASE_FOLDING, false );
* $val = execCurl();
if (!xml_parse( $xml_parser, $this->curlBuffer, true ) ) {
**
die( sprintf(
* @return bool Return value of curl_exec();
"XML error: %s at line %d",
**/
xml_error_string( xml_get_error_code( $xml_parser ) ),
public function execCurl(){
xml_get_current_line_number( $xml_parser )
ob_start();
) );
$val = curl_exec( $this->ch );
}
$this->curlBuffer = ob_get_contents();
ob_end_clean();
return $val;
}
}</pre>