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