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