diff --git "a/class-mw\\Site.php" "b/class-mw\\Site.php" index 7118eb8..ff385d6 100644 --- "a/class-mw\\Site.php" +++ "b/class-mw\\Site.php" @@ -1,188 +1,215 @@ . # 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 = self::createApi( $url ); } /** * 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 ); } /** * 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 we can continue fetching the next result set * * @see API#hasNext() * @return bool */ public function hasNext() { return $this->getApi()->hasNext(); } /** * Fetch the next result set * * @see API#fetchNext() * @return mixed Response */ public function fetchNext() { return $this->getApi()->fetchNext(); } /** * Set the default API data * * @see API#setData() * @data array HTTP GET/POST data * @return self */ public function setApiData( $data ) { $this->getApi()->setData( $data ); 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 a MediaWiki API object from an API URL * * @param $url string API URL * @return API */ private static function createApi( $url ) { return new API( $url ); } } diff --git "a/class-mw\\StaticSite.php" "b/class-mw\\StaticSite.php" index 3f166cc..b3c6fd9 100644 --- "a/class-mw\\StaticSite.php" +++ "b/class-mw\\StaticSite.php" @@ -1,58 +1,68 @@ . # MediaWiki namespace mw; class StaticSite extends \generic\Singleton { + /** + * Sort of unique identifier + * + * @var string + */ + const UID = 'none'; + /** * @override \generic\Singleton::create() */ protected static function create() { $site = new Site( 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'); } /** * To be overloaded. * * @param $site mw\Site */ protected static function onCreate( & $site ) { // Please override } } diff --git "a/class-wm\\Commons.php" "b/class-wm\\Commons.php" index ce61985..e06ed89 100644 --- "a/class-wm\\Commons.php" +++ "b/class-wm\\Commons.php" @@ -1,29 +1,34 @@ . # Wikimedia namespace wm; class Commons extends \mw\StaticSite { /** - * @overload \mw\StaticSite::getApiURL() + * @override \mw\StaticSite::UID + */ + const UID = 'commonswiki'; + + /** + * @override \mw\StaticSite::getApiURL() */ protected static function getApiURL() { return 'https://commons.wikimedia.org/w/api.php'; } } diff --git "a/class-wm\\WikipediaIt.php" "b/class-wm\\WikipediaIt.php" index 7500bfb..b30957d 100644 --- "a/class-wm\\WikipediaIt.php" +++ "b/class-wm\\WikipediaIt.php" @@ -1,56 +1,61 @@ . # Wikimedia namespace wm; class WikipediaIt extends \mw\StaticSite { /** - * @overload \mw\StaticSite::getApiURL() + * @override \mw\StaticSite::UID + */ + const UID = 'itwiki'; + + /** + * @override \mw\StaticSite::getApiURL() */ protected static function getApiURL() { return 'https://it.wikipedia.org/w/api.php'; } /** - * @overload \mw\StaticSite::onCreate( $site ) + * @override \mw\StaticSite::onCreate( $site ) */ 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' ) ] ); } }