/** * @param $link MediaWiki link target */ public static function parse($link) { PhpUtil::assertString($link, 'link'); self::init(); $link = urldecode($link); if (strpos($link, '#') !== false) { throw new WikiParserException('Invalid title: "' . $link . '" (Contains #)'); } $parts = explode(':', $link, 2); if (count($parts) === 2) { $prefix = mb_strtolower(WikiUtil::cleanSpace($parts[0])); // TODO: handle interwiki links like [[:de:Foo]] if (strlen($prefix) === 0) { throw new WikiParserException('cannot handle link [' . $link . ']'); } // TODO: handle special prefixes, e.g. [[q:Foo]] links to WikiQuotes if (isset(self::$nsCodes[$prefix])) { $code = self::$nsCodes[$prefix]; $name = StringUtil::mb_ucfirst(WikiUtil::cleanSpace($parts[1])); return new WikiTitle($code, $name); } } $name = StringUtil::mb_ucfirst(WikiUtil::cleanSpace($link)); return new WikiTitle(self::NS_MAIN, $name); }
/** * All of the following names will be encoded to 'Émile Zola': * '%C3%89mile_Zola', '%C3%A9mile_Zola', ' %C3%A9mile Zola ', ' %C3%A9mile _ Zola ', ' Émile _ Zola ' * * TODO: maybe we should expect (require) the name to be normalized, e.g. with uppercase * first letter and without duplicate spaces or spaces at start or end? * Would make this method much simpler. * * @param $name encoded MediaWiki page name, e.g. '%C3%89mile_Zola'. * Must not include the namespace (e.g. 'Template:'). */ public static function wikiDecode($name) { PhpUtil::assertString($name, 'name'); // make first character uppercase $name = StringUtil::mb_ucfirst(self::cleanSpace(urldecode($name))); return $name; }