AuthManager: Break AuthPlugin::addUser more explicitly
[mediawiki.git] / includes / cache / LinkBatch.php
blobec37dd61c3b3e1ae4fc63668fae9ba4967f1aa2b
1 <?php
2 /**
3 * Batch query to determine page existence.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Cache
23 use MediaWiki\Linker\LinkTarget;
24 use MediaWiki\MediaWikiServices;
26 /**
27 * Class representing a list of titles
28 * The execute() method checks them all for existence and adds them to a LinkCache object
30 * @ingroup Cache
32 class LinkBatch {
33 /**
34 * 2-d array, first index namespace, second index dbkey, value arbitrary
36 public $data = [];
38 /**
39 * For debugging which method is using this class.
41 protected $caller;
43 function __construct( $arr = [] ) {
44 foreach ( $arr as $item ) {
45 $this->addObj( $item );
49 /**
50 * Use ->setCaller( __METHOD__ ) to indicate which code is using this
51 * class. Only used in debugging output.
52 * @since 1.17
54 * @param string $caller
56 public function setCaller( $caller ) {
57 $this->caller = $caller;
60 /**
61 * @param LinkTarget $linkTarget
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" );
71 /**
72 * @param int $ns
73 * @param string $dbkey
75 public function add( $ns, $dbkey ) {
76 if ( $ns < 0 || $dbkey === '' ) {
77 return; // T137083
79 if ( !array_key_exists( $ns, $this->data ) ) {
80 $this->data[$ns] = [];
83 $this->data[$ns][strtr( $dbkey, ' ', '_' )] = 1;
86 /**
87 * Set the link list to a given 2-d array
88 * First key is the namespace, second is the DB key, value arbitrary
90 * @param array $array
92 public function setArray( $array ) {
93 $this->data = $array;
96 /**
97 * Returns true if no pages have been added, false otherwise.
99 * @return bool
101 public function isEmpty() {
102 return $this->getSize() == 0;
106 * Returns the size of the batch.
108 * @return int
110 public function getSize() {
111 return count( $this->data );
115 * Do the query and add the results to the LinkCache object
117 * @return array Mapping PDBK to ID
119 public function execute() {
120 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
122 return $this->executeInto( $linkCache );
126 * Do the query and add the results to a given LinkCache object
127 * Return an array mapping PDBK to ID
129 * @param LinkCache $cache
130 * @return array Remaining IDs
132 protected function executeInto( &$cache ) {
133 $res = $this->doQuery();
134 $this->doGenderQuery();
135 $ids = $this->addResultToCache( $cache, $res );
137 return $ids;
141 * Add a ResultWrapper containing IDs and titles to a LinkCache object.
142 * As normal, titles will go into the static Title cache field.
143 * This function *also* stores extra fields of the title used for link
144 * parsing to avoid extra DB queries.
146 * @param LinkCache $cache
147 * @param ResultWrapper $res
148 * @return array Array of remaining titles
150 public function addResultToCache( $cache, $res ) {
151 if ( !$res ) {
152 return [];
155 $titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter();
156 // For each returned entry, add it to the list of good links, and remove it from $remaining
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] );
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;
178 return $ids;
182 * Perform the existence test query, return a ResultWrapper with page_id fields
183 * @return bool|ResultWrapper
185 public function doQuery() {
186 if ( $this->isEmpty() ) {
187 return false;
190 // This is similar to LinkHolderArray::replaceInternal
191 $dbr = wfGetDB( DB_SLAVE );
192 $table = 'page';
193 $fields = array_merge(
194 LinkCache::getSelectFields(),
195 [ 'page_namespace', 'page_title' ]
198 $conds = $this->constructSet( 'page', $dbr );
200 // Do query
201 $caller = __METHOD__;
202 if ( strval( $this->caller ) !== '' ) {
203 $caller .= " (for {$this->caller})";
205 $res = $dbr->select( $table, $fields, $conds, $caller );
207 return $res;
211 * Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch
213 * @return bool Whether the query was successful
215 public function doGenderQuery() {
216 if ( $this->isEmpty() ) {
217 return false;
220 global $wgContLang;
221 if ( !$wgContLang->needsGenderDistinction() ) {
222 return false;
225 $genderCache = MediaWikiServices::getInstance()->getGenderCache();
226 $genderCache->doLinkBatch( $this->data, $this->caller );
228 return true;
232 * Construct a WHERE clause which will match all the given titles.
234 * @param string $prefix The appropriate table's field name prefix ('page', 'pl', etc)
235 * @param IDatabase $db DatabaseBase object to use
236 * @return string|bool String with SQL where clause fragment, or false if no items.
238 public function constructSet( $prefix, $db ) {
239 return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );