MediaWiki master
SearchUpdate.php
Go to the documentation of this file.
1<?php
26namespace MediaWiki\Deferred;
27
34use SearchEngine;
36
44 private $id = 0;
45
47 private $page;
48
50 private $content;
51
53 private $latestPage = null;
54
60 public function __construct( $id, $page, ?Content $c = null ) {
61 $this->page = $page;
62 $this->id = $id;
63 $this->content = $c;
64 }
65
69 public function doUpdate() {
71 $searchEngineConfig = $services->getSearchEngineConfig();
72
73 if ( $services->getMainConfig()->get( MainConfigNames::DisableSearchUpdate ) || !$this->id ) {
74 LoggerFactory::getInstance( "search" )
75 ->debug( "Skipping update: search updates disabled by config" );
76 return;
77 }
78
79 $seFactory = $services->getSearchEngineFactory();
80 foreach ( $searchEngineConfig->getSearchTypes() as $type ) {
81 $search = $seFactory->create( $type );
82 if ( !$search->supports( 'search-update' ) ) {
83 continue;
84 }
85
86 $normalTitle = $this->getNormalizedTitle( $search );
87
88 if ( $this->getLatestPage() === null ) {
89 $search->delete( $this->id, $normalTitle );
90 continue;
91 } elseif ( $this->content === null ) {
92 $search->updateTitle( $this->id, $normalTitle );
93 continue;
94 }
95
96 $text = $this->content !== null ? $this->content->getTextForSearchIndex() : '';
97 $text = $this->updateText( $text, $search );
98
99 # Perform the actual update
100 $search->update( $this->id, $normalTitle, $search->normalizeText( $text ) );
101 }
102 }
103
112 public function updateText( $text, ?SearchEngine $se = null ) {
113 $services = MediaWikiServices::getInstance();
114 $contLang = $services->getContentLanguage();
115 # Language-specific strip/conversion
116 $text = $contLang->normalizeForSearch( $text );
117 $se = $se ?: $services->newSearchEngine();
118 $lc = $se->legalSearchChars() . '&#;';
119
120 # Strip HTML markup
121 $text = preg_replace( "/<\\/?\\s*[A-Za-z][^>]*?>/",
122 ' ', $contLang->lc( " " . $text . " " ) );
123 $text = preg_replace( "/(^|\\n)==\\s*([^\\n]+)\\s*==(\\s)/",
124 "\\1\\2 \\2 \\2\\3", $text ); # Emphasize headings
125
126 # Strip external URLs
127 $uc = "A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\x80-\\xFF";
128 $protos = "http|https|ftp|mailto|news|gopher";
129 $pat = "/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/";
130 $text = preg_replace( $pat, "\\1 \\3", $text );
131
132 $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
133 $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
134 $text = preg_replace( $p1, "\\1 ", $text );
135 $text = preg_replace( $p2, "\\1 \\3 ", $text );
136
137 # Internal image links
138 $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
139 $text = preg_replace( $pat2, " \\1 \\3", $text );
140
141 $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
142 "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
143
144 # Strip all remaining non-search characters
145 $text = preg_replace( "/[^{$lc}]+/", " ", $text );
146
163 $text = strrev( preg_replace( "/ s'([{$lc}]+)/", " s'\\1 \\1", strrev( $text ) ) );
164 $text = strrev( preg_replace( "/ 's([{$lc}]+)/", " s\\1", strrev( $text ) ) );
165
166 # Strip wiki '' and '''
167 $text = preg_replace( "/''[']*/", " ", $text );
168
169 return $text;
170 }
171
181 private function getLatestPage() {
182 if ( !isset( $this->latestPage ) ) {
183 $this->latestPage = MediaWikiServices::getInstance()->getPageStore()
184 ->getPageById( $this->id, IDBAccessObject::READ_LATEST );
185 }
186
187 return $this->latestPage;
188 }
189
197 private function getNormalizedTitle( SearchEngine $search ) {
198 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
199 $ns = $this->page->getNamespace();
200 $title = str_replace( '_', ' ', $this->page->getDBkey() );
201
202 $lc = $search->legalSearchChars() . '&#;';
203 $t = $contLang->normalizeForSearch( $title );
204 $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
205 $t = $contLang->lc( $t );
206
207 # Handle 's, s'
208 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
209 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
210
211 $t = preg_replace( "/\\s+/", ' ', $t );
212
213 if ( $ns === NS_FILE ) {
214 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
215 }
216
217 return $search->normalizeText( trim( $t ) );
218 }
219}
220
222class_alias( SearchUpdate::class, 'SearchUpdate' );
const NS_FILE
Definition Defines.php:71
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)
Create PSR-3 logger objects.
A class containing constants representing the names of configuration variables.
const DisableSearchUpdate
Name constant for the DisableSearchUpdate setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
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.
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.
Interface for database access objects.