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 = 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();
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 $cache LinkCache
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__
);
140 * Add a ResultWrapper containing IDs and titles to a LinkCache object.
141 * As normal, titles will go into the static Title cache field.
142 * This function *also* stores extra fields of the title used for link
143 * parsing to avoid extra DB queries.
145 * @param $cache LinkCache
147 * @return Array of remaining titles
149 public function addResultToCache( $cache, $res ) {
154 // For each returned entry, add it to the list of good links, and remove it from $remaining
157 $remaining = $this->data
;
158 foreach ( $res as $row ) {
159 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
160 $cache->addGoodLinkObjFromRow( $title, $row );
161 $ids[$title->getPrefixedDBkey()] = $row->page_id
;
162 unset( $remaining[$row->page_namespace
][$row->page_title
] );
165 // The remaining links in $data are bad links, register them as such
166 foreach ( $remaining as $ns => $dbkeys ) {
167 foreach ( $dbkeys as $dbkey => $unused ) {
168 $title = Title
::makeTitle( $ns, $dbkey );
169 $cache->addBadLinkObj( $title );
170 $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 if ( $this->isEmpty() ) {
184 wfProfileIn( __METHOD__
);
186 // This is similar to LinkHolderArray::replaceInternal
187 $dbr = wfGetDB( DB_SLAVE
);
189 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_len',
190 'page_is_redirect', 'page_latest' );
191 $conds = $this->constructSet( 'page', $dbr );
194 $caller = __METHOD__
;
195 if ( strval( $this->caller
) !== '' ) {
196 $caller .= " (for {$this->caller})";
198 $res = $dbr->select( $table, $fields, $conds, $caller );
199 wfProfileOut( __METHOD__
);
204 * Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch
206 * @return bool whether the query was successful
208 public function doGenderQuery() {
209 if ( $this->isEmpty() ) {
214 if ( !$wgContLang->needsGenderDistinction() ) {
218 $genderCache = GenderCache
::singleton();
219 $genderCache->doLinkBatch( $this->data
, $this->caller
);
224 * Construct a WHERE clause which will match all the given titles.
226 * @param string $prefix the appropriate table's field name prefix ('page', 'pl', etc)
227 * @param $db DatabaseBase object to use
228 * @return mixed string with SQL where clause fragment, or false if no items.
230 public function constructSet( $prefix, $db ) {
231 return $db->makeWhereFrom2d( $this->data
, "{$prefix}_namespace", "{$prefix}_title" );