MediaWiki REL1_41
SearchUpdate.php
Go to the documentation of this file.
1<?php
32
40 private $id = 0;
41
43 private $page;
44
46 private $content;
47
49 private $latestPage = null;
50
56 public function __construct( $id, $page, ?Content $c = null ) {
57 $this->page = $page;
58 $this->id = $id;
59 $this->content = $c;
60 }
61
65 public function doUpdate() {
66 $services = MediaWikiServices::getInstance();
67 $config = $services->getSearchEngineConfig();
68
69 if ( $config->getConfig()->get( MainConfigNames::DisableSearchUpdate ) || !$this->id ) {
70 LoggerFactory::getInstance( "search" )
71 ->debug( "Skipping update: search updates disabled by config" );
72 return;
73 }
74
75 $seFactory = $services->getSearchEngineFactory();
76 foreach ( $config->getSearchTypes() as $type ) {
77 $search = $seFactory->create( $type );
78 if ( !$search->supports( 'search-update' ) ) {
79 continue;
80 }
81
82 $normalTitle = $this->getNormalizedTitle( $search );
83
84 if ( $this->getLatestPage() === null ) {
85 $search->delete( $this->id, $normalTitle );
86 continue;
87 } elseif ( $this->content === null ) {
88 $search->updateTitle( $this->id, $normalTitle );
89 continue;
90 }
91
92 $text = $this->content !== null ? $this->content->getTextForSearchIndex() : '';
93 $text = $this->updateText( $text, $search );
94
95 # Perform the actual update
96 $search->update( $this->id, $normalTitle, $search->normalizeText( $text ) );
97 }
98 }
99
108 public function updateText( $text, SearchEngine $se = null ) {
109 $services = MediaWikiServices::getInstance();
110 $contLang = $services->getContentLanguage();
111 # Language-specific strip/conversion
112 $text = $contLang->normalizeForSearch( $text );
113 $se = $se ?: $services->newSearchEngine();
114 $lc = $se->legalSearchChars() . '&#;';
115
116 # Strip HTML markup
117 $text = preg_replace( "/<\\/?\\s*[A-Za-z][^>]*?>/",
118 ' ', $contLang->lc( " " . $text . " " ) );
119 $text = preg_replace( "/(^|\\n)==\\s*([^\\n]+)\\s*==(\\s)/",
120 "\\1\\2 \\2 \\2\\3", $text ); # Emphasize headings
121
122 # Strip external URLs
123 $uc = "A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\x80-\\xFF";
124 $protos = "http|https|ftp|mailto|news|gopher";
125 $pat = "/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/";
126 $text = preg_replace( $pat, "\\1 \\3", $text );
127
128 $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
129 $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
130 $text = preg_replace( $p1, "\\1 ", $text );
131 $text = preg_replace( $p2, "\\1 \\3 ", $text );
132
133 # Internal image links
134 $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
135 $text = preg_replace( $pat2, " \\1 \\3", $text );
136
137 $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
138 "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
139
140 # Strip all remaining non-search characters
141 $text = preg_replace( "/[^{$lc}]+/", " ", $text );
142
159 $text = strrev( preg_replace( "/ s'([{$lc}]+)/", " s'\\1 \\1", strrev( $text ) ) );
160 $text = strrev( preg_replace( "/ 's([{$lc}]+)/", " s\\1", strrev( $text ) ) );
161
162 # Strip wiki '' and '''
163 $text = preg_replace( "/''[']*/", " ", $text );
164
165 return $text;
166 }
167
177 private function getLatestPage() {
178 if ( !isset( $this->latestPage ) ) {
179 $this->latestPage = MediaWikiServices::getInstance()->getPageStore()
180 ->getPageById( $this->id, PageStore::READ_LATEST );
181 }
182
183 return $this->latestPage;
184 }
185
193 private function getNormalizedTitle( SearchEngine $search ) {
194 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
195 $ns = $this->page->getNamespace();
196 $title = str_replace( '_', ' ', $this->page->getDBkey() );
197
198 $lc = $search->legalSearchChars() . '&#;';
199 $t = $contLang->normalizeForSearch( $title );
200 $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
201 $t = $contLang->lc( $t );
202
203 # Handle 's, s'
204 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
205 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
206
207 $t = preg_replace( "/\\s+/", ' ', $t );
208
209 if ( $ns === NS_FILE ) {
210 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
211 }
212
213 return $search->normalizeText( trim( $t ) );
214 }
215}
const NS_FILE
Definition Defines.php:70
Create PSR-3 logger objects.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Contain a class for special pages.
delete( $id, $title)
Delete an indexed page Title should be pre-processed.
supports( $feature)
update( $id, $title, $text)
Create or update the search index record for the given page.
normalizeText( $string)
When overridden in derived class, performs database-specific conversions on text to be used for searc...
updateTitle( $id, $title)
Update a search index record's title only.
legalSearchChars( $type=self::CHARS_ALL)
Get chars legal for search.
Database independent search index updater.
updateText( $text, SearchEngine $se=null)
Clean text for indexing.
doUpdate()
Perform actual update for the entry.
__construct( $id, $page, ?Content $c=null)
Base interface for representing page content.
Definition Content.php:39
Interface that deferrable updates should implement.
Data record representing a page that currently exists as an editable page on a wiki.
Interface for objects (potentially) representing an editable wiki page.
$content
Definition router.php:76