diff --git a/README.md b/README.md index 1c5d385..a02101f 100644 --- a/README.md +++ b/README.md @@ -1,56 +1,56 @@ # boz-mw This is another MediaWiki API handler in PHP. ## Tools See [the tools](./tools/README.md). ## API showcase Here some usage examples: ```php fetch( [ 'action' => 'query', 'prop' => 'info', 'titles' => [ 'Pagina principale' ] ] ); print_r( $response ); echo "Simple Italian Wikipedia API query with continuation support:\n"; -$members = \wm\WikipediaIt::instance()->createQuery( [ +$queries = \wm\WikipediaIt::instance()->createQuery( [ 'action' => 'query', 'list' => 'categorymembers', 'cmtitle' => 'Categoria:Software con licenza GNU GPL', ] ); -foreach( $members->getGenerator() as $response ) { - print_r( $response ); +foreach( $queries as $query ) { + print_r( $query ); } echo "Simple POST request:\n"; \mw\API::$DEFAULT_USERNAME = 'My username'; \mw\API::$DEFAULT_PASSWORD = 'My bot password'; $w = \wm\WikipediaIt::instance()->login(); $response = $w->edit( [ 'title' => 'Special:Nothing', 'text' => 'My wikitext', 'summary' => 'My edit summary', ] ); print_r( $response ); ``` ## Known usage * [ItalianWikipediaDeletionBot](https://github.com/valerio-bozzolan/ItalianWikipediaDeletionBot) * [wiki-users-leaflet](https://github.com/valerio-bozzolan/wiki-users-leaflet/) hosted at [WMF Labs](https://tools.wmflabs.org/it-wiki-users-leaflet/) diff --git "a/include/class-mw\\APIQuery.php" "b/include/class-mw\\APIQuery.php" index 351a08e..024a096 100644 --- "a/include/class-mw\\APIQuery.php" +++ "b/include/class-mw\\APIQuery.php" @@ -1,111 +1,172 @@ . # MediaWiki namespace mw; +use Iterator; use cli\Log; /** * MediaWiki API query with continuation handler */ -class APIQuery { +class APIQuery implements Iterator { /** * MediaWiki API object * * @var mw\API */ private $api; /** * Data of the GET/POST request * * @var array */ private $data; /** * MediaWiki continuation parameter * * @var mixed */ private $continue = null; + /** + * Actual (current) element + * + * @var mixed + */ + private $actual; + + /** + * Iterator key + * + * @var int + */ + private $i = -1; + /** * Construct * * @param $api mw\API * @param $data array */ public function __construct( $api, $data ) { $this->api = $api; $this->data = $data; } /** * Check if there are other requests to do to fetch the whole result set. * * @return bool */ public function hasNext() { return $this->continue !== false; } /** * Fetch the next results * * @return mixed */ public function fetchNext() { $data = $this->data; if( $this->continue ) { Log::debug( "query continue" ); foreach( $this->continue as $arg => $value ) { $data[ $arg ] = $value; } } - return $this->fetch( $data ); + return $this->actual = $this->fetch( $data ); } /** * Create an API result generator * * It handles the API query continuation transparently. * * @return Generator */ public function getGenerator() { while( $this->hasNext() ) { yield $this->fetchNext(); } } /** * Fetch results * * @param $data array GET/POST data * @return mixed */ private function fetch( $data ) { + $this->i++; $latest = $this->api->fetch( $data ); $this->continue = isset( $latest->continue ) ? $latest->continue : false; return $latest; } + + /** + * @override + */ + public function rewind() { + $this->i = -1; + $this->continue = null; + $this->fetchNext(); + } + + /** + * @override + * @return bool + */ + public function valid() { + return (bool) $this->actual; + } + + /** + * @override + * @return mixed + */ + public function current() { + return $this->actual; + } + + /** + * @override + * @return int + */ + public function key() { + return $this->i; + } + + /** + * @override + */ + public function next() { + if( $this->hasNext() ) { + $this->fetchNext(); + } else { + $this->actual = false; + } + } + }