summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Search/Form/NamespaceForm.php
blob: 571dda8a9236702b84871bf2bf59d1b6b2b4bcd6 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php

namespace SMW\MediaWiki\Search\Form;

use Html;
use MWNamespace;
use SMW\Message;
use SpecialSearch;
use Xml;

/**
 * @note Copied from SearchFormWidget::powerSearchBox, #3126 contains the reason
 * why we need to copy the code!
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class NamespaceForm {

	/**
	 * @var []
	 */
	private $activeNamespaces = [];

	/**
	 * @var []
	 */
	private $hiddenNamespaces = [];

	/**
	 * @var []
	 */
	private $searchableNamespaces = [];

	/**
	 * @var null|string
	 */
	private $token;

	/**
	 * @var null|string
	 */
	private $hideList = false;

	/**
	 * @since 3.0
	 *
	 * @param array $activeNamespaces
	 */
	public function setActiveNamespaces( array $activeNamespaces ) {
		$this->activeNamespaces = $activeNamespaces;
	}

	/**
	 * @since 3.0
	 *
	 * @param boolean $hideList
	 */
	public function setHideList( $hideList ) {
		$this->hideList = (bool)$hideList;
	}

	/**
	 * @since 3.0
	 *
	 * @param array $hiddenNamespaces
	 */
	public function setHiddenNamespaces( array $hiddenNamespaces ) {
		$this->hiddenNamespaces = $hiddenNamespaces;
	}

	/**
	 * @since 3.0
	 *
	 * @param array $searchableNamespaces
	 */
	public function setSearchableNamespaces( array $searchableNamespaces ) {
		$this->searchableNamespaces = $searchableNamespaces;
	}

	/**
	 * @see SearchFormWidget
	 *
	 * @since 3.0
	 *
	 * @param SpecialSearch $specialSearch
	 */
	public function checkNamespaceEditToken( SpecialSearch $specialSearch ) {

		$user = $specialSearch->getUser();

		if ( !$user->isLoggedIn() ) {
			return;
		}

		$this->token = $user->getEditToken( 'searchnamespace', $specialSearch->getRequest() );
	}

	/**
	 * @since 3.0
	 *
	 * @return string
	 */
	public function makeFields() {
		global $wgContLang;

		$divider = "<div class='divider'></div>";
		$rows = [];
		$tableRows = [];

		$hiddenNamespaces = array_flip( $this->hiddenNamespaces );

		foreach ( $this->searchableNamespaces as $namespace => $name ) {
			$subject = MWNamespace::getSubject( $namespace );

			if ( MWNamespace::isTalk( $namespace ) ) {
			//	continue;
			}

			if ( isset( $hiddenNamespaces[$namespace] ) ) {
				continue;
			}

			if ( !isset( $rows[$subject] ) ) {
				$rows[$subject] = "";
			}

			$name = $wgContLang->getConverter()->convertNamespace( $namespace );

			if ( $name === '' ) {
				$name = Message::get( 'blanknamespace', Message::TEXT, Message::USER_LANGUAGE );
			}

			$isChecked = in_array( $namespace, $this->activeNamespaces );

			$rows[$subject] .= Html::rawElement(
				'td',
				[],
				Xml::checkLabel( $name, "ns{$namespace}", "mw-search-ns{$namespace}", $isChecked )
			);
		}

		// Lays out namespaces in multiple floating two-column tables so they'll
		// be arranged nicely while still accomodating diferent screen widths
		foreach ( $rows as $row ) {
			$tableRows[] = "<tr>{$row}</tr>";
		}

		$namespaceTables = [];
		$display = $this->hideList ? 'none' : 'block';

		foreach ( array_chunk( $tableRows, 4 ) as $chunk ) {
			$namespaceTables[] = implode( '', $chunk );
		}

		$showSections = [
			'namespaceTables' => "<table>" . implode( '</table><table>', $namespaceTables ) . '</table>',
		];

		// Stuff to feed SpecialSearch::saveNamespaces()
		$remember = '';

		if ( $this->token ) {
			$remember = $divider . Xml::checkLabel(
				Message::get( 'powersearch-remember', Message::TEXT, Message::USER_LANGUAGE ),
				'nsRemember',
				'mw-search-powersearch-remember',
				false,
				// The token goes here rather than in a hidden field so it
				// is only sent when necessary (not every form submission)
				[ 'value' => $this->token ]
			);
		}

		return "<fieldset id='mw-searchoptions'>" .
			"<legend>" . Message::get( 'powersearch-legend', Message::ESCAPED, Message::USER_LANGUAGE ) . '</legend>' .
			"<h4>" . Message::get( 'powersearch-ns', Message::PARSE, Message::USER_LANGUAGE ) . '</h4>' .
			// populated by js if available
			"<div id='smw-search-togglensview'></div>" .
			"<div id='mw-search-togglebox'></div>" .
			"<div id='mw-search-ns' style='display:$display'>" . $divider .
			implode(
				$divider,
				$showSections
			) .
			$remember . "</div>" .
		"</fieldset>";
	}

}