MediaWiki  1.30.0
LinkBatch.php
Go to the documentation of this file.
1 <?php
27 
34 class LinkBatch {
38  public $data = [];
39 
43  protected $caller;
44 
48  public function __construct( $arr = [] ) {
49  foreach ( $arr as $item ) {
50  $this->addObj( $item );
51  }
52  }
53 
61  public function setCaller( $caller ) {
62  $this->caller = $caller;
63  }
64 
68  public function addObj( $linkTarget ) {
69  if ( is_object( $linkTarget ) ) {
70  $this->add( $linkTarget->getNamespace(), $linkTarget->getDBkey() );
71  } else {
72  wfDebug( "Warning: LinkBatch::addObj got invalid LinkTarget object\n" );
73  }
74  }
75 
80  public function add( $ns, $dbkey ) {
81  if ( $ns < 0 || $dbkey === '' ) {
82  return; // T137083
83  }
84  if ( !array_key_exists( $ns, $this->data ) ) {
85  $this->data[$ns] = [];
86  }
87 
88  $this->data[$ns][strtr( $dbkey, ' ', '_' )] = 1;
89  }
90 
97  public function setArray( $array ) {
98  $this->data = $array;
99  }
100 
106  public function isEmpty() {
107  return $this->getSize() == 0;
108  }
109 
115  public function getSize() {
116  return count( $this->data );
117  }
118 
124  public function execute() {
125  $linkCache = MediaWikiServices::getInstance()->getLinkCache();
126 
127  return $this->executeInto( $linkCache );
128  }
129 
137  protected function executeInto( &$cache ) {
138  $res = $this->doQuery();
139  $this->doGenderQuery();
140  $ids = $this->addResultToCache( $cache, $res );
141 
142  return $ids;
143  }
144 
155  public function addResultToCache( $cache, $res ) {
156  if ( !$res ) {
157  return [];
158  }
159 
160  $titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter();
161  // For each returned entry, add it to the list of good links, and remove it from $remaining
162 
163  $ids = [];
164  $remaining = $this->data;
165  foreach ( $res as $row ) {
166  $title = new TitleValue( (int)$row->page_namespace, $row->page_title );
167  $cache->addGoodLinkObjFromRow( $title, $row );
168  $pdbk = $titleFormatter->getPrefixedDBkey( $title );
169  $ids[$pdbk] = $row->page_id;
170  unset( $remaining[$row->page_namespace][$row->page_title] );
171  }
172 
173  // The remaining links in $data are bad links, register them as such
174  foreach ( $remaining as $ns => $dbkeys ) {
175  foreach ( $dbkeys as $dbkey => $unused ) {
176  $title = new TitleValue( (int)$ns, (string)$dbkey );
177  $cache->addBadLinkObj( $title );
178  $pdbk = $titleFormatter->getPrefixedDBkey( $title );
179  $ids[$pdbk] = 0;
180  }
181  }
182 
183  return $ids;
184  }
185 
190  public function doQuery() {
191  if ( $this->isEmpty() ) {
192  return false;
193  }
194 
195  // This is similar to LinkHolderArray::replaceInternal
196  $dbr = wfGetDB( DB_REPLICA );
197  $table = 'page';
198  $fields = array_merge(
200  [ 'page_namespace', 'page_title' ]
201  );
202 
203  $conds = $this->constructSet( 'page', $dbr );
204 
205  // Do query
206  $caller = __METHOD__;
207  if ( strval( $this->caller ) !== '' ) {
208  $caller .= " (for {$this->caller})";
209  }
210  $res = $dbr->select( $table, $fields, $conds, $caller );
211 
212  return $res;
213  }
214 
220  public function doGenderQuery() {
221  if ( $this->isEmpty() ) {
222  return false;
223  }
224 
226  if ( !$wgContLang->needsGenderDistinction() ) {
227  return false;
228  }
229 
230  $genderCache = MediaWikiServices::getInstance()->getGenderCache();
231  $genderCache->doLinkBatch( $this->data, $this->caller );
232 
233  return true;
234  }
235 
243  public function constructSet( $prefix, $db ) {
244  return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );
245  }
246 }
LinkBatch\doGenderQuery
doGenderQuery()
Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch.
Definition: LinkBatch.php:220
LinkBatch
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:34
LinkBatch\add
add( $ns, $dbkey)
Definition: LinkBatch.php:80
captcha-old.count
count
Definition: captcha-old.py:249
LinkBatch\setCaller
setCaller( $caller)
Use ->setCaller( METHOD ) to indicate which code is using this class.
Definition: LinkBatch.php:61
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
$res
$res
Definition: database.txt:21
Wikimedia\Rdbms\ResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: ResultWrapper.php:24
LinkBatch\addObj
addObj( $linkTarget)
Definition: LinkBatch.php:68
LinkBatch\setArray
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:97
LinkCache\getSelectFields
static getSelectFields()
Fields that LinkCache needs to select.
Definition: LinkCache.php:213
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
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:40
LinkBatch\execute
execute()
Do the query and add the results to the LinkCache object.
Definition: LinkBatch.php:124
LinkBatch\getSize
getSize()
Returns the size of the batch.
Definition: LinkBatch.php:115
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:932
LinkBatch\constructSet
constructSet( $prefix, $db)
Construct a WHERE clause which will match all the given titles.
Definition: LinkBatch.php:243
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2856
LinkBatch\__construct
__construct( $arr=[])
Definition: LinkBatch.php:48
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:1047
LinkBatch\$caller
$caller
For debugging which method is using this class.
Definition: LinkBatch.php:43
LinkBatch\$data
$data
2-d array, first index namespace, second index dbkey, value arbitrary
Definition: LinkBatch.php:38
LinkBatch\doQuery
doQuery()
Perform the existence test query, return a ResultWrapper with page_id fields.
Definition: LinkBatch.php:190
LinkBatch\executeInto
executeInto(&$cache)
Do the query and add the results to a given LinkCache object Return an array mapping PDBK to ID.
Definition: LinkBatch.php:137
LinkBatch\isEmpty
isEmpty()
Returns true if no pages have been added, false otherwise.
Definition: LinkBatch.php:106
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
$cache
$cache
Definition: mcc.php:33
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
MediaWikiServices
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
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:27
LinkBatch\addResultToCache
addResultToCache( $cache, $res)
Add a ResultWrapper containing IDs and titles to a LinkCache object.
Definition: LinkBatch.php:155
data
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
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:36
$wgContLang
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 content language as $wgContLang
Definition: design.txt:56