You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
4.0 KiB
PHP
119 lines
4.0 KiB
PHP
<?php
|
|
namespace Isekai\BlueLinkPatch;
|
|
|
|
use MediaWiki\Category\Category;
|
|
use MediaWiki\Config\Config;
|
|
use MediaWiki\Hook\TitleIsAlwaysKnownHook;
|
|
use MediaWiki\Page\Hook\BeforeDisplayNoArticleTextHook;
|
|
use MediaWiki\User\UserFactory;
|
|
use MediaWiki\Xml\Xml;
|
|
|
|
class Hooks implements TitleIsAlwaysKnownHook,
|
|
BeforeDisplayNoArticleTextHook {
|
|
private Config $config;
|
|
private UserFactory $userFactory;
|
|
|
|
public function __construct(
|
|
Config $config,
|
|
UserFactory $userFactory
|
|
) {
|
|
$this->config = $config;
|
|
$this->userFactory = $userFactory;
|
|
}
|
|
|
|
/**
|
|
* Always consider titles as known to avoid red links for blue links.
|
|
*
|
|
* @param \MediaWiki\Title\Title $title The title to check
|
|
* @param bool &$known Output parameter to indicate if the title is known
|
|
* @return bool Always returns true to continue hook processing
|
|
*/
|
|
public function onTitleIsAlwaysKnown( $title, &$known ) {
|
|
switch ( $title->getNamespace() ) {
|
|
case NS_USER:
|
|
// If user is registered, make the title known
|
|
if ( !$this->config->get( 'IsekaiBlueLinkPatchOnUser' ) ) {
|
|
return;
|
|
}
|
|
if ( $title->isSubpage() ) {
|
|
return;
|
|
}
|
|
|
|
$user = $this->userFactory->newFromName( $title->getText() );
|
|
if ( $user && $user->isRegistered() ) {
|
|
$known = true;
|
|
}
|
|
break;
|
|
case NS_CATEGORY:
|
|
// If category has members, make the title known
|
|
if ( !$this->config->get( 'IsekaiBlueLinkPatchOnCategory' ) ) {
|
|
return;
|
|
}
|
|
|
|
$category = Category::newFromTitle( $title );
|
|
if ( $category && $category->getMemberCount() > 0 ) {
|
|
$known = true;
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* This hook is called before displaying message key "noarticletext" or
|
|
* "noarticletext-nopermission" at Article::showMissingArticle().
|
|
*
|
|
* @since 1.35
|
|
*
|
|
* @param \Article $article
|
|
* @return bool|void True or no return value to continue or false to abort
|
|
*/
|
|
public function onBeforeDisplayNoArticleText( $article ) {
|
|
$text = null;
|
|
switch ( $article->getTitle()->getNamespace() ) {
|
|
case NS_USER:
|
|
// If user is registered, show an alternate message
|
|
if ( !$this->config->get( 'IsekaiBlueLinkPatchOnUser' ) ) {
|
|
return true;
|
|
}
|
|
if ( $article->getTitle()->isSubpage() ) {
|
|
return true;
|
|
}
|
|
|
|
$user = $this->userFactory->newFromName( $article->getTitle()->getText() );
|
|
if ( $user && $user->isRegistered() ) {
|
|
$text = $article->getContext()->msg( 'noarticletext-user-registered' )->text();
|
|
}
|
|
break;
|
|
case NS_CATEGORY:
|
|
// If category has members, do not show noarticletext
|
|
if ( !$this->config->get( 'IsekaiBlueLinkPatchOnCategory' ) ) {
|
|
return true;
|
|
}
|
|
|
|
$category = Category::newFromTitle( $article->getTitle() );
|
|
if ( $category && $category->getMemberCount() > 0 ) {
|
|
$text = $article->getContext()->msg( 'noarticletext-category-nonempty' )->text();
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
if ( !$text ) {
|
|
return true;
|
|
}
|
|
|
|
$context = $article->getContext();
|
|
$outputPage = $context->getOutput();
|
|
|
|
$dir = $context->getLanguage()->getDir();
|
|
$lang = $context->getLanguage()->getHtmlCode();
|
|
$outputPage->addWikiTextAsInterface( Xml::openElement( 'div', [
|
|
'class' => "noarticletext mw-content-$dir",
|
|
'dir' => $dir,
|
|
'lang' => $lang,
|
|
] ) . "\n$text\n</div>" );
|
|
|
|
return false; // Abort default noarticletext display
|
|
}
|
|
} |