MediaWiki  master
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 
53  public function __construct( $id, $title, $c = false ) {
54  if ( is_string( $title ) ) {
55  $nt = Title::newFromText( $title );
56  } else {
57  $nt = $title;
58  }
59 
60  if ( $nt ) {
61  $this->id = $id;
62  // is_string() check is back-compat for ApprovedRevs
63  if ( is_string( $c ) ) {
64  $this->content = new TextContent( $c );
65  } else {
66  $this->content = $c ?: false;
67  }
68  $this->title = $nt;
69  } else {
70  wfDebug( "SearchUpdate object created with invalid title '$title'\n" );
71  }
72  }
73 
77  public function doUpdate() {
78  $services = MediaWikiServices::getInstance();
79  $config = $services->getSearchEngineConfig();
80 
81  if ( $config->getConfig()->get( 'DisableSearchUpdate' ) || !$this->id ) {
82  return;
83  }
84 
85  $seFactory = $services->getSearchEngineFactory();
86  foreach ( $config->getSearchTypes() as $type ) {
87  $search = $seFactory->create( $type );
88  if ( !$search->supports( 'search-update' ) ) {
89  continue;
90  }
91 
92  $normalTitle = $this->getNormalizedTitle( $search );
93 
94  if ( $this->getLatestPage() === null ) {
95  $search->delete( $this->id, $normalTitle );
96  continue;
97  } elseif ( $this->content === false ) {
98  $search->updateTitle( $this->id, $normalTitle );
99  continue;
100  }
101 
102  $text = $search->getTextFromContent( $this->title, $this->content );
103  if ( !$search->textAlreadyUpdatedForIndex() ) {
104  $text = $this->updateText( $text, $search );
105  }
106 
107  # Perform the actual update
108  $search->update( $this->id, $normalTitle, $search->normalizeText( $text ) );
109  }
110  }
111 
120  public function updateText( $text, SearchEngine $se = null ) {
121  $services = MediaWikiServices::getInstance();
122  $contLang = $services->getContentLanguage();
123  # Language-specific strip/conversion
124  $text = $contLang->normalizeForSearch( $text );
125  $se = $se ?: $services->newSearchEngine();
126  $lc = $se->legalSearchChars() . '&#;';
127 
128  # Strip HTML markup
129  $text = preg_replace( "/<\\/?\\s*[A-Za-z][^>]*?>/",
130  ' ', $contLang->lc( " " . $text . " " ) );
131  $text = preg_replace( "/(^|\\n)==\\s*([^\\n]+)\\s*==(\\s)/sD",
132  "\\1\\2 \\2 \\2\\3", $text ); # Emphasize headings
133 
134  # Strip external URLs
135  $uc = "A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\x80-\\xFF";
136  $protos = "http|https|ftp|mailto|news|gopher";
137  $pat = "/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/";
138  $text = preg_replace( $pat, "\\1 \\3", $text );
139 
140  $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
141  $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
142  $text = preg_replace( $p1, "\\1 ", $text );
143  $text = preg_replace( $p2, "\\1 \\3 ", $text );
144 
145  # Internal image links
146  $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
147  $text = preg_replace( $pat2, " \\1 \\3", $text );
148 
149  $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
150  "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
151 
152  # Strip all remaining non-search characters
153  $text = preg_replace( "/[^{$lc}]+/", " ", $text );
154 
171  $text = strrev( preg_replace( "/ s'([{$lc}]+)/", " s'\\1 \\1", strrev( $text ) ) );
172  $text = strrev( preg_replace( "/ 's([{$lc}]+)/", " s\\1", strrev( $text ) ) );
173 
174  # Strip wiki '' and '''
175  $text = preg_replace( "/''[']*/", " ", $text );
176 
177  return $text;
178  }
179 
189  private function getLatestPage() {
190  if ( !isset( $this->page ) ) {
191  $this->page = WikiPage::newFromID( $this->id, WikiPage::READ_LATEST );
192  }
193 
194  return $this->page;
195  }
196 
204  private function getNormalizedTitle( SearchEngine $search ) {
205  $contLang = MediaWikiServices::getInstance()->getContentLanguage();
206  $ns = $this->title->getNamespace();
207  $title = $this->title->getText();
208 
209  $lc = $search->legalSearchChars() . '&#;';
210  $t = $contLang->normalizeForSearch( $title );
211  $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
212  $t = $contLang->lc( $t );
213 
214  # Handle 's, s'
215  $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
216  $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
217 
218  $t = preg_replace( "/\\s+/", ' ', $t );
219 
220  if ( $ns == NS_FILE ) {
221  $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
222  }
223 
224  return $search->normalizeText( trim( $t ) );
225  }
226 }
per default it will return the text for text based content
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
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
target page
title
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place or wrap services the preferred way to define a new service is the $wgServiceWiringFiles array $services
Definition: hooks.txt:2217
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
legalSearchChars( $type=self::CHARS_ALL)
Get chars legal for search.
int $id
Page id being updated.
Content bool $content
Content of the page (not text)
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
const NS_FILE
Definition: Defines.php:70
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
__construct( $id, $title, $c=false)
static newFromID( $id, $from='fromdb')
Constructor from a page id.
Definition: WikiPage.php:176
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&#39;re updating.
normalizeText( $string)
When overridden in derived class, performs database-specific conversions on text to be used for searc...
getLatestPage()
Get WikiPage for the SearchUpdate $id using WikiPage::READ_LATEST and ensure using the same WikiPage ...
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:319