summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Hooks/SpecialSearchResultsPrepend.php
blob: c5831cd684b2c4203f4e16fe04f58c2a6c2a29ff (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php

namespace SMW\MediaWiki\Hooks;

use Html;
use OutputPage;
use SMW\MediaWiki\Search\Search as SMWSearch;
use SMW\Message;
use SMW\Utils\HtmlModal;
use SpecialSearch;

/**
 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchResultsPrepend
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class SpecialSearchResultsPrepend extends HookHandler {

	/**
	 * @var SpecialSearch
	 */
	private $specialSearch;

	/**
	 * @var OutputPage
	 */
	private $outputPage;

	/**
	 * @since  3.0
	 *
	 * @param SpecialSearch $specialSearch
	 * @param OutputPage &$outputPage
	 */
	public function __construct( SpecialSearch $specialSearch, OutputPage $outputPage ) {
		$this->specialSearch = $specialSearch;
		$this->outputPage = $outputPage;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $term
	 *
	 * @return boolean
	 */
	public function process( $term ) {

		$html = '';

		if ( $this->specialSearch->getSearchEngine() instanceof SMWSearch ) {
			$this->outputPage->addModuleStyles( [  'smw.ui.styles', 'smw.special.search.styles' ] );
			$this->outputPage->addModules( [ 'smw.special.search', 'smw.ui' ] );

			$this->outputPage->addModuleStyles( HtmlModal::getModuleStyles() );
			$this->outputPage->addModules( HtmlModal::getModules() );

			$html .=  HtmlModal::link(
				'<span class="smw-icon-info" style="margin-left: -5px; padding: 10px 12px 12px 12px;"></span>',
				[
					'data-id' => 'smw-search-cheat-sheet'
				]
			);

			$html .= Message::get(
				'smw-search-syntax-support',
				Message::PARSE,
				Message::USER_LANGUAGE
			);

			if ( $this->getOption( 'prefs-suggester-textinput' ) ) {
				$html .= ' ' . Message::get(
					'smw-search-input-assistance',
					Message::PARSE,
					Message::USER_LANGUAGE
				);
			}

			$html .= HtmlModal::modal(
				Message::get( 'smw-cheat-sheet', Message::TEXT, Message::USER_LANGUAGE ),
				$this->search_sheet( $this->getOption( 'prefs-suggester-textinput' ) ),
				[
					'id' => 'smw-search-cheat-sheet',
					'class' => 'plainlinks',
					'style' => 'display:none;'
				]
			);
		}

		if ( $html !== '' && !$this->getOption( 'prefs-disable-search-info' ) ) {
			$this->outputPage->addHtml(
				"<div class='smw-search-results-prepend plainlinks'>$html</div>"
			);
		}

		return true;
	}

	private function search_sheet( $inputAssistance ) {

		$text = $this->msg( 'smw-search-help-intro' );
		$text .= $this->section( 'smw-search-input' );

		$text .= $this->msg( 'smw-search-help-structured' );
		$text .= $this->msg( 'smw-search-help-proximity' );

		if ( $inputAssistance ) {
			$text .= $this->section( 'smw-ask-input-assistance' );
			$text .= $this->msg( 'smw-search-help-input-assistance' );
		}

		$text .= $this->section( 'smw-search-syntax' );
		$text .= $this->msg( 'smw-search-help-ask' );

		return $text;
	}

	private function section( $msg, $attributes = [] ) {
		return Html::rawElement(
			'div',
			[
				'class' => 'smw-text-strike',
				'style' => 'padding: 5px 0 5px 0;'
			],
			Html::rawElement(
				'span',
				[
					'style' => 'font-size: 1.2em; margin-left:0px'
				],
				Message::get( $msg, Message::TEXT, Message::USER_LANGUAGE )
			)
		);
	}

	private function msg( $msg, $html = '', $attributes = [] ) {
		return Html::rawElement(
			'div',
			[
				'class' => $msg
			] + $attributes,
			Message::get( $msg, Message::PARSE, Message::USER_LANGUAGE ) . $html
		);
	}

}