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.
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 $dbkey string
74 public function add( $ns, $dbkey ) {
78 if ( !array_key_exists( $ns, $this->data
) ) {
79 $this->data
[$ns] = array();
82 $this->data
[$ns][str_replace( ' ', '_', $dbkey )] = 1;
86 * Set the link list to a given 2-d array
87 * First key is the namespace, second is the DB key, value arbitrary
91 public function setArray( $array ) {
96 * Returns true if no pages have been added, false otherwise.
100 public function isEmpty() {
101 return $this->getSize() == 0;
105 * Returns the size of the batch.
109 public function getSize() {
110 return count( $this->data
);
114 * Do the query and add the results to the LinkCache object
116 * @return Array mapping PDBK to ID
118 public function execute() {
119 $linkCache = LinkCache
::singleton();
121 return $this->executeInto( $linkCache );
125 * Do the query and add the results to a given LinkCache object
126 * Return an array mapping PDBK to ID
128 * @param $cache LinkCache
129 * @return Array remaining IDs
131 protected function executeInto( &$cache ) {
132 wfProfileIn( __METHOD__
);
133 $res = $this->doQuery();
134 $this->doGenderQuery();
135 $ids = $this->addResultToCache( $cache, $res );
136 wfProfileOut( __METHOD__
);
142 * Add a ResultWrapper containing IDs and titles to a LinkCache object.
143 * As normal, titles will go into the static Title cache field.
144 * This function *also* stores extra fields of the title used for link
145 * parsing to avoid extra DB queries.
147 * @param $cache LinkCache
149 * @return Array of remaining titles
151 public function addResultToCache( $cache, $res ) {
156 // For each returned entry, add it to the list of good links, and remove it from $remaining
159 $remaining = $this->data
;
160 foreach ( $res as $row ) {
161 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
162 $cache->addGoodLinkObjFromRow( $title, $row );
163 $ids[$title->getPrefixedDBkey()] = $row->page_id
;
164 unset( $remaining[$row->page_namespace
][$row->page_title
] );
167 // The remaining links in $data are bad links, register them as such
168 foreach ( $remaining as $ns => $dbkeys ) {
169 foreach ( $dbkeys as $dbkey => $unused ) {
170 $title = Title
::makeTitle( $ns, $dbkey );
171 $cache->addBadLinkObj( $title );
172 $ids[$title->getPrefixedDBkey()] = 0;
180 * Perform the existence test query, return a ResultWrapper with page_id fields
181 * @return Bool|ResultWrapper
183 public function doQuery() {
184 if ( $this->isEmpty() ) {
187 wfProfileIn( __METHOD__
);
189 // This is similar to LinkHolderArray::replaceInternal
190 $dbr = wfGetDB( DB_SLAVE
);
192 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_len',
193 'page_is_redirect', 'page_latest' );
194 $conds = $this->constructSet( 'page', $dbr );
197 $caller = __METHOD__
;
198 if ( strval( $this->caller
) !== '' ) {
199 $caller .= " (for {$this->caller})";
201 $res = $dbr->select( $table, $fields, $conds, $caller );
202 wfProfileOut( __METHOD__
);
208 * Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch
210 * @return bool whether the query was successful
212 public function doGenderQuery() {
213 if ( $this->isEmpty() ) {
218 if ( !$wgContLang->needsGenderDistinction() ) {
222 $genderCache = GenderCache
::singleton();
223 $genderCache->doLinkBatch( $this->data
, $this->caller
);
229 * Construct a WHERE clause which will match all the given titles.
231 * @param string $prefix the appropriate table's field name prefix ('page', 'pl', etc)
232 * @param $db DatabaseBase object to use
233 * @return mixed string with SQL where clause fragment, or false if no items.
235 public function constructSet( $prefix, $db ) {
236 return $db->makeWhereFrom2d( $this->data
, "{$prefix}_namespace", "{$prefix}_title" );