MediaWiki  1.27.2
SearchUpdate.php
Go to the documentation of this file.
1 <?php
27 
33 class SearchUpdate implements DeferrableUpdate {
35  private $id = 0;
36 
38  private $title;
39 
41  private $content;
42 
44  private $page;
45 
55  public function __construct( $id, $title, $c = false ) {
56  if ( is_string( $title ) ) {
57  $nt = Title::newFromText( $title );
58  } else {
59  $nt = $title;
60  }
61 
62  if ( $nt ) {
63  $this->id = $id;
64  // is_string() check is back-compat for ApprovedRevs
65  if ( is_string( $c ) ) {
66  $this->content = new TextContent( $c );
67  } else {
68  $this->content = $c ?: false;
69  }
70  $this->title = $nt;
71  } else {
72  wfDebug( "SearchUpdate object created with invalid title '$title'\n" );
73  }
74  }
75 
79  public function doUpdate() {
80  $config = MediaWikiServices::getInstance()->getSearchEngineConfig();
81 
82  if ( $config->getConfig()->get( 'DisableSearchUpdate' ) || !$this->id ) {
83  return;
84  }
85 
86  $seFactory = MediaWikiServices::getInstance()->getSearchEngineFactory();
87  foreach ( $config->getSearchTypes() as $type ) {
88  $search = $seFactory->create( $type );
89  if ( !$search->supports( 'search-update' ) ) {
90  continue;
91  }
92 
93  $normalTitle = $this->getNormalizedTitle( $search );
94 
95  if ( $this->getLatestPage() === null ) {
96  $search->delete( $this->id, $normalTitle );
97  continue;
98  } elseif ( $this->content === false ) {
99  $search->updateTitle( $this->id, $normalTitle );
100  continue;
101  }
102 
103  $text = $search->getTextFromContent( $this->title, $this->content );
104  if ( !$search->textAlreadyUpdatedForIndex() ) {
105  $text = $this->updateText( $text, $search );
106  }
107 
108  # Perform the actual update
109  $search->update( $this->id, $normalTitle, $search->normalizeText( $text ) );
110  }
111 
112  }
113 
122  public function updateText( $text, SearchEngine $se = null ) {
124 
125  # Language-specific strip/conversion
126  $text = $wgContLang->normalizeForSearch( $text );
127  $se = $se ?: MediaWikiServices::getInstance()->newSearchEngine();
128  $lc = $se->legalSearchChars() . '&#;';
129 
130  $text = preg_replace( "/<\\/?\\s*[A-Za-z][^>]*?>/",
131  ' ', $wgContLang->lc( " " . $text . " " ) ); # Strip HTML markup
132  $text = preg_replace( "/(^|\\n)==\\s*([^\\n]+)\\s*==(\\s)/sD",
133  "\\1\\2 \\2 \\2\\3", $text ); # Emphasize headings
134 
135  # Strip external URLs
136  $uc = "A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\x80-\\xFF";
137  $protos = "http|https|ftp|mailto|news|gopher";
138  $pat = "/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/";
139  $text = preg_replace( $pat, "\\1 \\3", $text );
140 
141  $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
142  $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
143  $text = preg_replace( $p1, "\\1 ", $text );
144  $text = preg_replace( $p2, "\\1 \\3 ", $text );
145 
146  # Internal image links
147  $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
148  $text = preg_replace( $pat2, " \\1 \\3", $text );
149 
150  $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
151  "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
152 
153  # Strip all remaining non-search characters
154  $text = preg_replace( "/[^{$lc}]+/", " ", $text );
155 
172  $text = strrev( preg_replace( "/ s'([{$lc}]+)/", " s'\\1 \\1", strrev( $text ) ) );
173  $text = strrev( preg_replace( "/ 's([{$lc}]+)/", " s\\1", strrev( $text ) ) );
174 
175  # Strip wiki '' and '''
176  $text = preg_replace( "/''[']*/", " ", $text );
177 
178  return $text;
179  }
180 
190  private function getLatestPage() {
191  if ( !isset( $this->page ) ) {
192  $this->page = WikiPage::newFromID( $this->id, WikiPage::READ_LATEST );
193  }
194 
195  return $this->page;
196  }
197 
205  private function getNormalizedTitle( SearchEngine $search ) {
207 
208  $ns = $this->title->getNamespace();
209  $title = $this->title->getText();
210 
211  $lc = $search->legalSearchChars() . '&#;';
212  $t = $wgContLang->normalizeForSearch( $title );
213  $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
214  $t = $wgContLang->lc( $t );
215 
216  # Handle 's, s'
217  $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
218  $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
219 
220  $t = preg_replace( "/\\s+/", ' ', $t );
221 
222  if ( $ns == NS_FILE ) {
223  $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
224  }
225 
226  return $search->normalizeText( trim( $t ) );
227  }
228 }
Interface that deferrable updates should implement.
per default it will return the text for text based content
static legalSearchChars()
Get chars legal for search.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
__construct($id, $title, $c=false)
Constructor.
doUpdate()
Perform actual update for the entry.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
updateText($text, SearchEngine $se=null)
Clean text for indexing.
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:277
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Database independant search index updater.
Content object implementation for representing flat text.
Definition: TextContent.php:35
normalizeText($string)
When overridden in derived class, performs database-specific conversions on text to be used for searc...
int $id
Page id being updated.
Content bool $content
Content of the page (not text)
title
const NS_FILE
Definition: Defines.php:75
getNormalizedTitle(SearchEngine $search)
Get a normalized string representation of a title suitable for including in a search index...
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
WikiPage $page
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Title $title
Title we're updating.
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition: design.txt:56
static newFromID($id, $from= 'fromdb')
Constructor from a page id.
Definition: WikiPage.php:132
getLatestPage()
Get WikiPage for the SearchUpdate $id using WikiPage::READ_LATEST and ensure using the same WikiPage ...
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk page
Definition: hooks.txt:2338
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2338