Merge "Http::getProxy() method to get proxy configuration"
[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 public $data = [];
35
36 /**
37 * For debugging which method is using this class.
38 */
39 protected $caller;
40
41 function __construct( $arr = [] ) {
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 string $caller
53 */
54 public function setCaller( $caller ) {
55 $this->caller = $caller;
56 }
57
58 /**
59 * @param LinkTarget $linkTarget
60 */
61 public function addObj( $linkTarget ) {
62 if ( is_object( $linkTarget ) ) {
63 $this->add( $linkTarget->getNamespace(), $linkTarget->getDBkey() );
64 } else {
65 wfDebug( "Warning: LinkBatch::addObj got invalid LinkTarget object\n" );
66 }
67 }
68
69 /**
70 * @param int $ns
71 * @param string $dbkey
72 */
73 public function add( $ns, $dbkey ) {
74 if ( $ns < 0 ) {
75 return;
76 }
77 if ( !array_key_exists( $ns, $this->data ) ) {
78 $this->data[$ns] = [];
79 }
80
81 $this->data[$ns][strtr( $dbkey, ' ', '_' )] = 1;
82 }
83
84 /**
85 * Set the link list to a given 2-d array
86 * First key is the namespace, second is the DB key, value arbitrary
87 *
88 * @param array $array
89 */
90 public function setArray( $array ) {
91 $this->data = $array;
92 }
93
94 /**
95 * Returns true if no pages have been added, false otherwise.
96 *
97 * @return bool
98 */
99 public function isEmpty() {
100 return $this->getSize() == 0;
101 }
102
103 /**
104 * Returns the size of the batch.
105 *
106 * @return int
107 */
108 public function getSize() {
109 return count( $this->data );
110 }
111
112 /**
113 * Do the query and add the results to the LinkCache object
114 *
115 * @return array Mapping PDBK to ID
116 */
117 public function execute() {
118 $linkCache = LinkCache::singleton();
119
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 LinkCache $cache
128 * @return array Remaining IDs
129 */
130 protected function executeInto( &$cache ) {
131 $res = $this->doQuery();
132 $this->doGenderQuery();
133 $ids = $this->addResultToCache( $cache, $res );
134
135 return $ids;
136 }
137
138 /**
139 * Add a ResultWrapper containing IDs and titles to a LinkCache object.
140 * As normal, titles will go into the static Title cache field.
141 * This function *also* stores extra fields of the title used for link
142 * parsing to avoid extra DB queries.
143 *
144 * @param LinkCache $cache
145 * @param ResultWrapper $res
146 * @return array Array of remaining titles
147 */
148 public function addResultToCache( $cache, $res ) {
149 if ( !$res ) {
150 return [];
151 }
152
153 // For each returned entry, add it to the list of good links, and remove it from $remaining
154
155 $ids = [];
156 $remaining = $this->data;
157 foreach ( $res as $row ) {
158 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
159 $cache->addGoodLinkObjFromRow( $title, $row );
160 $ids[$title->getPrefixedDBkey()] = $row->page_id;
161 unset( $remaining[$row->page_namespace][$row->page_title] );
162 }
163
164 // The remaining links in $data are bad links, register them as such
165 foreach ( $remaining as $ns => $dbkeys ) {
166 foreach ( $dbkeys as $dbkey => $unused ) {
167 $title = Title::makeTitle( $ns, $dbkey );
168 $cache->addBadLinkObj( $title );
169 $ids[$title->getPrefixedDBkey()] = 0;
170 }
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 global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
182
183 if ( $this->isEmpty() ) {
184 return false;
185 }
186
187 // This is similar to LinkHolderArray::replaceInternal
188 $dbr = wfGetDB( DB_SLAVE );
189 $table = 'page';
190 $fields = [ 'page_id', 'page_namespace', 'page_title', 'page_len',
191 'page_is_redirect', 'page_latest' ];
192
193 if ( $wgContentHandlerUseDB ) {
194 $fields[] = 'page_content_model';
195 }
196 if ( $wgPageLanguageUseDB ) {
197 $fields[] = 'page_lang';
198 }
199
200 $conds = $this->constructSet( 'page', $dbr );
201
202 // Do query
203 $caller = __METHOD__;
204 if ( strval( $this->caller ) !== '' ) {
205 $caller .= " (for {$this->caller})";
206 }
207 $res = $dbr->select( $table, $fields, $conds, $caller );
208
209 return $res;
210 }
211
212 /**
213 * Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch
214 *
215 * @return bool Whether the query was successful
216 */
217 public function doGenderQuery() {
218 if ( $this->isEmpty() ) {
219 return false;
220 }
221
222 global $wgContLang;
223 if ( !$wgContLang->needsGenderDistinction() ) {
224 return false;
225 }
226
227 $genderCache = GenderCache::singleton();
228 $genderCache->doLinkBatch( $this->data, $this->caller );
229
230 return true;
231 }
232
233 /**
234 * Construct a WHERE clause which will match all the given titles.
235 *
236 * @param string $prefix The appropriate table's field name prefix ('page', 'pl', etc)
237 * @param IDatabase $db DatabaseBase object to use
238 * @return string|bool String with SQL where clause fragment, or false if no items.
239 */
240 public function constructSet( $prefix, $db ) {
241 return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );
242 }
243 }