|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SIL\Search; |
|
4
|
|
|
|
|
5
|
|
|
use Html; |
|
6
|
|
|
use LanguageNames; |
|
7
|
|
|
use Language; |
|
8
|
|
|
use XmlSelect; |
|
9
|
|
|
use Xml; |
|
10
|
|
|
use SearchResultSet; |
|
11
|
|
|
use SpecialSearch; |
|
12
|
|
|
|
|
13
|
|
|
use SMW\Localizer; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @license GNU GPL v2+ |
|
17
|
|
|
* @since 1.0 |
|
18
|
|
|
* |
|
19
|
|
|
* @author mwjames |
|
20
|
|
|
*/ |
|
21
|
|
|
class SearchResultModifier { |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var LanguageResultMatchFinder|null |
|
25
|
|
|
*/ |
|
26
|
|
|
private $languageResultMatchFinder = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @since 1.0 |
|
30
|
|
|
* |
|
31
|
|
|
* @param LanguageResultMatchFinder $languageResultMatchFinder |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct( LanguageResultMatchFinder $languageResultMatchFinder ) { |
|
34
|
|
|
$this->languageResultMatchFinder = $languageResultMatchFinder; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @since 1.0 |
|
39
|
|
|
* |
|
40
|
|
|
* @param array &$profiles |
|
41
|
|
|
*/ |
|
42
|
|
|
public function addSearchProfile( array &$profiles ) { |
|
43
|
|
|
|
|
44
|
|
|
$profiles['sil'] = [ |
|
45
|
|
|
'message' => 'sil-search-profile', |
|
46
|
|
|
'tooltip' => 'sil-search-profile-tooltip', |
|
47
|
|
|
'namespaces' => \SearchEngine::defaultNamespaces() |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
return true; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @since 1.0 |
|
55
|
|
|
* |
|
56
|
|
|
* @param SpecialSearch $search |
|
57
|
|
|
* @param string $profile, |
|
58
|
|
|
* @param string &$form |
|
59
|
|
|
* @param array $opts |
|
60
|
|
|
* |
|
61
|
|
|
* @return boolean |
|
62
|
|
|
*/ |
|
63
|
|
|
public function addSearchProfileForm( SpecialSearch $search, $profile, &$form, $opts ) { |
|
64
|
|
|
|
|
65
|
|
|
if ( $profile !== 'sil' ) { |
|
66
|
|
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$hidden = ''; |
|
70
|
|
|
|
|
71
|
|
|
foreach ( $opts as $key => $value ) { |
|
72
|
|
|
$hidden .= Html::hidden( $key, $value ); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$languagefilter = $search->getContext()->getRequest()->getVal( 'languagefilter' ); |
|
76
|
|
|
|
|
77
|
|
|
if ( $languagefilter !== '' && $languagefilter !== null ) { |
|
78
|
|
|
$search->setExtraParam( 'languagefilter', $languagefilter ); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$params = [ 'id' => 'mw-searchoptions' ]; |
|
82
|
|
|
|
|
83
|
|
|
$form = Xml::fieldset( false, false, $params ) . |
|
84
|
|
|
$hidden . $this->createHtmlLanguageFilterSelector( $languagefilter ) . |
|
85
|
|
|
Html::closeElement( 'fieldset' ); |
|
86
|
|
|
|
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @since 1.0 |
|
92
|
|
|
* |
|
93
|
|
|
* @param WebRequest $request |
|
94
|
|
|
* @param array &$showSections |
|
95
|
|
|
* |
|
96
|
|
|
* @return boolean |
|
97
|
|
|
*/ |
|
98
|
|
|
public function addLanguageFilterToPowerBox( $request, &$showSections ) { |
|
99
|
|
|
|
|
100
|
|
|
$showSections['sil-languagefilter'] = $this->createHtmlLanguageFilterSelector( |
|
101
|
|
|
$request->getVal( 'languagefilter' ) |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
|
|
return true; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @since 1.0 |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $defaultLanguagefilter |
|
111
|
|
|
* |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
public function createHtmlLanguageFilterSelector( $defaultLanguagefilter ) { |
|
115
|
|
|
|
|
116
|
|
|
$languages = Language::fetchLanguageNames(); |
|
117
|
|
|
|
|
118
|
|
|
ksort( $languages ); |
|
119
|
|
|
|
|
120
|
|
|
$selector = new XmlSelect( 'languagefilter', 'languagefilter' ); |
|
121
|
|
|
$selector->setDefault( $defaultLanguagefilter ); |
|
122
|
|
|
$selector->addOption( wfMessage( 'sil-search-nolanguagefilter' )->text(), '-' ); |
|
123
|
|
|
|
|
124
|
|
|
foreach ( $languages as $code => $name ) { |
|
125
|
|
|
$selector->addOption( "$code - $name", $code ); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$selector = $selector->getHTML(); |
|
129
|
|
|
|
|
130
|
|
|
$label = Xml::label( |
|
131
|
|
|
wfMessage( 'sil-search-languagefilter-label' )->text(), |
|
132
|
|
|
'languagefilter' |
|
133
|
|
|
) . ' '; |
|
134
|
|
|
|
|
135
|
|
|
return $label . $selector; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @since 1.0 |
|
140
|
|
|
* |
|
141
|
|
|
* @param WebRequest $request |
|
142
|
|
|
* @param SearchResultSet|false $titleMatches |
|
143
|
|
|
* @param SearchResultSet|false $textMatches |
|
144
|
|
|
* |
|
145
|
|
|
* @return boolean |
|
146
|
|
|
*/ |
|
147
|
|
|
public function applyLanguageFilterToResultMatches( $request, &$titleMatches, &$textMatches ) { |
|
148
|
|
|
|
|
149
|
|
|
if ( !in_array( $request->getVal( 'profile' ), [ 'sil', 'advanced' ] ) ) { |
|
150
|
|
|
return false; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$languageCode = Localizer::asBCP47FormattedLanguageCode( $request->getVal( 'languagefilter' ) ); |
|
154
|
|
|
|
|
155
|
|
|
if ( in_array( $languageCode, [ null, '', '-' ] ) ) { |
|
156
|
|
|
return false; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
if ( $titleMatches instanceOf SearchResultSet ) { |
|
160
|
|
|
$titleMatches = $this->languageResultMatchFinder->matchResultsToLanguage( |
|
161
|
|
|
$titleMatches, |
|
162
|
|
|
$languageCode |
|
163
|
|
|
); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
if ( $textMatches instanceOf SearchResultSet ) { |
|
167
|
|
|
$textMatches = $this->languageResultMatchFinder->matchResultsToLanguage( |
|
168
|
|
|
$textMatches, |
|
169
|
|
|
$languageCode |
|
170
|
|
|
); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return true; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
|