diff --git "a/class-mw\\Ns.php" "b/class-mw\\Ns.php" new file mode 100644 index 0000000..f1ba761 --- /dev/null +++ "b/class-mw\\Ns.php" @@ -0,0 +1,132 @@ +. + +# MediaWiki +namespace mw; + +/** + * A Namespace. + * + * Yes, PHP does not allow to call a class "Namespace". + */ +class Ns { + + private $id; + + private $name; + + private $canonicalName; + + private $aliases = []; + + public function __construct( $id, $name, $aliases = [] ) { + $this->setID( $id ); + $this->setName( $name ); + $canonical_name = self::existsDefaultCanonicalName( $id ) + ? self::defaultCanonicalName( $id ) + : $name; + $this->setCanonicalName( $canonical_name ); + foreach( $aliases as $alias ) { + $this->addAlias( $alias ); + } + } + + public function getID() { + return $this->id; + } + + public function getName() { + return $this->name->get(); + } + + public function getCanonicalName() { + return $this->canonicalName->get(); + } + + public function setID( $id ) { + $this->id = $id; + return $this; + } + + public function setName( $name ) { + $this->name = new TitlePartCapitalized( $name ); + return $this; + } + + public function setCanonicalName( $canonical_name ) { + $this->canonicalName = new TitlePartCapitalized( $canonical_name ); + return $this; + } + + public function addAlias( $alias ) { + $this->aliases[] = new TitlePartCapitalized( $alias ); + return $this; + } + + /** + * @return TitlePartCapitalized[] + */ + public function getAllTitlePartsCapitalized() { + $all = []; + $all[] = $this->name; + if( $this->isCanonicalNameDifferent() ) { + $all[] = $this->canonicalName; + } + foreach( $this->aliases as $alias ) { + $all[] = $alias; + } + return $all; + } + + public function isCanonicalNameDifferent() { + return $this->getName() !== $this->getCanonicalName(); + } + + public static function existsDefaultCanonicalName( $id ) { + return array_key_exists( $id, self::defaultCanonicalNames() ); + } + + public static function defaultCanonicalName( $id ) { + if( ! self::existsDefaultCanonicalName( $id ) ) { + throw new \Exception('unexisting namespace ID'); + } + return self::defaultCanonicalNames()[ $id ]; + } + + public static function defaultCanonicalNames() { + return [ + 0 => '', + 1 => 'Talk', + 2 => 'User', + 3 => 'User talk', + 4 => 'Project', + 5 => 'Project talk', + 6 => 'File', + 7 => 'File talk', + 8 => 'MediaWiki', + 9 => 'MediaWiki talk', + 10 => 'Template', + 11 => 'Template talk', + 12 => 'Help', + 13 => 'Help talk', + 14 => 'Category', + 15 => 'Category talk', + -1 => 'Special', + -2 => 'Media' + ]; + } +} diff --git "a/class-mw\\Title.php" "b/class-mw\\Title.php" new file mode 100644 index 0000000..c734fc2 --- /dev/null +++ "b/class-mw\\Title.php" @@ -0,0 +1,24 @@ +. + +# MediaWiki +namespace mw; + +/** + * A page Title. + */ +class Title extends TitlePartCapitalized { } diff --git "a/class-mw\\TitlePart.php" "b/class-mw\\TitlePart.php" new file mode 100644 index 0000000..faa851a --- /dev/null +++ "b/class-mw\\TitlePart.php" @@ -0,0 +1,73 @@ +. + +# MediaWiki +namespace mw; + +/** + * A string part of a wikilink, a namespace, etc. + */ +class TitlePart { + + private $s; + + public function __construct( $s ) { + $this->set( $s ); + } + + public static function factory( $s ) { + return new static( $s ); + } + + public function get() { + return $this->s; + } + + public function set( $s ) { + $this->s = static::normalize( $s ); + return $this; + } + + public function isEmpty() { + return '' === $this->get(); + } + + public function getRegex( $delimiter = null ) { + return self::regex( $this->get(), $delimiter ); + } + + public static function normalize( $s ) { + $s = trim( $s ); + $s = self::underscore2space( $s ); + return $s; + } + + public static function underscore2space( $s ) { + return str_replace('_', ' ', $s); + } + + public static function regex( $s, $delimiter ) { + $s = preg_quote( $s, $delimiter ); + + // These are all valids + // [[Main page]] + // [[Main page]] + // [[Main______page]] + // [[Main_ _ _ page]] + return str_replace( ' ', '[ _]*', $s ); + } +} diff --git "a/class-mw\\TitlePartCapitalized.php" "b/class-mw\\TitlePartCapitalized.php" new file mode 100644 index 0000000..913aee9 --- /dev/null +++ "b/class-mw\\TitlePartCapitalized.php" @@ -0,0 +1,40 @@ +. + +# MediaWiki +namespace mw; + +/** + * A string part of a wikilink, a namespace, etc. + * + * The first character is always capitalized. + */ +class TitlePartCapitalized extends TitlePart { + + public static function normalize( $s ) { + $s = ucfirst( $s ); + return parent::normalize( $s ); + } + + public function getRegexFirstCaseInsensitive( $first_case_group_name = null, $delimiter = null ) { + return \regex\Generic::firstCaseInsensitive( + $this->getRegex( $delimiter ), + $first_case_group_name + ); + } + +}