fe97d588ed4544bf1f6137461a2c433e335ea4d2
[lhc/web/wiklou.git] / includes / LinkBatch.php
1 <?php
2
3 /**
4 * Class representing a list of titles
5 * The execute() method checks them all for existence and adds them to a LinkCache object
6 +
7 * @package MediaWiki
8 * @subpackage Cache
9 */
10 class LinkBatch {
11 /**
12 * 2-d array, first index namespace, second index dbkey, value arbitrary
13 */
14 var $data = array();
15
16 function __construct( $arr = array() ) {
17 foreach( $arr as $item ) {
18 $this->addObj( $item );
19 }
20 }
21
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" );
27 }
28 }
29
30 function add( $ns, $dbkey ) {
31 if ( $ns < 0 ) {
32 return;
33 }
34 if ( !array_key_exists( $ns, $this->data ) ) {
35 $this->data[$ns] = array();
36 }
37
38 $this->data[$ns][$dbkey] = 1;
39 }
40
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
44 */
45 function setArray( $array ) {
46 $this->data = $array;
47 }
48
49 /**
50 * Returns true if no pages have been added, false otherwise.
51 */
52 function isEmpty() {
53 return ($this->getSize() == 0);
54 }
55
56 /**
57 * Returns the size of the batch.
58 */
59 function getSize() {
60 return count( $this->data );
61 }
62
63 /**
64 * Do the query and add the results to the LinkCache object
65 * Return an array mapping PDBK to ID
66 */
67 function execute() {
68 $linkCache =& LinkCache::singleton();
69 return $this->executeInto( $linkCache );
70 }
71
72 /**
73 * Do the query and add the results to a given LinkCache object
74 * Return an array mapping PDBK to ID
75 */
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();
84 }
85
86 // For each returned entry, add it to the list of good links, and remove it from $remaining
87
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] );
95 }
96 $res->free();
97
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;
104 }
105 }
106 wfProfileOut( $fname );
107 return $ids;
108 }
109
110 /**
111 * Perform the existence test query, return a ResultWrapper with page_id fields
112 */
113 function doQuery() {
114 $fname = 'LinkBatch::doQuery';
115
116 if ( $this->isEmpty() ) {
117 return false;
118 }
119 wfProfileIn( $fname );
120
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;
129 }
130 $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE $set";
131
132 // Do query
133 $res = new ResultWrapper( $dbr, $dbr->query( $sql, $fname ) );
134 wfProfileOut( $fname );
135 return $res;
136 }
137
138 /**
139 * Construct a WHERE clause which will match all the given titles.
140 * Give the appropriate table's field name prefix ('page', 'pl', etc).
141 *
142 * @param $prefix String: ??
143 * @return string
144 * @public
145 */
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;
153 }
154
155 if ( $first ) {
156 $first = false;
157 } else {
158 $sql .= ' OR ';
159 }
160 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
161
162 $firstTitle = true;
163 foreach( $dbkeys as $dbkey => $unused ) {
164 if ( $firstTitle ) {
165 $firstTitle = false;
166 } else {
167 $sql .= ',';
168 }
169 $sql .= $db->addQuotes( $dbkey );
170 }
171
172 $sql .= '))';
173 }
174 if ( $first && $firstTitle ) {
175 # No titles added
176 return false;
177 } else {
178 return $sql;
179 }
180 }
181 }
182
183 ?>