From a35065fccb0633caf82a2e2cf1e4beb3381586ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8F=E5=AD=90=E5=A4=8D=E5=90=88=E6=80=81?= Date: Fri, 31 Jul 2020 13:40:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=9D=9E=E4=B8=BB=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E7=A9=BA=E9=97=B4=E7=9A=84URL=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/Hooks.php | 16 +++++++++------- includes/Utils.php | 4 +++- 2 files changed, 12 insertions(+), 8 deletions(-) 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)))); } /**