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
25 * Class representing a list of titles
26 * The execute() method checks them all for existence and adds them to a LinkCache object
32 * 2-d array, first index namespace, second index dbkey, value arbitrary
37 * For debugging which method is using this class.
41 function __construct( $arr = [] ) {
42 foreach ( $arr as $item ) {
43 $this->addObj( $item );
48 * Use ->setCaller( __METHOD__ ) to indicate which code is using this
49 * class. Only used in debugging output.
52 * @param string $caller
54 public function setCaller( $caller ) {
55 $this->caller
= $caller;
59 * @param LinkTarget $linkTarget
61 public function addObj( LinkTarget
$linkTarget ) {
62 $this->add( $linkTarget->getNamespace(), $linkTarget->getDBkey() );
67 * @param string $dbkey
69 public function add( $ns, $dbkey ) {
73 if ( !array_key_exists( $ns, $this->data
) ) {
74 $this->data
[$ns] = [];
77 $this->data
[$ns][strtr( $dbkey, ' ', '_' )] = 1;
81 * Set the link list to a given 2-d array
82 * First key is the namespace, second is the DB key, value arbitrary
86 public function setArray( $array ) {
91 * Returns true if no pages have been added, false otherwise.
95 public function isEmpty() {
96 return $this->getSize() == 0;
100 * Returns the size of the batch.
104 public function getSize() {
105 return count( $this->data
);
109 * Do the query and add the results to the LinkCache object
111 * @return array Mapping PDBK to ID
113 public function execute() {
114 $linkCache = LinkCache
::singleton();
116 return $this->executeInto( $linkCache );
120 * Do the query and add the results to a given LinkCache object
121 * Return an array mapping PDBK to ID
123 * @param LinkCache $cache
124 * @return array Remaining IDs
126 protected function executeInto( &$cache ) {
127 $res = $this->doQuery();
128 $this->doGenderQuery();
129 $ids = $this->addResultToCache( $cache, $res );
135 * Add a ResultWrapper containing IDs and titles to a LinkCache object.
136 * As normal, titles will go into the static Title cache field.
137 * This function *also* stores extra fields of the title used for link
138 * parsing to avoid extra DB queries.
140 * @param LinkCache $cache
141 * @param ResultWrapper $res
142 * @return array Array of remaining titles
144 public function addResultToCache( $cache, $res ) {
149 // For each returned entry, add it to the list of good links, and remove it from $remaining
152 $remaining = $this->data
;
153 foreach ( $res as $row ) {
154 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
155 $cache->addGoodLinkObjFromRow( $title, $row );
156 $ids[$title->getPrefixedDBkey()] = $row->page_id
;
157 unset( $remaining[$row->page_namespace
][$row->page_title
] );
160 // The remaining links in $data are bad links, register them as such
161 foreach ( $remaining as $ns => $dbkeys ) {
162 foreach ( $dbkeys as $dbkey => $unused ) {
163 $title = Title
::makeTitle( $ns, $dbkey );
164 $cache->addBadLinkObj( $title );
165 $ids[$title->getPrefixedDBkey()] = 0;
173 * Perform the existence test query, return a ResultWrapper with page_id fields
174 * @return bool|ResultWrapper
176 public function doQuery() {
177 global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
179 if ( $this->isEmpty() ) {
183 // This is similar to LinkHolderArray::replaceInternal
184 $dbr = wfGetDB( DB_SLAVE
);
186 $fields = [ 'page_id', 'page_namespace', 'page_title', 'page_len',
187 'page_is_redirect', 'page_latest' ];
189 if ( $wgContentHandlerUseDB ) {
190 $fields[] = 'page_content_model';
192 if ( $wgPageLanguageUseDB ) {
193 $fields[] = 'page_lang';
196 $conds = $this->constructSet( 'page', $dbr );
199 $caller = __METHOD__
;
200 if ( strval( $this->caller
) !== '' ) {
201 $caller .= " (for {$this->caller})";
203 $res = $dbr->select( $table, $fields, $conds, $caller );
209 * Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch
211 * @return bool Whether the query was successful
213 public function doGenderQuery() {
214 if ( $this->isEmpty() ) {
219 if ( !$wgContLang->needsGenderDistinction() ) {
223 $genderCache = GenderCache
::singleton();
224 $genderCache->doLinkBatch( $this->data
, $this->caller
);
230 * Construct a WHERE clause which will match all the given titles.
232 * @param string $prefix The appropriate table's field name prefix ('page', 'pl', etc)
233 * @param IDatabase $db DatabaseBase object to use
234 * @return string|bool String with SQL where clause fragment, or false if no items.
236 public function constructSet( $prefix, $db ) {
237 return $db->makeWhereFrom2d( $this->data
, "{$prefix}_namespace", "{$prefix}_title" );