diff --git "a/include/class-generic\\Singleton.php" "b/include/class-generic\\Singleton.php" index c466edd..b5c62fc 100644 --- "a/include/class-generic\\Singleton.php" +++ "b/include/class-generic\\Singleton.php" @@ -1,38 +1,38 @@ . # generic stuff namespace generic; -class Singleton { +trait Singleton { public static function getInstance() { static $instance; if( ! $instance ) { $instance = static::create(); } return $instance; } protected static function create() { throw new \Exception('must override'); } - public function __construct() { + public static function throwWrongUsage() { throw new \Exception('wrong usage'); } } diff --git "a/include/class-mw\\Site.php" "b/include/class-mw\\Site.php" index 86b4523..e31f74e 100644 --- "a/include/class-mw\\Site.php" +++ "b/include/class-mw\\Site.php" @@ -1,228 +1,238 @@ . # MediaWiki namespace mw; class Site { /** * MediaWiki API * * @var API */ private $api; /** * Site namespaces * * @var array Array of namespaces */ private $namespaces = []; /** * A sort of internal UID * * @var null|string E.g. 'enwiki' */ private $uid; /** * Constructor * * @param $url string MediaWiki API URL */ public function __construct( $url ) { $this->api = new API( $url ); } /** * Create an API query with continuation handler * * @param $data array GET/POST data arguments * @return mw\APIQuery */ public function createQuery( $data ) { return $this->getApi()->createQuery( $data ); } /** * Make an HTTP GET request to the API * * @param $data array HTTP GET data * @return mixed API result */ public function fetch( $data ) { return $this->getApi()->fetch( $data ); } /** * Make an HTTP POST request to the API * * This method will call the API#login() method. * * @param $data array HTTP GET data * @return mixed API result */ public function post( $data ) { return $this->getApi()->post( $data ); } /** * Do an API edit request * * @param $data array API request data * @return mixed * @see https://www.mediawiki.org/wiki/API:Edit */ public function edit( $data ) { return $this->getApi()->post( array_replace( $data, [ 'action' => 'edit', - 'token' => $this->getToken( mw\Tokens::CSRF ) + 'token' => $this->getToken( \mw\Tokens::CSRF ) ] ) ); } /** * Preload some tokens * * @return self */ public function preloadTokens( $tokens ) { $this->getApi()->preloadTokens( $tokens ); return $this; } /** * Get the value of a token * * @param $token string Token name * @return string Token value */ public function getToken( $token ) { return $this->getApi()->getToken( $token ); } /** * Invalidate a token * * @param $token string Token name * @return self */ public function invalidateToken( $token ) { $this->getApi()->invalidateToken( $token ); return $this; } /** * Make a login * * @see API#login() * @param $username string MediaWiki username * @param $password string MediaWiki password * @return self */ public function login( $username = null, $password = null ) { $this->getApi()->login( $username, $password ); return $this; } /** * Check if a certain namespace ID is registered * * @param int $id Namespace ID * @return bool */ public function hasNamespace( $id ) { return array_key_exists( $id, $this->namespaces ); } /** * Get the namespace related to the specified ID * * @param int $id Namespace ID * @return Ns Corresponding namespace */ public function getNamespace( $id ) { if( ! $this->hasNamespace( $id ) ) { throw new \Exception( sprintf( 'missing namespace %d', $id ) ); } return $this->namespaces[ $id ]; } /** * Set/overwrite a namespace * * @param $namespace Ns Namespace to be set/overwrited * @return self */ public function setNamespace( Ns $namespace ) { $id = $namespace->getID(); $this->namespaces[ $id ] = $namespace; return $this; } /** * Stupid shortcut for setting multiple namespaces * * @param $namespaces array Array of namespaces * @return self */ public function setNamespaces( $namespaces ) { foreach( $namespaces as $namespace ) { $this->setNamespace( $namespace ); } return $this; } /** * Set the UID * * @param string E.g. 'enwiki' * @return self */ public function setUID( $uid ) { $this->uid = $uid; return $this; } /** * Get internal MediaWiki API object * * @return API */ public function getApi() { return $this->api; } /** * Get the UID * * @return null|string e.g. 'enwiki' */ public function getUID() { return $this->uid; } /** * Create a Wikitext object * * @return Wikitext */ public function createWikitext( $wikitext ) { return new Wikitext( $this, $wikitext ); } + + /** + * Create from an API URL + * + * @param $url string MediaWiki API URL + * @return self + */ + public static function createFromAPIURL( $url ) { + return new static( $url ); + } } diff --git "a/include/class-mw\\StaticSite.php" "b/include/class-mw\\StaticSite.php" index 70e8d83..494902d 100644 --- "a/include/class-mw\\StaticSite.php" +++ "b/include/class-mw\\StaticSite.php" @@ -1,68 +1,68 @@ . # MediaWiki namespace mw; -class StaticSite extends \generic\Singleton { +// traits can't be autoloadded +require 'class-generic\Singleton.php'; - /** - * Sort of unique identifier - * - * @var string - */ - const UID = 'none'; +/** + * A site that can be instantiated once using getInstance() + */ +class StaticSite extends Site { + + use \generic\Singleton; /** - * @override \generic\Singleton::create() + * @override */ protected static function create() { - $site = new Site( static::getApiURL() ); + $site = static::createFromAPIURL( static::getApiURL() ); - // Set the UID $site->setUID( static::UID ); // Set default namespaces foreach( Ns::defaultCanonicalNames() as $ns_id => $ns ) { $site->setNamespace( new Ns( $ns_id, $ns ) ); } // Callback static::onCreate( $site ); return $site; } /** * To be overloaded. * * @return string */ protected static function getApiURL() { - throw new \Exception('must overload'); + return static::API_URL; } /** * To be overloaded. * * @param $site mw\Site */ protected static function onCreate( $site ) { // Please override } } diff --git "a/include/class-wm\\Commons.php" "b/include/class-wm\\Commons.php" index ac7389f..5349359 100644 --- "a/include/class-wm\\Commons.php" +++ "b/include/class-wm\\Commons.php" @@ -1,35 +1,33 @@ . # Wikimedia namespace wm; class Commons extends \mw\StaticSite { /** - * @override \mw\StaticSite::UID + * @override */ const UID = 'commonswiki'; /** - * @override \mw\StaticSite::getApiURL() + * @override */ - protected static function getApiURL() { - return 'https://commons.wikimedia.org/w/api.php'; - } + const API_URL = 'https://commons.wikimedia.org/w/api.php'; } diff --git "a/include/class-wm\\Wikidata.php" "b/include/class-wm\\Wikidata.php" index 7e2df33..5357a45 100644 --- "a/include/class-wm\\Wikidata.php" +++ "b/include/class-wm\\Wikidata.php" @@ -1,85 +1,83 @@ . # Wikimedia namespace wm; class Wikidata extends \mw\StaticSite { /** - * @override \mw\StaticSite::UID + * @override */ const UID = 'wikidatawiki'; /** - * @override \mw\StaticSite::getApiURL() + * @override */ - protected static function getApiURL() { - return 'https://www.wikidata.org/w/api.php'; - } + 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 } ); + 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( [ + return $this->post( array_replace( [ 'action' => 'wbeditentity', - 'token' => $this->getToken( mw\Tokens::CSRF ), - ] ); + 'token' => $this->getToken( \mw\Tokens::CSRF ), + ], $data ) ); } } diff --git "a/include/class-wm\\WikipediaIt.php" "b/include/class-wm\\WikipediaIt.php" index b30957d..e3b76d9 100644 --- "a/include/class-wm\\WikipediaIt.php" +++ "b/include/class-wm\\WikipediaIt.php" @@ -1,61 +1,59 @@ . # Wikimedia namespace wm; class WikipediaIt extends \mw\StaticSite { /** - * @override \mw\StaticSite::UID + * @override */ const UID = 'itwiki'; /** - * @override \mw\StaticSite::getApiURL() + * @override */ - protected static function getApiURL() { - return 'https://it.wikipedia.org/w/api.php'; - } + const API_URL = 'https://it.wikipedia.org/w/api.php'; /** - * @override \mw\StaticSite::onCreate( $site ) + * @override */ protected static function onCreate( $site ) { $site->setNamespaces( [ new \mw\Ns( 1, 'Discussione' ), new \mw\Ns( 2, 'Utente' ), new \mw\Ns( 3, 'Discussioni utente' ), new \mw\Ns( 4, 'Wikipedia', ['Wp'] ), new \mw\Ns( 5, 'Discussioni Wikipedia', ['Dw'] ), new \mw\Ns( 6, 'File', ['Image', 'Immagine'] ), new \mw\Ns( 7, 'Discussioni file', ['Discussioni immagine', 'Image talk'] ), new \mw\Ns( 9, 'Discussioni MediaWiki' ), new \mw\Ns( 10, 'Template', ['T'] ), new \mw\Ns( 11, 'Discussioni template' ), new \mw\Ns( 12, 'Aiuto' ), new \mw\Ns( 13, 'Discussioni aiuto' ), new \mw\Ns( 14, 'Categoria', [ 'Cat' ] ), new \mw\Ns( 15, 'Discussioni categoria' ), new \mw\Ns( 100, 'Portale' ), new \mw\Ns( 101, 'Discussioni portale' ), new \mw\Ns( 102, 'Progetto' ), new \mw\Ns( 103, 'Discussioni proggetto', ['Dp'] ), new \mw\Ns( 828, 'Modulo' ), new \mw\Ns( 829, 'Discussioni modulo' ) ] ); } }