Properly deprecate srprop=score|hasrelated
[mediawiki.git] / includes / cache / LinkBatch.php
blob48c063f4e5849d4beeb5a172030fc62a144ff362
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
24 /**
25 * Class representing a list of titles
26 * The execute() method checks them all for existence and adds them to a LinkCache object
28 * @ingroup Cache
30 class LinkBatch {
31 /**
32 * 2-d array, first index namespace, second index dbkey, value arbitrary
34 public $data = array();
36 /**
37 * For debugging which method is using this class.
39 protected $caller;
41 function __construct( $arr = array() ) {
42 foreach ( $arr as $item ) {
43 $this->addObj( $item );
47 /**
48 * Use ->setCaller( __METHOD__ ) to indicate which code is using this
49 * class. Only used in debugging output.
50 * @since 1.17
52 * @param string $caller
54 public function setCaller( $caller ) {
55 $this->caller = $caller;
58 /**
59 * @param Title $title
61 public function addObj( $title ) {
62 if ( is_object( $title ) ) {
63 $this->add( $title->getNamespace(), $title->getDBkey() );
64 } else {
65 wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
69 /**
70 * @param int $ns
71 * @param string $dbkey
73 public function add( $ns, $dbkey ) {
74 if ( $ns < 0 ) {
75 return;
77 if ( !array_key_exists( $ns, $this->data ) ) {
78 $this->data[$ns] = array();
81 $this->data[$ns][str_replace( ' ', '_', $dbkey )] = 1;
84 /**
85 * Set the link list to a given 2-d array
86 * First key is the namespace, second is the DB key, value arbitrary
88 * @param array $array
90 public function setArray( $array ) {
91 $this->data = $array;
94 /**
95 * Returns true if no pages have been added, false otherwise.
97 * @return bool
99 public function isEmpty() {
100 return $this->getSize() == 0;
104 * Returns the size of the batch.
106 * @return int
108 public function getSize() {
109 return count( $this->data );
113 * Do the query and add the results to the LinkCache object
115 * @return array Mapping PDBK to ID
117 public function execute() {
118 $linkCache = LinkCache::singleton();
120 return $this->executeInto( $linkCache );
124 * Do the query and add the results to a given LinkCache object
125 * Return an array mapping PDBK to ID
127 * @param LinkCache $cache
128 * @return array Remaining IDs
130 protected function executeInto( &$cache ) {
131 wfProfileIn( __METHOD__ );
132 $res = $this->doQuery();
133 $this->doGenderQuery();
134 $ids = $this->addResultToCache( $cache, $res );
135 wfProfileOut( __METHOD__ );
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 array();
155 // For each returned entry, add it to the list of good links, and remove it from $remaining
157 $ids = array();
158 $remaining = $this->data;
159 foreach ( $res as $row ) {
160 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
161 $cache->addGoodLinkObjFromRow( $title, $row );
162 $ids[$title->getPrefixedDBkey()] = $row->page_id;
163 unset( $remaining[$row->page_namespace][$row->page_title] );
166 // The remaining links in $data are bad links, register them as such
167 foreach ( $remaining as $ns => $dbkeys ) {
168 foreach ( $dbkeys as $dbkey => $unused ) {
169 $title = Title::makeTitle( $ns, $dbkey );
170 $cache->addBadLinkObj( $title );
171 $ids[$title->getPrefixedDBkey()] = 0;
175 return $ids;
179 * Perform the existence test query, return a ResultWrapper with page_id fields
180 * @return bool|ResultWrapper
182 public function doQuery() {
183 global $wgContentHandlerUseDB;
185 if ( $this->isEmpty() ) {
186 return false;
188 wfProfileIn( __METHOD__ );
190 // This is similar to LinkHolderArray::replaceInternal
191 $dbr = wfGetDB( DB_SLAVE );
192 $table = 'page';
193 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_len',
194 'page_is_redirect', 'page_latest' );
196 if ( $wgContentHandlerUseDB ) {
197 $fields[] = 'page_content_model';
200 $conds = $this->constructSet( 'page', $dbr );
202 // Do query
203 $caller = __METHOD__;
204 if ( strval( $this->caller ) !== '' ) {
205 $caller .= " (for {$this->caller})";
207 $res = $dbr->select( $table, $fields, $conds, $caller );
208 wfProfileOut( __METHOD__ );
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 DatabaseBase $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" );