summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/HideNamespace/HideNamespace.php
blob: 4ba349acf1f03e00bc4ef6b13745f269311eff1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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;
	}
}