summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Hooks/BaseTemplateToolbox.php
blob: 57956de6e4f23f2e6888d7490f3a9fe37a4bd0cb (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
<?php

namespace SMW\MediaWiki\Hooks;

use SMW\NamespaceExaminer;
use SMWInfolink as Infolink;
use SpecialPage;
use Title;

/**
 * Hook: Called by BaseTemplate when building the toolbox array and
 * returning it for the skin to output.
 *
 * Add a link to the toolbox to view the properties of the current page in
 * Special:Browse. The links has the CSS id "t-smwbrowselink" so that it can be
 * skinned or hidden with all standard mechanisms (also by individual users
 * with custom CSS).
 *
 * @see http://www.mediawiki.org/wiki/Manual:Hooks/BaseTemplateToolbox
 *
 * @license GNU GPL v2+
 * @since 1.9
 *
 * @author mwjames
 */
class BaseTemplateToolbox extends HookHandler {

	/**
	 * @var NamespaceExaminer
	 */
	private $namespaceExaminer;

	/**
	 * @since 1.9
	 *
	 * @param NamespaceExaminer $namespaceExaminer
	 */
	public function __construct( NamespaceExaminer $namespaceExaminer ) {
		$this->namespaceExaminer = $namespaceExaminer;
	}

	/**
	 * @since 1.9
	 *
	 * @param $skinTemplate
	 * @param &$toolbox
	 *
	 * @return boolean
	 */
	public function process( $skinTemplate, &$toolbox ) {

		$title = $skinTemplate->getSkin()->getTitle();

		if ( $this->canProcess( $title, $skinTemplate ) ) {
			$this->performUpdate( $title, $toolbox );
		}

		return true;
	}

	private function canProcess( Title $title, $skinTemplate ) {

		if ( $title->isSpecialPage() || !$this->namespaceExaminer->isSemanticEnabled( $title->getNamespace() ) ) {
			return false;
		}

		if ( !$this->isFlagSet( 'smwgBrowseFeatures', SMW_BROWSE_TLINK ) || !$skinTemplate->data['isarticle'] ) {
			return false;
		}

		return true;
	}

	private function performUpdate( $title, &$toolbox ) {

		$link = Infolink::encodeParameters(
			[
				$title->getPrefixedDBkey()
			],
			true
		);

		$toolbox['smw-browse'] = [
			'text' => wfMessage( 'smw_browselink' )->text(),
			'href' => SpecialPage::getTitleFor( 'Browse', ':' . $link )->getLocalUrl(),
			'id'   => 't-smwbrowselink',
			'rel'  => 'search'
		];
	}

}