Added missing GPLv2 headers in some places.
[lhc/web/wiklou.git] / includes / cache / LinkBatch.php
1 <?php
2 /**
3 * Batch query to determine page existence.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * Class representing a list of titles
26 * The execute() method checks them all for existence and adds them to a LinkCache object
27 *
28 * @ingroup Cache
29 */
30 class LinkBatch {
31 /**
32 * 2-d array, first index namespace, second index dbkey, value arbitrary
33 */
34 var $data = array();
35
36 /**
37 * For debugging which method is using this class.
38 */
39 protected $caller;
40
41 function __construct( $arr = array() ) {
42 foreach( $arr as $item ) {
43 $this->addObj( $item );
44 }
45 }
46
47 /**
48 * Use ->setCaller( __METHOD__ ) to indicate which code is using this
49 * class. Only used in debugging output.
50 * @since 1.17
51 *
52 * @param $caller
53 */
54 public function setCaller( $caller ) {
55 $this->caller = $caller;
56 }
57
58 /**
59 * @param $title Title
60 */
61 public function addObj( $title ) {
62 if ( is_object( $title ) ) {
63 $this->add( $title->getNamespace(), $title->getDBkey() );
64 } else {
65 wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
66 }
67 }
68
69 /**
70 * @param $ns int
71 * @param $dbkey string
72 * @return mixed
73 */
74 public function add( $ns, $dbkey ) {
75 if ( $ns < 0 ) {
76 return;
77 }
78 if ( !array_key_exists( $ns, $this->data ) ) {
79 $this->data[$ns] = array();
80 }
81
82 $this->data[$ns][str_replace( ' ', '_', $dbkey )] = 1;
83 }
84
85 /**
86 * Set the link list to a given 2-d array
87 * First key is the namespace, second is the DB key, value arbitrary
88 *
89 * @param $array array
90 */
91 public function setArray( $array ) {
92 $this->data = $array;
93 }
94
95 /**
96 * Returns true if no pages have been added, false otherwise.
97 *
98 * @return bool
99 */
100 public function isEmpty() {
101 return ($this->getSize() == 0);
102 }
103
104 /**
105 * Returns the size of the batch.
106 *
107 * @return int
108 */
109 public function getSize() {
110 return count( $this->data );
111 }
112
113 /**
114 * Do the query and add the results to the LinkCache object
115 *
116 * @return Array mapping PDBK to ID
117 */
118 public function execute() {
119 $linkCache = LinkCache::singleton();
120 return $this->executeInto( $linkCache );
121 }
122
123 /**
124 * Do the query and add the results to a given LinkCache object
125 * Return an array mapping PDBK to ID
126 *
127 * @param $cache LinkCache
128 * @return Array remaining IDs
129 */
130 protected function executeInto( &$cache ) {
131 wfProfileIn( __METHOD__ );
132 $res = $this->doQuery();
133 $this->doGenderQuery();
134 $ids = $this->addResultToCache( $cache, $res );
135 wfProfileOut( __METHOD__ );
136 return $ids;
137 }
138
139 /**
140 * Add a ResultWrapper containing IDs and titles to a LinkCache object.
141 * As normal, titles will go into the static Title cache field.
142 * This function *also* stores extra fields of the title used for link
143 * parsing to avoid extra DB queries.
144 *
145 * @param $cache LinkCache
146 * @param $res
147 * @return Array of remaining titles
148 */
149 public function addResultToCache( $cache, $res ) {
150 if ( !$res ) {
151 return array();
152 }
153
154 // For each returned entry, add it to the list of good links, and remove it from $remaining
155
156 $ids = array();
157 $remaining = $this->data;
158 foreach ( $res as $row ) {
159 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
160 $cache->addGoodLinkObjFromRow( $title, $row );
161 $ids[$title->getPrefixedDBkey()] = $row->page_id;
162 unset( $remaining[$row->page_namespace][$row->page_title] );
163 }
164
165 // The remaining links in $data are bad links, register them as such
166 foreach ( $remaining as $ns => $dbkeys ) {
167 foreach ( $dbkeys as $dbkey => $unused ) {
168 $title = Title::makeTitle( $ns, $dbkey );
169 $cache->addBadLinkObj( $title );
170 $ids[$title->getPrefixedDBkey()] = 0;
171 }
172 }
173 return $ids;
174 }
175
176 /**
177 * Perform the existence test query, return a ResultWrapper with page_id fields
178 * @return Bool|ResultWrapper
179 */
180 public function doQuery() {
181 if ( $this->isEmpty() ) {
182 return false;
183 }
184 wfProfileIn( __METHOD__ );
185
186 // This is similar to LinkHolderArray::replaceInternal
187 $dbr = wfGetDB( DB_SLAVE );
188 $table = 'page';
189 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_len',
190 'page_is_redirect', 'page_latest' );
191 $conds = $this->constructSet( 'page', $dbr );
192
193 // Do query
194 $caller = __METHOD__;
195 if ( strval( $this->caller ) !== '' ) {
196 $caller .= " (for {$this->caller})";
197 }
198 $res = $dbr->select( $table, $fields, $conds, $caller );
199 wfProfileOut( __METHOD__ );
200 return $res;
201 }
202
203 /**
204 * Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch
205 *
206 * @return bool whether the query was successful
207 */
208 public function doGenderQuery() {
209 if ( $this->isEmpty() ) {
210 return false;
211 }
212
213 global $wgContLang;
214 if ( !$wgContLang->needsGenderDistinction() ) {
215 return false;
216 }
217
218 $genderCache = GenderCache::singleton();
219 $genderCache->doLinkBatch( $this->data, $this->caller );
220 return true;
221 }
222
223 /**
224 * Construct a WHERE clause which will match all the given titles.
225 *
226 * @param $prefix String: the appropriate table's field name prefix ('page', 'pl', etc)
227 * @param $db DatabaseBase object to use
228 * @return mixed string with SQL where clause fragment, or false if no items.
229 */
230 public function constructSet( $prefix, $db ) {
231 return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );
232 }
233 }