Yet another wfMsg, which includes hack to get inline wikitext blocks
[mediawiki.git] / includes / LinkBatch.php
blob43282abe00bcaa99331519862b0756731eb64106
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 the LinkCache object
51 * Return an array mapping PDBK to ID
53 function execute() {
54 $linkCache =& LinkCache::singleton();
55 $this->executeInto( $linkCache );
58 /**
59 * Do the query and add the results to a given LinkCache object
60 * Return an array mapping PDBK to ID
62 function executeInto( &$cache ) {
63 $fname = 'LinkBatch::executeInto';
64 wfProfileIn( $fname );
65 // Do query
66 $res = $this->doQuery();
67 if ( !$res ) {
68 wfProfileOut( $fname );
69 return array();
72 // For each returned entry, add it to the list of good links, and remove it from $remaining
74 $ids = array();
75 $remaining = $this->data;
76 while ( $row = $res->fetchObject() ) {
77 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
78 $cache->addGoodLinkObj( $row->page_id, $title );
79 $ids[$title->getPrefixedDBkey()] = $row->page_id;
80 unset( $remaining[$row->page_namespace][$row->page_title] );
82 $res->free();
84 // The remaining links in $data are bad links, register them as such
85 foreach ( $remaining as $ns => $dbkeys ) {
86 foreach ( $dbkeys as $dbkey => $nothing ) {
87 $title = Title::makeTitle( $ns, $dbkey );
88 $cache->addBadLinkObj( $title );
89 $ids[$title->getPrefixedDBkey()] = 0;
92 wfProfileOut( $fname );
93 return $ids;
96 /**
97 * Perform the existence test query, return a ResultWrapper with page_id fields
99 function doQuery() {
100 $fname = 'LinkBatch::doQuery';
101 $namespaces = array();
103 if ( !count( $this->data ) ) {
104 return false;
106 wfProfileIn( $fname );
108 // Construct query
109 // This is very similar to Parser::replaceLinkHolders
110 $dbr =& wfGetDB( DB_SLAVE );
111 $page = $dbr->tableName( 'page' );
112 $set = $this->constructSet( 'page', $dbr );
113 if ( $set === false ) {
114 wfProfileOut( $fname );
115 return false;
117 $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE $set";
119 // Do query
120 $res = new ResultWrapper( $dbr, $dbr->query( $sql, $fname ) );
121 wfProfileOut( $fname );
122 return $res;
126 * Construct a WHERE clause which will match all the given titles.
127 * Give the appropriate table's field name prefix ('page', 'pl', etc).
129 * @param string $prefix
130 * @return string
131 * @access public
133 function constructSet( $prefix, &$db ) {
134 $first = true;
135 $firstTitle = true;
136 $sql = '';
137 foreach ( $this->data as $ns => $dbkeys ) {
138 if ( !count( $dbkeys ) ) {
139 continue;
142 if ( $first ) {
143 $first = false;
144 } else {
145 $sql .= ' OR ';
147 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
149 $firstTitle = true;
150 foreach( $dbkeys as $dbkey => $nothing ) {
151 if ( $firstTitle ) {
152 $firstTitle = false;
153 } else {
154 $sql .= ',';
156 $sql .= $db->addQuotes( $dbkey );
159 $sql .= '))';
161 if ( $first && $firstTitle ) {
162 # No titles added
163 return false;
164 } else {
165 return $sql;