diff options
author | Yaco <franco@reevo.org> | 2020-06-04 11:01:00 -0300 |
---|---|---|
committer | Yaco <franco@reevo.org> | 2020-06-04 11:01:00 -0300 |
commit | fc7369835258467bf97eb64f184b93691f9a9fd5 (patch) | |
tree | daabd60089d2dd76d9f5fb416b005fbe159c799d /www/wiki/extensions/HideNamespace/HideNamespace.php |
first commit
Diffstat (limited to 'www/wiki/extensions/HideNamespace/HideNamespace.php')
-rw-r--r-- | www/wiki/extensions/HideNamespace/HideNamespace.php | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/www/wiki/extensions/HideNamespace/HideNamespace.php b/www/wiki/extensions/HideNamespace/HideNamespace.php new file mode 100644 index 00000000..4ba349ac --- /dev/null +++ b/www/wiki/extensions/HideNamespace/HideNamespace.php @@ -0,0 +1,115 @@ +<?php + +/** + * Extension HideNamespace + * Allows hiding namespace in the page title. + * + * @file + * @ingroup Extensions + * @author Matěj Grabovský (mgrabovsky.github.com) + * @license GNU General Public Licence 2.0 or later + */ + +if( !defined( 'MEDIAWIKI' ) ) { + echo 'This file is an extension to the MediaWiki software and ', + 'cannot be used standalone.', PHP_EOL; + die(); +} + +$dir = dirname( __FILE__ ) . DIRECTORY_SEPARATOR; +$wgExtensionMessagesFiles['HideNamespace'] = $dir . 'HideNamespace.i18n.php'; +$wgExtensionMessagesFiles['HideNamespaceMagic'] = $dir . 'HideNamespace.i18n.magic.php'; + +$wgExtensionCredits['other'][] = array( + 'path' => __FILE__, + 'name' => 'HideNamespace', + 'descriptionmsg' => 'hidens-desc', + 'version' => '1.4.3', + 'author' => 'Matěj Grabovský', + 'url' => 'https://www.mediawiki.org/wiki/Extension:HideNamespace', +); + +$wgHidensNamespaces = array(); + +$wgHooks['ParserFirstCallInit'][] = 'ExtensionHideNamespace::registerParser'; +$wgHooks['ArticleViewHeader'][] = 'ExtensionHideNamespace::onArticleViewHeader'; +$wgHooks['BeforePageDisplay'][] = 'ExtensionHideNamespace::onBeforePageDisplay'; + +class ExtensionHideNamespace { + private static $namespaceText; + private static $hide = null; + + /** + * Register the parser functions + */ + public static function registerParser( $parser ) { + $parser->setFunctionHook( 'hidens', array( __CLASS__, 'hideNs' ) ); + $parser->setFunctionHook( 'showns', array( __CLASS__, 'showNs' ) ); + + return true; + } + + /** + * Callback for our parser function {{#hidens:}} + */ + public static function hideNs() { + self::$hide = true; + + return null; + } + + /** + * Callback for our parser function {{#showns:}} + */ + public static function showNs() { + self::$hide = false; + + return null; + } + + /** + * Callback for the ArticleViewHeader hook. + * + * Retrieves the namespace and localized namespace text and decides whether the + * namespace should be hidden + */ + public static function onArticleViewHeader( $article ) { + global $wgHidensNamespaces, $wgContLang; + + $namespace = $article->getTitle()->getNamespace(); + self::$namespaceText = $wgContLang->getNsText( $namespace ); + + if( $namespace == NS_MAIN ) { + self::$hide = false; + } else { + /** + * Hide namespace if either + * - it was forced by user (with {{#hidens:}}) or + * - the current namespace is in $wgHidensNamespaces AND + * {{#showns:}} wasn't called + */ + $visibilityForced = !is_null( self::$hide ); + $hideByUser = $visibilityForced && self::$hide; + $hideBySetting = in_array( $namespace, $wgHidensNamespaces ); + + self::$hide = $hideByUser || ( $hideBySetting && self::$hide !== false ); + } + + return true; + } + + /** + * Callback for the BeforePageDisplay hook + * + * Removes the namespace from article header and page title + */ + public static function onBeforePageDisplay( $out ) { + if( self::$hide ) { + // Agrega estilos a cada parte del titulo de la pagina + $title = mb_substr( $out->getPageTitle(), mb_strlen( self::$namespaceText ) + 1 ); + $out->setPageTitle('<span id="title-namespace">' . self::$namespaceText . '</span> <span id="title-name">' . $title . '</span>'); + } + + return true; + } +} |