/**
* Class representing a list of titles
* The execute() method checks them all for existence and adds them to a LinkCache object
- +
- * @package MediaWiki
- * @subpackage Cache
+ *
+ * @addtogroup Cache
*/
class LinkBatch {
/**
*/
var $data = array();
- function LinkBatch( $arr = array() ) {
+ function __construct( $arr = array() ) {
foreach( $arr as $item ) {
$this->addObj( $item );
}
$this->data[$ns] = array();
}
- $this->data[$ns][$dbkey] = 1;
+ $this->data[$ns][str_replace( ' ', '_', $dbkey )] = 1;
}
/**
function setArray( $array ) {
$this->data = $array;
}
-
+
+ /**
+ * Returns true if no pages have been added, false otherwise.
+ */
+ function isEmpty() {
+ return ($this->getSize() == 0);
+ }
+
+ /**
+ * Returns the size of the batch.
+ */
+ function getSize() {
+ return count( $this->data );
+ }
+
/**
* Do the query and add the results to the LinkCache object
* Return an array mapping PDBK to ID
*/
function execute() {
$linkCache =& LinkCache::singleton();
- $this->executeInto( $linkCache );
+ return $this->executeInto( $linkCache );
}
/**
* Return an array mapping PDBK to ID
*/
function executeInto( &$cache ) {
- $fname = 'LinkBatch::execute';
+ $fname = 'LinkBatch::executeInto';
wfProfileIn( $fname );
// Do query
$res = $this->doQuery();
// The remaining links in $data are bad links, register them as such
foreach ( $remaining as $ns => $dbkeys ) {
- foreach ( $dbkeys as $dbkey => $nothing ) {
+ foreach ( $dbkeys as $dbkey => $unused ) {
$title = Title::makeTitle( $ns, $dbkey );
$cache->addBadLinkObj( $title );
$ids[$title->getPrefixedDBkey()] = 0;
* Perform the existence test query, return a ResultWrapper with page_id fields
*/
function doQuery() {
- $fname = 'LinkBatch::execute';
- $namespaces = array();
+ $fname = 'LinkBatch::doQuery';
- if ( !count( $this->data ) ) {
+ if ( $this->isEmpty() ) {
return false;
}
wfProfileIn( $fname );
// Construct query
// This is very similar to Parser::replaceLinkHolders
- $dbr =& wfGetDB( DB_SLAVE );
+ $dbr = wfGetDB( DB_SLAVE );
$page = $dbr->tableName( 'page' );
$set = $this->constructSet( 'page', $dbr );
if ( $set === false ) {
+ wfProfileOut( $fname );
return false;
}
$sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE $set";
* Construct a WHERE clause which will match all the given titles.
* Give the appropriate table's field name prefix ('page', 'pl', etc).
*
- * @param string $prefix
+ * @param $prefix String: ??
* @return string
- * @access public
+ * @public
*/
function constructSet( $prefix, &$db ) {
$first = true;
} else {
$sql .= ' OR ';
}
- $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
-
- $firstTitle = true;
- foreach( $dbkeys as $dbkey => $nothing ) {
- if ( $firstTitle ) {
- $firstTitle = false;
- } else {
- $sql .= ',';
+
+ if (count($dbkeys)==1) { // avoid multiple-reference syntax if simple equality can be used
+ $singleKey = array_keys($dbkeys);
+ $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title=".
+ $db->addQuotes($singleKey[0]).
+ ")";
+ } else {
+ $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
+
+ $firstTitle = true;
+ foreach( $dbkeys as $dbkey => $unused ) {
+ if ( $firstTitle ) {
+ $firstTitle = false;
+ } else {
+ $sql .= ',';
+ }
+ $sql .= $db->addQuotes( $dbkey );
}
- $sql .= $db->addQuotes( $dbkey );
+ $sql .= '))';
}
-
- $sql .= '))';
}
if ( $first && $firstTitle ) {
# No titles added
}
}
-?>
+