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