diff --git a/README.md b/README.md index b4b81ac..3a7927a 100644 --- a/README.md +++ b/README.md @@ -1,51 +1,50 @@ # Boz-MW This is another MediaWiki API handler in PHP. ## Showcase Here some usage examples: // autoload classes require 'autoload.php'; // enable verbose messages \cli\Log::$DEBUG = true; echo "Simple Italian Wikipedia API query:\n"; $WIT = \wm\WikipediaIt::getInstance(); $response = $WIT->fetch( [ 'action' => 'query', 'prop' => 'info', 'titles' => [ 'Pagina principale' ] ] ); print_r( $response ); echo "Simple Italian Wikipedia API query with continuation support:\n"; - $categories = \wm\WikipediaIt::getInstance()->createQuery( [ + $members = \wm\WikipediaIt::getInstance()->createQuery( [ 'action' => 'query', - 'prop' => 'categories', - 'titles' => 'Gallus gallus domesticus' + 'list' => 'categorymembers', + 'cmtitle' => 'Categoria:Software con licenza GNU GPL', ] ); - while( $categories->hasNext() ) { - $response = $categories->fetchNext(); + foreach( $members->getGenerator() as $response ) { print_r( $response ); } echo "Simple POST request (every POST do an implicit login):\n"; \mw\API::$DEFAULT_USERNAME = 'My username'; \mw\API::$DEFAULT_PASSWORD = 'My bot password'; $wit = \wm\WikipediaIt::getInstance(); $response = $WIT->post( [ 'action' => 'edit', 'title' => 'Special:Nothing', 'text' => 'My wikitext', 'summary' => 'My edit summary', 'token' => $wit->getToken( 'csrf' ), ] ); print_r( $response ); ## Known usage * [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/class-mw\\APIQuery.php" "b/class-mw\\APIQuery.php" index 247d66e..351a08e 100644 --- "a/class-mw\\APIQuery.php" +++ "b/class-mw\\APIQuery.php" @@ -1,98 +1,111 @@ . # MediaWiki namespace mw; use cli\Log; /** * MediaWiki API query with continuation handler */ class APIQuery { /** * 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; /** * 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 ); } + /** + * 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 ) { $latest = $this->api->fetch( $data ); $this->continue = isset( $latest->continue ) ? $latest->continue : false; return $latest; } }