diff --git "a/include/class-mw\\CompleteTitle.php" "b/include/class-mw\\CompleteTitle.php" new file mode 100644 index 0000000..77b8af9 --- /dev/null +++ "b/include/class-mw\\CompleteTitle.php" @@ -0,0 +1,135 @@ +. + +# MediaWiki +namespace mw; + +/** + * A MediaWiki complete title (with namespace) + */ +class CompleteTitle { + + /** + * Which MediaWiki site + * + * @var object + */ + private $wiki; + + /** + * @var object + */ + private $ns; + + /** + * @var object + */ + private $title; + + /** + * Constructor + * + * @param $wiki object + * @param $ns object + * @param $title object + */ + public function __construct( $wiki, $ns, $title ) { + $this->wiki = $wiki; + $this->ns = $ns; + $this->title = $title; + } + + /** + * Get the namespace object + * + * @return object + */ + public function getNs() { + return $this->ns; + } + + /** + * Get the title object + * + * @return object + */ + public function getTitle() { + return $this->title; + } + + /** + * Get the regex able to match this complete title + * + * @param $args array + * ns-group-name: Name of the capturing group for the namespace + * title-group-name: Name of the capturing group for the title + * @return string + */ + public function getRegex( $args = [] ) { + + // default options + $args = array_replace( [ + 'ns-group-name' => null, + 'title-group-name' => null, + ], $args ); + + // single regexes + $ns = $this->getNs()->getRegex(); + $title = $this->title->getRegex(); + + // eventually group + $ns = \regex\Generic::groupNamed( $ns, $args[ 'ns-group-name' ] ); + $title = \regex\Generic::groupNamed( $title, $args[ 'title-group-name' ] ); + + return $ns . '[ _]*' . $title; + } + + /** + * Static constructor parsing a string + * + * @param $wiki object + * @param $s string e.g. ' Mediawiki: test ' + * @return self + */ + public static function createParsingTitle( $wiki, $s ) { + + // split namespace and title + $ns_raw = ''; + $tokens = explode( ':', $s, 2 ); + if( count( $tokens ) === 2 ) { + $ns_raw = $tokens[ 0 ]; + $title_raw = $tokens[ 1 ]; + } else { + // no namespace? that's the main namespace! + $ns_raw = ''; + $title_raw = $tokens[ 0 ]; + } + + // validate namespace + $ns = $wiki->findNamespace( $ns_raw ); + if( ! $ns ) { + // that was the main namespace with a ':' in the title + $ns = $wiki->getNamespace( 0 ); + $title_raw = "$ns_raw:$title"; + } + + $title = new Title( $title_raw, $wiki ); + + return new self( $wiki, $ns, $title ); + } + +} diff --git "a/include/class-mw\\Site.php" "b/include/class-mw\\Site.php" index f395177..93e9836 100644 --- "a/include/class-mw\\Site.php" +++ "b/include/class-mw\\Site.php" @@ -1,254 +1,289 @@ . # MediaWiki namespace mw; class Site { + /** + * A sort of $wgCapitalLinks + * + * @see https://www.mediawiki.org/wiki/Manual:$wgCapitalLinks + */ + const CAPITAL_LINKS = true; + /** * 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 ) ] ) ); } /** * 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; } /** * Find a namespace by it's name * * @param $name string * @return object|false */ public function findNamespace( $name ) { $name = Ns::normalizeName( $name ); foreach( $this->namespaces as $ns ) { if( $ns->getName() === $name ) { return $ns; } } return false; } /** * 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 complete title object + * + * @return object + */ + public function createTitleParsing( $s ) { + return CompleteTitle::createParsingTitle( $this, $s ); + } + + /** + * Create a wikilink object + * + * @return object + */ + public function createWikilink( CompleteTitle $title, $alias = null ) { + return new Wikilink( $title, $alias ); + } + + /** + * Check if in this wiki the first case is insensitive + * + * @TODO generalize + * @return boolean + */ + public function hasCapitalLinks() { + return static::CAPITAL_LINKS; + } + /** * 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\\Title.php" "b/include/class-mw\\Title.php" index c734fc2..20803db 100644 --- "a/include/class-mw\\Title.php" +++ "b/include/class-mw\\Title.php" @@ -1,24 +1,52 @@ . # MediaWiki namespace mw; /** - * A page Title. + * A page Title without a namespace. + * + * See also CompleteTitle class. */ -class Title extends TitlePartCapitalized { } +class Title extends TitlePartCapitalized { + + private $site; + + /** + * Constructor + * + * @param $name string + * @param $site object + */ + public function __construct( $name, $site ) { + parent::__construct( $name ); + $this->site = $site; + } + + /** + * Get a regex matching this title part + * + * @return string + */ + public function getRegex() { + return $this->site->hasCapitalLinks() + ? $this->getRegexFirstCaseInsensitive() + : parent::getRegex(); + } + +} diff --git "a/include/class-mw\\Wikilink.php" "b/include/class-mw\\Wikilink.php" new file mode 100644 index 0000000..3cf8915 --- /dev/null +++ "b/include/class-mw\\Wikilink.php" @@ -0,0 +1,174 @@ +. + +# MediaWiki +namespace mw; + +/** + * A MediaWiki wikilink + * + * Something like [[Wikipedia:Contatti|contatti]]. + */ +class Wikilink { + + /** + * A valid alias value + */ + const WHATEVER_ALIAS = null; + + /** + * A valid alias value + */ + const NO_ALIAS = false; + + /** + * Legal characters for a title + * + * @see https://www.mediawiki.org/wiki/Manual:$wgLegalTitleChars + */ + const LEGAL_TITLE_CHARS = ' %!\"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+'; + + /** + * Legal characters for an alias + * + * @see https://www.mediawiki.org/wiki/Manual:$wgLegalTitleChars + */ + const LEGAL_ALIAS_CHARS = self::LEGAL_TITLE_CHARS . '#<>\[\]{}\n\t'; + + /** + * MediaWiki title + * + * When falsy, it means that can be whatever + * + * @var object|null + */ + private $title; + + /** + * Displayed alias + * + * When NULL, it means that can be whatever, + * When false, it means that there is no one. + * + * @var object + */ + private $alias; + + /** + * Constructor + * + * @param $title object + * @param $alias string|false|null (NULL: whatever, false: no one) + */ + public function __construct( CompleteTitle $title, $alias = null ) { + $this->setTitle( $title ) + ->setAlias( $alias ); + } + + /** + * Set the title (link) + * + * @param $title object + * @return self + */ + public function setTitle( CompleteTitle $title ) { + $this->title = $title; + return $this; + } + + /** + * Set the alias + * + * @param $alias string|null|false (NULL: whatever, false: no one + * @return self + */ + public function setAlias( $alias ) { + $this->alias = $alias; + return $this; + } + + /** + * Get a regex matching this wikilink's title + * + * If there is no title, match a generic one. + * + * @return string + */ + public function getRegexTitle() { + return $this->title + ? $this->title->getRegex() + : '[' . self::LEGAL_TITLE_CHARS . ']*'; + } + + /** + * Get a regex matching this wikilink's alias + * + * If there is no alias, match a generic one. + * + * @return string + */ + public function getRegexAlias() { + // match the alias exactly if present + if( $this->alias ) { + return preg_quote( $this->alias ); + } + + // match whatever alias otherwise (non-greedy, because of it can contains ']') + return '[' . self::LEGAL_ALIAS_CHARS . ']*?'; + } + + /** + * Get a regex matching this wikilink + * + * @param $args array Arguments to be specified + * 'title-group-name': If specified, the title will be captured in a group with this name + * 'alias-group-name': If specified, the alias will be captured in a group with this name + */ + public function getRegex( $args = [] ) { + + // default options + $args = array_replace( [ + 'title-group-name' => null, + 'alias-group-name' => null, + ], $args ); + + // regex matching the title + $title_regex = \regex\Generic::groupNamed( $this->getRegexTitle(), $args[ 'title-group-name' ] ); + + // regex matching the alias (if any) + $alias_regex = false; + if( $this->alias !== self::NO_ALIAS ) { + $alias_regex = \regex\Generic::groupNamed( $this->getRegexAlias(), $args[ 'alias-group-name' ] ); + + // the alias part, when it's a captch-all, is optional + if( $this->alias === self::WHATEVER_ALIAS ) { + // note, do not try to create an atomic group, because atomic groups do not backreference past + $alias_regex = "(\|$alias_regex)?"; + } + } + + // complete regex + $regex = $title_regex; + if( $alias_regex ) { + $regex .= $alias_regex; + } + + // surround with brackets + return "\[\[$regex\]\]"; + } + +}