Make most special pages class names match filename
[lhc/web/wiklou.git] / includes / specialpage / QueryPage.php
1 <?php
2 /**
3 * Base code for "query" special pages.
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 SpecialPage
22 */
23
24 use MediaWiki\Linker\LinkTarget;
25 use MediaWiki\MediaWikiServices;
26 use Wikimedia\Rdbms\IResultWrapper;
27 use Wikimedia\Rdbms\IDatabase;
28 use Wikimedia\Rdbms\DBError;
29
30 /**
31 * This is a class for doing query pages; since they're almost all the same,
32 * we factor out some of the functionality into a superclass, and let
33 * subclasses derive from it.
34 * @ingroup SpecialPage
35 */
36 abstract class QueryPage extends SpecialPage {
37 /** @var bool Whether or not we want plain listoutput rather than an ordered list */
38 protected $listoutput = false;
39
40 /** @var int The offset and limit in use, as passed to the query() function */
41 protected $offset = 0;
42
43 /** @var int */
44 protected $limit = 0;
45
46 /**
47 * The number of rows returned by the query. Reading this variable
48 * only makes sense in functions that are run after the query has been
49 * done, such as preprocessResults() and formatRow().
50 *
51 * @var int
52 */
53 protected $numRows;
54
55 /**
56 * @var string|null
57 */
58 protected $cachedTimestamp = null;
59
60 /**
61 * @var bool Whether to show prev/next links
62 */
63 protected $shownavigation = true;
64
65 /**
66 * Get a list of query page classes and their associated special pages,
67 * for periodic updates.
68 *
69 * DO NOT CHANGE THIS LIST without testing that
70 * maintenance/updateSpecialPages.php still works.
71 *
72 * @return string[][]
73 */
74 public static function getPages() {
75 static $qp = null;
76
77 if ( $qp === null ) {
78 // QueryPage subclass, Special page name
79 $qp = [
80 [ SpecialAncientPages::class, 'Ancientpages' ],
81 [ SpecialBrokenRedirects::class, 'BrokenRedirects' ],
82 [ SpecialDeadendPages::class, 'Deadendpages' ],
83 [ SpecialDoubleRedirects::class, 'DoubleRedirects' ],
84 [ SpecialFileDuplicateSearch::class, 'FileDuplicateSearch' ],
85 [ SpecialListDuplicatedFiles::class, 'ListDuplicatedFiles' ],
86 [ SpecialLinkSearch::class, 'LinkSearch' ],
87 [ SpecialListRedirects::class, 'Listredirects' ],
88 [ SpecialLonelyPages::class, 'Lonelypages' ],
89 [ SpecialLongPages::class, 'Longpages' ],
90 [ SpecialMediaStatistics::class, 'MediaStatistics' ],
91 [ SpecialMIMESearch::class, 'MIMEsearch' ],
92 [ SpecialMostCategories::class, 'Mostcategories' ],
93 [ MostimagesPage::class, 'Mostimages' ],
94 [ SpecialMostInterwikis::class, 'Mostinterwikis' ],
95 [ SpecialMostLinkedCategories::class, 'Mostlinkedcategories' ],
96 [ SpecialMostLinkedTemplates::class, 'Mostlinkedtemplates' ],
97 [ SpecialMostLinked::class, 'Mostlinked' ],
98 [ SpecialMostRevisions::class, 'Mostrevisions' ],
99 [ SpecialFewestRevisions::class, 'Fewestrevisions' ],
100 [ SpecialShortPages::class, 'Shortpages' ],
101 [ SpecialUncategorizedCategories::class, 'Uncategorizedcategories' ],
102 [ SpecialUncategorizedPages::class, 'Uncategorizedpages' ],
103 [ SpecialUncategorizedImages::class, 'Uncategorizedimages' ],
104 [ SpecialUncategorizedTemplates::class, 'Uncategorizedtemplates' ],
105 [ SpecialUnusedCategories::class, 'Unusedcategories' ],
106 [ SpecialUnusedImages::class, 'Unusedimages' ],
107 [ SpecialWantedCategories::class, 'Wantedcategories' ],
108 [ WantedFilesPage::class, 'Wantedfiles' ],
109 [ WantedPagesPage::class, 'Wantedpages' ],
110 [ SpecialWantedTemplates::class, 'Wantedtemplates' ],
111 [ SpecialUnwatchedPages::class, 'Unwatchedpages' ],
112 [ SpecialUnusedTemplates::class, 'Unusedtemplates' ],
113 [ SpecialWithoutInterwiki::class, 'Withoutinterwiki' ],
114 ];
115 Hooks::run( 'wgQueryPages', [ &$qp ] );
116 }
117
118 return $qp;
119 }
120
121 /**
122 * A mutator for $this->listoutput;
123 *
124 * @param bool $bool
125 */
126 function setListoutput( $bool ) {
127 $this->listoutput = $bool;
128 }
129
130 /**
131 * Subclasses return an SQL query here, formatted as an array with the
132 * following keys:
133 * tables => Table(s) for passing to Database::select()
134 * fields => Field(s) for passing to Database::select(), may be *
135 * conds => WHERE conditions
136 * options => options
137 * join_conds => JOIN conditions
138 *
139 * Note that the query itself should return the following three columns:
140 * 'namespace', 'title', and 'value'. 'value' is used for sorting.
141 *
142 * These may be stored in the querycache table for expensive queries,
143 * and that cached data will be returned sometimes, so the presence of
144 * extra fields can't be relied upon. The cached 'value' column will be
145 * an integer; non-numeric values are useful only for sorting the
146 * initial query (except if they're timestamps, see usesTimestamps()).
147 *
148 * Don't include an ORDER or LIMIT clause, they will be added.
149 *
150 * If this function is not overridden or returns something other than
151 * an array, getSQL() will be used instead. This is for backwards
152 * compatibility only and is strongly deprecated.
153 * @return array
154 * @since 1.18
155 */
156 public function getQueryInfo() {
157 return null;
158 }
159
160 /**
161 * For back-compat, subclasses may return a raw SQL query here, as a string.
162 * This is strongly deprecated; getQueryInfo() should be overridden instead.
163 * @throws MWException
164 * @return string
165 */
166 function getSQL() {
167 /* Implement getQueryInfo() instead */
168 throw new MWException( "Bug in a QueryPage: doesn't implement getQueryInfo() nor "
169 . "getQuery() properly" );
170 }
171
172 /**
173 * Subclasses return an array of fields to order by here. Don't append
174 * DESC to the field names, that'll be done automatically if
175 * sortDescending() returns true.
176 * @return string[]
177 * @since 1.18
178 */
179 function getOrderFields() {
180 return [ 'value' ];
181 }
182
183 /**
184 * Does this query return timestamps rather than integers in its
185 * 'value' field? If true, this class will convert 'value' to a
186 * UNIX timestamp for caching.
187 * NOTE: formatRow() may get timestamps in TS_MW (mysql), TS_DB (pgsql)
188 * or TS_UNIX (querycache) format, so be sure to always run them
189 * through wfTimestamp()
190 * @return bool
191 * @since 1.18
192 */
193 public function usesTimestamps() {
194 return false;
195 }
196
197 /**
198 * Override to sort by increasing values
199 *
200 * @return bool
201 */
202 function sortDescending() {
203 return true;
204 }
205
206 /**
207 * Is this query expensive (for some definition of expensive)? Then we
208 * don't let it run in miser mode. $wgDisableQueryPages causes all query
209 * pages to be declared expensive. Some query pages are always expensive.
210 *
211 * @return bool
212 */
213 public function isExpensive() {
214 return $this->getConfig()->get( 'DisableQueryPages' );
215 }
216
217 /**
218 * Is the output of this query cacheable? Non-cacheable expensive pages
219 * will be disabled in miser mode and will not have their results written
220 * to the querycache table.
221 * @return bool
222 * @since 1.18
223 */
224 public function isCacheable() {
225 return true;
226 }
227
228 /**
229 * Whether or not the output of the page in question is retrieved from
230 * the database cache.
231 *
232 * @return bool
233 */
234 public function isCached() {
235 return $this->isExpensive() && $this->getConfig()->get( 'MiserMode' );
236 }
237
238 /**
239 * Sometime we don't want to build rss / atom feeds.
240 *
241 * @return bool
242 */
243 function isSyndicated() {
244 return true;
245 }
246
247 /**
248 * Formats the results of the query for display. The skin is the current
249 * skin; you can use it for making links. The result is a single row of
250 * result data. You should be able to grab SQL results off of it.
251 * If the function returns false, the line output will be skipped.
252 * @param Skin $skin
253 * @param object $result Result row
254 * @return string|bool String or false to skip
255 */
256 abstract function formatResult( $skin, $result );
257
258 /**
259 * The content returned by this function will be output before any result
260 *
261 * @return string
262 */
263 function getPageHeader() {
264 return '';
265 }
266
267 /**
268 * Outputs some kind of an informative message (via OutputPage) to let the
269 * user know that the query returned nothing and thus there's nothing to
270 * show.
271 *
272 * @since 1.26
273 */
274 protected function showEmptyText() {
275 $this->getOutput()->addWikiMsg( 'specialpage-empty' );
276 }
277
278 /**
279 * If using extra form wheely-dealies, return a set of parameters here
280 * as an associative array. They will be encoded and added to the paging
281 * links (prev/next/lengths).
282 *
283 * @return array
284 */
285 function linkParameters() {
286 return [];
287 }
288
289 /**
290 * Clear the cache and save new results
291 *
292 * @param int|bool $limit Limit for SQL statement
293 * @param bool $ignoreErrors Whether to ignore database errors
294 * @throws DBError|Exception
295 * @return bool|int
296 */
297 public function recache( $limit, $ignoreErrors = true ) {
298 if ( !$this->isCacheable() ) {
299 return 0;
300 }
301
302 $fname = static::class . '::recache';
303 $dbw = wfGetDB( DB_MASTER );
304 if ( !$dbw ) {
305 return false;
306 }
307
308 try {
309 # Do query
310 $res = $this->reallyDoQuery( $limit, false );
311 $num = false;
312 if ( $res ) {
313 $num = $res->numRows();
314 # Fetch results
315 $vals = [];
316 foreach ( $res as $i => $row ) {
317 if ( isset( $row->value ) ) {
318 if ( $this->usesTimestamps() ) {
319 $value = wfTimestamp( TS_UNIX,
320 $row->value );
321 } else {
322 $value = intval( $row->value ); // T16414
323 }
324 } else {
325 $value = $i;
326 }
327
328 $vals[] = [
329 'qc_type' => $this->getName(),
330 'qc_namespace' => $row->namespace,
331 'qc_title' => $row->title,
332 'qc_value' => $value
333 ];
334 }
335
336 $dbw->doAtomicSection(
337 __METHOD__,
338 function ( IDatabase $dbw, $fname ) use ( $vals ) {
339 # Clear out any old cached data
340 $dbw->delete( 'querycache',
341 [ 'qc_type' => $this->getName() ],
342 $fname
343 );
344 # Save results into the querycache table on the master
345 if ( count( $vals ) ) {
346 $dbw->insert( 'querycache', $vals, $fname );
347 }
348 # Update the querycache_info record for the page
349 $dbw->delete( 'querycache_info',
350 [ 'qci_type' => $this->getName() ],
351 $fname
352 );
353 $dbw->insert( 'querycache_info',
354 [ 'qci_type' => $this->getName(),
355 'qci_timestamp' => $dbw->timestamp() ],
356 $fname
357 );
358 }
359 );
360 }
361 } catch ( DBError $e ) {
362 if ( !$ignoreErrors ) {
363 throw $e; // report query error
364 }
365 $num = false; // set result to false to indicate error
366 }
367
368 return $num;
369 }
370
371 /**
372 * Get a DB connection to be used for slow recache queries
373 * @return IDatabase
374 */
375 function getRecacheDB() {
376 return wfGetDB( DB_REPLICA, [ $this->getName(), 'QueryPage::recache', 'vslow' ] );
377 }
378
379 /**
380 * Remove a cached result.
381 * Useful for interactive backlogs where the user can fix problems in-place.
382 * @param LinkTarget $title The page to remove.
383 * @since 1.34
384 */
385 public function delete( LinkTarget $title ) {
386 if ( $this->isCached() ) {
387 $dbw = wfGetDB( DB_MASTER );
388 $dbw->delete( 'querycache', [
389 'qc_type' => $this->getName(),
390 'qc_namespace' => $title->getNamespace(),
391 'qc_title' => $title->getDBkey(),
392 ], __METHOD__ );
393 }
394 }
395
396 /**
397 * Run the query and return the result
398 * @param int|bool $limit Numerical limit or false for no limit
399 * @param int|bool $offset Numerical offset or false for no offset
400 * @return IResultWrapper
401 * @since 1.18
402 */
403 public function reallyDoQuery( $limit, $offset = false ) {
404 $fname = static::class . '::reallyDoQuery';
405 $dbr = $this->getRecacheDB();
406 $query = $this->getQueryInfo();
407 $order = $this->getOrderFields();
408
409 if ( $this->sortDescending() ) {
410 foreach ( $order as &$field ) {
411 $field .= ' DESC';
412 }
413 }
414
415 if ( is_array( $query ) ) {
416 $tables = isset( $query['tables'] ) ? (array)$query['tables'] : [];
417 $fields = isset( $query['fields'] ) ? (array)$query['fields'] : [];
418 $conds = isset( $query['conds'] ) ? (array)$query['conds'] : [];
419 $options = isset( $query['options'] ) ? (array)$query['options'] : [];
420 $join_conds = isset( $query['join_conds'] ) ? (array)$query['join_conds'] : [];
421
422 if ( $order ) {
423 $options['ORDER BY'] = $order;
424 }
425
426 if ( $limit !== false ) {
427 $options['LIMIT'] = intval( $limit );
428 }
429
430 if ( $offset !== false ) {
431 $options['OFFSET'] = intval( $offset );
432 }
433
434 $res = $dbr->select( $tables, $fields, $conds, $fname,
435 $options, $join_conds
436 );
437 } else {
438 // Old-fashioned raw SQL style, deprecated
439 $sql = $this->getSQL();
440 $sql .= ' ORDER BY ' . implode( ', ', $order );
441 $sql = $dbr->limitResult( $sql, $limit, $offset );
442 $res = $dbr->query( $sql, $fname );
443 }
444
445 return $res;
446 }
447
448 /**
449 * Somewhat deprecated, you probably want to be using execute()
450 * @param int|bool $offset
451 * @param int|bool $limit
452 * @return IResultWrapper
453 */
454 public function doQuery( $offset = false, $limit = false ) {
455 if ( $this->isCached() && $this->isCacheable() ) {
456 return $this->fetchFromCache( $limit, $offset );
457 } else {
458 return $this->reallyDoQuery( $limit, $offset );
459 }
460 }
461
462 /**
463 * Fetch the query results from the query cache
464 * @param int|bool $limit Numerical limit or false for no limit
465 * @param int|bool $offset Numerical offset or false for no offset
466 * @return IResultWrapper
467 * @since 1.18
468 */
469 public function fetchFromCache( $limit, $offset = false ) {
470 $dbr = wfGetDB( DB_REPLICA );
471 $options = [];
472
473 if ( $limit !== false ) {
474 $options['LIMIT'] = intval( $limit );
475 }
476
477 if ( $offset !== false ) {
478 $options['OFFSET'] = intval( $offset );
479 }
480
481 $order = $this->getCacheOrderFields();
482 if ( $this->sortDescending() ) {
483 foreach ( $order as &$field ) {
484 $field .= " DESC";
485 }
486 }
487 if ( $order ) {
488 $options['ORDER BY'] = $order;
489 }
490
491 return $dbr->select( 'querycache',
492 [ 'qc_type',
493 'namespace' => 'qc_namespace',
494 'title' => 'qc_title',
495 'value' => 'qc_value' ],
496 [ 'qc_type' => $this->getName() ],
497 __METHOD__,
498 $options
499 );
500 }
501
502 /**
503 * Return the order fields for fetchFromCache. Default is to always use
504 * "ORDER BY value" which was the default prior to this function.
505 * @return array
506 * @since 1.29
507 */
508 function getCacheOrderFields() {
509 return [ 'value' ];
510 }
511
512 /**
513 * @return string
514 */
515 public function getCachedTimestamp() {
516 if ( is_null( $this->cachedTimestamp ) ) {
517 $dbr = wfGetDB( DB_REPLICA );
518 $fname = static::class . '::getCachedTimestamp';
519 $this->cachedTimestamp = $dbr->selectField( 'querycache_info', 'qci_timestamp',
520 [ 'qci_type' => $this->getName() ], $fname );
521 }
522 return $this->cachedTimestamp;
523 }
524
525 /**
526 * Returns limit and offset, as returned by $this->getRequest()->getLimitOffset().
527 * Subclasses may override this to further restrict or modify limit and offset.
528 *
529 * @note Restricts the offset parameter, as most query pages have inefficient paging
530 *
531 * Its generally expected that the returned limit will not be 0, and the returned
532 * offset will be less than the max results.
533 *
534 * @since 1.26
535 * @return int[] list( $limit, $offset )
536 */
537 protected function getLimitOffset() {
538 list( $limit, $offset ) = $this->getRequest()->getLimitOffset();
539 if ( $this->getConfig()->get( 'MiserMode' ) ) {
540 $maxResults = $this->getMaxResults();
541 // Can't display more than max results on a page
542 $limit = min( $limit, $maxResults );
543 // Can't skip over more than the end of $maxResults
544 $offset = min( $offset, $maxResults + 1 );
545 }
546 return [ $limit, $offset ];
547 }
548
549 /**
550 * What is limit to fetch from DB
551 *
552 * Used to make it appear the DB stores less results then it actually does
553 * @param int $uiLimit Limit from UI
554 * @param int $uiOffset Offset from UI
555 * @return int Limit to use for DB (not including extra row to see if at end)
556 */
557 protected function getDBLimit( $uiLimit, $uiOffset ) {
558 $maxResults = $this->getMaxResults();
559 if ( $this->getConfig()->get( 'MiserMode' ) ) {
560 $limit = min( $uiLimit + 1, $maxResults - $uiOffset );
561 return max( $limit, 0 );
562 } else {
563 return $uiLimit + 1;
564 }
565 }
566
567 /**
568 * Get max number of results we can return in miser mode.
569 *
570 * Most QueryPage subclasses use inefficient paging, so limit the max amount we return
571 * This matters for uncached query pages that might otherwise accept an offset of 3 million
572 *
573 * @since 1.27
574 * @return int
575 */
576 protected function getMaxResults() {
577 // Max of 10000, unless we store more than 10000 in query cache.
578 return max( $this->getConfig()->get( 'QueryCacheLimit' ), 10000 );
579 }
580
581 /**
582 * This is the actual workhorse. It does everything needed to make a
583 * real, honest-to-gosh query page.
584 * @param string|null $par
585 */
586 public function execute( $par ) {
587 $user = $this->getUser();
588 if ( !$this->userCanExecute( $user ) ) {
589 $this->displayRestrictionError();
590 return;
591 }
592
593 $this->setHeaders();
594 $this->outputHeader();
595
596 $out = $this->getOutput();
597
598 if ( $this->isCached() && !$this->isCacheable() ) {
599 $out->addWikiMsg( 'querypage-disabled' );
600 return;
601 }
602
603 $out->setSyndicated( $this->isSyndicated() );
604
605 if ( $this->limit == 0 && $this->offset == 0 ) {
606 list( $this->limit, $this->offset ) = $this->getLimitOffset();
607 }
608 $dbLimit = $this->getDBLimit( $this->limit, $this->offset );
609 // @todo Use doQuery()
610 if ( !$this->isCached() ) {
611 # select one extra row for navigation
612 $res = $this->reallyDoQuery( $dbLimit, $this->offset );
613 } else {
614 # Get the cached result, select one extra row for navigation
615 $res = $this->fetchFromCache( $dbLimit, $this->offset );
616 if ( !$this->listoutput ) {
617 # Fetch the timestamp of this update
618 $ts = $this->getCachedTimestamp();
619 $lang = $this->getLanguage();
620 $maxResults = $lang->formatNum( $this->getConfig()->get( 'QueryCacheLimit' ) );
621
622 if ( $ts ) {
623 $updated = $lang->userTimeAndDate( $ts, $user );
624 $updateddate = $lang->userDate( $ts, $user );
625 $updatedtime = $lang->userTime( $ts, $user );
626 $out->addMeta( 'Data-Cache-Time', $ts );
627 $out->addJsConfigVars( 'dataCacheTime', $ts );
628 $out->addWikiMsg( 'perfcachedts', $updated, $updateddate, $updatedtime, $maxResults );
629 } else {
630 $out->addWikiMsg( 'perfcached', $maxResults );
631 }
632
633 # If updates on this page have been disabled, let the user know
634 # that the data set won't be refreshed for now
635 if ( is_array( $this->getConfig()->get( 'DisableQueryPageUpdate' ) )
636 && in_array( $this->getName(), $this->getConfig()->get( 'DisableQueryPageUpdate' ) )
637 ) {
638 $out->wrapWikiMsg(
639 "<div class=\"mw-querypage-no-updates\">\n$1\n</div>",
640 'querypage-no-updates'
641 );
642 }
643 }
644 }
645
646 $this->numRows = $res->numRows();
647
648 $dbr = $this->getRecacheDB();
649 $this->preprocessResults( $dbr, $res );
650
651 $out->addHTML( Xml::openElement( 'div', [ 'class' => 'mw-spcontent' ] ) );
652
653 # Top header and navigation
654 if ( $this->shownavigation ) {
655 $out->addHTML( $this->getPageHeader() );
656 if ( $this->numRows > 0 ) {
657 $out->addHTML( $this->msg( 'showingresultsinrange' )->numParams(
658 min( $this->numRows, $this->limit ), # do not show the one extra row, if exist
659 $this->offset + 1, ( min( $this->numRows, $this->limit ) + $this->offset ) )->parseAsBlock() );
660 # Disable the "next" link when we reach the end
661 $miserMaxResults = $this->getConfig()->get( 'MiserMode' )
662 && ( $this->offset + $this->limit >= $this->getMaxResults() );
663 $atEnd = ( $this->numRows <= $this->limit ) || $miserMaxResults;
664 $paging = $this->buildPrevNextNavigation( $this->offset,
665 $this->limit, $this->linkParameters(), $atEnd, $par );
666 $out->addHTML( '<p>' . $paging . '</p>' );
667 } else {
668 # No results to show, so don't bother with "showing X of Y" etc.
669 # -- just let the user know and give up now
670 $this->showEmptyText();
671 $out->addHTML( Xml::closeElement( 'div' ) );
672 return;
673 }
674 }
675
676 # The actual results; specialist subclasses will want to handle this
677 # with more than a straight list, so we hand them the info, plus
678 # an OutputPage, and let them get on with it
679 $this->outputResults( $out,
680 $this->getSkin(),
681 $dbr, # Should use IResultWrapper for this
682 $res,
683 min( $this->numRows, $this->limit ), # do not format the one extra row, if exist
684 $this->offset );
685
686 # Repeat the paging links at the bottom
687 if ( $this->shownavigation ) {
688 $out->addHTML( '<p>' . $paging . '</p>' );
689 }
690
691 $out->addHTML( Xml::closeElement( 'div' ) );
692 }
693
694 /**
695 * Format and output report results using the given information plus
696 * OutputPage
697 *
698 * @param OutputPage $out OutputPage to print to
699 * @param Skin $skin User skin to use
700 * @param IDatabase $dbr Database (read) connection to use
701 * @param IResultWrapper $res Result pointer
702 * @param int $num Number of available result rows
703 * @param int $offset Paging offset
704 */
705 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
706 if ( $num > 0 ) {
707 $html = [];
708 if ( !$this->listoutput ) {
709 $html[] = $this->openList( $offset );
710 }
711
712 # $res might contain the whole 1,000 rows, so we read up to
713 # $num [should update this to use a Pager]
714 for ( $i = 0; $i < $num && $row = $res->fetchObject(); $i++ ) {
715 $line = $this->formatResult( $skin, $row );
716 if ( $line ) {
717 $html[] = $this->listoutput
718 ? $line
719 : "<li>{$line}</li>\n";
720 }
721 }
722
723 if ( !$this->listoutput ) {
724 $html[] = $this->closeList();
725 }
726
727 $html = $this->listoutput
728 ? MediaWikiServices::getInstance()->getContentLanguage()->listToText( $html )
729 : implode( '', $html );
730
731 $out->addHTML( $html );
732 }
733 }
734
735 /**
736 * @param int $offset
737 * @return string
738 */
739 function openList( $offset ) {
740 return "\n<ol start='" . ( $offset + 1 ) . "' class='special'>\n";
741 }
742
743 /**
744 * @return string
745 */
746 function closeList() {
747 return "</ol>\n";
748 }
749
750 /**
751 * Do any necessary preprocessing of the result object.
752 * @param IDatabase $db
753 * @param IResultWrapper $res
754 */
755 function preprocessResults( $db, $res ) {
756 }
757
758 /**
759 * Creates a new LinkBatch object, adds all pages from the passed result wrapper (MUST include
760 * title and optional the namespace field) and executes the batch. This operation will pre-cache
761 * LinkCache information like page existence and information for stub color and redirect hints.
762 *
763 * @param IResultWrapper $res The result wrapper to process. Needs to include the title
764 * field and namespace field, if the $ns parameter isn't set.
765 * @param null $ns Use this namespace for the given titles in the result wrapper,
766 * instead of the namespace value of $res.
767 */
768 protected function executeLBFromResultWrapper( IResultWrapper $res, $ns = null ) {
769 if ( !$res->numRows() ) {
770 return;
771 }
772
773 $batch = new LinkBatch;
774 foreach ( $res as $row ) {
775 $batch->add( $ns ?? $row->namespace, $row->title );
776 }
777 $batch->execute();
778
779 $res->seek( 0 );
780 }
781 }