|
|
@ -1,37 +1,44 @@
|
|
|
|
<?php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace LatinizeUrl;
|
|
|
|
namespace LatinizeUrl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use MediaWiki\Actions\ActionEntryPoint;
|
|
|
|
use MediaWiki\Linker\LinkTarget;
|
|
|
|
use MediaWiki\Linker\LinkTarget;
|
|
|
|
use Title;
|
|
|
|
use MediaWiki\Title\Title;
|
|
|
|
use User;
|
|
|
|
use MediaWiki\SpecialPage\SpecialPage;
|
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
use MediaWiki\Page\ProperPageIdentity;
|
|
|
|
use MediaWiki\Page\ProperPageIdentity;
|
|
|
|
use MediaWiki\Permissions\Authority;
|
|
|
|
use MediaWiki\Permissions\Authority;
|
|
|
|
use MediaWiki\User\UserIdentity;
|
|
|
|
use MediaWiki\User\UserIdentity;
|
|
|
|
use TitleValue;
|
|
|
|
use MediaWiki\Title\TitleValue;
|
|
|
|
use Wikimedia\Rdbms\DBQueryError;
|
|
|
|
use Wikimedia\Rdbms\DBQueryError;
|
|
|
|
|
|
|
|
use MediaWiki\Output\OutputPage;
|
|
|
|
|
|
|
|
use MediaWiki\User\User;
|
|
|
|
|
|
|
|
use MediaWiki\Request\WebRequest;
|
|
|
|
|
|
|
|
use MediaWiki\Context\RequestContext;
|
|
|
|
|
|
|
|
use MediaWiki\Installer\DatabaseUpdater;
|
|
|
|
|
|
|
|
|
|
|
|
class Hooks {
|
|
|
|
class Hooks {
|
|
|
|
public static $allowedNS = [NS_MAIN, NS_TALK];
|
|
|
|
public static $allowedNS = [NS_MAIN, NS_TALK];
|
|
|
|
|
|
|
|
|
|
|
|
public static function onLoadExtensionSchemaUpdates($updater){
|
|
|
|
public static function onLoadExtensionSchemaUpdates(DatabaseUpdater $updater) {
|
|
|
|
//更新数据库
|
|
|
|
//更新数据库
|
|
|
|
$dir = dirname(__DIR__) . '/sql';
|
|
|
|
$dir = dirname(__DIR__) . '/sql';
|
|
|
|
|
|
|
|
|
|
|
|
$dbType = $updater->getDB()->getType();
|
|
|
|
$dbType = $updater->getDB()->getType();
|
|
|
|
// For non-MySQL/MariaDB/SQLite DBMSes, use the appropriately named file
|
|
|
|
// For non-MySQL/MariaDB/SQLite DBMSes, use the appropriately named file
|
|
|
|
if ($dbType == 'mysql') {
|
|
|
|
if ($dbType == 'mysql') {
|
|
|
|
$filename = 'mysql.sql';
|
|
|
|
$updater->addExtensionTable('url_slug', "{$dir}/mysql.sql");
|
|
|
|
} elseif ($dbType == 'sqlite') {
|
|
|
|
} elseif ($dbType == 'sqlite') {
|
|
|
|
$filename = 'sqlite.sql';
|
|
|
|
$updater->addExtensionTable('url_slug', "{$dir}/sqlite.sql");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
throw new \Exception('Database type not currently supported');
|
|
|
|
throw new \Exception('Database type not currently supported');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$updater->addExtensionTable('url_slug', "{$dir}/{$filename}");
|
|
|
|
|
|
|
|
//更新文件patch
|
|
|
|
//更新文件patch
|
|
|
|
global $IP;
|
|
|
|
global $IP;
|
|
|
|
|
|
|
|
|
|
|
|
$patcher = new Patcher($IP . '/includes/MediaWiki.php', 'LatinizeUrl', Utils::getVersion());
|
|
|
|
$patcher = new Patcher($IP . '/includes/actions/ActionEntryPoint.php', 'LatinizeUrl', Utils::getVersion());
|
|
|
|
$patcher->patchInitializeParseTitleHook();
|
|
|
|
$patcher->patchInitializeParseTitleHook();
|
|
|
|
$patcher->save();
|
|
|
|
$patcher->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -43,34 +50,51 @@ class Hooks {
|
|
|
|
$config = $service->getMainConfig();
|
|
|
|
$config = $service->getMainConfig();
|
|
|
|
$wgLatinizeUrlForceRedirect = $config->get('LatinizeUrlForceRedirect');
|
|
|
|
$wgLatinizeUrlForceRedirect = $config->get('LatinizeUrlForceRedirect');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$slugText = $title->getText();
|
|
|
|
|
|
|
|
|
|
|
|
if (in_array($title->getNamespace(), self::$allowedNS)) {
|
|
|
|
if (in_array($title->getNamespace(), self::$allowedNS)) {
|
|
|
|
$realTitle = Utils::getTitleBySlugUrl($title, $title->getNamespace());
|
|
|
|
$realTitle = Utils::getTitleBySlugUrl($slugText, $title->getNamespace());
|
|
|
|
if ($realTitle) {
|
|
|
|
if ($realTitle) {
|
|
|
|
$title = $realTitle;
|
|
|
|
$title = $realTitle;
|
|
|
|
$request->setVal('title', $title->getPrefixedDBkey());
|
|
|
|
$request->setVal('title', $title->getPrefixedDBkey());
|
|
|
|
} elseif($wgLatinizeUrlForceRedirect
|
|
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
|
|
|
$wgLatinizeUrlForceRedirect
|
|
|
|
&& !($request->getVal('action') && $request->getVal('action') != 'view')
|
|
|
|
&& !($request->getVal('action') && $request->getVal('action') != 'view')
|
|
|
|
&& !$request->getVal('veaction')
|
|
|
|
&& !$request->getVal('veaction')
|
|
|
|
&& !defined('MW_API')
|
|
|
|
&& !defined('MW_API')
|
|
|
|
&& in_array($title->getNamespace(), self::$allowedNS)) { //把原标题页面重定向到拼音页面
|
|
|
|
&& in_array($title->getNamespace(), self::$allowedNS)
|
|
|
|
$slug = Utils::getSlugUrlByTitle($title);
|
|
|
|
) { //把原标题页面重定向到拼音页面
|
|
|
|
if($slug) $title = Title::newFromText($slug, $title->getNamespace());
|
|
|
|
$absoluteSlug = Utils::getSlugUrlByTitle($title);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$slugText = str_replace(' ', '_', $slugText);
|
|
|
|
|
|
|
|
$absoluteSlug = str_replace(' ', '_', $absoluteSlug);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($slugText !== $absoluteSlug) {
|
|
|
|
|
|
|
|
$title = Title::newFromText($absoluteSlug, $title->getNamespace());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static function onGetArticleUrl(\Title &$title, &$url, $query){
|
|
|
|
public static function onBeforeInitialize(Title &$title, $unused, OutputPage $output, User $user, WebRequest $request, ActionEntryPoint $entryPoint) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static function onGetArticleUrl(Title &$title, &$url, $query) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (in_array($title->getNamespace(), self::$allowedNS) && Utils::titleSlugExists($title)) {
|
|
|
|
if (in_array($title->getNamespace(), self::$allowedNS) && Utils::titleSlugExists($title)) {
|
|
|
|
$slug = Title::newFromText(Utils::getSlugUrlByTitle($title), $title->getNamespace());
|
|
|
|
$slugText = Utils::getSlugUrlByTitle($title);
|
|
|
|
if ($slug) {
|
|
|
|
if (!$slugText) return;
|
|
|
|
$slugEncoded = Utils::encodeUriComponent($slug->getPrefixedText());
|
|
|
|
|
|
|
|
|
|
|
|
$slugTitle = Title::newFromText($slugText, $title->getNamespace());
|
|
|
|
|
|
|
|
if (!$slugTitle) return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$slugEncoded = Utils::encodeUriComponent($slugTitle->getPrefixedText());
|
|
|
|
$titleEncoded = Utils::encodeUriComponent($title->getPrefixedText());
|
|
|
|
$titleEncoded = Utils::encodeUriComponent($title->getPrefixedText());
|
|
|
|
$url = str_replace($titleEncoded, $slugEncoded, $url);
|
|
|
|
$url = str_replace($titleEncoded, $slugEncoded, $url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (DBQueryError $ex) {
|
|
|
|
} catch (DBQueryError $ex) {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -97,10 +121,14 @@ class Hooks {
|
|
|
|
|
|
|
|
|
|
|
|
if ($flags & EDIT_NEW) {
|
|
|
|
if ($flags & EDIT_NEW) {
|
|
|
|
$title = $wikiPage->getTitle();
|
|
|
|
$title = $wikiPage->getTitle();
|
|
|
|
|
|
|
|
|
|
|
|
$parsedData = Utils::parseTitleToAscii($title, $title->getPageLanguage());
|
|
|
|
$parsedData = Utils::parseTitleToAscii($title, $title->getPageLanguage());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($parsedData) {
|
|
|
|
Utils::addTitleSlugMap($title->getText(), $parsedData['slug'], $parsedData['latinize']);
|
|
|
|
Utils::addTitleSlugMap($title->getText(), $parsedData['slug'], $parsedData['latinize']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static function onPageMoveComplete(LinkTarget $old, LinkTarget $new, UserIdentity $userIdentity, $pageid, $redirid, $reason, $revision) {
|
|
|
|
public static function onPageMoveComplete(LinkTarget $old, LinkTarget $new, UserIdentity $userIdentity, $pageid, $redirid, $reason, $revision) {
|
|
|
|
if (!in_array($new->getNamespace(), self::$allowedNS)) { //不是普通页面就跳过
|
|
|
|
if (!in_array($new->getNamespace(), self::$allowedNS)) { //不是普通页面就跳过
|
|
|
@ -108,11 +136,10 @@ class Hooks {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$title = MediaWikiServices::getInstance()->getTitleFactory()->newFromLinkTarget($new);
|
|
|
|
$title = MediaWikiServices::getInstance()->getTitleFactory()->newFromLinkTarget($new);
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
$parsedData = Utils::parseTitleToAscii($title, $title->getPageLanguage());
|
|
|
|
$parsedData = Utils::parseTitleToAscii($title, $title->getPageLanguage());
|
|
|
|
Utils::addTitleSlugMap($title->getText(), $parsedData['slug'], $parsedData['latinize']);
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($parsedData) {
|
|
|
|
|
|
|
|
Utils::addTitleSlugMap($title->getText(), $parsedData['slug'], $parsedData['latinize']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -139,10 +166,10 @@ class Hooks {
|
|
|
|
$title = $skin->getRelevantTitle();
|
|
|
|
$title = $skin->getRelevantTitle();
|
|
|
|
if (in_array($title->getNamespace(), self::$allowedNS)) {
|
|
|
|
if (in_array($title->getNamespace(), self::$allowedNS)) {
|
|
|
|
if ($service->getPermissionManager()->userHasRight($user, 'delete') || Utils::hasUserEditedPage($title, $user)) {
|
|
|
|
if ($service->getPermissionManager()->userHasRight($user, 'delete') || Utils::hasUserEditedPage($title, $user)) {
|
|
|
|
$links['page-secondary']['custom-url'] = [
|
|
|
|
$links['TOOLBOX']['custom-url'] = [
|
|
|
|
'class' => false,
|
|
|
|
'class' => false,
|
|
|
|
'text' => wfMessage('latinizeurl-customurl')->text(),
|
|
|
|
'text' => wfMessage('latinizeurl-customurl')->text(),
|
|
|
|
'href' => \SpecialPage::getTitleFor('CustomUrl', $title->getPrefixedDBKey())->getLocalURL(),
|
|
|
|
'href' => SpecialPage::getTitleFor('CustomUrl', $title->getPrefixedDBKey())->getLocalURL(),
|
|
|
|
'id' => 'ca-custom-url',
|
|
|
|
'id' => 'ca-custom-url',
|
|
|
|
];
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|