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
;
27 * Class representing a list of titles
28 * The execute() method checks them all for existence and adds them to a LinkCache object
34 * 2-d array, first index namespace, second index dbkey, value arbitrary
39 * For debugging which method is using this class.
44 * LinkBatch constructor.
45 * @param LinkTarget[] $arr Initial items to be added to the batch
47 public function __construct( $arr = [] ) {
48 foreach ( $arr as $item ) {
49 $this->addObj( $item );
54 * Use ->setCaller( __METHOD__ ) to indicate which code is using this
55 * class. Only used in debugging output.
58 * @param string $caller
60 public function setCaller( $caller ) {
61 $this->caller
= $caller;
65 * @param LinkTarget $linkTarget
67 public function addObj( $linkTarget ) {
68 if ( is_object( $linkTarget ) ) {
69 $this->add( $linkTarget->getNamespace(), $linkTarget->getDBkey() );
71 wfDebug( "Warning: LinkBatch::addObj got invalid LinkTarget object\n" );
77 * @param string $dbkey
79 public function add( $ns, $dbkey ) {
80 if ( $ns < 0 ||
$dbkey === '' ) {
83 if ( !array_key_exists( $ns, $this->data
) ) {
84 $this->data
[$ns] = [];
87 $this->data
[$ns][strtr( $dbkey, ' ', '_' )] = 1;
91 * Set the link list to a given 2-d array
92 * First key is the namespace, second is the DB key, value arbitrary
96 public function setArray( $array ) {
101 * Returns true if no pages have been added, false otherwise.
105 public function isEmpty() {
106 return $this->getSize() == 0;
110 * Returns the size of the batch.
114 public function getSize() {
115 return count( $this->data
);
119 * Do the query and add the results to the LinkCache object
121 * @return array Mapping PDBK to ID
123 public function execute() {
124 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
126 return $this->executeInto( $linkCache );
130 * Do the query and add the results to a given LinkCache object
131 * Return an array mapping PDBK to ID
133 * @param LinkCache $cache
134 * @return array Remaining IDs
136 protected function executeInto( &$cache ) {
137 $res = $this->doQuery();
138 $this->doGenderQuery();
139 $ids = $this->addResultToCache( $cache, $res );
145 * Add a ResultWrapper containing IDs and titles to a LinkCache object.
146 * As normal, titles will go into the static Title cache field.
147 * This function *also* stores extra fields of the title used for link
148 * parsing to avoid extra DB queries.
150 * @param LinkCache $cache
151 * @param ResultWrapper $res
152 * @return array Array of remaining titles
154 public function addResultToCache( $cache, $res ) {
159 $titleFormatter = MediaWikiServices
::getInstance()->getTitleFormatter();
160 // For each returned entry, add it to the list of good links, and remove it from $remaining
163 $remaining = $this->data
;
164 foreach ( $res as $row ) {
165 $title = new TitleValue( (int)$row->page_namespace
, $row->page_title
);
166 $cache->addGoodLinkObjFromRow( $title, $row );
167 $pdbk = $titleFormatter->getPrefixedDBkey( $title );
168 $ids[$pdbk] = $row->page_id
;
169 unset( $remaining[$row->page_namespace
][$row->page_title
] );
172 // The remaining links in $data are bad links, register them as such
173 foreach ( $remaining as $ns => $dbkeys ) {
174 foreach ( $dbkeys as $dbkey => $unused ) {
175 $title = new TitleValue( (int)$ns, (string)$dbkey );
176 $cache->addBadLinkObj( $title );
177 $pdbk = $titleFormatter->getPrefixedDBkey( $title );
186 * Perform the existence test query, return a ResultWrapper with page_id fields
187 * @return bool|ResultWrapper
189 public function doQuery() {
190 if ( $this->isEmpty() ) {
194 // This is similar to LinkHolderArray::replaceInternal
195 $dbr = wfGetDB( DB_REPLICA
);
197 $fields = array_merge(
198 LinkCache
::getSelectFields(),
199 [ 'page_namespace', 'page_title' ]
202 $conds = $this->constructSet( 'page', $dbr );
205 $caller = __METHOD__
;
206 if ( strval( $this->caller
) !== '' ) {
207 $caller .= " (for {$this->caller})";
209 $res = $dbr->select( $table, $fields, $conds, $caller );
215 * Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch
217 * @return bool Whether the query was successful
219 public function doGenderQuery() {
220 if ( $this->isEmpty() ) {
225 if ( !$wgContLang->needsGenderDistinction() ) {
229 $genderCache = MediaWikiServices
::getInstance()->getGenderCache();
230 $genderCache->doLinkBatch( $this->data
, $this->caller
);
236 * Construct a WHERE clause which will match all the given titles.
238 * @param string $prefix The appropriate table's field name prefix ('page', 'pl', etc)
239 * @param IDatabase $db DB object to use
240 * @return string|bool String with SQL where clause fragment, or false if no items.
242 public function constructSet( $prefix, $db ) {
243 return $db->makeWhereFrom2d( $this->data
, "{$prefix}_namespace", "{$prefix}_title" );