gave LinkBatch its own file
[mediawiki.git] / includes / LinkBatch.php
blob7586cbc2f998555828f2a8b7475d86e394d0519b
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 * 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 );
56 // Do query
57 $res = $this->doQuery();
58 if ( !$res ) {
59 wfProfileOut( $fname );
60 return array();
63 // For each returned entry, add it to the list of good links, and remove it from $remaining
65 $ids = array();
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] );
73 $res->free();
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 );
84 return $ids;
87 /**
88 * Perform the existence test query, return a ResultWrapper with page_id fields
90 function doQuery() {
91 $fname = 'LinkBatch::execute';
92 $namespaces = array();
94 if ( !count( $this->data ) ) {
95 return false;
97 wfProfileIn( $fname );
99 // Construct query
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 ) {
105 return false;
107 $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE $set";
109 // Do query
110 $res = new ResultWrapper( $dbr, $dbr->query( $sql, $fname ) );
111 wfProfileOut( $fname );
112 return $res;
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
120 * @return string
121 * @access public
123 function constructSet( $prefix, &$db ) {
124 $first = true;
125 $firstTitle = true;
126 $sql = '';
127 foreach ( $this->data as $ns => $dbkeys ) {
128 if ( !count( $dbkeys ) ) {
129 continue;
132 if ( $first ) {
133 $first = false;
134 } else {
135 $sql .= ' OR ';
137 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
139 $firstTitle = true;
140 foreach( $dbkeys as $dbkey => $nothing ) {
141 if ( $firstTitle ) {
142 $firstTitle = false;
143 } else {
144 $sql .= ',';
146 $sql .= $db->addQuotes( $dbkey );
149 $sql .= '))';
151 if ( $first && $firstTitle ) {
152 # No titles added
153 return false;
154 } else {
155 return $sql;