Merge "Provide direction hinting in the personal toolbar"
[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 "
159 . "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 if ( !$dbw ) {
299 return false;
300 }
301
302 try {
303 # Do query
304 $res = $this->reallyDoQuery( $limit, false );
305 $num = false;
306 if ( $res ) {
307 $num = $res->numRows();
308 # Fetch results
309 $vals = array();
310 foreach ( $res as $row ) {
311 if ( isset( $row->value ) ) {
312 if ( $this->usesTimestamps() ) {
313 $value = wfTimestamp( TS_UNIX,
314 $row->value );
315 } else {
316 $value = intval( $row->value ); // @bug 14414
317 }
318 } else {
319 $value = 0;
320 }
321
322 $vals[] = array( 'qc_type' => $this->getName(),
323 'qc_namespace' => $row->namespace,
324 'qc_title' => $row->title,
325 'qc_value' => $value );
326 }
327
328 $dbw->begin( __METHOD__ );
329 # Clear out any old cached data
330 $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
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 $dbw->commit( __METHOD__ );
341 }
342 } catch ( DBError $e ) {
343 if ( !$ignoreErrors ) {
344 throw $e; // report query error
345 }
346 $num = false; // set result to false to indicate error
347 }
348
349 return $num;
350 }
351
352 /**
353 * Get a DB connection to be used for slow recache queries
354 */
355 function getRecacheDB() {
356 return wfGetDB( DB_SLAVE, array( $this->getName(), 'QueryPage::recache', 'vslow' ) );
357 }
358
359 /**
360 * Run the query and return the result
361 * @param int|bool $limit Numerical limit or false for no limit
362 * @param int|bool $offset Numerical offset or false for no offset
363 * @return ResultWrapper
364 * @since 1.18
365 */
366 function reallyDoQuery( $limit, $offset = false ) {
367 $fname = get_class( $this ) . "::reallyDoQuery";
368 $dbr = $this->getRecacheDB();
369 $query = $this->getQueryInfo();
370 $order = $this->getOrderFields();
371
372 if ( $this->sortDescending() ) {
373 foreach ( $order as &$field ) {
374 $field .= ' DESC';
375 }
376 }
377
378 if ( is_array( $query ) ) {
379 $tables = isset( $query['tables'] ) ? (array)$query['tables'] : array();
380 $fields = isset( $query['fields'] ) ? (array)$query['fields'] : array();
381 $conds = isset( $query['conds'] ) ? (array)$query['conds'] : array();
382 $options = isset( $query['options'] ) ? (array)$query['options'] : array();
383 $join_conds = isset( $query['join_conds'] ) ? (array)$query['join_conds'] : array();
384
385 if ( count( $order ) ) {
386 $options['ORDER BY'] = $order;
387 }
388
389 if ( $limit !== false ) {
390 $options['LIMIT'] = intval( $limit );
391 }
392
393 if ( $offset !== false ) {
394 $options['OFFSET'] = intval( $offset );
395 }
396
397 $res = $dbr->select( $tables, $fields, $conds, $fname,
398 $options, $join_conds
399 );
400 } else {
401 // Old-fashioned raw SQL style, deprecated
402 $sql = $this->getSQL();
403 $sql .= ' ORDER BY ' . implode( ', ', $order );
404 $sql = $dbr->limitResult( $sql, $limit, $offset );
405 $res = $dbr->query( $sql, $fname );
406 }
407
408 return $dbr->resultObject( $res );
409 }
410
411 /**
412 * Somewhat deprecated, you probably want to be using execute()
413 * @param int|bool $offset
414 * @param int|bool $limit
415 * @return ResultWrapper
416 */
417 function doQuery( $offset = false, $limit = false ) {
418 if ( $this->isCached() && $this->isCacheable() ) {
419 return $this->fetchFromCache( $limit, $offset );
420 } else {
421 return $this->reallyDoQuery( $limit, $offset );
422 }
423 }
424
425 /**
426 * Fetch the query results from the query cache
427 * @param int|bool $limit Numerical limit or false for no limit
428 * @param int|bool $offset Numerical offset or false for no offset
429 * @return ResultWrapper
430 * @since 1.18
431 */
432 function fetchFromCache( $limit, $offset = false ) {
433 $dbr = wfGetDB( DB_SLAVE );
434 $options = array();
435 if ( $limit !== false ) {
436 $options['LIMIT'] = intval( $limit );
437 }
438 if ( $offset !== false ) {
439 $options['OFFSET'] = intval( $offset );
440 }
441 if ( $this->sortDescending() ) {
442 $options['ORDER BY'] = 'qc_value DESC';
443 } else {
444 $options['ORDER BY'] = 'qc_value ASC';
445 }
446 $res = $dbr->select( 'querycache', array( 'qc_type',
447 'namespace' => 'qc_namespace',
448 'title' => 'qc_title',
449 'value' => 'qc_value' ),
450 array( 'qc_type' => $this->getName() ),
451 __METHOD__, $options
452 );
453 return $dbr->resultObject( $res );
454 }
455
456 public function getCachedTimestamp() {
457 if ( is_null( $this->cachedTimestamp ) ) {
458 $dbr = wfGetDB( DB_SLAVE );
459 $fname = get_class( $this ) . '::getCachedTimestamp';
460 $this->cachedTimestamp = $dbr->selectField( 'querycache_info', 'qci_timestamp',
461 array( 'qci_type' => $this->getName() ), $fname );
462 }
463 return $this->cachedTimestamp;
464 }
465
466 /**
467 * This is the actual workhorse. It does everything needed to make a
468 * real, honest-to-gosh query page.
469 * @param string $par
470 * @return int
471 */
472 function execute( $par ) {
473 global $wgQueryCacheLimit, $wgDisableQueryPageUpdate;
474
475 $user = $this->getUser();
476 if ( !$this->userCanExecute( $user ) ) {
477 $this->displayRestrictionError();
478 return;
479 }
480
481 $this->setHeaders();
482 $this->outputHeader();
483
484 $out = $this->getOutput();
485
486 if ( $this->isCached() && !$this->isCacheable() ) {
487 $out->addWikiMsg( 'querypage-disabled' );
488 return 0;
489 }
490
491 $out->setSyndicated( $this->isSyndicated() );
492
493 if ( $this->limit == 0 && $this->offset == 0 ) {
494 list( $this->limit, $this->offset ) = $this->getRequest()->getLimitOffset();
495 }
496
497 // TODO: Use doQuery()
498 if ( !$this->isCached() ) {
499 # select one extra row for navigation
500 $res = $this->reallyDoQuery( $this->limit + 1, $this->offset );
501 } else {
502 # Get the cached result, select one extra row for navigation
503 $res = $this->fetchFromCache( $this->limit + 1, $this->offset );
504 if ( !$this->listoutput ) {
505
506 # Fetch the timestamp of this update
507 $ts = $this->getCachedTimestamp();
508 $lang = $this->getLanguage();
509 $maxResults = $lang->formatNum( $wgQueryCacheLimit );
510
511 if ( $ts ) {
512 $updated = $lang->userTimeAndDate( $ts, $user );
513 $updateddate = $lang->userDate( $ts, $user );
514 $updatedtime = $lang->userTime( $ts, $user );
515 $out->addMeta( 'Data-Cache-Time', $ts );
516 $out->addJsConfigVars( 'dataCacheTime', $ts );
517 $out->addWikiMsg( 'perfcachedts', $updated, $updateddate, $updatedtime, $maxResults );
518 } else {
519 $out->addWikiMsg( 'perfcached', $maxResults );
520 }
521
522 # If updates on this page have been disabled, let the user know
523 # that the data set won't be refreshed for now
524 if ( is_array( $wgDisableQueryPageUpdate )
525 && in_array( $this->getName(), $wgDisableQueryPageUpdate )
526 ) {
527 $out->wrapWikiMsg(
528 "<div class=\"mw-querypage-no-updates\">\n$1\n</div>",
529 'querypage-no-updates'
530 );
531 }
532 }
533 }
534
535 $this->numRows = $res->numRows();
536
537 $dbr = wfGetDB( DB_SLAVE );
538 $this->preprocessResults( $dbr, $res );
539
540 $out->addHTML( Xml::openElement( 'div', array( 'class' => 'mw-spcontent' ) ) );
541
542 # Top header and navigation
543 if ( $this->shownavigation ) {
544 $out->addHTML( $this->getPageHeader() );
545 if ( $this->numRows > 0 ) {
546 $out->addHTML( $this->msg( 'showingresultsinrange' )->numParams(
547 min( $this->numRows, $this->limit ), # do not show the one extra row, if exist
548 $this->offset + 1, (min( $this->numRows, $this->limit ) + $this->offset) )->parseAsBlock() );
549 # Disable the "next" link when we reach the end
550 $paging = $this->getLanguage()->viewPrevNext( $this->getPageTitle( $par ), $this->offset,
551 $this->limit, $this->linkParameters(), ( $this->numRows <= $this->limit ) );
552 $out->addHTML( '<p>' . $paging . '</p>' );
553 } else {
554 # No results to show, so don't bother with "showing X of Y" etc.
555 # -- just let the user know and give up now
556 $out->addWikiMsg( 'specialpage-empty' );
557 $out->addHTML( Xml::closeElement( 'div' ) );
558 return;
559 }
560 }
561
562 # The actual results; specialist subclasses will want to handle this
563 # with more than a straight list, so we hand them the info, plus
564 # an OutputPage, and let them get on with it
565 $this->outputResults( $out,
566 $this->getSkin(),
567 $dbr, # Should use a ResultWrapper for this
568 $res,
569 min( $this->numRows, $this->limit ), # do not format the one extra row, if exist
570 $this->offset );
571
572 # Repeat the paging links at the bottom
573 if ( $this->shownavigation ) {
574 $out->addHTML( '<p>' . $paging . '</p>' );
575 }
576
577 $out->addHTML( Xml::closeElement( 'div' ) );
578
579 return min( $this->numRows, $this->limit ); # do not return the one extra row, if exist
580 }
581
582 /**
583 * Format and output report results using the given information plus
584 * OutputPage
585 *
586 * @param OutputPage $out OutputPage to print to
587 * @param Skin $skin User skin to use
588 * @param DatabaseBase $dbr Database (read) connection to use
589 * @param int $res Result pointer
590 * @param int $num Number of available result rows
591 * @param int $offset Paging offset
592 */
593 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
594 global $wgContLang;
595
596 if ( $num > 0 ) {
597 $html = array();
598 if ( !$this->listoutput ) {
599 $html[] = $this->openList( $offset );
600 }
601
602 # $res might contain the whole 1,000 rows, so we read up to
603 # $num [should update this to use a Pager]
604 for ( $i = 0; $i < $num && $row = $res->fetchObject(); $i++ ) {
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 # Flush the final result
617 if ( $this->tryLastResult() ) {
618 $row = null;
619 $line = $this->formatResult( $skin, $row );
620 if ( $line ) {
621 $attr = ( isset( $row->usepatrol ) && $row->usepatrol && $row->patrolled == 0 )
622 ? ' class="not-patrolled"'
623 : '';
624 $html[] = $this->listoutput
625 ? $line
626 : "<li{$attr}>{$line}</li>\n";
627 }
628 }
629
630 if ( !$this->listoutput ) {
631 $html[] = $this->closeList();
632 }
633
634 $html = $this->listoutput
635 ? $wgContLang->listToText( $html )
636 : implode( '', $html );
637
638 $out->addHTML( $html );
639 }
640 }
641
642 /**
643 * @param $offset
644 * @return string
645 */
646 function openList( $offset ) {
647 return "\n<ol start='" . ( $offset + 1 ) . "' class='special'>\n";
648 }
649
650 /**
651 * @return string
652 */
653 function closeList() {
654 return "</ol>\n";
655 }
656
657 /**
658 * Do any necessary preprocessing of the result object.
659 * @param DatabaseBase $db
660 * @param ResultWrapper $res
661 */
662 function preprocessResults( $db, $res ) {}
663
664 /**
665 * Similar to above, but packaging in a syndicated feed instead of a web page
666 * @param string $class
667 * @param int $limit
668 * @return bool
669 */
670 function doFeed( $class = '', $limit = 50 ) {
671 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
672
673 if ( !$wgFeed ) {
674 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
675 return false;
676 }
677
678 $limit = min( $limit, $wgFeedLimit );
679
680 if ( isset( $wgFeedClasses[$class] ) ) {
681 $feed = new $wgFeedClasses[$class](
682 $this->feedTitle(),
683 $this->feedDesc(),
684 $this->feedUrl() );
685 $feed->outHeader();
686
687 $res = $this->reallyDoQuery( $limit, 0 );
688 foreach ( $res as $obj ) {
689 $item = $this->feedResult( $obj );
690 if ( $item ) {
691 $feed->outItem( $item );
692 }
693 }
694
695 $feed->outFooter();
696 return true;
697 } else {
698 return false;
699 }
700 }
701
702 /**
703 * Override for custom handling. If the titles/links are ok, just do
704 * feedItemDesc()
705 * @param object $row
706 * @return FeedItem|null
707 */
708 function feedResult( $row ) {
709 if ( !isset( $row->title ) ) {
710 return null;
711 }
712 $title = Title::makeTitle( intval( $row->namespace ), $row->title );
713 if ( $title ) {
714 $date = isset( $row->timestamp ) ? $row->timestamp : '';
715 $comments = '';
716 if ( $title ) {
717 $talkpage = $title->getTalkPage();
718 $comments = $talkpage->getFullURL();
719 }
720
721 return new FeedItem(
722 $title->getPrefixedText(),
723 $this->feedItemDesc( $row ),
724 $title->getFullURL(),
725 $date,
726 $this->feedItemAuthor( $row ),
727 $comments );
728 } else {
729 return null;
730 }
731 }
732
733 function feedItemDesc( $row ) {
734 return isset( $row->comment ) ? htmlspecialchars( $row->comment ) : '';
735 }
736
737 function feedItemAuthor( $row ) {
738 return isset( $row->user_text ) ? $row->user_text : '';
739 }
740
741 function feedTitle() {
742 global $wgLanguageCode, $wgSitename;
743 $desc = $this->getDescription();
744 return "$wgSitename - $desc [$wgLanguageCode]";
745 }
746
747 function feedDesc() {
748 return $this->msg( 'tagline' )->text();
749 }
750
751 function feedUrl() {
752 return $this->getPageTitle()->getFullURL();
753 }
754 }
755
756 /**
757 * Class definition for a wanted query page like
758 * WantedPages, WantedTemplates, etc
759 */
760 abstract class WantedQueryPage extends QueryPage {
761 function isExpensive() {
762 return true;
763 }
764
765 function isSyndicated() {
766 return false;
767 }
768
769 /**
770 * Cache page existence for performance
771 * @param DatabaseBase $db
772 * @param ResultWrapper $res
773 */
774 function preprocessResults( $db, $res ) {
775 if ( !$res->numRows() ) {
776 return;
777 }
778
779 $batch = new LinkBatch;
780 foreach ( $res as $row ) {
781 $batch->add( $row->namespace, $row->title );
782 }
783 $batch->execute();
784
785 // Back to start for display
786 $res->seek( 0 );
787 }
788
789 /**
790 * Should formatResult() always check page existence, even if
791 * the results are fresh? This is a (hopefully temporary)
792 * kluge for Special:WantedFiles, which may contain false
793 * positives for files that exist e.g. in a shared repo (bug
794 * 6220).
795 * @return bool
796 */
797 function forceExistenceCheck() {
798 return false;
799 }
800
801 /**
802 * Format an individual result
803 *
804 * @param Skin $skin Skin to use for UI elements
805 * @param object $result Result row
806 * @return string
807 */
808 public function formatResult( $skin, $result ) {
809 $title = Title::makeTitleSafe( $result->namespace, $result->title );
810 if ( $title instanceof Title ) {
811 if ( $this->isCached() || $this->forceExistenceCheck() ) {
812 $pageLink = $title->isKnown()
813 ? '<del>' . Linker::link( $title ) . '</del>'
814 : Linker::link(
815 $title,
816 null,
817 array(),
818 array(),
819 array( 'broken' )
820 );
821 } else {
822 $pageLink = Linker::link(
823 $title,
824 null,
825 array(),
826 array(),
827 array( 'broken' )
828 );
829 }
830 return $this->getLanguage()->specialList( $pageLink, $this->makeWlhLink( $title, $result ) );
831 } else {
832 return $this->msg( 'wantedpages-badtitle', $result->title )->escaped();
833 }
834 }
835
836 /**
837 * Make a "what links here" link for a given title
838 *
839 * @param Title $title Title to make the link for
840 * @param object $result Result row
841 * @return string
842 */
843 private function makeWlhLink( $title, $result ) {
844 $wlh = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
845 $label = $this->msg( 'nlinks' )->numParams( $result->value )->escaped();
846 return Linker::link( $wlh, $label );
847 }
848 }