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