4e01338fd85e58f84ea035d30bd6adbb54485696
[lhc/web/wiklou.git] / includes / QueryPage.php
1 <?php
2 /**
3 * Contain a class for special pages
4 * @file
5 * @ingroup SpecialPages
6 */
7
8 /**
9 * List of query page classes and their associated special pages,
10 * for periodic updates.
11 *
12 * DO NOT CHANGE THIS LIST without testing that
13 * maintenance/updateSpecialPages.php still works.
14 */
15 global $wgQueryPages; // not redundant
16 $wgQueryPages = array(
17 // QueryPage subclass Special page name Limit (false for none, none for the default)
18 // ----------------------------------------------------------------------------
19 array( 'AncientPagesPage', 'Ancientpages' ),
20 array( 'BrokenRedirectsPage', 'BrokenRedirects' ),
21 array( 'DeadendPagesPage', 'Deadendpages' ),
22 array( 'DisambiguationsPage', 'Disambiguations' ),
23 array( 'DoubleRedirectsPage', 'DoubleRedirects' ),
24 array( 'FileDuplicateSearchPage', 'FileDuplicateSearch' ),
25 array( 'LinkSearchPage', 'LinkSearch' ),
26 array( 'ListredirectsPage', 'Listredirects' ),
27 array( 'LonelyPagesPage', 'Lonelypages' ),
28 array( 'LongPagesPage', 'Longpages' ),
29 array( 'MIMEsearchPage', 'MIMEsearch' ),
30 array( 'MostcategoriesPage', 'Mostcategories' ),
31 array( 'MostimagesPage', 'Mostimages' ),
32 array( 'MostlinkedCategoriesPage', 'Mostlinkedcategories' ),
33 array( 'MostlinkedtemplatesPage', 'Mostlinkedtemplates' ),
34 array( 'MostlinkedPage', 'Mostlinked' ),
35 array( 'MostrevisionsPage', 'Mostrevisions' ),
36 array( 'FewestrevisionsPage', 'Fewestrevisions' ),
37 array( 'ShortPagesPage', 'Shortpages' ),
38 array( 'UncategorizedCategoriesPage', 'Uncategorizedcategories' ),
39 array( 'UncategorizedPagesPage', 'Uncategorizedpages' ),
40 array( 'UncategorizedImagesPage', 'Uncategorizedimages' ),
41 array( 'UncategorizedTemplatesPage', 'Uncategorizedtemplates' ),
42 array( 'UnusedCategoriesPage', 'Unusedcategories' ),
43 array( 'UnusedimagesPage', 'Unusedimages' ),
44 array( 'WantedCategoriesPage', 'Wantedcategories' ),
45 array( 'WantedFilesPage', 'Wantedfiles' ),
46 array( 'WantedPagesPage', 'Wantedpages' ),
47 array( 'WantedTemplatesPage', 'Wantedtemplates' ),
48 array( 'UnwatchedPagesPage', 'Unwatchedpages' ),
49 array( 'UnusedtemplatesPage', 'Unusedtemplates' ),
50 array( 'WithoutInterwikiPage', 'Withoutinterwiki' ),
51 );
52 wfRunHooks( 'wgQueryPages', array( &$wgQueryPages ) );
53
54 global $wgDisableCounters;
55 if ( !$wgDisableCounters )
56 $wgQueryPages[] = array( 'PopularPagesPage', 'Popularpages' );
57
58
59 /**
60 * This is a class for doing query pages; since they're almost all the same,
61 * we factor out some of the functionality into a superclass, and let
62 * subclasses derive from it.
63 * @ingroup SpecialPage
64 */
65 abstract class QueryPage extends SpecialPage {
66 /**
67 * Whether or not we want plain listoutput rather than an ordered list
68 *
69 * @var bool
70 */
71 var $listoutput = false;
72
73 /**
74 * The offset and limit in use, as passed to the query() function
75 *
76 * @var integer
77 */
78 var $offset = 0;
79 var $limit = 0;
80
81 /**
82 * The number of rows returned by the query. Reading this variable
83 * only makes sense in functions that are run after the query has been
84 * done, such as preprocessResults() and formatRow().
85 */
86 protected $numRows;
87
88 protected $cachedTimestamp = null;
89
90 /**
91 * Wheter to show prev/next links
92 */
93 protected $shownavigation = true;
94
95 /**
96 * A mutator for $this->listoutput;
97 *
98 * @param $bool Boolean
99 */
100 function setListoutput( $bool ) {
101 $this->listoutput = $bool;
102 }
103
104 /**
105 * Subclasses return an SQL query here, formatted as an array with the
106 * following keys:
107 * tables => Table(s) for passing to Database::select()
108 * fields => Field(s) for passing to Database::select(), may be *
109 * conds => WHERE conditions
110 * options => options
111 * join_conds => JOIN conditions
112 *
113 * Note that the query itself should return the following three columns:
114 * 'namespace', 'title', and 'value'. 'value' is used for sorting.
115 *
116 * These may be stored in the querycache table for expensive queries,
117 * and that cached data will be returned sometimes, so the presence of
118 * extra fields can't be relied upon. The cached 'value' column will be
119 * an integer; non-numeric values are useful only for sorting the
120 * initial query (except if they're timestamps, see usesTimestamps()).
121 *
122 * Don't include an ORDER or LIMIT clause, they will be added.
123 *
124 * If this function is not overridden or returns something other than
125 * an array, getSQL() will be used instead. This is for backwards
126 * compatibility only and is strongly deprecated.
127 * @return array
128 * @since 1.18
129 */
130 function getQueryInfo() {
131 return null;
132 }
133
134 /**
135 * For back-compat, subclasses may return a raw SQL query here, as a string.
136 * This is stronly deprecated; getQueryInfo() should be overridden instead.
137 * @return string
138 */
139 function getSQL() {
140 /* Implement getQueryInfo() instead */
141 throw new MWException( "Bug in a QueryPage: doesn't implement getQueryInfo() nor getQuery() properly" );
142 }
143
144 /**
145 * Subclasses return an array of fields to order by here. Don't append
146 * DESC to the field names, that'll be done automatically if
147 * sortDescending() returns true.
148 * @return array
149 * @since 1.18
150 */
151 function getOrderFields() {
152 return array( 'value' );
153 }
154
155 /**
156 * Does this query return timestamps rather than integers in its
157 * 'value' field? If true, this class will convert 'value' to a
158 * UNIX timestamp for caching.
159 * NOTE: formatRow() may get timestamps in TS_MW (mysql), TS_DB (pgsql)
160 * or TS_UNIX (querycache) format, so be sure to always run them
161 * through wfTimestamp()
162 * @return bool
163 * @since 1.18
164 */
165 function usesTimestamps() {
166 return false;
167 }
168
169 /**
170 * Override to sort by increasing values
171 *
172 * @return Boolean
173 */
174 function sortDescending() {
175 return true;
176 }
177
178 /**
179 * Is this query expensive (for some definition of expensive)? Then we
180 * don't let it run in miser mode. $wgDisableQueryPages causes all query
181 * pages to be declared expensive. Some query pages are always expensive.
182 *
183 * @return Boolean
184 */
185 function isExpensive() {
186 global $wgDisableQueryPages;
187 return $wgDisableQueryPages;
188 }
189
190 /**
191 * Is the output of this query cacheable? Non-cacheable expensive pages
192 * will be disabled in miser mode and will not have their results written
193 * to the querycache table.
194 * @return Boolean
195 * @since 1.18
196 */
197 public function isCacheable() {
198 return true;
199 }
200
201 /**
202 * Whether or not the output of the page in question is retrieved from
203 * the database cache.
204 *
205 * @return Boolean
206 */
207 function isCached() {
208 global $wgMiserMode;
209
210 return $this->isExpensive() && $wgMiserMode;
211 }
212
213 /**
214 * Sometime we dont want to build rss / atom feeds.
215 *
216 * @return Boolean
217 */
218 function isSyndicated() {
219 return true;
220 }
221
222 /**
223 * Formats the results of the query for display. The skin is the current
224 * skin; you can use it for making links. The result is a single row of
225 * result data. You should be able to grab SQL results off of it.
226 * If the function returns false, the line output will be skipped.
227 * @param $skin Skin
228 * @param $result object Result row
229 * @return mixed String or false to skip
230 *
231 * @param $skin Skin object
232 * @param $result Object: database row
233 */
234 abstract function formatResult( $skin, $result );
235
236 /**
237 * The content returned by this function will be output before any result
238 *
239 * @return String
240 */
241 function getPageHeader() {
242 return '';
243 }
244
245 /**
246 * If using extra form wheely-dealies, return a set of parameters here
247 * as an associative array. They will be encoded and added to the paging
248 * links (prev/next/lengths).
249 *
250 * @return Array
251 */
252 function linkParameters() {
253 return array();
254 }
255
256 /**
257 * Some special pages (for example SpecialListusers) might not return the
258 * current object formatted, but return the previous one instead.
259 * Setting this to return true will ensure formatResult() is called
260 * one more time to make sure that the very last result is formatted
261 * as well.
262 */
263 function tryLastResult() {
264 return false;
265 }
266
267 /**
268 * Clear the cache and save new results
269 *
270 * @param $limit Integer: limit for SQL statement
271 * @param $ignoreErrors Boolean: whether to ignore database errors
272 */
273 function recache( $limit, $ignoreErrors = true ) {
274 if ( !$this->isCacheable() ) {
275 return 0;
276 }
277
278 $fname = get_class( $this ) . '::recache';
279 $dbw = wfGetDB( DB_MASTER );
280 $dbr = wfGetDB( DB_SLAVE, array( $this->getName(), __METHOD__, 'vslow' ) );
281 if ( !$dbw || !$dbr ) {
282 return false;
283 }
284
285 if ( $ignoreErrors ) {
286 $ignoreW = $dbw->ignoreErrors( true );
287 $ignoreR = $dbr->ignoreErrors( true );
288 }
289
290 # Clear out any old cached data
291 $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
292 # Do query
293 $res = $this->reallyDoQuery( $limit, false );
294 $num = false;
295 if ( $res ) {
296 $num = $dbr->numRows( $res );
297 # Fetch results
298 $vals = array();
299 while ( $res && $row = $dbr->fetchObject( $res ) ) {
300 if ( isset( $row->value ) ) {
301 if ( $this->usesTimestamps() ) {
302 $value = wfTimestamp( TS_UNIX,
303 $row->value );
304 } else {
305 $value = intval( $row->value ); // @bug 14414
306 }
307 } else {
308 $value = 0;
309 }
310
311 $vals[] = array( 'qc_type' => $this->getName(),
312 'qc_namespace' => $row->namespace,
313 'qc_title' => $row->title,
314 'qc_value' => $value );
315 }
316
317 # Save results into the querycache table on the master
318 if ( count( $vals ) ) {
319 if ( !$dbw->insert( 'querycache', $vals, __METHOD__ ) ) {
320 // Set result to false to indicate error
321 $num = false;
322 }
323 }
324 if ( $ignoreErrors ) {
325 $dbw->ignoreErrors( $ignoreW );
326 $dbr->ignoreErrors( $ignoreR );
327 }
328
329 # Update the querycache_info record for the page
330 $dbw->delete( 'querycache_info', array( 'qci_type' => $this->getName() ), $fname );
331 $dbw->insert( 'querycache_info', array( 'qci_type' => $this->getName(), 'qci_timestamp' => $dbw->timestamp() ), $fname );
332
333 }
334 return $num;
335 }
336
337 /**
338 * Run the query and return the result
339 * @param $limit mixed Numerical limit or false for no limit
340 * @param $offset mixed Numerical offset or false for no offset
341 * @return ResultWrapper
342 * @since 1.18
343 */
344 function reallyDoQuery( $limit, $offset = false ) {
345 $fname = get_class( $this ) . "::reallyDoQuery";
346 $dbr = wfGetDB( DB_SLAVE );
347 $query = $this->getQueryInfo();
348 $order = $this->getOrderFields();
349 if ( $this->sortDescending() ) {
350 foreach ( $order as &$field ) {
351 $field .= ' DESC';
352 }
353 }
354 if ( is_array( $query ) ) {
355 $tables = isset( $query['tables'] ) ? (array)$query['tables'] : array();
356 $fields = isset( $query['fields'] ) ? (array)$query['fields'] : array();
357 $conds = isset( $query['conds'] ) ? (array)$query['conds'] : array();
358 $options = isset( $query['options'] ) ? (array)$query['options'] : array();
359 $join_conds = isset( $query['join_conds'] ) ? (array)$query['join_conds'] : array();
360 if ( count( $order ) ) {
361 $options['ORDER BY'] = implode( ', ', $order );
362 }
363 if ( $limit !== false ) {
364 $options['LIMIT'] = intval( $limit );
365 }
366 if ( $offset !== false ) {
367 $options['OFFSET'] = intval( $offset );
368 }
369
370 $res = $dbr->select( $tables, $fields, $conds, $fname,
371 $options, $join_conds
372 );
373 } else {
374 // Old-fashioned raw SQL style, deprecated
375 $sql = $this->getSQL();
376 $sql .= ' ORDER BY ' . implode( ', ', $order );
377 $sql = $dbr->limitResult( $sql, $limit, $offset );
378 $res = $dbr->query( $sql, $fname );
379 }
380 return $dbr->resultObject( $res );
381 }
382
383 /**
384 * Parameters and order changed in 1.18
385 *
386 * Somewhat deprecated, you probably want to be using execute()
387 */
388 function doQuery( $offset = false, $limit ) {
389 if ( $this->isCached() && $this->isCacheable() ) {
390 return $this->fetchFromCache( $limit, $offset );
391 } else {
392 return $this->reallyDoQuery( $limit, $offset );
393 }
394 }
395
396 /**
397 * Fetch the query results from the query cache
398 * @param $limit mixed Numerical limit or false for no limit
399 * @param $offset mixed Numerical offset or false for no offset
400 * @return ResultWrapper
401 * @since 1.18
402 */
403 function fetchFromCache( $limit, $offset = false ) {
404 $dbr = wfGetDB( DB_SLAVE );
405 $options = array ();
406 if ( $limit !== false ) {
407 $options['LIMIT'] = intval( $limit );
408 }
409 if ( $offset !== false ) {
410 $options['OFFSET'] = intval( $offset );
411 }
412 if ( $this->sortDescending() ) {
413 $options['ORDER BY'] = 'qc_value DESC';
414 } else {
415 $options['ORDER BY'] = 'qc_value ASC';
416 }
417 $res = $dbr->select( 'querycache', array( 'qc_type',
418 'qc_namespace AS namespace',
419 'qc_title AS title',
420 'qc_value AS value' ),
421 array( 'qc_type' => $this->getName() ),
422 __METHOD__, $options
423 );
424 return $dbr->resultObject( $res );
425 }
426
427 public function getCachedTimestamp() {
428 if ( !is_null( $this->cachedTimestamp ) ) {
429 $dbr = wfGetDB( DB_SLAVE );
430 $fname = get_class( $this ) . '::getCachedTimestamp';
431 $this->cachedTimestamp = $dbr->selectField( 'querycache_info', 'qci_timestamp',
432 array( 'qci_type' => $this->getName() ), $fname );
433 }
434 return $this->cachedTimestamp;
435 }
436
437 /**
438 * This is the actual workhorse. It does everything needed to make a
439 * real, honest-to-gosh query page.
440 */
441 function execute( $par ) {
442 global $wgUser, $wgOut, $wgLang;
443
444 if ( !$this->userCanExecute( $wgUser ) ) {
445 $this->displayRestrictionError();
446 return;
447 }
448
449 if ( $this->limit == 0 && $this->offset == 0 )
450 list( $this->limit, $this->offset ) = wfCheckLimits();
451 $dbr = wfGetDB( DB_SLAVE );
452
453 $this->setHeaders();
454 $wgOut->setSyndicated( $this->isSyndicated() );
455
456 if ( $this->isCached() && !$this->isCacheable() ) {
457 $wgOut->setSyndicated( false );
458 $wgOut->addWikiMsg( 'querypage-disabled' );
459 return 0;
460 }
461
462 // TODO: Use doQuery()
463 // $res = null;
464 if ( !$this->isCached() ) {
465 $res = $this->reallyDoQuery( $this->limit, $this->offset );
466 } else {
467 # Get the cached result
468 $res = $this->fetchFromCache( $this->limit, $this->offset );
469 if ( !$this->listoutput ) {
470
471 # Fetch the timestamp of this update
472 $ts = $this->getCachedTimestamp();
473
474 if ( $ts ) {
475 $updated = $wgLang->timeanddate( $ts, true, true );
476 $updateddate = $wgLang->date( $ts, true, true );
477 $updatedtime = $wgLang->time( $ts, true, true );
478 $wgOut->addMeta( 'Data-Cache-Time', $ts );
479 $wgOut->addInlineScript( "var dataCacheTime = '$ts';" );
480 $wgOut->addWikiMsg( 'perfcachedts', $updated, $updateddate, $updatedtime );
481 } else {
482 $wgOut->addWikiMsg( 'perfcached' );
483 }
484
485 # If updates on this page have been disabled, let the user know
486 # that the data set won't be refreshed for now
487 global $wgDisableQueryPageUpdate;
488 if ( is_array( $wgDisableQueryPageUpdate ) && in_array( $this->getName(), $wgDisableQueryPageUpdate ) ) {
489 $wgOut->addWikiMsg( 'querypage-no-updates' );
490 }
491
492 }
493
494 }
495
496 $this->numRows = $dbr->numRows( $res );
497
498 $this->preprocessResults( $dbr, $res );
499
500 $wgOut->addHTML( Xml::openElement( 'div', array( 'class' => 'mw-spcontent' ) ) );
501
502 # Top header and navigation
503 if ( $this->shownavigation ) {
504 $wgOut->addHTML( $this->getPageHeader() );
505 if ( $this->numRows > 0 ) {
506 $wgOut->addHTML( '<p>' . wfShowingResults( $this->offset, $this->numRows ) . '</p>' );
507 # Disable the "next" link when we reach the end
508 $paging = wfViewPrevNext( $this->offset, $this->limit,
509 $this->getTitle( $par ),
510 wfArrayToCGI( $this->linkParameters() ), ( $this->numRows < $this->limit ) );
511 $wgOut->addHTML( '<p>' . $paging . '</p>' );
512 } else {
513 # No results to show, so don't bother with "showing X of Y" etc.
514 # -- just let the user know and give up now
515 $wgOut->addHTML( '<p>' . wfMsgHtml( 'specialpage-empty' ) . '</p>' );
516 $wgOut->addHTML( Xml::closeElement( 'div' ) );
517 return;
518 }
519 }
520
521 # The actual results; specialist subclasses will want to handle this
522 # with more than a straight list, so we hand them the info, plus
523 # an OutputPage, and let them get on with it
524 $this->outputResults( $wgOut,
525 $wgUser->getSkin(),
526 $dbr, # Should use a ResultWrapper for this
527 $res,
528 $this->numRows,
529 $this->offset );
530
531 # Repeat the paging links at the bottom
532 if ( $this->shownavigation ) {
533 $wgOut->addHTML( '<p>' . $paging . '</p>' );
534 }
535
536 $wgOut->addHTML( Xml::closeElement( 'div' ) );
537
538 return $this->numRows;
539 }
540
541 /**
542 * Format and output report results using the given information plus
543 * OutputPage
544 *
545 * @param $out OutputPage to print to
546 * @param $skin Skin: user skin to use
547 * @param $dbr Database (read) connection to use
548 * @param $res Integer: result pointer
549 * @param $num Integer: number of available result rows
550 * @param $offset Integer: paging offset
551 */
552 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
553 global $wgContLang;
554
555 if ( $num > 0 ) {
556 $html = array();
557 if ( !$this->listoutput ) {
558 $html[] = $this->openList( $offset );
559 }
560
561 # $res might contain the whole 1,000 rows, so we read up to
562 # $num [should update this to use a Pager]
563 for ( $i = 0; $i < $num && $row = $dbr->fetchObject( $res ); $i++ ) {
564 $line = $this->formatResult( $skin, $row );
565 if ( $line ) {
566 $attr = ( isset( $row->usepatrol ) && $row->usepatrol && $row->patrolled == 0 )
567 ? ' class="not-patrolled"'
568 : '';
569 $html[] = $this->listoutput
570 ? $line
571 : "<li{$attr}>{$line}</li>\n";
572 }
573 }
574
575 # Flush the final result
576 if ( $this->tryLastResult() ) {
577 $row = null;
578 $line = $this->formatResult( $skin, $row );
579 if ( $line ) {
580 $attr = ( isset( $row->usepatrol ) && $row->usepatrol && $row->patrolled == 0 )
581 ? ' class="not-patrolled"'
582 : '';
583 $html[] = $this->listoutput
584 ? $line
585 : "<li{$attr}>{$line}</li>\n";
586 }
587 }
588
589 if ( !$this->listoutput ) {
590 $html[] = $this->closeList();
591 }
592
593 $html = $this->listoutput
594 ? $wgContLang->listToText( $html )
595 : implode( '', $html );
596
597 $out->addHTML( $html );
598 }
599 }
600
601 function openList( $offset ) {
602 return "\n<ol start='" . ( $offset + 1 ) . "' class='special'>\n";
603 }
604
605 function closeList() {
606 return "</ol>\n";
607 }
608
609 /**
610 * Do any necessary preprocessing of the result object.
611 */
612 function preprocessResults( $db, $res ) {}
613
614 /**
615 * Similar to above, but packaging in a syndicated feed instead of a web page
616 */
617 function doFeed( $class = '', $limit = 50 ) {
618 global $wgFeed, $wgFeedClasses;
619
620 if ( !$wgFeed ) {
621 global $wgOut;
622 $wgOut->addWikiMsg( 'feed-unavailable' );
623 return;
624 }
625
626 global $wgFeedLimit;
627 if ( $limit > $wgFeedLimit ) {
628 $limit = $wgFeedLimit;
629 }
630
631 if ( isset( $wgFeedClasses[$class] ) ) {
632 $feed = new $wgFeedClasses[$class](
633 $this->feedTitle(),
634 $this->feedDesc(),
635 $this->feedUrl() );
636 $feed->outHeader();
637
638 $res = $this->reallyDoQuery( $limit, 0 );
639 foreach ( $res as $obj ) {
640 $item = $this->feedResult( $obj );
641 if ( $item ) {
642 $feed->outItem( $item );
643 }
644 }
645
646 $feed->outFooter();
647 return true;
648 } else {
649 return false;
650 }
651 }
652
653 /**
654 * Override for custom handling. If the titles/links are ok, just do
655 * feedItemDesc()
656 */
657 function feedResult( $row ) {
658 if ( !isset( $row->title ) ) {
659 return null;
660 }
661 $title = Title::MakeTitle( intval( $row->namespace ), $row->title );
662 if ( $title ) {
663 $date = isset( $row->timestamp ) ? $row->timestamp : '';
664 $comments = '';
665 if ( $title ) {
666 $talkpage = $title->getTalkPage();
667 $comments = $talkpage->getFullURL();
668 }
669
670 return new FeedItem(
671 $title->getPrefixedText(),
672 $this->feedItemDesc( $row ),
673 $title->getFullURL(),
674 $date,
675 $this->feedItemAuthor( $row ),
676 $comments );
677 } else {
678 return null;
679 }
680 }
681
682 function feedItemDesc( $row ) {
683 return isset( $row->comment ) ? htmlspecialchars( $row->comment ) : '';
684 }
685
686 function feedItemAuthor( $row ) {
687 return isset( $row->user_text ) ? $row->user_text : '';
688 }
689
690 function feedTitle() {
691 global $wgLanguageCode, $wgSitename;
692 $page = SpecialPage::getPage( $this->getName() );
693 $desc = $page->getDescription();
694 return "$wgSitename - $desc [$wgLanguageCode]";
695 }
696
697 function feedDesc() {
698 return wfMsgExt( 'tagline', 'parsemag' );
699 }
700
701 function feedUrl() {
702 $title = SpecialPage::getTitleFor( $this->getName() );
703 return $title->getFullURL();
704 }
705 }
706
707 /**
708 * Class definition for a wanted query page like
709 * WantedPages, WantedTemplates, etc
710 */
711 abstract class WantedQueryPage extends QueryPage {
712
713 function isExpensive() {
714 return true;
715 }
716
717 function isSyndicated() {
718 return false;
719 }
720
721 /**
722 * Cache page existence for performance
723 */
724 function preprocessResults( $db, $res ) {
725 $batch = new LinkBatch;
726 foreach ( $res as $row ) {
727 $batch->add( $row->namespace, $row->title );
728 }
729 $batch->execute();
730
731 // Back to start for display
732 if ( $db->numRows( $res ) > 0 )
733 // If there are no rows we get an error seeking.
734 $db->dataSeek( $res, 0 );
735 }
736
737 /**
738 * Should formatResult() always check page existence, even if
739 * the results are fresh? This is a (hopefully temporary)
740 * kluge for Special:WantedFiles, which may contain false
741 * positives for files that exist e.g. in a shared repo (bug
742 * 6220).
743 */
744 function forceExistenceCheck() {
745 return false;
746 }
747
748 /**
749 * Format an individual result
750 *
751 * @param $skin Skin to use for UI elements
752 * @param $result Result row
753 * @return string
754 */
755 public function formatResult( $skin, $result ) {
756 $title = Title::makeTitleSafe( $result->namespace, $result->title );
757 if ( $title instanceof Title ) {
758 if ( $this->isCached() || $this->forceExistenceCheck() ) {
759 $pageLink = $title->isKnown()
760 ? '<del>' . $skin->link( $title ) . '</del>'
761 : $skin->link(
762 $title,
763 null,
764 array(),
765 array(),
766 array( 'broken' )
767 );
768 } else {
769 $pageLink = $skin->link(
770 $title,
771 null,
772 array(),
773 array(),
774 array( 'broken' )
775 );
776 }
777 return wfSpecialList( $pageLink, $this->makeWlhLink( $title, $skin, $result ) );
778 } else {
779 $tsafe = htmlspecialchars( $result->title );
780 return wfMsgHtml( 'wantedpages-badtitle', $tsafe );
781 }
782 }
783
784 /**
785 * Make a "what links here" link for a given title
786 *
787 * @param $title Title to make the link for
788 * @param $skin Skin object to use
789 * @param $result Object: result row
790 * @return string
791 */
792 private function makeWlhLink( $title, $skin, $result ) {
793 global $wgLang;
794 $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' );
795 $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ),
796 $wgLang->formatNum( $result->value ) );
797 return $skin->link( $wlh, $label, array(), array( 'target' => $title->getPrefixedText() ) );
798 }
799 }