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