Merge "Update default hash storage settings"
[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 use MediaWiki\Linker\LinkTarget;
24
25 /**
26 * Class representing a list of titles
27 * The execute() method checks them all for existence and adds them to a LinkCache object
28 *
29 * @ingroup Cache
30 */
31 class LinkBatch {
32 /**
33 * 2-d array, first index namespace, second index dbkey, value arbitrary
34 */
35 public $data = [];
36
37 /**
38 * For debugging which method is using this class.
39 */
40 protected $caller;
41
42 function __construct( $arr = [] ) {
43 foreach ( $arr as $item ) {
44 $this->addObj( $item );
45 }
46 }
47
48 /**
49 * Use ->setCaller( __METHOD__ ) to indicate which code is using this
50 * class. Only used in debugging output.
51 * @since 1.17
52 *
53 * @param string $caller
54 */
55 public function setCaller( $caller ) {
56 $this->caller = $caller;
57 }
58
59 /**
60 * @param LinkTarget $linkTarget
61 */
62 public function addObj( $linkTarget ) {
63 if ( is_object( $linkTarget ) ) {
64 $this->add( $linkTarget->getNamespace(), $linkTarget->getDBkey() );
65 } else {
66 wfDebug( "Warning: LinkBatch::addObj got invalid LinkTarget object\n" );
67 }
68 }
69
70 /**
71 * @param int $ns
72 * @param string $dbkey
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] = [];
80 }
81
82 $this->data[$ns][strtr( $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
121 return $this->executeInto( $linkCache );
122 }
123
124 /**
125 * Do the query and add the results to a given LinkCache object
126 * Return an array mapping PDBK to ID
127 *
128 * @param LinkCache $cache
129 * @return array Remaining IDs
130 */
131 protected function executeInto( &$cache ) {
132 $res = $this->doQuery();
133 $this->doGenderQuery();
134 $ids = $this->addResultToCache( $cache, $res );
135
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 LinkCache $cache
146 * @param ResultWrapper $res
147 * @return array Array of remaining titles
148 */
149 public function addResultToCache( $cache, $res ) {
150 if ( !$res ) {
151 return [];
152 }
153
154 // For each returned entry, add it to the list of good links, and remove it from $remaining
155
156 $ids = [];
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
174 return $ids;
175 }
176
177 /**
178 * Perform the existence test query, return a ResultWrapper with page_id fields
179 * @return bool|ResultWrapper
180 */
181 public function doQuery() {
182 global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
183
184 if ( $this->isEmpty() ) {
185 return false;
186 }
187
188 // This is similar to LinkHolderArray::replaceInternal
189 $dbr = wfGetDB( DB_SLAVE );
190 $table = 'page';
191 $fields = [ 'page_id', 'page_namespace', 'page_title', 'page_len',
192 'page_is_redirect', 'page_latest' ];
193
194 if ( $wgContentHandlerUseDB ) {
195 $fields[] = 'page_content_model';
196 }
197 if ( $wgPageLanguageUseDB ) {
198 $fields[] = 'page_lang';
199 }
200
201 $conds = $this->constructSet( 'page', $dbr );
202
203 // Do query
204 $caller = __METHOD__;
205 if ( strval( $this->caller ) !== '' ) {
206 $caller .= " (for {$this->caller})";
207 }
208 $res = $dbr->select( $table, $fields, $conds, $caller );
209
210 return $res;
211 }
212
213 /**
214 * Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch
215 *
216 * @return bool Whether the query was successful
217 */
218 public function doGenderQuery() {
219 if ( $this->isEmpty() ) {
220 return false;
221 }
222
223 global $wgContLang;
224 if ( !$wgContLang->needsGenderDistinction() ) {
225 return false;
226 }
227
228 $genderCache = GenderCache::singleton();
229 $genderCache->doLinkBatch( $this->data, $this->caller );
230
231 return true;
232 }
233
234 /**
235 * Construct a WHERE clause which will match all the given titles.
236 *
237 * @param string $prefix The appropriate table's field name prefix ('page', 'pl', etc)
238 * @param IDatabase $db DatabaseBase object to use
239 * @return string|bool String with SQL where clause fragment, or false if no items.
240 */
241 public function constructSet( $prefix, $db ) {
242 return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );
243 }
244 }