4 * Class representing a list of titles
5 * The execute() method checks them all for existence and adds them to a LinkCache object
11 * 2-d array, first index namespace, second index dbkey, value arbitrary
15 function __construct( $arr = array() ) {
16 foreach( $arr as $item ) {
17 $this->addObj( $item );
21 function addObj( $title ) {
22 if ( is_object( $title ) ) {
23 $this->add( $title->getNamespace(), $title->getDBkey() );
25 wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
29 function add( $ns, $dbkey ) {
33 if ( !array_key_exists( $ns, $this->data
) ) {
34 $this->data
[$ns] = array();
37 $this->data
[$ns][str_replace( ' ', '_', $dbkey )] = 1;
41 * Set the link list to a given 2-d array
42 * First key is the namespace, second is the DB key, value arbitrary
44 function setArray( $array ) {
49 * Returns true if no pages have been added, false otherwise.
52 return ($this->getSize() == 0);
56 * Returns the size of the batch.
59 return count( $this->data
);
63 * Do the query and add the results to the LinkCache object
64 * Return an array mapping PDBK to ID
67 $linkCache =& LinkCache
::singleton();
68 return $this->executeInto( $linkCache );
72 * Do the query and add the results to a given LinkCache object
73 * Return an array mapping PDBK to ID
75 function executeInto( &$cache ) {
76 wfProfileIn( __METHOD__
);
77 $res = $this->doQuery();
78 $ids = $this->addResultToCache( $cache, $res );
79 wfProfileOut( __METHOD__
);
84 * Add a ResultWrapper containing IDs and titles to a LinkCache object
86 function addResultToCache( $cache, $res ) {
91 // For each returned entry, add it to the list of good links, and remove it from $remaining
94 $remaining = $this->data
;
95 while ( $row = $res->fetchObject() ) {
96 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
97 $cache->addGoodLinkObj( $row->page_id
, $title );
98 $ids[$title->getPrefixedDBkey()] = $row->page_id
;
99 unset( $remaining[$row->page_namespace
][$row->page_title
] );
102 // The remaining links in $data are bad links, register them as such
103 foreach ( $remaining as $ns => $dbkeys ) {
104 foreach ( $dbkeys as $dbkey => $unused ) {
105 $title = Title
::makeTitle( $ns, $dbkey );
106 $cache->addBadLinkObj( $title );
107 $ids[$title->getPrefixedDBkey()] = 0;
114 * Perform the existence test query, return a ResultWrapper with page_id fields
117 if ( $this->isEmpty() ) {
120 wfProfileIn( __METHOD__
);
123 // This is very similar to Parser::replaceLinkHolders
124 $dbr = wfGetDB( DB_SLAVE
);
125 $page = $dbr->tableName( 'page' );
126 $set = $this->constructSet( 'page', $dbr );
127 if ( $set === false ) {
128 wfProfileOut( __METHOD__
);
131 $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE $set";
134 $res = new ResultWrapper( $dbr, $dbr->query( $sql, __METHOD__
) );
135 wfProfileOut( __METHOD__
);
140 * Construct a WHERE clause which will match all the given titles.
142 * @param string $prefix the appropriate table's field name prefix ('page', 'pl', etc)
146 function constructSet( $prefix, &$db ) {
150 foreach ( $this->data
as $ns => $dbkeys ) {
151 if ( !count( $dbkeys ) ) {
161 if (count($dbkeys)==1) { // avoid multiple-reference syntax if simple equality can be used
162 $singleKey = array_keys($dbkeys);
163 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title=".
164 $db->addQuotes($singleKey[0]).
167 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
170 foreach( $dbkeys as $dbkey => $unused ) {
176 $sql .= $db->addQuotes( $dbkey );
181 if ( $first && $firstTitle ) {