Fix ApiBase::getErrorFromStatus() and ApiMessages
[mediawiki.git] / includes / cache / LinkBatch.php
blobc5bd29058ce36c60d9176fe15e21766bca1458ff
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;
25 /**
26 * Class representing a list of titles
27 * The execute() method checks them all for existence and adds them to a LinkCache object
29 * @ingroup Cache
31 class LinkBatch {
32 /**
33 * 2-d array, first index namespace, second index dbkey, value arbitrary
35 public $data = [];
37 /**
38 * For debugging which method is using this class.
40 protected $caller;
42 function __construct( $arr = [] ) {
43 foreach ( $arr as $item ) {
44 $this->addObj( $item );
48 /**
49 * Use ->setCaller( __METHOD__ ) to indicate which code is using this
50 * class. Only used in debugging output.
51 * @since 1.17
53 * @param string $caller
55 public function setCaller( $caller ) {
56 $this->caller = $caller;
59 /**
60 * @param LinkTarget $linkTarget
62 public function addObj( $linkTarget ) {
63 if ( is_object( $linkTarget ) ) {
64 $this->add( $linkTarget->getNamespace(), $linkTarget->getDBkey() );
65 } else {
66 wfDebug( "Warning: LinkBatch::addObj got invalid LinkTarget object\n" );
70 /**
71 * @param int $ns
72 * @param string $dbkey
74 public function add( $ns, $dbkey ) {
75 if ( $ns < 0 ) {
76 return;
78 if ( !array_key_exists( $ns, $this->data ) ) {
79 $this->data[$ns] = [];
82 $this->data[$ns][strtr( $dbkey, ' ', '_' )] = 1;
85 /**
86 * Set the link list to a given 2-d array
87 * First key is the namespace, second is the DB key, value arbitrary
89 * @param array $array
91 public function setArray( $array ) {
92 $this->data = $array;
95 /**
96 * Returns true if no pages have been added, false otherwise.
98 * @return bool
100 public function isEmpty() {
101 return $this->getSize() == 0;
105 * Returns the size of the batch.
107 * @return int
109 public function getSize() {
110 return count( $this->data );
114 * Do the query and add the results to the LinkCache object
116 * @return array Mapping PDBK to ID
118 public function execute() {
119 $linkCache = LinkCache::singleton();
121 return $this->executeInto( $linkCache );
125 * Do the query and add the results to a given LinkCache object
126 * Return an array mapping PDBK to ID
128 * @param LinkCache $cache
129 * @return array Remaining IDs
131 protected function executeInto( &$cache ) {
132 $res = $this->doQuery();
133 $this->doGenderQuery();
134 $ids = $this->addResultToCache( $cache, $res );
136 return $ids;
140 * Add a ResultWrapper containing IDs and titles to a LinkCache object.
141 * As normal, titles will go into the static Title cache field.
142 * This function *also* stores extra fields of the title used for link
143 * parsing to avoid extra DB queries.
145 * @param LinkCache $cache
146 * @param ResultWrapper $res
147 * @return array Array of remaining titles
149 public function addResultToCache( $cache, $res ) {
150 if ( !$res ) {
151 return [];
154 // For each returned entry, add it to the list of good links, and remove it from $remaining
156 $ids = [];
157 $remaining = $this->data;
158 foreach ( $res as $row ) {
159 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
160 $cache->addGoodLinkObjFromRow( $title, $row );
161 $ids[$title->getPrefixedDBkey()] = $row->page_id;
162 unset( $remaining[$row->page_namespace][$row->page_title] );
165 // The remaining links in $data are bad links, register them as such
166 foreach ( $remaining as $ns => $dbkeys ) {
167 foreach ( $dbkeys as $dbkey => $unused ) {
168 $title = Title::makeTitle( $ns, $dbkey );
169 $cache->addBadLinkObj( $title );
170 $ids[$title->getPrefixedDBkey()] = 0;
174 return $ids;
178 * Perform the existence test query, return a ResultWrapper with page_id fields
179 * @return bool|ResultWrapper
181 public function doQuery() {
182 global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
184 if ( $this->isEmpty() ) {
185 return false;
188 // This is similar to LinkHolderArray::replaceInternal
189 $dbr = wfGetDB( DB_SLAVE );
190 $table = 'page';
191 $fields = [ 'page_id', 'page_namespace', 'page_title', 'page_len',
192 'page_is_redirect', 'page_latest' ];
194 if ( $wgContentHandlerUseDB ) {
195 $fields[] = 'page_content_model';
197 if ( $wgPageLanguageUseDB ) {
198 $fields[] = 'page_lang';
201 $conds = $this->constructSet( 'page', $dbr );
203 // Do query
204 $caller = __METHOD__;
205 if ( strval( $this->caller ) !== '' ) {
206 $caller .= " (for {$this->caller})";
208 $res = $dbr->select( $table, $fields, $conds, $caller );
210 return $res;
214 * Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch
216 * @return bool Whether the query was successful
218 public function doGenderQuery() {
219 if ( $this->isEmpty() ) {
220 return false;
223 global $wgContLang;
224 if ( !$wgContLang->needsGenderDistinction() ) {
225 return false;
228 $genderCache = GenderCache::singleton();
229 $genderCache->doLinkBatch( $this->data, $this->caller );
231 return true;
235 * Construct a WHERE clause which will match all the given titles.
237 * @param string $prefix The appropriate table's field name prefix ('page', 'pl', etc)
238 * @param IDatabase $db DatabaseBase object to use
239 * @return string|bool String with SQL where clause fragment, or false if no items.
241 public function constructSet( $prefix, $db ) {
242 return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );