* Add redirect and size fields to title. Add accessors.
[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][str_replace( ' ', '_', $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 wfProfileIn( __METHOD__ );
77 $res = $this->doQuery();
78 $ids = $this->addResultToCache( $cache, $res );
79 wfProfileOut( __METHOD__ );
80 return $ids;
81 }
82
83 /**
84 * Add a ResultWrapper containing IDs and titles to a LinkCache object
85 * Title are initialized here and they will go to the static title cache
86 * field of the Title class.
87 */
88 function addResultToCache( $cache, $res ) {
89 if ( !$res ) {
90 return array();
91 }
92
93 // For each returned entry, add it to the list of good links, and remove it from $remaining
94
95 $ids = array();
96 $remaining = $this->data;
97 while ( $row = $res->fetchObject() ) {
98 $title = Title::newFromRow( $row );
99 $cache->addGoodLinkObj( $row->page_id, $title );
100 $ids[$title->getPrefixedDBkey()] = $row->page_id;
101 unset( $remaining[$row->page_namespace][$row->page_title] );
102 }
103
104 // The remaining links in $data are bad links, register them as such
105 foreach ( $remaining as $ns => $dbkeys ) {
106 foreach ( $dbkeys as $dbkey => $unused ) {
107 $title = Title::makeTitle( $ns, $dbkey );
108 $cache->addBadLinkObj( $title );
109 $ids[$title->getPrefixedDBkey()] = 0;
110 }
111 }
112 return $ids;
113 }
114
115 /**
116 * Perform the existence test query, return a ResultWrapper with page_id fields
117 */
118 function doQuery() {
119 if ( $this->isEmpty() ) {
120 return false;
121 }
122 wfProfileIn( __METHOD__ );
123
124 // Construct query
125 // This is very similar to Parser::replaceLinkHolders
126 $dbr = wfGetDB( DB_SLAVE );
127 $page = $dbr->tableName( 'page' );
128 $set = $this->constructSet( 'page', $dbr );
129 if ( $set === false ) {
130 wfProfileOut( __METHOD__ );
131 return false;
132 }
133 $sql = "SELECT page_id, page_namespace, page_title, page_len, page_is_redirect FROM $page WHERE $set";
134
135 // Do query
136 $res = new ResultWrapper( $dbr, $dbr->query( $sql, __METHOD__ ) );
137 wfProfileOut( __METHOD__ );
138 return $res;
139 }
140
141 /**
142 * Construct a WHERE clause which will match all the given titles.
143 *
144 * @param string $prefix the appropriate table's field name prefix ('page', 'pl', etc)
145 * @return string
146 * @public
147 */
148 function constructSet( $prefix, &$db ) {
149 $first = true;
150 $firstTitle = true;
151 $sql = '';
152 foreach ( $this->data as $ns => $dbkeys ) {
153 if ( !count( $dbkeys ) ) {
154 continue;
155 }
156
157 if ( $first ) {
158 $first = false;
159 } else {
160 $sql .= ' OR ';
161 }
162
163 if (count($dbkeys)==1) { // avoid multiple-reference syntax if simple equality can be used
164 $singleKey = array_keys($dbkeys);
165 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title=".
166 $db->addQuotes($singleKey[0]).
167 ")";
168 } else {
169 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
170
171 $firstTitle = true;
172 foreach( $dbkeys as $dbkey => $unused ) {
173 if ( $firstTitle ) {
174 $firstTitle = false;
175 } else {
176 $sql .= ',';
177 }
178 $sql .= $db->addQuotes( $dbkey );
179 }
180 $sql .= '))';
181 }
182 }
183 if ( $first && $firstTitle ) {
184 # No titles added
185 return false;
186 } else {
187 return $sql;
188 }
189 }
190 }
191
192