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
23 use MediaWiki\Linker\LinkTarget
;
24 use MediaWiki\MediaWikiServices
;
25 use Wikimedia\Rdbms\IResultWrapper
;
26 use Wikimedia\Rdbms\IDatabase
;
29 * Class representing a list of titles
30 * The execute() method checks them all for existence and adds them to a LinkCache object
36 * 2-d array, first index namespace, second index dbkey, value arbitrary
41 * For debugging which method is using this class.
46 * @param Traversable|LinkTarget[] $arr Initial items to be added to the batch
48 public function __construct( $arr = [] ) {
49 foreach ( $arr as $item ) {
50 $this->addObj( $item );
55 * Use ->setCaller( __METHOD__ ) to indicate which code is using this
56 * class. Only used in debugging output.
59 * @param string $caller
60 * @return self (since 1.32)
62 public function setCaller( $caller ) {
63 $this->caller
= $caller;
69 * @param LinkTarget $linkTarget
71 public function addObj( $linkTarget ) {
72 if ( is_object( $linkTarget ) ) {
73 $this->add( $linkTarget->getNamespace(), $linkTarget->getDBkey() );
75 wfDebug( "Warning: LinkBatch::addObj got invalid LinkTarget object\n" );
81 * @param string $dbkey
83 public function add( $ns, $dbkey ) {
84 if ( $ns < 0 ||
$dbkey === '' ) {
87 if ( !array_key_exists( $ns, $this->data
) ) {
88 $this->data
[$ns] = [];
91 $this->data
[$ns][strtr( $dbkey, ' ', '_' )] = 1;
95 * Set the link list to a given 2-d array
96 * First key is the namespace, second is the DB key, value arbitrary
100 public function setArray( $array ) {
101 $this->data
= $array;
105 * Returns true if no pages have been added, false otherwise.
109 public function isEmpty() {
110 return $this->getSize() == 0;
114 * Returns the size of the batch.
118 public function getSize() {
119 return count( $this->data
);
123 * Do the query and add the results to the LinkCache object
125 * @return array Mapping PDBK to ID
127 public function execute() {
128 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
130 return $this->executeInto( $linkCache );
134 * Do the query and add the results to a given LinkCache object
135 * Return an array mapping PDBK to ID
137 * @param LinkCache &$cache
138 * @return array Remaining IDs
140 protected function executeInto( &$cache ) {
141 $res = $this->doQuery();
142 $this->doGenderQuery();
143 $ids = $this->addResultToCache( $cache, $res );
149 * Add a ResultWrapper containing IDs and titles to a LinkCache object.
150 * As normal, titles will go into the static Title cache field.
151 * This function *also* stores extra fields of the title used for link
152 * parsing to avoid extra DB queries.
154 * @param LinkCache $cache
155 * @param IResultWrapper $res
156 * @return array Array of remaining titles
158 public function addResultToCache( $cache, $res ) {
163 $titleFormatter = MediaWikiServices
::getInstance()->getTitleFormatter();
164 // For each returned entry, add it to the list of good links, and remove it from $remaining
167 $remaining = $this->data
;
168 foreach ( $res as $row ) {
169 $title = new TitleValue( (int)$row->page_namespace
, $row->page_title
);
170 $cache->addGoodLinkObjFromRow( $title, $row );
171 $pdbk = $titleFormatter->getPrefixedDBkey( $title );
172 $ids[$pdbk] = $row->page_id
;
173 unset( $remaining[$row->page_namespace
][$row->page_title
] );
176 // The remaining links in $data are bad links, register them as such
177 foreach ( $remaining as $ns => $dbkeys ) {
178 foreach ( $dbkeys as $dbkey => $unused ) {
179 $title = new TitleValue( (int)$ns, (string)$dbkey );
180 $cache->addBadLinkObj( $title );
181 $pdbk = $titleFormatter->getPrefixedDBkey( $title );
190 * Perform the existence test query, return a ResultWrapper with page_id fields
191 * @return bool|IResultWrapper
193 public function doQuery() {
194 if ( $this->isEmpty() ) {
198 // This is similar to LinkHolderArray::replaceInternal
199 $dbr = wfGetDB( DB_REPLICA
);
201 $fields = array_merge(
202 LinkCache
::getSelectFields(),
203 [ 'page_namespace', 'page_title' ]
206 $conds = $this->constructSet( 'page', $dbr );
209 $caller = __METHOD__
;
210 if ( strval( $this->caller
) !== '' ) {
211 $caller .= " (for {$this->caller})";
213 $res = $dbr->select( $table, $fields, $conds, $caller );
219 * Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch
221 * @return bool Whether the query was successful
223 public function doGenderQuery() {
224 if ( $this->isEmpty() ) {
227 $services = MediaWikiServices
::getInstance();
229 if ( !$services->getContentLanguage()->needsGenderDistinction() ) {
233 $genderCache = $services->getGenderCache();
234 $genderCache->doLinkBatch( $this->data
, $this->caller
);
240 * Construct a WHERE clause which will match all the given titles.
242 * @param string $prefix The appropriate table's field name prefix ('page', 'pl', etc)
243 * @param IDatabase $db DB object to use
244 * @return string|bool String with SQL where clause fragment, or false if no items.
246 public function constructSet( $prefix, $db ) {
247 return $db->makeWhereFrom2d( $this->data
, "{$prefix}_namespace", "{$prefix}_title" );