MediaWiki
master
Toggle main menu visibility
SearchUpdate.php
Go to the documentation of this file.
1
<?php
6
7
namespace
MediaWiki\Search;
8
9
use MediaWiki\Content\Content;
10
use MediaWiki\Logger\LoggerFactory;
11
use MediaWiki\MainConfigNames;
12
use MediaWiki\MediaWikiServices;
13
use MediaWiki\Page\ExistingPageRecord;
14
use MediaWiki\Page\PageIdentity;
15
use MediaWiki\Title\Title;
16
use Wikimedia\Rdbms\IDBAccessObject;
17
24
class
SearchUpdate
{
26
private
$id = 0;
27
29
private
$page;
30
32
private
$content;
33
35
private
$latestPage =
null
;
36
42
public
function
__construct
( $id, $page, ?
Content
$c =
null
) {
43
$this->page = $page;
44
$this->
id
= $id;
45
$this->content = $c;
46
}
47
51
public
function
doUpdate
() {
52
$services =
MediaWikiServices::getInstance
();
53
$searchEngineConfig = $services->getSearchEngineConfig();
54
55
if
( $services->getMainConfig()->get(
MainConfigNames::DisableSearchUpdate
) || !$this->id ) {
56
LoggerFactory::getInstance(
"search"
)
57
->debug(
"Skipping update: search updates disabled by config"
);
58
return
;
59
}
60
61
$seFactory = $services->getSearchEngineFactory();
62
foreach
( $searchEngineConfig->getSearchTypes() as $type ) {
63
$search = $seFactory->create( $type );
64
if
( !$search->supports(
'search-update'
) ) {
65
continue
;
66
}
67
68
$normalTitle = $this->getNormalizedTitle( $search );
69
70
if
( $this->getLatestPage() ===
null
) {
71
$search->delete( $this->
id
, $normalTitle );
72
continue
;
73
}
74
if
( $this->content ===
null
) {
75
$search->updateTitle( $this->
id
, $normalTitle );
76
continue
;
77
}
78
79
$text = $this->content->getTextForSearchIndex();
80
$text = $this->
updateText
( $text, $search );
81
82
# Perform the actual update
83
$search->update( $this->
id
, $normalTitle, $search->normalizeText( $text ) );
84
}
85
}
86
95
public
function
updateText
( $text, ?
SearchEngine
$se =
null
) {
96
$services =
MediaWikiServices::getInstance
();
97
$contLang = $services->getContentLanguage();
98
# Language-specific strip/conversion
99
$text = $contLang->normalizeForSearch( $text );
100
$se = $se ?: $services->newSearchEngine();
101
$lc = $se->legalSearchChars() .
'&#;'
;
102
103
# Strip HTML markup
104
$text = preg_replace(
"/<\\/?\\s*[A-Za-z][^>]*?>/"
,
105
' '
, $contLang->lc(
" "
. $text .
" "
) );
106
$text = preg_replace(
"/(^|\\n)==\\s*([^\\n]+)\\s*==(\\s)/"
,
107
"\\1\\2 \\2 \\2\\3"
, $text ); # Emphasize headings
108
109
# Strip external URLs
110
$uc =
"A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\x80-\\xFF"
;
111
$protos =
"http|https|ftp|mailto|news|gopher"
;
112
$pat =
"/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/"
;
113
$text = preg_replace( $pat,
"\\1 \\3"
, $text );
114
115
$p1 =
"/([^\\[])\\[({$protos}):[{$uc}]+]/"
;
116
$p2 =
"/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/"
;
117
$text = preg_replace( $p1,
"\\1 "
, $text );
118
$text = preg_replace( $p2,
"\\1 \\3 "
, $text );
119
120
$text = preg_replace(
"/([^{$lc}])([{$lc}]+)]]([a-z]+)/"
,
121
"\\1\\2 \\2\\3"
, $text ); # Handle [[game]]s
122
123
# Strip all remaining non-search characters
124
$text = preg_replace(
"/[^{$lc}]+/"
,
" "
, $text );
125
142
$text = strrev( preg_replace(
"/ s'([{$lc}]+)/"
,
" s'\\1 \\1"
, strrev( $text ) ) );
143
$text = strrev( preg_replace(
"/ 's([{$lc}]+)/"
,
" s\\1"
, strrev( $text ) ) );
144
145
# Strip wiki '' and '''
146
$text = preg_replace(
"/''[']*/"
,
" "
, $text );
147
148
return
$text;
149
}
150
160
private
function
getLatestPage() {
161
if
( !$this->latestPage ) {
162
$this->latestPage =
MediaWikiServices::getInstance
()->getPageStore()
163
->getPageById( $this->
id
, IDBAccessObject::READ_LATEST );
164
}
165
166
return
$this->latestPage;
167
}
168
176
private
function
getNormalizedTitle( SearchEngine $search ) {
177
$contLang =
MediaWikiServices::getInstance
()->getContentLanguage();
178
$title = Title::newFromPageIdentity( $this->page )->getText();
179
180
$lc = $search->legalSearchChars() .
'&#;'
;
181
$t = $contLang->normalizeForSearch( $title );
182
$t = preg_replace(
"/[^{$lc}]+/"
,
' '
, $t );
183
$t = $contLang->lc( $t );
184
185
if
( $this->page->getNamespace() ===
NS_FILE
) {
186
$t = preg_replace(
"/([{$lc}]+)\\.(\\w{1,4})$/"
,
"\\1 \\1.\\2"
, $t );
187
}
188
189
# Handle 's, s'
190
$t
= preg_replace(
"/([{$lc}]+)'s( |$)/"
,
"\\1 \\1's "
, $t );
191
$t
= preg_replace(
"/([{$lc}]+)s'( |$)/"
,
"\\1s "
, $t );
192
193
$t
= preg_replace(
"/\\s+/"
,
' '
, $t );
194
195
return
$search->normalizeText( trim( $t ) );
196
}
197
}
NS_FILE
const NS_FILE
Definition
Defines.php:57
MediaWiki\MainConfigNames\DisableSearchUpdate
const DisableSearchUpdate
Name constant for the DisableSearchUpdate setting, for use with Config\get().
Definition
MainConfigNames.php:3553
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition
MediaWikiServices.php:361
MediaWiki\Search\SearchEngine
Definition
SearchEngine.php:35
MediaWiki\Search\SearchUpdate
Database independent search index updater.
Definition
SearchUpdate.php:24
MediaWiki\Search\SearchUpdate\doUpdate
doUpdate()
Perform actual update for the entry.
Definition
SearchUpdate.php:51
MediaWiki\Search\SearchUpdate\__construct
__construct( $id, $page, ?Content $c=null)
Definition
SearchUpdate.php:42
MediaWiki\Search\SearchUpdate\updateText
updateText( $text, ?SearchEngine $se=null)
Clean text for indexing.
Definition
SearchUpdate.php:95
MediaWiki\Content\Content
Content objects represent page content, e.g.
Definition
Content.php:28
MediaWiki\Page\ExistingPageRecord
Data record representing a page that currently exists as an editable page on a wiki.
Definition
ExistingPageRecord.php:17
MediaWiki\Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
Definition
PageIdentity.php:53
MediaWiki\Parser\CoreParserFunctions\null\$t
$t
Definition
CoreParserFunctions.php:828
includes
Search
SearchUpdate.php
Generated on
for MediaWiki by
1.18.0