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