diff --git a/includes/Hooks.php b/includes/Hooks.php index d192b85..afa30b6 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -39,10 +39,10 @@ class Hooks { global $wgLatinizeUrlForceRedirect; if(in_array($title->getNamespace(), self::$allowedNS)){ - $realTitle = Utils::getTitleBySlugUrl($title); + $realTitle = Utils::getTitleBySlugUrl($title, $title->getNamespace()); if($realTitle){ $title = $realTitle; - $request->setVal('title', $title->getDBkey()); + $request->setVal('title', $title->getPrefixedDBkey()); } elseif($wgLatinizeUrlForceRedirect && !($request->getVal('action') && $request->getVal('action') != 'view') && in_array($title->getNamespace(), self::$allowedNS)) { //把原标题页面重定向到拼音页面 @@ -55,9 +55,10 @@ class Hooks { public static function onGetArticleUrl(\Title &$title, &$url, $query){ try { if(in_array($title->getNamespace(), self::$allowedNS) && Utils::titleSlugExists($title)){ - $slug = Utils::encodeUriComponent(Utils::getSlugUrlByTitle($title)); - $titleEncoded = Utils::encodeUriComponent($title->getText()); - $url = str_replace($titleEncoded, $slug, $url); + $slug = Title::newFromText(Utils::getSlugUrlByTitle($title), $title->getNamespace()); + $slugEncoded = Utils::encodeUriComponent($slug->getPrefixedText()); + $titleEncoded = Utils::encodeUriComponent($title->getPrefixedText()); + $url = str_replace($titleEncoded, $slugEncoded, $url); } } catch(DBQueryError $ex){ @@ -100,9 +101,10 @@ class Hooks { if($titles){ $titles = explode('|', $titles); foreach($titles as $id => $title){ - $realTitle = Utils::getTitleBySlugUrl($title); + $title = Title::newFromText($title); + $realTitle = Utils::getTitleBySlugUrl($title, $title->getNamespace()); if($realTitle){ - $titles[$id] = $realTitle->getText(); + $titles[$id] = $realTitle->getPrefixedText(); } } $request->setVal('titles', implode('|', $titles)); diff --git a/includes/Utils.php b/includes/Utils.php index 3c1cbe7..f525fbb 100644 --- a/includes/Utils.php +++ b/includes/Utils.php @@ -305,7 +305,9 @@ class Utils { } public static function encodeUriComponent($str){ - return str_replace('+', '_', implode("/", array_map("urlencode", explode("/", $str)))); + $entities = ['+', '%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D']; + $replacements = ['_', '!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]"]; + return str_replace($entities, $replacements, implode("/", array_map("urlencode", explode("/", $str)))); } /**