diff --git "a/include/class-mw\\StaticSite.php" "b/include/class-mw\\StaticSite.php" index 6df49f1..96659f3 100644 --- "a/include/class-mw\\StaticSite.php" +++ "b/include/class-mw\\StaticSite.php" @@ -1,56 +1,67 @@ . # MediaWiki namespace mw; -// traits can't be autoloadded -require 'class-generic\Singleton.php'; +// traits can't be autoloaded, so... +require_once 'class-generic\Singleton.php'; /** - * A site that can be instantiated once using getInstance() + * The behaviours for a static site + * + * They are declared in a trait on order to be recycled it e.g. in mw\StaticWikibaseSite */ -class StaticSite extends Site { +trait StaticSiteTrait { use \generic\Singleton; /** * @override */ protected static function create() { $site = static::createFromAPIURL( static::getApiURL() ); $site->setUID( static::UID ); // Set default namespaces foreach( Ns::defaultCanonicalNames() as $ns_id => $ns ) { $site->setNamespace( new Ns( $ns_id, $ns ) ); } return $site; } /** * To be overloaded. * * @return string */ protected static function getApiURL() { return static::API_URL; } } + +/** + * A static site is a site that must be used statically + */ +class StaticSite extends Site { + + use StaticSiteTrait; + +} diff --git "a/include/class-mw\\StaticSite.php" "b/include/class-mw\\StaticWikibaseSite.php" similarity index 52% copy from "include/class-mw\\StaticSite.php" copy to "include/class-mw\\StaticWikibaseSite.php" index 6df49f1..57fc65a 100644 --- "a/include/class-mw\\StaticSite.php" +++ "b/include/class-mw\\StaticWikibaseSite.php" @@ -1,56 +1,31 @@ . # MediaWiki namespace mw; -// traits can't be autoloadded -require 'class-generic\Singleton.php'; +# traits can't be autoloaded, so... +require_once 'class-mw\StaticSite.php'; /** - * A site that can be instantiated once using getInstance() + * A static MediaWiki site with Wikibase enabled */ -class StaticSite extends Site { +class StaticWikibaseSite extends WikibaseSite { - use \generic\Singleton; - - /** - * @override - */ - protected static function create() { - $site = static::createFromAPIURL( static::getApiURL() ); - - $site->setUID( static::UID ); - - // Set default namespaces - foreach( Ns::defaultCanonicalNames() as $ns_id => $ns ) { - $site->setNamespace( new Ns( $ns_id, $ns ) ); - } - - return $site; - } - - /** - * To be overloaded. - * - * @return string - */ - protected static function getApiURL() { - return static::API_URL; - } + use \mw\StaticSiteTrait; } diff --git "a/include/class-wm\\Wikidata.php" "b/include/class-mw\\WikibaseSite.php" similarity index 71% copy from "include/class-wm\\Wikidata.php" copy to "include/class-mw\\WikibaseSite.php" index 5baecb8..ccea67d 100644 --- "a/include/class-wm\\Wikidata.php" +++ "b/include/class-mw\\WikibaseSite.php" @@ -1,88 +1,60 @@ . -# Wikimedia -namespace wm; +# MediaWiki +namespace mw; /** - * Wikidata singleton instance - * - * @see https://www.wikidata.org/ + * A site with wikibase enabled */ -class Wikidata extends \mw\StaticSite { - - /** - * @override - */ - const UID = 'wikidatawiki'; - - /** - * @override - */ - const API_URL = 'https://www.wikidata.org/w/api.php'; - - /** - * Execute a SPARQL query - * - * @param $query string SPARQL query - * @return array - */ - public static function querySPARQL( $query ) { - $request = new \network\HTTPRequest( 'https://query.wikidata.org/sparql' ); - $response = $request->fetch( [ - 'format' => 'json', - 'query' => $query - ] ); - $response_obj = json_decode( $response ); - return $response_obj->results->bindings; - } +class WikibaseSite extends Site { /** * Fetch a single Wikidata entity * * @param $entity_id string Entity Q-ID * @param $data array Additional data such as [ 'props' => '..' ] * @return wb\DataModel */ public function fetchSingleEntity( $entity_id, $data = [] ) { $data = array_replace( [ 'action' => 'wbgetentities', 'ids' => $entity_id, ], $data ); $entity = $this->fetch( $data ); if( ! isset( $entity->entities->{ $entity_id } ) ) { throw new Exception( "$wikidata_item does not exist" ); } return \wb\DataModel::createFromObject( $entity->entities->{ $entity_id } ); } /** * Edit a Wikidata entity * * @param $data array API data request * @return mixed * @see https://www.wikidata.org/w/api.php?action=help&modules=wbgetentities */ public function editEntity( $data = [] ) { return $this->post( array_replace( [ 'action' => 'wbeditentity', 'token' => $this->getToken( \mw\Tokens::CSRF ), ], $data ) ); } } diff --git "a/include/class-wm\\Wikidata.php" "b/include/class-wm\\Wikidata.php" index 5baecb8..05ce539 100644 --- "a/include/class-wm\\Wikidata.php" +++ "b/include/class-wm\\Wikidata.php" @@ -1,88 +1,54 @@ . # Wikimedia namespace wm; /** * Wikidata singleton instance * * @see https://www.wikidata.org/ */ -class Wikidata extends \mw\StaticSite { +class Wikidata extends \mw\StaticWikibaseSite { /** * @override */ const UID = 'wikidatawiki'; /** * @override */ const API_URL = 'https://www.wikidata.org/w/api.php'; /** * Execute a SPARQL query * * @param $query string SPARQL query * @return array */ public static function querySPARQL( $query ) { $request = new \network\HTTPRequest( 'https://query.wikidata.org/sparql' ); $response = $request->fetch( [ 'format' => 'json', 'query' => $query ] ); $response_obj = json_decode( $response ); return $response_obj->results->bindings; } - /** - * Fetch a single Wikidata entity - * - * @param $entity_id string Entity Q-ID - * @param $data array Additional data such as [ 'props' => '..' ] - * @return wb\DataModel - */ - public function fetchSingleEntity( $entity_id, $data = [] ) { - $data = array_replace( [ - 'action' => 'wbgetentities', - 'ids' => $entity_id, - ], $data ); - - $entity = $this->fetch( $data ); - if( ! isset( $entity->entities->{ $entity_id } ) ) { - throw new Exception( "$wikidata_item does not exist" ); - } - return \wb\DataModel::createFromObject( $entity->entities->{ $entity_id } ); - } - - /** - * Edit a Wikidata entity - * - * @param $data array API data request - * @return mixed - * @see https://www.wikidata.org/w/api.php?action=help&modules=wbgetentities - */ - public function editEntity( $data = [] ) { - return $this->post( array_replace( [ - 'action' => 'wbeditentity', - 'token' => $this->getToken( \mw\Tokens::CSRF ), - ], $data ) ); - } - }