Remove ?>'s from files. They're pointless, and just asking for people to mess with...
[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 * @addtogroup Cache
8 */
9 class LinkBatch {
10 /**
11 * 2-d array, first index namespace, second index dbkey, value arbitrary
12 */
13 var $data = array();
14
15 function __construct( $arr = array() ) {
16 foreach( $arr as $item ) {
17 $this->addObj( $item );
18 }
19 }
20
21 function addObj( $title ) {
22 if ( is_object( $title ) ) {
23 $this->add( $title->getNamespace(), $title->getDBkey() );
24 } else {
25 wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
26 }
27 }
28
29 function add( $ns, $dbkey ) {
30 if ( $ns < 0 ) {
31 return;
32 }
33 if ( !array_key_exists( $ns, $this->data ) ) {
34 $this->data[$ns] = array();
35 }
36
37 $this->data[$ns][$dbkey] = 1;
38 }
39
40 /**
41 * Set the link list to a given 2-d array
42 * First key is the namespace, second is the DB key, value arbitrary
43 */
44 function setArray( $array ) {
45 $this->data = $array;
46 }
47
48 /**
49 * Returns true if no pages have been added, false otherwise.
50 */
51 function isEmpty() {
52 return ($this->getSize() == 0);
53 }
54
55 /**
56 * Returns the size of the batch.
57 */
58 function getSize() {
59 return count( $this->data );
60 }
61
62 /**
63 * Do the query and add the results to the LinkCache object
64 * Return an array mapping PDBK to ID
65 */
66 function execute() {
67 $linkCache =& LinkCache::singleton();
68 return $this->executeInto( $linkCache );
69 }
70
71 /**
72 * Do the query and add the results to a given LinkCache object
73 * Return an array mapping PDBK to ID
74 */
75 function executeInto( &$cache ) {
76 $fname = 'LinkBatch::executeInto';
77 wfProfileIn( $fname );
78 // Do query
79 $res = $this->doQuery();
80 if ( !$res ) {
81 wfProfileOut( $fname );
82 return array();
83 }
84
85 // For each returned entry, add it to the list of good links, and remove it from $remaining
86
87 $ids = array();
88 $remaining = $this->data;
89 while ( $row = $res->fetchObject() ) {
90 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
91 $cache->addGoodLinkObj( $row->page_id, $title );
92 $ids[$title->getPrefixedDBkey()] = $row->page_id;
93 unset( $remaining[$row->page_namespace][$row->page_title] );
94 }
95 $res->free();
96
97 // The remaining links in $data are bad links, register them as such
98 foreach ( $remaining as $ns => $dbkeys ) {
99 foreach ( $dbkeys as $dbkey => $unused ) {
100 $title = Title::makeTitle( $ns, $dbkey );
101 $cache->addBadLinkObj( $title );
102 $ids[$title->getPrefixedDBkey()] = 0;
103 }
104 }
105 wfProfileOut( $fname );
106 return $ids;
107 }
108
109 /**
110 * Perform the existence test query, return a ResultWrapper with page_id fields
111 */
112 function doQuery() {
113 $fname = 'LinkBatch::doQuery';
114
115 if ( $this->isEmpty() ) {
116 return false;
117 }
118 wfProfileIn( $fname );
119
120 // Construct query
121 // This is very similar to Parser::replaceLinkHolders
122 $dbr = wfGetDB( DB_SLAVE );
123 $page = $dbr->tableName( 'page' );
124 $set = $this->constructSet( 'page', $dbr );
125 if ( $set === false ) {
126 wfProfileOut( $fname );
127 return false;
128 }
129 $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE $set";
130
131 // Do query
132 $res = new ResultWrapper( $dbr, $dbr->query( $sql, $fname ) );
133 wfProfileOut( $fname );
134 return $res;
135 }
136
137 /**
138 * Construct a WHERE clause which will match all the given titles.
139 * Give the appropriate table's field name prefix ('page', 'pl', etc).
140 *
141 * @param $prefix String: ??
142 * @return string
143 * @public
144 */
145 function constructSet( $prefix, &$db ) {
146 $first = true;
147 $firstTitle = true;
148 $sql = '';
149 foreach ( $this->data as $ns => $dbkeys ) {
150 if ( !count( $dbkeys ) ) {
151 continue;
152 }
153
154 if ( $first ) {
155 $first = false;
156 } else {
157 $sql .= ' OR ';
158 }
159
160 if (count($dbkeys)==1) { // avoid multiple-reference syntax if simple equality can be used
161
162 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title=".
163 $db->addQuotes(current(array_keys($dbkeys))).
164 ")";
165 } else {
166 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
167
168 $firstTitle = true;
169 foreach( $dbkeys as $dbkey => $unused ) {
170 if ( $firstTitle ) {
171 $firstTitle = false;
172 } else {
173 $sql .= ',';
174 }
175 $sql .= $db->addQuotes( $dbkey );
176 }
177 $sql .= '))';
178 }
179 }
180 if ( $first && $firstTitle ) {
181 # No titles added
182 return false;
183 } else {
184 return $sql;
185 }
186 }
187 }
188
189