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