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
34 public $data = array();
37 * For debugging which method is using this class.
41 function __construct( $arr = array() ) {
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;
61 public function addObj( $title ) {
62 if ( is_object( $title ) ) {
63 $this->add( $title->getNamespace(), $title->getDBkey() );
65 wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
71 * @param string $dbkey
73 public function add( $ns, $dbkey ) {
77 if ( !array_key_exists( $ns, $this->data
) ) {
78 $this->data
[$ns] = array();
81 $this->data
[$ns][strtr( $dbkey, ' ', '_' )] = 1;
85 * Set the link list to a given 2-d array
86 * First key is the namespace, second is the DB key, value arbitrary
90 public function setArray( $array ) {
95 * Returns true if no pages have been added, false otherwise.
99 public function isEmpty() {
100 return $this->getSize() == 0;
104 * Returns the size of the batch.
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 $res = $this->doQuery();
132 $this->doGenderQuery();
133 $ids = $this->addResultToCache( $cache, $res );
139 * Add a ResultWrapper containing IDs and titles to a LinkCache object.
140 * As normal, titles will go into the static Title cache field.
141 * This function *also* stores extra fields of the title used for link
142 * parsing to avoid extra DB queries.
144 * @param LinkCache $cache
145 * @param ResultWrapper $res
146 * @return array Array of remaining titles
148 public function addResultToCache( $cache, $res ) {
153 // For each returned entry, add it to the list of good links, and remove it from $remaining
156 $remaining = $this->data
;
157 foreach ( $res as $row ) {
158 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
159 $cache->addGoodLinkObjFromRow( $title, $row );
160 $ids[$title->getPrefixedDBkey()] = $row->page_id
;
161 unset( $remaining[$row->page_namespace
][$row->page_title
] );
164 // The remaining links in $data are bad links, register them as such
165 foreach ( $remaining as $ns => $dbkeys ) {
166 foreach ( $dbkeys as $dbkey => $unused ) {
167 $title = Title
::makeTitle( $ns, $dbkey );
168 $cache->addBadLinkObj( $title );
169 $ids[$title->getPrefixedDBkey()] = 0;
177 * Perform the existence test query, return a ResultWrapper with page_id fields
178 * @return bool|ResultWrapper
180 public function doQuery() {
181 global $wgContentHandlerUseDB;
183 if ( $this->isEmpty() ) {
187 // This is similar to LinkHolderArray::replaceInternal
188 $dbr = wfGetDB( DB_SLAVE
);
190 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_len',
191 'page_is_redirect', 'page_latest' );
193 if ( $wgContentHandlerUseDB ) {
194 $fields[] = 'page_content_model';
197 $conds = $this->constructSet( 'page', $dbr );
200 $caller = __METHOD__
;
201 if ( strval( $this->caller
) !== '' ) {
202 $caller .= " (for {$this->caller})";
204 $res = $dbr->select( $table, $fields, $conds, $caller );
210 * Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch
212 * @return bool Whether the query was successful
214 public function doGenderQuery() {
215 if ( $this->isEmpty() ) {
220 if ( !$wgContLang->needsGenderDistinction() ) {
224 $genderCache = GenderCache
::singleton();
225 $genderCache->doLinkBatch( $this->data
, $this->caller
);
231 * Construct a WHERE clause which will match all the given titles.
233 * @param string $prefix The appropriate table's field name prefix ('page', 'pl', etc)
234 * @param IDatabase $db DatabaseBase object to use
235 * @return string|bool String with SQL where clause fragment, or false if no items.
237 public function constructSet( $prefix, $db ) {
238 return $db->makeWhereFrom2d( $this->data
, "{$prefix}_namespace", "{$prefix}_title" );