MediaWiki REL1_40
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, $c = null ) {
57 $this->page = $page;
58
59 $this->id = $id;
60 // is_string() check is back-compat for ApprovedRevs
61 if ( is_string( $c ) ) {
62 wfDeprecated( __METHOD__ . " with a string for the content", '1.34' );
63 $c = new TextContent( $c );
64 } elseif ( is_bool( $c ) ) {
65 wfDeprecated( __METHOD__ . " with a boolean for the content", '1.34' );
66 $c = null;
67 }
68 $this->content = $c;
69 }
70
74 public function doUpdate() {
75 $services = MediaWikiServices::getInstance();
76 $config = $services->getSearchEngineConfig();
77
78 if ( $config->getConfig()->get( MainConfigNames::DisableSearchUpdate ) || !$this->id ) {
79 LoggerFactory::getInstance( "search" )
80 ->debug( "Skipping update: search updates disabled by config" );
81 return;
82 }
83
84 $seFactory = $services->getSearchEngineFactory();
85 foreach ( $config->getSearchTypes() as $type ) {
86 $search = $seFactory->create( $type );
87 if ( !$search->supports( 'search-update' ) ) {
88 continue;
89 }
90
91 $normalTitle = $this->getNormalizedTitle( $search );
92
93 if ( $this->getLatestPage() === null ) {
94 $search->delete( $this->id, $normalTitle );
95 continue;
96 } elseif ( $this->content === null ) {
97 $search->updateTitle( $this->id, $normalTitle );
98 continue;
99 }
100
101 $text = $this->content !== null ? $this->content->getTextForSearchIndex() : '';
102 $text = $this->updateText( $text, $search );
103
104 # Perform the actual update
105 $search->update( $this->id, $normalTitle, $search->normalizeText( $text ) );
106 }
107 }
108
117 public function updateText( $text, SearchEngine $se = null ) {
118 $services = MediaWikiServices::getInstance();
119 $contLang = $services->getContentLanguage();
120 # Language-specific strip/conversion
121 $text = $contLang->normalizeForSearch( $text );
122 $se = $se ?: $services->newSearchEngine();
123 $lc = $se->legalSearchChars() . '&#;';
124
125 # Strip HTML markup
126 $text = preg_replace( "/<\\/?\\s*[A-Za-z][^>]*?>/",
127 ' ', $contLang->lc( " " . $text . " " ) );
128 $text = preg_replace( "/(^|\\n)==\\s*([^\\n]+)\\s*==(\\s)/sD",
129 "\\1\\2 \\2 \\2\\3", $text ); # Emphasize headings
130
131 # Strip external URLs
132 $uc = "A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\x80-\\xFF";
133 $protos = "http|https|ftp|mailto|news|gopher";
134 $pat = "/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/";
135 $text = preg_replace( $pat, "\\1 \\3", $text );
136
137 $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
138 $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
139 $text = preg_replace( $p1, "\\1 ", $text );
140 $text = preg_replace( $p2, "\\1 \\3 ", $text );
141
142 # Internal image links
143 $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
144 $text = preg_replace( $pat2, " \\1 \\3", $text );
145
146 $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
147 "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
148
149 # Strip all remaining non-search characters
150 $text = preg_replace( "/[^{$lc}]+/", " ", $text );
151
168 $text = strrev( preg_replace( "/ s'([{$lc}]+)/", " s'\\1 \\1", strrev( $text ) ) );
169 $text = strrev( preg_replace( "/ 's([{$lc}]+)/", " s\\1", strrev( $text ) ) );
170
171 # Strip wiki '' and '''
172 $text = preg_replace( "/''[']*/", " ", $text );
173
174 return $text;
175 }
176
186 private function getLatestPage() {
187 if ( !isset( $this->latestPage ) ) {
188 $this->latestPage = MediaWikiServices::getInstance()->getPageStore()
189 ->getPageById( $this->id, PageStore::READ_LATEST );
190 }
191
192 return $this->latestPage;
193 }
194
202 private function getNormalizedTitle( SearchEngine $search ) {
203 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
204 $ns = $this->page->getNamespace();
205 $title = str_replace( '_', ' ', $this->page->getDBkey() );
206
207 $lc = $search->legalSearchChars() . '&#;';
208 $t = $contLang->normalizeForSearch( $title );
209 $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
210 $t = $contLang->lc( $t );
211
212 # Handle 's, s'
213 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
214 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
215
216 $t = preg_replace( "/\\s+/", ' ', $t );
217
218 if ( $ns === NS_FILE ) {
219 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
220 }
221
222 return $search->normalizeText( trim( $t ) );
223 }
224}
const NS_FILE
Definition Defines.php:70
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
PSR-3 logger instance factory.
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, $c=null)
Content object implementation for representing flat text.
Base interface for representing page content.
Definition Content.php:37
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