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