MediaWiki  1.28.0
SearchSuggestionSet.php
Go to the documentation of this file.
1 <?php
2 
31  private $suggestions = [];
32 
37  private $pageMap = [];
38 
50  public function __construct( array $suggestions ) {
51  foreach ( $suggestions as $suggestion ) {
52  $pageID = $suggestion->getSuggestedTitleID();
53  if ( $pageID && empty( $this->pageMap[$pageID] ) ) {
54  $this->pageMap[$pageID] = true;
55  }
56  $this->suggestions[] = $suggestion;
57  }
58  }
59 
64  public function getSuggestions() {
65  return $this->suggestions;
66  }
67 
73  public function map( $callback ) {
74  return array_map( $callback, $this->suggestions );
75  }
76 
84  public function append( SearchSuggestion $suggestion ) {
85  $pageID = $suggestion->getSuggestedTitleID();
86  if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
87  return;
88  }
89  if ( $this->getSize() > 0 && $suggestion->getScore() >= $this->getWorstScore() ) {
90  $suggestion->setScore( $this->getWorstScore() - 1 );
91  }
92  $this->suggestions[] = $suggestion;
93  if ( $pageID ) {
94  $this->pageMap[$pageID] = true;
95  }
96  }
97 
102  public function appendAll( SearchSuggestionSet $set ) {
103  foreach ( $set->getSuggestions() as $sugg ) {
104  $this->append( $sugg );
105  }
106  }
107 
111  public function rescore( $key ) {
112  $removed = array_splice( $this->suggestions, $key, 1 );
113  unset( $this->pageMap[$removed[0]->getSuggestedTitleID()] );
114  $this->prepend( $removed[0] );
115  }
116 
122  public function prepend( SearchSuggestion $suggestion ) {
123  $pageID = $suggestion->getSuggestedTitleID();
124  if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
125  return;
126  }
127  if ( $this->getSize() > 0 && $suggestion->getScore() <= $this->getBestScore() ) {
128  $suggestion->setScore( $this->getBestScore() + 1 );
129  }
130  array_unshift( $this->suggestions, $suggestion );
131  if ( $pageID ) {
132  $this->pageMap[$pageID] = true;
133  }
134  }
135 
139  public function getBestScore() {
140  if ( empty( $this->suggestions ) ) {
141  return 0;
142  }
143  return $this->suggestions[0]->getScore();
144  }
145 
149  public function getWorstScore() {
150  if ( empty( $this->suggestions ) ) {
151  return 0;
152  }
153  return end( $this->suggestions )->getScore();
154  }
155 
159  public function getSize() {
160  return count( $this->suggestions );
161  }
162 
167  public function shrink( $limit ) {
168  if ( count( $this->suggestions ) > $limit ) {
169  $this->suggestions = array_slice( $this->suggestions, 0, $limit );
170  }
171  }
172 
182  public static function fromTitles( array $titles ) {
183  $score = count( $titles );
184  $suggestions = array_map( function( $title ) use ( &$score ) {
185  return SearchSuggestion::fromTitle( $score--, $title );
186  }, $titles );
187  return new SearchSuggestionSet( $suggestions );
188  }
189 
198  public static function fromStrings( array $titles ) {
199  $score = count( $titles );
200  $suggestions = array_map( function( $title ) use ( &$score ) {
201  return SearchSuggestion::fromText( $score--, $title );
202  }, $titles );
203  return new SearchSuggestionSet( $suggestions );
204  }
205 
209  public static function emptySuggestionSet() {
210  return new SearchSuggestionSet( [] );
211  }
212 }
the array() calling protocol came about after MediaWiki 1.4rc1.
Search suggestion.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static fromStrings(array $titles)
Builds a new set of suggestion based on a string array.
getScore()
Suggestion score.
__construct(array $suggestions)
Builds a new set of suggestions.
map($callback)
Call array_map on the suggestions array.
prepend(SearchSuggestion $suggestion)
Add a new suggestion at the top.
append(SearchSuggestion $suggestion)
Add a new suggestion at the end.
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:953
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
shrink($limit)
Remove any extra elements in the suggestions set.
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
linkcache txt The LinkCache class maintains a list of article titles and the information about whether or not the article exists in the database This is used to mark up links when displaying a page If the same link appears more than once on any page then it only has to be looked up once In most cases link lookups are done in batches with the LinkBatch class or the equivalent in so the link cache is mostly useful for short snippets of parsed and for links in the navigation areas of the skin The link cache was formerly used to track links used in a document for the purposes of updating the link tables This application is now deprecated To create a you can use the following $titles
Definition: linkcache.txt:17
getSuggestions()
Get the list of suggestions.
SearchSuggestion[] $suggestions
rescore($key)
Move the suggestion at index $key to the first position.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object to manipulate or replace but no entry for that model exists in $wgContentHandlers if desired whether it is OK to use $contentModel on $title Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok inclusive $limit
Definition: hooks.txt:1046
Search suggestion sets.
setScore($score)
Set the suggestion score.
getSuggestedTitleID()
Title ID in the case this suggestion is based on a title.
static fromTitle($score, Title $title)
Create suggestion from Title.
static fromTitles(array $titles)
Builds a new set of suggestion based on a title array.
static fromText($score, $text)
Create suggestion from text Will also create a title if text if not empty.
appendAll(SearchSuggestionSet $set)
Add suggestion set to the end of the current one.