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