diff --git "a/include/class-mw\\TitlePart.php" "b/include/class-mw\\TitlePart.php" index 36fa118..99f8fff 100644 --- "a/include/class-mw\\TitlePart.php" +++ "b/include/class-mw\\TitlePart.php" @@ -1,81 +1,97 @@ . # 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; } + /** + * Convert underscores to spaces + * + * @param string $s + * @return s + */ public static function underscore2space( $s ) { - return str_replace('_', ' ', $s); + return str_replace( '_', ' ', $s ); + } + + /** + * Convert spaces to underscores + * + * @param string $s + * @return s + */ + public static function space2underscore( $s ) { + return str_replace( ' ', '_', $s ); } /** * Create a regex matching this title * * Spaces will be converted into optional tabs/spaces. * * @param $s string * @param $delimiter string * @return string */ public static function regex( $s, $delimiter = null ) { $s = preg_quote( $s, $delimiter ); // These are all valids // [[Main page]] // [[Main _ page]] // [[_ _ Main_ _ _ page _ _ ]] return str_replace( ' ', '[ _]+', $s ); } }