MediaWiki  master
LinkBatch.php
Go to the documentation of this file.
1 <?php
25 
32 class LinkBatch {
36  public $data = [];
37 
41  protected $caller;
42 
43  function __construct( $arr = [] ) {
44  foreach ( $arr as $item ) {
45  $this->addObj( $item );
46  }
47  }
48 
56  public function setCaller( $caller ) {
57  $this->caller = $caller;
58  }
59 
63  public function addObj( $linkTarget ) {
64  if ( is_object( $linkTarget ) ) {
65  $this->add( $linkTarget->getNamespace(), $linkTarget->getDBkey() );
66  } else {
67  wfDebug( "Warning: LinkBatch::addObj got invalid LinkTarget object\n" );
68  }
69  }
70 
75  public function add( $ns, $dbkey ) {
76  if ( $ns < 0 || $dbkey === '' ) {
77  return; // T137083
78  }
79  if ( !array_key_exists( $ns, $this->data ) ) {
80  $this->data[$ns] = [];
81  }
82 
83  $this->data[$ns][strtr( $dbkey, ' ', '_' )] = 1;
84  }
85 
92  public function setArray( $array ) {
93  $this->data = $array;
94  }
95 
101  public function isEmpty() {
102  return $this->getSize() == 0;
103  }
104 
110  public function getSize() {
111  return count( $this->data );
112  }
113 
119  public function execute() {
120  $linkCache = MediaWikiServices::getInstance()->getLinkCache();
121 
122  return $this->executeInto( $linkCache );
123  }
124 
132  protected function executeInto( &$cache ) {
133  $res = $this->doQuery();
134  $this->doGenderQuery();
135  $ids = $this->addResultToCache( $cache, $res );
136 
137  return $ids;
138  }
139 
150  public function addResultToCache( $cache, $res ) {
151  if ( !$res ) {
152  return [];
153  }
154 
155  $titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter();
156  // For each returned entry, add it to the list of good links, and remove it from $remaining
157 
158  $ids = [];
159  $remaining = $this->data;
160  foreach ( $res as $row ) {
161  $title = new TitleValue( (int)$row->page_namespace, $row->page_title );
162  $cache->addGoodLinkObjFromRow( $title, $row );
163  $pdbk = $titleFormatter->getPrefixedDBkey( $title );
164  $ids[$pdbk] = $row->page_id;
165  unset( $remaining[$row->page_namespace][$row->page_title] );
166  }
167 
168  // The remaining links in $data are bad links, register them as such
169  foreach ( $remaining as $ns => $dbkeys ) {
170  foreach ( $dbkeys as $dbkey => $unused ) {
171  $title = new TitleValue( (int)$ns, (string)$dbkey );
172  $cache->addBadLinkObj( $title );
173  $pdbk = $titleFormatter->getPrefixedDBkey( $title );
174  $ids[$pdbk] = 0;
175  }
176  }
177 
178  return $ids;
179  }
180 
185  public function doQuery() {
186  if ( $this->isEmpty() ) {
187  return false;
188  }
189 
190  // This is similar to LinkHolderArray::replaceInternal
191  $dbr = wfGetDB( DB_SLAVE );
192  $table = 'page';
193  $fields = array_merge(
195  [ 'page_namespace', 'page_title' ]
196  );
197 
198  $conds = $this->constructSet( 'page', $dbr );
199 
200  // Do query
201  $caller = __METHOD__;
202  if ( strval( $this->caller ) !== '' ) {
203  $caller .= " (for {$this->caller})";
204  }
205  $res = $dbr->select( $table, $fields, $conds, $caller );
206 
207  return $res;
208  }
209 
215  public function doGenderQuery() {
216  if ( $this->isEmpty() ) {
217  return false;
218  }
219 
221  if ( !$wgContLang->needsGenderDistinction() ) {
222  return false;
223  }
224 
225  $genderCache = MediaWikiServices::getInstance()->getGenderCache();
226  $genderCache->doLinkBatch( $this->data, $this->caller );
227 
228  return true;
229  }
230 
238  public function constructSet( $prefix, $db ) {
239  return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );
240  }
241 }
wfGetDB($db, $groups=[], $wiki=false)
Get a Database object.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:36
and how to run hooks for an and one after Each event has a preferably in CamelCase For ArticleDelete hook A clump of code and data that should be run when an event happens This can be either a function and a chunk of data
Definition: hooks.txt:6
doGenderQuery()
Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch.
Definition: LinkBatch.php:215
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
setCaller($caller)
Use ->setCaller( METHOD ) to indicate which code is using this class.
Definition: LinkBatch.php:56
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
__construct($arr=[])
Definition: LinkBatch.php:43
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:32
isEmpty()
Returns true if no pages have been added, false otherwise.
Definition: LinkBatch.php:101
constructSet($prefix, $db)
Construct a WHERE clause which will match all the given titles.
Definition: LinkBatch.php:238
$res
Definition: database.txt:21
$caller
For debugging which method is using this class.
Definition: LinkBatch.php:41
doQuery()
Perform the existence test query, return a ResultWrapper with page_id fields.
Definition: LinkBatch.php:185
$cache
Definition: mcc.php:33
const DB_SLAVE
Definition: Defines.php:46
Allows to change the fields on the form that will be generated are created Can be used to omit specific feeds from being outputted You must not use this hook to add use OutputPage::addFeedLink() instead.&$feedLinks conditions will AND in the final query as a Content object as a Content object $title
Definition: hooks.txt:312
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
addResultToCache($cache, $res)
Add a ResultWrapper containing IDs and titles to a LinkCache object.
Definition: LinkBatch.php:150
static getSelectFields()
Fields that LinkCache needs to select.
Definition: LinkCache.php:211
$data
2-d array, first index namespace, second index dbkey, value arbitrary
Definition: LinkBatch.php:36
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
execute()
Do the query and add the results to the LinkCache object.
Definition: LinkBatch.php:119
executeInto(&$cache)
Do the query and add the results to a given LinkCache object Return an array mapping PDBK to ID...
Definition: LinkBatch.php:132
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
add($ns, $dbkey)
Definition: LinkBatch.php:75
setArray($array)
Set the link list to a given 2-d array First key is the namespace, second is the DB key...
Definition: LinkBatch.php:92
addObj($linkTarget)
Definition: LinkBatch.php:63
getSize()
Returns the size of the batch.
Definition: LinkBatch.php:110