Revert to r14165 . Did too many changes, didnt even run parserTests (i am bad)
[mediawiki.git] / includes / LinkBatch.php
blobe0f0f6fd1055194b28090dc54d346b4b31d24915
1 <?php
3 /**
4 * Class representing a list of titles
5 * The execute() method checks them all for existence and adds them to a LinkCache object
7 * @package MediaWiki
8 * @subpackage Cache
9 */
10 class LinkBatch {
11 /**
12 * 2-d array, first index namespace, second index dbkey, value arbitrary
14 var $data = array();
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() );
25 } else {
26 wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
30 function add( $ns, $dbkey ) {
31 if ( $ns < 0 ) {
32 return;
34 if ( !array_key_exists( $ns, $this->data ) ) {
35 $this->data[$ns] = array();
38 $this->data[$ns][$dbkey] = 1;
41 /**
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 ) {
46 $this->data = $array;
49 /**
50 * Returns true if no pages have been added, false otherwise.
52 function isEmpty() {
53 return ($this->getSize() == 0);
56 /**
57 * Returns the size of the batch.
59 function getSize() {
60 return count( $this->data );
63 /**
64 * Do the query and add the results to the LinkCache object
65 * Return an array mapping PDBK to ID
67 function execute() {
68 $linkCache =& LinkCache::singleton();
69 $this->executeInto( $linkCache );
72 /**
73 * Do the query and add the results to a given LinkCache object
74 * Return an array mapping PDBK to ID
76 function executeInto( &$cache ) {
77 $fname = 'LinkBatch::executeInto';
78 wfProfileIn( $fname );
79 // Do query
80 $res = $this->doQuery();
81 if ( !$res ) {
82 wfProfileOut( $fname );
83 return array();
86 // For each returned entry, add it to the list of good links, and remove it from $remaining
88 $ids = array();
89 $remaining = $this->data;
90 while ( $row = $res->fetchObject() ) {
91 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
92 $cache->addGoodLinkObj( $row->page_id, $title );
93 $ids[$title->getPrefixedDBkey()] = $row->page_id;
94 unset( $remaining[$row->page_namespace][$row->page_title] );
96 $res->free();
98 // The remaining links in $data are bad links, register them as such
99 foreach ( $remaining as $ns => $dbkeys ) {
100 foreach ( $dbkeys as $dbkey => $nothing ) {
101 $title = Title::makeTitle( $ns, $dbkey );
102 $cache->addBadLinkObj( $title );
103 $ids[$title->getPrefixedDBkey()] = 0;
106 wfProfileOut( $fname );
107 return $ids;
111 * Perform the existence test query, return a ResultWrapper with page_id fields
113 function doQuery() {
114 $fname = 'LinkBatch::doQuery';
115 $namespaces = array();
117 if ( $this->isEmpty() ) {
118 return false;
120 wfProfileIn( $fname );
122 // Construct query
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( $fname );
129 return false;
131 $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE $set";
133 // Do query
134 $res = new ResultWrapper( $dbr, $dbr->query( $sql, $fname ) );
135 wfProfileOut( $fname );
136 return $res;
140 * Construct a WHERE clause which will match all the given titles.
141 * Give the appropriate table's field name prefix ('page', 'pl', etc).
143 * @param $prefix String: ??
144 * @return string
145 * @public
147 function constructSet( $prefix, &$db ) {
148 $first = true;
149 $firstTitle = true;
150 $sql = '';
151 foreach ( $this->data as $ns => $dbkeys ) {
152 if ( !count( $dbkeys ) ) {
153 continue;
156 if ( $first ) {
157 $first = false;
158 } else {
159 $sql .= ' OR ';
161 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
163 $firstTitle = true;
164 foreach( $dbkeys as $dbkey => $nothing ) {
165 if ( $firstTitle ) {
166 $firstTitle = false;
167 } else {
168 $sql .= ',';
170 $sql .= $db->addQuotes( $dbkey );
173 $sql .= '))';
175 if ( $first && $firstTitle ) {
176 # No titles added
177 return false;
178 } else {
179 return $sql;