MediaWiki  1.29.2
SearchSuggestionSet.php
Go to the documentation of this file.
1 <?php
2 
30  private $suggestions = [];
31 
36  private $pageMap = [];
37 
49  public function __construct( array $suggestions ) {
50  foreach ( $suggestions as $suggestion ) {
51  $pageID = $suggestion->getSuggestedTitleID();
52  if ( $pageID && empty( $this->pageMap[$pageID] ) ) {
53  $this->pageMap[$pageID] = true;
54  }
55  $this->suggestions[] = $suggestion;
56  }
57  }
58 
63  public function getSuggestions() {
64  return $this->suggestions;
65  }
66 
72  public function map( $callback ) {
73  return array_map( $callback, $this->suggestions );
74  }
75 
83  public function append( SearchSuggestion $suggestion ) {
84  $pageID = $suggestion->getSuggestedTitleID();
85  if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
86  return;
87  }
88  if ( $this->getSize() > 0 && $suggestion->getScore() >= $this->getWorstScore() ) {
89  $suggestion->setScore( $this->getWorstScore() - 1 );
90  }
91  $this->suggestions[] = $suggestion;
92  if ( $pageID ) {
93  $this->pageMap[$pageID] = true;
94  }
95  }
96 
101  public function appendAll( SearchSuggestionSet $set ) {
102  foreach ( $set->getSuggestions() as $sugg ) {
103  $this->append( $sugg );
104  }
105  }
106 
110  public function rescore( $key ) {
111  $removed = array_splice( $this->suggestions, $key, 1 );
112  unset( $this->pageMap[$removed[0]->getSuggestedTitleID()] );
113  $this->prepend( $removed[0] );
114  }
115 
121  public function prepend( SearchSuggestion $suggestion ) {
122  $pageID = $suggestion->getSuggestedTitleID();
123  if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
124  return;
125  }
126  if ( $this->getSize() > 0 && $suggestion->getScore() <= $this->getBestScore() ) {
127  $suggestion->setScore( $this->getBestScore() + 1 );
128  }
129  array_unshift( $this->suggestions, $suggestion );
130  if ( $pageID ) {
131  $this->pageMap[$pageID] = true;
132  }
133  }
134 
138  public function getBestScore() {
139  if ( empty( $this->suggestions ) ) {
140  return 0;
141  }
142  return $this->suggestions[0]->getScore();
143  }
144 
148  public function getWorstScore() {
149  if ( empty( $this->suggestions ) ) {
150  return 0;
151  }
152  return end( $this->suggestions )->getScore();
153  }
154 
158  public function getSize() {
159  return count( $this->suggestions );
160  }
161 
166  public function shrink( $limit ) {
167  if ( count( $this->suggestions ) > $limit ) {
168  $this->suggestions = array_slice( $this->suggestions, 0, $limit );
169  }
170  }
171 
181  public static function fromTitles( array $titles ) {
182  $score = count( $titles );
183  $suggestions = array_map( function( $title ) use ( &$score ) {
184  return SearchSuggestion::fromTitle( $score--, $title );
185  }, $titles );
186  return new SearchSuggestionSet( $suggestions );
187  }
188 
197  public static function fromStrings( array $titles ) {
198  $score = count( $titles );
199  $suggestions = array_map( function( $title ) use ( &$score ) {
200  return SearchSuggestion::fromText( $score--, $title );
201  }, $titles );
202  return new SearchSuggestionSet( $suggestions );
203  }
204 
208  public static function emptySuggestionSet() {
209  return new SearchSuggestionSet( [] );
210  }
211 }
SearchSuggestionSet\map
map( $callback)
Call array_map on the suggestions array.
Definition: SearchSuggestionSet.php:72
SearchSuggestionSet\getSize
getSize()
Definition: SearchSuggestionSet.php:158
captcha-old.count
count
Definition: captcha-old.py:225
SearchSuggestion\setScore
setScore( $score)
Set the suggestion score.
Definition: SearchSuggestion.php:139
SearchSuggestionSet\__construct
__construct(array $suggestions)
Builds a new set of suggestions.
Definition: SearchSuggestionSet.php:49
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
SearchSuggestionSet\prepend
prepend(SearchSuggestion $suggestion)
Add a new suggestion at the top.
Definition: SearchSuggestionSet.php:121
php
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
SearchSuggestion\fromTitle
static fromTitle( $score, Title $title)
Create suggestion from Title.
Definition: SearchSuggestion.php:166
SearchSuggestionSet\getSuggestions
getSuggestions()
Get the list of suggestions.
Definition: SearchSuggestionSet.php:63
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
$titles
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
SearchSuggestionSet\getBestScore
getBestScore()
Definition: SearchSuggestionSet.php:138
SearchSuggestion\getSuggestedTitleID
getSuggestedTitleID()
Title ID in the case this suggestion is based on a title.
Definition: SearchSuggestion.php:115
$limit
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup 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 please use GetContentModels hook to make them known to core 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:1049
SearchSuggestionSet\appendAll
appendAll(SearchSuggestionSet $set)
Add suggestion set to the end of the current one.
Definition: SearchSuggestionSet.php:101
SearchSuggestion
Search suggestion.
Definition: SearchSuggestion.php:25
SearchSuggestionSet\$pageMap
array $pageMap
Definition: SearchSuggestionSet.php:36
SearchSuggestionSet\shrink
shrink( $limit)
Remove any extra elements in the suggestions set.
Definition: SearchSuggestionSet.php:166
SearchSuggestionSet\fromStrings
static fromStrings(array $titles)
Builds a new set of suggestion based on a string array.
Definition: SearchSuggestionSet.php:197
SearchSuggestionSet\fromTitles
static fromTitles(array $titles)
Builds a new set of suggestion based on a title array.
Definition: SearchSuggestionSet.php:181
SearchSuggestionSet\rescore
rescore( $key)
Move the suggestion at index $key to the first position.
Definition: SearchSuggestionSet.php:110
SearchSuggestionSet
Search suggestion sets.
Definition: SearchSuggestionSet.php:26
SearchSuggestionSet\getWorstScore
getWorstScore()
Definition: SearchSuggestionSet.php:148
SearchSuggestionSet\emptySuggestionSet
static emptySuggestionSet()
Definition: SearchSuggestionSet.php:208
SearchSuggestionSet\append
append(SearchSuggestion $suggestion)
Add a new suggestion at the end.
Definition: SearchSuggestionSet.php:83
as
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
SearchSuggestion\fromText
static fromText( $score, $text)
Create suggestion from text Will also create a title if text if not empty.
Definition: SearchSuggestion.php:177
SearchSuggestion\getScore
getScore()
Suggestion score.
Definition: SearchSuggestion.php:131
SearchSuggestionSet\$suggestions
SearchSuggestion[] $suggestions
Definition: SearchSuggestionSet.php:30
array
the array() calling protocol came about after MediaWiki 1.4rc1.