* Reset the release notes
[mediawiki.git] / includes / LinkBatch.php
blob61e1c040d4fbb0935f18e3468f5a5e0e792e4efd
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 return $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 => $unused ) {
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';
116 if ( $this->isEmpty() ) {
117 return false;
119 wfProfileIn( $fname );
121 // Construct query
122 // This is very similar to Parser::replaceLinkHolders
123 $dbr =& wfGetDB( DB_SLAVE );
124 $page = $dbr->tableName( 'page' );
125 $set = $this->constructSet( 'page', $dbr );
126 if ( $set === false ) {
127 wfProfileOut( $fname );
128 return false;
130 $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE $set";
132 // Do query
133 $res = new ResultWrapper( $dbr, $dbr->query( $sql, $fname ) );
134 wfProfileOut( $fname );
135 return $res;
139 * Construct a WHERE clause which will match all the given titles.
140 * Give the appropriate table's field name prefix ('page', 'pl', etc).
142 * @param $prefix String: ??
143 * @return string
144 * @public
146 function constructSet( $prefix, &$db ) {
147 $first = true;
148 $firstTitle = true;
149 $sql = '';
150 foreach ( $this->data as $ns => $dbkeys ) {
151 if ( !count( $dbkeys ) ) {
152 continue;
155 if ( $first ) {
156 $first = false;
157 } else {
158 $sql .= ' OR ';
160 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
162 $firstTitle = true;
163 foreach( $dbkeys as $dbkey => $unused ) {
164 if ( $firstTitle ) {
165 $firstTitle = false;
166 } else {
167 $sql .= ',';
169 $sql .= $db->addQuotes( $dbkey );
172 $sql .= '))';
174 if ( $first && $firstTitle ) {
175 # No titles added
176 return false;
177 } else {
178 return $sql;