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

namespace SMW\MediaWiki\Hooks;

use OutputPage;
use Skin;
use SpecialPage;
use Title;
use SMW\Message;
use Html;

/**
 * BeforePageDisplay hook which allows last minute changes to the
 * output page, e.g. adding of CSS or JavaScript
 *
 * @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
 *
 * @license GNU GPL v2+
 * @since 1.9
 *
 * @author mwjames
 */
class BeforePageDisplay extends HookHandler {

	/**
	 * @since 1.9
	 *
	 * @param OutputPage $outputPage,
	 * @param Skin $skin
	 *
	 * @return boolean
	 */
	public function process( OutputPage $outputPage, Skin $skin ) {

		$title = $outputPage->getTitle();
		$user = $outputPage->getUser();

		// MW 1.26 / T107399 / Async RL causes style delay
		$outputPage->addModuleStyles(
			[
				'ext.smw.style',
				'ext.smw.tooltip.styles'
			]
		);

		// Add style resources to avoid unstyled content
		$outputPage->addModules( 'ext.smw.style' );

		// #2726
		if ( $user->getOption( 'smw-prefs-general-options-suggester-textinput' ) ) {
			$outputPage->addModules( 'ext.smw.suggester.textInput' );
		}

		if ( ( $tasks = $this->getOption( 'installer.incomplete_tasks', [] ) ) !== [] ) {
			$outputPage->prependHTML( $this->incompleteTasksHTML( $tasks ) );
		}

		// Add export link to the head
		if ( $title instanceof Title && !$title->isSpecialPage() ) {
			$link['rel']   = 'alternate';
			$link['type']  = 'application/rdf+xml';
			$link['title'] = $title->getPrefixedText();
			$link['href']  = SpecialPage::getTitleFor( 'ExportRDF', $title->getPrefixedText() )->getLocalUrl( 'xmlmime=rdf' );
			$outputPage->addLink( $link );
		}

		$request = $skin->getContext()->getRequest();

		if ( in_array( $request->getVal( 'action' ), [ 'delete', 'edit', 'protect', 'unprotect', 'diff', 'history' ] ) || $request->getVal( 'diff' ) ) {
			return true;
		}

		return true;
	}

	private function incompleteTasksHTML( array $messages ) {

		$html = '';

		foreach ( $messages as $message ) {
			$html .= Html::rawElement( 'li', [], Message::get( $message, Message::PARSE ) );
		}

		return Html::rawElement(
			'div',
			[
				'class' => 'smw-callout smw-callout-error plainlinks'
			],
			Message::get( 'smw-install-incomplete-intro' ) . "<ul>$html</ul>"
		);
	}

}