4 * Class representing a list of titles
5 * The execute() method checks them all for existence and adds them to a LinkCache object
12 * 2-d array, first index namespace, second index dbkey, value arbitrary
16 function LinkBatch( $arr = array() ) {
17 foreach( $arr as $item ) {
18 $this->addObj( $item );
22 function addObj( $title ) {
23 if ( is_object( $title ) ) {
24 $this->add( $title->getNamespace(), $title->getDBkey() );
26 wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
30 function add( $ns, $dbkey ) {
34 if ( !array_key_exists( $ns, $this->data
) ) {
35 $this->data
[$ns] = array();
38 $this->data
[$ns][$dbkey] = 1;
42 * Set the link list to a given 2-d array
43 * First key is the namespace, second is the DB key, value arbitrary
45 function setArray( $array ) {
50 * Do the query and add the results to a LinkCache object
51 * Return an array mapping PDBK to ID
53 function execute( &$cache ) {
54 $fname = 'LinkBatch::execute';
55 wfProfileIn( $fname );
57 $res = $this->doQuery();
59 wfProfileOut( $fname );
63 // For each returned entry, add it to the list of good links, and remove it from $remaining
66 $remaining = $this->data
;
67 while ( $row = $res->fetchObject() ) {
68 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
69 $cache->addGoodLinkObj( $row->page_id
, $title );
70 $ids[$title->getPrefixedDBkey()] = $row->page_id
;
71 unset( $remaining[$row->page_namespace
][$row->page_title
] );
75 // The remaining links in $data are bad links, register them as such
76 foreach ( $remaining as $ns => $dbkeys ) {
77 foreach ( $dbkeys as $dbkey => $nothing ) {
78 $title = Title
::makeTitle( $ns, $dbkey );
79 $cache->addBadLinkObj( $title );
80 $ids[$title->getPrefixedDBkey()] = 0;
83 wfProfileOut( $fname );
88 * Perform the existence test query, return a ResultWrapper with page_id fields
91 $fname = 'LinkBatch::execute';
92 $namespaces = array();
94 if ( !count( $this->data
) ) {
97 wfProfileIn( $fname );
100 // This is very similar to Parser::replaceLinkHolders
101 $dbr =& wfGetDB( DB_SLAVE
);
102 $page = $dbr->tableName( 'page' );
103 $set = $this->constructSet( 'page', $dbr );
104 if ( $set === false ) {
107 $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE $set";
110 $res = new ResultWrapper( $dbr, $dbr->query( $sql, $fname ) );
111 wfProfileOut( $fname );
116 * Construct a WHERE clause which will match all the given titles.
117 * Give the appropriate table's field name prefix ('page', 'pl', etc).
119 * @param string $prefix
123 function constructSet( $prefix, &$db ) {
127 foreach ( $this->data
as $ns => $dbkeys ) {
128 if ( !count( $dbkeys ) ) {
137 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
140 foreach( $dbkeys as $dbkey => $nothing ) {
146 $sql .= $db->addQuotes( $dbkey );
151 if ( $first && $firstTitle ) {