Merge "HTMLForm: Use <button> and allow differing label and value"
[lhc/web/wiklou.git] / includes / specialpage / 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 static $qp = null;
64
65 if ( $qp === null ) {
66 // QueryPage subclass, Special page name
67 $qp = array(
68 array( 'AncientPagesPage', 'Ancientpages' ),
69 array( 'BrokenRedirectsPage', 'BrokenRedirects' ),
70 array( 'DeadendPagesPage', 'Deadendpages' ),
71 array( 'DoubleRedirectsPage', 'DoubleRedirects' ),
72 array( 'FileDuplicateSearchPage', 'FileDuplicateSearch' ),
73 array( 'ListDuplicatedFilesPage', 'ListDuplicatedFiles' ),
74 array( 'LinkSearchPage', 'LinkSearch' ),
75 array( 'ListredirectsPage', 'Listredirects' ),
76 array( 'LonelyPagesPage', 'Lonelypages' ),
77 array( 'LongPagesPage', 'Longpages' ),
78 array( 'MediaStatisticsPage', 'MediaStatistics' ),
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 Hooks::run( 'wgQueryPages', array( &$qp ) );
104 }
105
106 return $qp;
107 }
108
109 /**
110 * A mutator for $this->listoutput;
111 *
112 * @param bool $bool
113 */
114 function setListoutput( $bool ) {
115 $this->listoutput = $bool;
116 }
117
118 /**
119 * Subclasses return an SQL query here, formatted as an array with the
120 * following keys:
121 * tables => Table(s) for passing to Database::select()
122 * fields => Field(s) for passing to Database::select(), may be *
123 * conds => WHERE conditions
124 * options => options
125 * join_conds => JOIN conditions
126 *
127 * Note that the query itself should return the following three columns:
128 * 'namespace', 'title', and 'value'. 'value' is used for sorting.
129 *
130 * These may be stored in the querycache table for expensive queries,
131 * and that cached data will be returned sometimes, so the presence of
132 * extra fields can't be relied upon. The cached 'value' column will be
133 * an integer; non-numeric values are useful only for sorting the
134 * initial query (except if they're timestamps, see usesTimestamps()).
135 *
136 * Don't include an ORDER or LIMIT clause, they will be added.
137 *
138 * If this function is not overridden or returns something other than
139 * an array, getSQL() will be used instead. This is for backwards
140 * compatibility only and is strongly deprecated.
141 * @return array
142 * @since 1.18
143 */
144 public function getQueryInfo() {
145 return null;
146 }
147
148 /**
149 * For back-compat, subclasses may return a raw SQL query here, as a string.
150 * This is strongly deprecated; getQueryInfo() should be overridden instead.
151 * @throws MWException
152 * @return string
153 */
154 function getSQL() {
155 /* Implement getQueryInfo() instead */
156 throw new MWException( "Bug in a QueryPage: doesn't implement getQueryInfo() nor "
157 . "getQuery() properly" );
158 }
159
160 /**
161 * Subclasses return an array of fields to order by here. Don't append
162 * DESC to the field names, that'll be done automatically if
163 * sortDescending() returns true.
164 * @return array
165 * @since 1.18
166 */
167 function getOrderFields() {
168 return array( 'value' );
169 }
170
171 /**
172 * Does this query return timestamps rather than integers in its
173 * 'value' field? If true, this class will convert 'value' to a
174 * UNIX timestamp for caching.
175 * NOTE: formatRow() may get timestamps in TS_MW (mysql), TS_DB (pgsql)
176 * or TS_UNIX (querycache) format, so be sure to always run them
177 * through wfTimestamp()
178 * @return bool
179 * @since 1.18
180 */
181 public function usesTimestamps() {
182 return false;
183 }
184
185 /**
186 * Override to sort by increasing values
187 *
188 * @return bool
189 */
190 function sortDescending() {
191 return true;
192 }
193
194 /**
195 * Is this query expensive (for some definition of expensive)? Then we
196 * don't let it run in miser mode. $wgDisableQueryPages causes all query
197 * pages to be declared expensive. Some query pages are always expensive.
198 *
199 * @return bool
200 */
201 public function isExpensive() {
202 return $this->getConfig()->get( 'DisableQueryPages' );
203 }
204
205 /**
206 * Is the output of this query cacheable? Non-cacheable expensive pages
207 * will be disabled in miser mode and will not have their results written
208 * to the querycache table.
209 * @return bool
210 * @since 1.18
211 */
212 public function isCacheable() {
213 return true;
214 }
215
216 /**
217 * Whether or not the output of the page in question is retrieved from
218 * the database cache.
219 *
220 * @return bool
221 */
222 public function isCached() {
223 return $this->isExpensive() && $this->getConfig()->get( 'MiserMode' );
224 }
225
226 /**
227 * Sometime we don't want to build rss / atom feeds.
228 *
229 * @return bool
230 */
231 function isSyndicated() {
232 return true;
233 }
234
235 /**
236 * Formats the results of the query for display. The skin is the current
237 * skin; you can use it for making links. The result is a single row of
238 * result data. You should be able to grab SQL results off of it.
239 * If the function returns false, the line output will be skipped.
240 * @param Skin $skin
241 * @param object $result Result row
242 * @return string|bool String or false to skip
243 */
244 abstract function formatResult( $skin, $result );
245
246 /**
247 * The content returned by this function will be output before any result
248 *
249 * @return string
250 */
251 function getPageHeader() {
252 return '';
253 }
254
255 /**
256 * Outputs some kind of an informative message (via OutputPage) to let the
257 * user know that the query returned nothing and thus there's nothing to
258 * show.
259 *
260 * @since 1.26
261 */
262 protected function showEmptyText() {
263 $this->getOutput()->addWikiMsg( 'specialpage-empty' );
264 }
265
266 /**
267 * If using extra form wheely-dealies, return a set of parameters here
268 * as an associative array. They will be encoded and added to the paging
269 * links (prev/next/lengths).
270 *
271 * @return array
272 */
273 function linkParameters() {
274 return array();
275 }
276
277 /**
278 * Some special pages (for example SpecialListusers) might not return the
279 * current object formatted, but return the previous one instead.
280 * Setting this to return true will ensure formatResult() is called
281 * one more time to make sure that the very last result is formatted
282 * as well.
283 * @return bool
284 */
285 function tryLastResult() {
286 return false;
287 }
288
289 /**
290 * Clear the cache and save new results
291 *
292 * @param int|bool $limit Limit for SQL statement
293 * @param bool $ignoreErrors Whether to ignore database errors
294 * @throws DBError|Exception
295 * @return bool|int
296 */
297 public function recache( $limit, $ignoreErrors = true ) {
298 if ( !$this->isCacheable() ) {
299 return 0;
300 }
301
302 $fname = get_class( $this ) . '::recache';
303 $dbw = wfGetDB( DB_MASTER );
304 if ( !$dbw ) {
305 return false;
306 }
307
308 try {
309 # Do query
310 $res = $this->reallyDoQuery( $limit, false );
311 $num = false;
312 if ( $res ) {
313 $num = $res->numRows();
314 # Fetch results
315 $vals = array();
316 foreach ( $res as $row ) {
317 if ( isset( $row->value ) ) {
318 if ( $this->usesTimestamps() ) {
319 $value = wfTimestamp( TS_UNIX,
320 $row->value );
321 } else {
322 $value = intval( $row->value ); // @bug 14414
323 }
324 } else {
325 $value = 0;
326 }
327
328 $vals[] = array(
329 'qc_type' => $this->getName(),
330 'qc_namespace' => $row->namespace,
331 'qc_title' => $row->title,
332 'qc_value' => $value
333 );
334 }
335
336 $that = $this;
337 $dbw->doAtomicSection(
338 __METHOD__,
339 function ( IDatabase $dbw, $fname ) use ( $that, $vals ) {
340 # Clear out any old cached data
341 $dbw->delete( 'querycache',
342 array( 'qc_type' => $that->getName() ),
343 $fname
344 );
345 # Save results into the querycache table on the master
346 if ( count( $vals ) ) {
347 $dbw->insert( 'querycache', $vals, $fname );
348 }
349 # Update the querycache_info record for the page
350 $dbw->delete( 'querycache_info',
351 array( 'qci_type' => $that->getName() ),
352 $fname
353 );
354 $dbw->insert( 'querycache_info',
355 array( 'qci_type' => $that->getName(),
356 'qci_timestamp' => $dbw->timestamp() ),
357 $fname
358 );
359 }
360 );
361 }
362 } catch ( DBError $e ) {
363 if ( !$ignoreErrors ) {
364 throw $e; // report query error
365 }
366 $num = false; // set result to false to indicate error
367 }
368
369 return $num;
370 }
371
372 /**
373 * Get a DB connection to be used for slow recache queries
374 * @return IDatabase
375 */
376 function getRecacheDB() {
377 return wfGetDB( DB_SLAVE, array( $this->getName(), 'QueryPage::recache', 'vslow' ) );
378 }
379
380 /**
381 * Run the query and return the result
382 * @param int|bool $limit Numerical limit or false for no limit
383 * @param int|bool $offset Numerical offset or false for no offset
384 * @return ResultWrapper
385 * @since 1.18
386 */
387 public function reallyDoQuery( $limit, $offset = false ) {
388 $fname = get_class( $this ) . "::reallyDoQuery";
389 $dbr = $this->getRecacheDB();
390 $query = $this->getQueryInfo();
391 $order = $this->getOrderFields();
392
393 if ( $this->sortDescending() ) {
394 foreach ( $order as &$field ) {
395 $field .= ' DESC';
396 }
397 }
398
399 if ( is_array( $query ) ) {
400 $tables = isset( $query['tables'] ) ? (array)$query['tables'] : array();
401 $fields = isset( $query['fields'] ) ? (array)$query['fields'] : array();
402 $conds = isset( $query['conds'] ) ? (array)$query['conds'] : array();
403 $options = isset( $query['options'] ) ? (array)$query['options'] : array();
404 $join_conds = isset( $query['join_conds'] ) ? (array)$query['join_conds'] : array();
405
406 if ( count( $order ) ) {
407 $options['ORDER BY'] = $order;
408 }
409
410 if ( $limit !== false ) {
411 $options['LIMIT'] = intval( $limit );
412 }
413
414 if ( $offset !== false ) {
415 $options['OFFSET'] = intval( $offset );
416 }
417
418 $res = $dbr->select( $tables, $fields, $conds, $fname,
419 $options, $join_conds
420 );
421 } else {
422 // Old-fashioned raw SQL style, deprecated
423 $sql = $this->getSQL();
424 $sql .= ' ORDER BY ' . implode( ', ', $order );
425 $sql = $dbr->limitResult( $sql, $limit, $offset );
426 $res = $dbr->query( $sql, $fname );
427 }
428
429 return $res;
430 }
431
432 /**
433 * Somewhat deprecated, you probably want to be using execute()
434 * @param int|bool $offset
435 * @param int|bool $limit
436 * @return ResultWrapper
437 */
438 public function doQuery( $offset = false, $limit = false ) {
439 if ( $this->isCached() && $this->isCacheable() ) {
440 return $this->fetchFromCache( $limit, $offset );
441 } else {
442 return $this->reallyDoQuery( $limit, $offset );
443 }
444 }
445
446 /**
447 * Fetch the query results from the query cache
448 * @param int|bool $limit Numerical limit or false for no limit
449 * @param int|bool $offset Numerical offset or false for no offset
450 * @return ResultWrapper
451 * @since 1.18
452 */
453 public function fetchFromCache( $limit, $offset = false ) {
454 $dbr = wfGetDB( DB_SLAVE );
455 $options = array();
456 if ( $limit !== false ) {
457 $options['LIMIT'] = intval( $limit );
458 }
459 if ( $offset !== false ) {
460 $options['OFFSET'] = intval( $offset );
461 }
462 if ( $this->sortDescending() ) {
463 $options['ORDER BY'] = 'qc_value DESC';
464 } else {
465 $options['ORDER BY'] = 'qc_value ASC';
466 }
467 return $dbr->select( 'querycache', array( 'qc_type',
468 'namespace' => 'qc_namespace',
469 'title' => 'qc_title',
470 'value' => 'qc_value' ),
471 array( 'qc_type' => $this->getName() ),
472 __METHOD__, $options
473 );
474 }
475
476 public function getCachedTimestamp() {
477 if ( is_null( $this->cachedTimestamp ) ) {
478 $dbr = wfGetDB( DB_SLAVE );
479 $fname = get_class( $this ) . '::getCachedTimestamp';
480 $this->cachedTimestamp = $dbr->selectField( 'querycache_info', 'qci_timestamp',
481 array( 'qci_type' => $this->getName() ), $fname );
482 }
483 return $this->cachedTimestamp;
484 }
485
486 /**
487 * Returns limit and offset, as returned by $this->getRequest()->getLimitOffset().
488 * Subclasses may override this to further restrict or modify limit and offset.
489 *
490 * @note Restricts the offset parameter, as most query pages have inefficient paging
491 * @since 1.26
492 *
493 * @return int[] list( $limit, $offset )
494 */
495 protected function getLimitOffset() {
496 list( $limit, $offset ) = $this->getRequest()->getLimitOffset();
497 if ( !$this->getConfig()->get( 'MiserMode' ) ) {
498 $maxResults = $this->getMaxResults();
499 // Can't display more than max results on a page
500 $limit = min( $limit, $maxResults );
501 // Can't skip over more than $maxResults
502 $offset = min( $offset, $maxResults );
503 // Can't let $offset + $limit > $maxResults
504 $limit = min( $limit, $maxResults - $offset );
505 }
506 return array( $limit, $offset );
507 }
508
509 /**
510 * Get max number of results we can return in miser mode.
511 *
512 * Most QueryPage subclasses use inefficient paging, so limit the max amount we return
513 * This matters for uncached query pages that might otherwise accept an offset of 3 million
514 *
515 * @since 1.27
516 * @return int
517 */
518 protected function getMaxResults() {
519 // Max of 10000, unless we store more than 5000 in query cache.
520 return max( $this->getConfig()->get( 'QueryCacheLimit' ), 10000 );
521 }
522
523 /**
524 * This is the actual workhorse. It does everything needed to make a
525 * real, honest-to-gosh query page.
526 * @param string $par
527 */
528 public function execute( $par ) {
529 $user = $this->getUser();
530 if ( !$this->userCanExecute( $user ) ) {
531 $this->displayRestrictionError();
532 return;
533 }
534
535 $this->setHeaders();
536 $this->outputHeader();
537
538 $out = $this->getOutput();
539
540 if ( $this->isCached() && !$this->isCacheable() ) {
541 $out->addWikiMsg( 'querypage-disabled' );
542 return;
543 }
544
545 $out->setSyndicated( $this->isSyndicated() );
546
547 if ( $this->limit == 0 && $this->offset == 0 ) {
548 list( $this->limit, $this->offset ) = $this->getLimitOffset();
549 }
550
551 // @todo Use doQuery()
552 if ( !$this->isCached() ) {
553 # select one extra row for navigation
554 $res = $this->reallyDoQuery( $this->limit + 1, $this->offset );
555 } else {
556 # Get the cached result, select one extra row for navigation
557 $res = $this->fetchFromCache( $this->limit + 1, $this->offset );
558 if ( !$this->listoutput ) {
559
560 # Fetch the timestamp of this update
561 $ts = $this->getCachedTimestamp();
562 $lang = $this->getLanguage();
563 $maxResults = $lang->formatNum( $this->getConfig()->get( 'QueryCacheLimit' ) );
564
565 if ( $ts ) {
566 $updated = $lang->userTimeAndDate( $ts, $user );
567 $updateddate = $lang->userDate( $ts, $user );
568 $updatedtime = $lang->userTime( $ts, $user );
569 $out->addMeta( 'Data-Cache-Time', $ts );
570 $out->addJsConfigVars( 'dataCacheTime', $ts );
571 $out->addWikiMsg( 'perfcachedts', $updated, $updateddate, $updatedtime, $maxResults );
572 } else {
573 $out->addWikiMsg( 'perfcached', $maxResults );
574 }
575
576 # If updates on this page have been disabled, let the user know
577 # that the data set won't be refreshed for now
578 if ( is_array( $this->getConfig()->get( 'DisableQueryPageUpdate' ) )
579 && in_array( $this->getName(), $this->getConfig()->get( 'DisableQueryPageUpdate' ) )
580 ) {
581 $out->wrapWikiMsg(
582 "<div class=\"mw-querypage-no-updates\">\n$1\n</div>",
583 'querypage-no-updates'
584 );
585 }
586 }
587 }
588
589 $this->numRows = $res->numRows();
590
591 $dbr = $this->getRecacheDB();
592 $this->preprocessResults( $dbr, $res );
593
594 $out->addHTML( Xml::openElement( 'div', array( 'class' => 'mw-spcontent' ) ) );
595
596 # Top header and navigation
597 if ( $this->shownavigation ) {
598 $out->addHTML( $this->getPageHeader() );
599 if ( $this->numRows > 0 ) {
600 $out->addHTML( $this->msg( 'showingresultsinrange' )->numParams(
601 min( $this->numRows, $this->limit ), # do not show the one extra row, if exist
602 $this->offset + 1, ( min( $this->numRows, $this->limit ) + $this->offset ) )->parseAsBlock() );
603 # Disable the "next" link when we reach the end
604 $atEnd = ( $this->numRows <= $this->limit )
605 || ( $this->offset + $this-> limit >= $this->getMaxResults() );
606 $paging = $this->getLanguage()->viewPrevNext( $this->getPageTitle( $par ), $this->offset,
607 $this->limit, $this->linkParameters(), $atEnd );
608 $out->addHTML( '<p>' . $paging . '</p>' );
609 } else {
610 # No results to show, so don't bother with "showing X of Y" etc.
611 # -- just let the user know and give up now
612 $this->showEmptyText();
613 $out->addHTML( Xml::closeElement( 'div' ) );
614 return;
615 }
616 }
617
618 # The actual results; specialist subclasses will want to handle this
619 # with more than a straight list, so we hand them the info, plus
620 # an OutputPage, and let them get on with it
621 $this->outputResults( $out,
622 $this->getSkin(),
623 $dbr, # Should use a ResultWrapper for this
624 $res,
625 min( $this->numRows, $this->limit ), # do not format the one extra row, if exist
626 $this->offset );
627
628 # Repeat the paging links at the bottom
629 if ( $this->shownavigation ) {
630 $out->addHTML( '<p>' . $paging . '</p>' );
631 }
632
633 $out->addHTML( Xml::closeElement( 'div' ) );
634 }
635
636 /**
637 * Format and output report results using the given information plus
638 * OutputPage
639 *
640 * @param OutputPage $out OutputPage to print to
641 * @param Skin $skin User skin to use
642 * @param IDatabase $dbr Database (read) connection to use
643 * @param ResultWrapper $res Result pointer
644 * @param int $num Number of available result rows
645 * @param int $offset Paging offset
646 */
647 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
648 global $wgContLang;
649
650 if ( $num > 0 ) {
651 $html = array();
652 if ( !$this->listoutput ) {
653 $html[] = $this->openList( $offset );
654 }
655
656 # $res might contain the whole 1,000 rows, so we read up to
657 # $num [should update this to use a Pager]
658 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
659 for ( $i = 0; $i < $num && $row = $res->fetchObject(); $i++ ) {
660 // @codingStandardsIgnoreEnd
661 $line = $this->formatResult( $skin, $row );
662 if ( $line ) {
663 $attr = ( isset( $row->usepatrol ) && $row->usepatrol && $row->patrolled == 0 )
664 ? ' class="not-patrolled"'
665 : '';
666 $html[] = $this->listoutput
667 ? $line
668 : "<li{$attr}>{$line}</li>\n";
669 }
670 }
671
672 # Flush the final result
673 if ( $this->tryLastResult() ) {
674 $row = null;
675 $line = $this->formatResult( $skin, $row );
676 if ( $line ) {
677 $attr = ( isset( $row->usepatrol ) && $row->usepatrol && $row->patrolled == 0 )
678 ? ' class="not-patrolled"'
679 : '';
680 $html[] = $this->listoutput
681 ? $line
682 : "<li{$attr}>{$line}</li>\n";
683 }
684 }
685
686 if ( !$this->listoutput ) {
687 $html[] = $this->closeList();
688 }
689
690 $html = $this->listoutput
691 ? $wgContLang->listToText( $html )
692 : implode( '', $html );
693
694 $out->addHTML( $html );
695 }
696 }
697
698 /**
699 * @param int $offset
700 * @return string
701 */
702 function openList( $offset ) {
703 return "\n<ol start='" . ( $offset + 1 ) . "' class='special'>\n";
704 }
705
706 /**
707 * @return string
708 */
709 function closeList() {
710 return "</ol>\n";
711 }
712
713 /**
714 * Do any necessary preprocessing of the result object.
715 * @param IDatabase $db
716 * @param ResultWrapper $res
717 */
718 function preprocessResults( $db, $res ) {
719 }
720
721 /**
722 * Similar to above, but packaging in a syndicated feed instead of a web page
723 * @param string $class
724 * @param int $limit
725 * @return bool
726 */
727 function doFeed( $class = '', $limit = 50 ) {
728 if ( !$this->getConfig()->get( 'Feed' ) ) {
729 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
730 return false;
731 }
732
733 $limit = min( $limit, $this->getConfig()->get( 'FeedLimit' ) );
734
735 $feedClasses = $this->getConfig()->get( 'FeedClasses' );
736 if ( isset( $feedClasses[$class] ) ) {
737 /** @var RSSFeed|AtomFeed $feed */
738 $feed = new $feedClasses[$class](
739 $this->feedTitle(),
740 $this->feedDesc(),
741 $this->feedUrl() );
742 $feed->outHeader();
743
744 $res = $this->reallyDoQuery( $limit, 0 );
745 foreach ( $res as $obj ) {
746 $item = $this->feedResult( $obj );
747 if ( $item ) {
748 $feed->outItem( $item );
749 }
750 }
751
752 $feed->outFooter();
753 return true;
754 } else {
755 return false;
756 }
757 }
758
759 /**
760 * Override for custom handling. If the titles/links are ok, just do
761 * feedItemDesc()
762 * @param object $row
763 * @return FeedItem|null
764 */
765 function feedResult( $row ) {
766 if ( !isset( $row->title ) ) {
767 return null;
768 }
769 $title = Title::makeTitle( intval( $row->namespace ), $row->title );
770 if ( $title ) {
771 $date = isset( $row->timestamp ) ? $row->timestamp : '';
772 $comments = '';
773 if ( $title ) {
774 $talkpage = $title->getTalkPage();
775 $comments = $talkpage->getFullURL();
776 }
777
778 return new FeedItem(
779 $title->getPrefixedText(),
780 $this->feedItemDesc( $row ),
781 $title->getFullURL(),
782 $date,
783 $this->feedItemAuthor( $row ),
784 $comments );
785 } else {
786 return null;
787 }
788 }
789
790 function feedItemDesc( $row ) {
791 return isset( $row->comment ) ? htmlspecialchars( $row->comment ) : '';
792 }
793
794 function feedItemAuthor( $row ) {
795 return isset( $row->user_text ) ? $row->user_text : '';
796 }
797
798 function feedTitle() {
799 $desc = $this->getDescription();
800 $code = $this->getConfig()->get( 'LanguageCode' );
801 $sitename = $this->getConfig()->get( 'Sitename' );
802 return "$sitename - $desc [$code]";
803 }
804
805 function feedDesc() {
806 return $this->msg( 'tagline' )->text();
807 }
808
809 function feedUrl() {
810 return $this->getPageTitle()->getFullURL();
811 }
812 }