2006-01-04 23:53:51 +00:00
|
|
|
<?php
|
2012-05-08 12:51:21 +00:00
|
|
|
/**
|
|
|
|
|
* Batch query to determine page existence.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Cache
|
|
|
|
|
*/
|
2016-04-20 08:29:21 +00:00
|
|
|
use MediaWiki\Linker\LinkTarget;
|
2016-05-23 05:55:38 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2017-02-19 05:03:13 +00:00
|
|
|
use Wikimedia\Rdbms\ResultWrapper;
|
2017-02-10 18:09:05 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2006-01-04 23:53:51 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class representing a list of titles
|
|
|
|
|
* The execute() method checks them all for existence and adds them to a LinkCache object
|
2007-05-06 22:18:17 +00:00
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Cache
|
2006-01-04 23:53:51 +00:00
|
|
|
*/
|
|
|
|
|
class LinkBatch {
|
|
|
|
|
/**
|
|
|
|
|
* 2-d array, first index namespace, second index dbkey, value arbitrary
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
public $data = [];
|
2006-01-04 23:53:51 +00:00
|
|
|
|
2010-07-08 08:11:14 +00:00
|
|
|
/**
|
|
|
|
|
* For debugging which method is using this class.
|
|
|
|
|
*/
|
|
|
|
|
protected $caller;
|
|
|
|
|
|
2016-09-09 05:02:39 +00:00
|
|
|
/**
|
|
|
|
|
* @param LinkTarget[] $arr Initial items to be added to the batch
|
|
|
|
|
*/
|
|
|
|
|
public function __construct( $arr = [] ) {
|
2013-04-20 17:18:13 +00:00
|
|
|
foreach ( $arr as $item ) {
|
2006-01-04 23:53:51 +00:00
|
|
|
$this->addObj( $item );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-08 08:11:14 +00:00
|
|
|
/**
|
|
|
|
|
* Use ->setCaller( __METHOD__ ) to indicate which code is using this
|
|
|
|
|
* class. Only used in debugging output.
|
|
|
|
|
* @since 1.17
|
2011-05-28 18:58:51 +00:00
|
|
|
*
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param string $caller
|
2010-07-08 08:11:14 +00:00
|
|
|
*/
|
|
|
|
|
public function setCaller( $caller ) {
|
|
|
|
|
$this->caller = $caller;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-05 02:52:04 +00:00
|
|
|
/**
|
2016-02-03 15:17:38 +00:00
|
|
|
* @param LinkTarget $linkTarget
|
2011-01-05 02:52:04 +00:00
|
|
|
*/
|
2016-02-29 23:31:48 +00:00
|
|
|
public function addObj( $linkTarget ) {
|
|
|
|
|
if ( is_object( $linkTarget ) ) {
|
|
|
|
|
$this->add( $linkTarget->getNamespace(), $linkTarget->getDBkey() );
|
|
|
|
|
} else {
|
|
|
|
|
wfDebug( "Warning: LinkBatch::addObj got invalid LinkTarget object\n" );
|
|
|
|
|
}
|
2006-01-04 23:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-11 22:05:54 +00:00
|
|
|
/**
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param int $ns
|
|
|
|
|
* @param string $dbkey
|
2012-03-11 22:05:54 +00:00
|
|
|
*/
|
2008-04-09 13:02:34 +00:00
|
|
|
public function add( $ns, $dbkey ) {
|
2016-06-06 06:04:20 +00:00
|
|
|
if ( $ns < 0 || $dbkey === '' ) {
|
|
|
|
|
return; // T137083
|
2006-01-04 23:53:51 +00:00
|
|
|
}
|
|
|
|
|
if ( !array_key_exists( $ns, $this->data ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->data[$ns] = [];
|
2006-01-04 23:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
2015-07-06 09:17:47 +00:00
|
|
|
$this->data[$ns][strtr( $dbkey, ' ', '_' )] = 1;
|
2006-01-04 23:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the link list to a given 2-d array
|
|
|
|
|
* First key is the namespace, second is the DB key, value arbitrary
|
2011-05-29 14:24:27 +00:00
|
|
|
*
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param array $array
|
2006-01-04 23:53:51 +00:00
|
|
|
*/
|
2008-04-09 13:02:34 +00:00
|
|
|
public function setArray( $array ) {
|
2006-01-04 23:53:51 +00:00
|
|
|
$this->data = $array;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2006-04-22 02:12:59 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if no pages have been added, false otherwise.
|
2011-05-29 14:24:27 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2006-04-22 02:12:59 +00:00
|
|
|
*/
|
2008-04-09 13:02:34 +00:00
|
|
|
public function isEmpty() {
|
2013-05-17 14:47:00 +00:00
|
|
|
return $this->getSize() == 0;
|
2006-04-22 02:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the size of the batch.
|
2011-05-29 14:24:27 +00:00
|
|
|
*
|
|
|
|
|
* @return int
|
2006-04-22 02:12:59 +00:00
|
|
|
*/
|
2008-04-09 13:02:34 +00:00
|
|
|
public function getSize() {
|
2006-04-22 02:12:59 +00:00
|
|
|
return count( $this->data );
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-05 02:05:53 +00:00
|
|
|
/**
|
|
|
|
|
* Do the query and add the results to the LinkCache object
|
2011-10-06 13:22:53 +00:00
|
|
|
*
|
2014-04-18 23:19:46 +00:00
|
|
|
* @return array Mapping PDBK to ID
|
2006-01-05 02:05:53 +00:00
|
|
|
*/
|
2011-05-29 14:24:27 +00:00
|
|
|
public function execute() {
|
2016-05-23 05:55:38 +00:00
|
|
|
$linkCache = MediaWikiServices::getInstance()->getLinkCache();
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2011-05-29 14:24:27 +00:00
|
|
|
return $this->executeInto( $linkCache );
|
|
|
|
|
}
|
2006-01-04 23:53:51 +00:00
|
|
|
|
|
|
|
|
/**
|
2006-01-05 02:05:53 +00:00
|
|
|
* Do the query and add the results to a given LinkCache object
|
2006-01-04 23:53:51 +00:00
|
|
|
* Return an array mapping PDBK to ID
|
2011-10-06 13:22:53 +00:00
|
|
|
*
|
2017-08-11 00:23:16 +00:00
|
|
|
* @param LinkCache &$cache
|
2014-04-18 23:19:46 +00:00
|
|
|
* @return array Remaining IDs
|
2006-01-04 23:53:51 +00:00
|
|
|
*/
|
2008-04-09 13:02:34 +00:00
|
|
|
protected function executeInto( &$cache ) {
|
2006-01-04 23:53:51 +00:00
|
|
|
$res = $this->doQuery();
|
2011-02-12 20:40:40 +00:00
|
|
|
$this->doGenderQuery();
|
2011-12-08 08:51:21 +00:00
|
|
|
$ids = $this->addResultToCache( $cache, $res );
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2008-02-20 08:53:12 +00:00
|
|
|
return $ids;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-04-09 05:34:54 +00:00
|
|
|
* Add a ResultWrapper containing IDs and titles to a LinkCache object.
|
2008-04-14 07:45:50 +00:00
|
|
|
* As normal, titles will go into the static Title cache field.
|
|
|
|
|
* This function *also* stores extra fields of the title used for link
|
2008-04-09 05:34:54 +00:00
|
|
|
* parsing to avoid extra DB queries.
|
2011-05-29 14:24:27 +00:00
|
|
|
*
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param LinkCache $cache
|
|
|
|
|
* @param ResultWrapper $res
|
|
|
|
|
* @return array Array of remaining titles
|
2008-02-20 08:53:12 +00:00
|
|
|
*/
|
2008-04-09 13:02:34 +00:00
|
|
|
public function addResultToCache( $cache, $res ) {
|
2006-01-04 23:53:51 +00:00
|
|
|
if ( !$res ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2006-01-04 23:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-23 05:55:38 +00:00
|
|
|
$titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter();
|
2006-01-04 23:53:51 +00:00
|
|
|
// For each returned entry, add it to the list of good links, and remove it from $remaining
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$ids = [];
|
2006-01-04 23:53:51 +00:00
|
|
|
$remaining = $this->data;
|
2010-10-13 23:11:40 +00:00
|
|
|
foreach ( $res as $row ) {
|
2016-05-23 05:55:38 +00:00
|
|
|
$title = new TitleValue( (int)$row->page_namespace, $row->page_title );
|
2011-09-20 15:19:18 +00:00
|
|
|
$cache->addGoodLinkObjFromRow( $title, $row );
|
2016-05-23 05:55:38 +00:00
|
|
|
$pdbk = $titleFormatter->getPrefixedDBkey( $title );
|
|
|
|
|
$ids[$pdbk] = $row->page_id;
|
2006-01-04 23:53:51 +00:00
|
|
|
unset( $remaining[$row->page_namespace][$row->page_title] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The remaining links in $data are bad links, register them as such
|
|
|
|
|
foreach ( $remaining as $ns => $dbkeys ) {
|
2006-11-25 17:11:58 +00:00
|
|
|
foreach ( $dbkeys as $dbkey => $unused ) {
|
2016-06-01 20:12:59 +00:00
|
|
|
$title = new TitleValue( (int)$ns, (string)$dbkey );
|
2006-01-04 23:53:51 +00:00
|
|
|
$cache->addBadLinkObj( $title );
|
2016-05-23 05:55:38 +00:00
|
|
|
$pdbk = $titleFormatter->getPrefixedDBkey( $title );
|
|
|
|
|
$ids[$pdbk] = 0;
|
2006-01-04 23:53:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2006-01-04 23:53:51 +00:00
|
|
|
return $ids;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Perform the existence test query, return a ResultWrapper with page_id fields
|
2014-04-18 23:19:46 +00:00
|
|
|
* @return bool|ResultWrapper
|
2006-01-04 23:53:51 +00:00
|
|
|
*/
|
2008-04-09 13:02:34 +00:00
|
|
|
public function doQuery() {
|
2006-04-22 02:12:59 +00:00
|
|
|
if ( $this->isEmpty() ) {
|
2006-01-04 23:53:51 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-10 08:14:39 +00:00
|
|
|
// This is similar to LinkHolderArray::replaceInternal
|
2016-09-05 19:55:19 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2010-07-10 08:14:39 +00:00
|
|
|
$table = 'page';
|
2016-05-13 07:00:39 +00:00
|
|
|
$fields = array_merge(
|
|
|
|
|
LinkCache::getSelectFields(),
|
|
|
|
|
[ 'page_namespace', 'page_title' ]
|
|
|
|
|
);
|
2014-08-20 21:46:11 +00:00
|
|
|
|
2010-07-10 08:14:39 +00:00
|
|
|
$conds = $this->constructSet( 'page', $dbr );
|
2006-01-04 23:53:51 +00:00
|
|
|
|
|
|
|
|
// Do query
|
2010-07-10 07:36:09 +00:00
|
|
|
$caller = __METHOD__;
|
|
|
|
|
if ( strval( $this->caller ) !== '' ) {
|
|
|
|
|
$caller .= " (for {$this->caller})";
|
|
|
|
|
}
|
2010-07-10 08:14:39 +00:00
|
|
|
$res = $dbr->select( $table, $fields, $conds, $caller );
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2006-01-04 23:53:51 +00:00
|
|
|
return $res;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-06 13:22:53 +00:00
|
|
|
/**
|
|
|
|
|
* Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch
|
|
|
|
|
*
|
2014-04-18 23:19:46 +00:00
|
|
|
* @return bool Whether the query was successful
|
2011-10-06 13:22:53 +00:00
|
|
|
*/
|
2011-02-12 20:40:40 +00:00
|
|
|
public function doGenderQuery() {
|
|
|
|
|
if ( $this->isEmpty() ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
global $wgContLang;
|
|
|
|
|
if ( !$wgContLang->needsGenderDistinction() ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-23 05:55:38 +00:00
|
|
|
$genderCache = MediaWikiServices::getInstance()->getGenderCache();
|
2012-04-21 08:32:09 +00:00
|
|
|
$genderCache->doLinkBatch( $this->data, $this->caller );
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2011-10-06 13:22:53 +00:00
|
|
|
return true;
|
2011-02-12 20:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-04 23:53:51 +00:00
|
|
|
/**
|
|
|
|
|
* Construct a WHERE clause which will match all the given titles.
|
|
|
|
|
*
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param string $prefix The appropriate table's field name prefix ('page', 'pl', etc)
|
2016-09-26 22:40:07 +00:00
|
|
|
* @param IDatabase $db DB object to use
|
2014-04-18 23:19:46 +00:00
|
|
|
* @return string|bool String with SQL where clause fragment, or false if no items.
|
2006-01-04 23:53:51 +00:00
|
|
|
*/
|
2010-04-16 01:40:05 +00:00
|
|
|
public function constructSet( $prefix, $db ) {
|
2010-07-10 08:14:39 +00:00
|
|
|
return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );
|
2006-01-04 23:53:51 +00:00
|
|
|
}
|
|
|
|
|
}
|