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