Merge "resourceloader: Tidy up RL to simplify ResourceLoaderEditToolbarModule"
[lhc/web/wiklou.git] / includes / actions / InfoAction.php
1 <?php
2 /**
3 * Displays information about a page.
4 *
5 * Copyright © 2011 Alexandre Emsenhuber
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 *
21 * @file
22 * @ingroup Actions
23 */
24
25 /**
26 * Displays information about a page.
27 *
28 * @ingroup Actions
29 */
30 class InfoAction extends FormlessAction {
31 const CACHE_VERSION = '2013-03-17';
32
33 /**
34 * Returns the name of the action this object responds to.
35 *
36 * @return string Lowercase name
37 */
38 public function getName() {
39 return 'info';
40 }
41
42 /**
43 * Whether this action can still be executed by a blocked user.
44 *
45 * @return bool
46 */
47 public function requiresUnblock() {
48 return false;
49 }
50
51 /**
52 * Whether this action requires the wiki not to be locked.
53 *
54 * @return bool
55 */
56 public function requiresWrite() {
57 return false;
58 }
59
60 /**
61 * Clear the info cache for a given Title.
62 *
63 * @since 1.22
64 * @param Title $title Title to clear cache for
65 * @param int|null $revid Revision id to clear
66 */
67 public static function invalidateCache( Title $title, $revid = null ) {
68 $cache = ObjectCache::getMainWANInstance();
69
70 if ( !$revid ) {
71 $revision = Revision::newFromTitle( $title, 0, Revision::READ_LATEST );
72 $revid = $revision ? $revision->getId() : null;
73 }
74 if ( $revid !== null ) {
75 $key = wfMemcKey( 'infoaction', sha1( $title->getPrefixedText() ), $revid );
76 $cache->delete( $key );
77 }
78 }
79
80 /**
81 * Shows page information on GET request.
82 *
83 * @return string Page information that will be added to the output
84 */
85 public function onView() {
86 $content = '';
87
88 // Validate revision
89 $oldid = $this->page->getOldID();
90 if ( $oldid ) {
91 $revision = $this->page->getRevisionFetched();
92
93 // Revision is missing
94 if ( $revision === null ) {
95 return $this->msg( 'missing-revision', $oldid )->parse();
96 }
97
98 // Revision is not current
99 if ( !$revision->isCurrent() ) {
100 return $this->msg( 'pageinfo-not-current' )->plain();
101 }
102 }
103
104 // Page header
105 if ( !$this->msg( 'pageinfo-header' )->isDisabled() ) {
106 $content .= $this->msg( 'pageinfo-header' )->parse();
107 }
108
109 // Hide "This page is a member of # hidden categories" explanation
110 $content .= Html::element( 'style', array(),
111 '.mw-hiddenCategoriesExplanation { display: none; }' ) . "\n";
112
113 // Hide "Templates used on this page" explanation
114 $content .= Html::element( 'style', array(),
115 '.mw-templatesUsedExplanation { display: none; }' ) . "\n";
116
117 // Get page information
118 $pageInfo = $this->pageInfo();
119
120 // Allow extensions to add additional information
121 Hooks::run( 'InfoAction', array( $this->getContext(), &$pageInfo ) );
122
123 // Render page information
124 foreach ( $pageInfo as $header => $infoTable ) {
125 // Messages:
126 // pageinfo-header-basic, pageinfo-header-edits, pageinfo-header-restrictions,
127 // pageinfo-header-properties, pageinfo-category-info
128 $content .= $this->makeHeader( $this->msg( "pageinfo-${header}" )->escaped() ) . "\n";
129 $table = "\n";
130 foreach ( $infoTable as $infoRow ) {
131 $name = ( $infoRow[0] instanceof Message ) ? $infoRow[0]->escaped() : $infoRow[0];
132 $value = ( $infoRow[1] instanceof Message ) ? $infoRow[1]->escaped() : $infoRow[1];
133 $id = ( $infoRow[0] instanceof Message ) ? $infoRow[0]->getKey() : null;
134 $table = $this->addRow( $table, $name, $value, $id ) . "\n";
135 }
136 $content = $this->addTable( $content, $table ) . "\n";
137 }
138
139 // Page footer
140 if ( !$this->msg( 'pageinfo-footer' )->isDisabled() ) {
141 $content .= $this->msg( 'pageinfo-footer' )->parse();
142 }
143
144 // Page credits
145 /*if ( $this->page->exists() ) {
146 $content .= Html::rawElement( 'div', array( 'id' => 'mw-credits' ), $this->getContributors() );
147 }*/
148
149 return $content;
150 }
151
152 /**
153 * Creates a header that can be added to the output.
154 *
155 * @param string $header The header text.
156 * @return string The HTML.
157 */
158 protected function makeHeader( $header ) {
159 $spanAttribs = array( 'class' => 'mw-headline', 'id' => Sanitizer::escapeId( $header ) );
160
161 return Html::rawElement( 'h2', array(), Html::element( 'span', $spanAttribs, $header ) );
162 }
163
164 /**
165 * Adds a row to a table that will be added to the content.
166 *
167 * @param string $table The table that will be added to the content
168 * @param string $name The name of the row
169 * @param string $value The value of the row
170 * @param string $id The ID to use for the 'tr' element
171 * @return string The table with the row added
172 */
173 protected function addRow( $table, $name, $value, $id ) {
174 return $table . Html::rawElement( 'tr', $id === null ? array() : array( 'id' => 'mw-' . $id ),
175 Html::rawElement( 'td', array( 'style' => 'vertical-align: top;' ), $name ) .
176 Html::rawElement( 'td', array(), $value )
177 );
178 }
179
180 /**
181 * Adds a table to the content that will be added to the output.
182 *
183 * @param string $content The content that will be added to the output
184 * @param string $table The table
185 * @return string The content with the table added
186 */
187 protected function addTable( $content, $table ) {
188 return $content . Html::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
189 $table );
190 }
191
192 /**
193 * Returns page information in an easily-manipulated format. Array keys are used so extensions
194 * may add additional information in arbitrary positions. Array values are arrays with one
195 * element to be rendered as a header, arrays with two elements to be rendered as a table row.
196 *
197 * @return array
198 */
199 protected function pageInfo() {
200 global $wgContLang;
201
202 $user = $this->getUser();
203 $lang = $this->getLanguage();
204 $title = $this->getTitle();
205 $id = $title->getArticleID();
206 $config = $this->context->getConfig();
207
208 $cache = ObjectCache::getMainWANInstance();
209 $memcKey = wfMemcKey( 'infoaction',
210 sha1( $title->getPrefixedText() ), $this->page->getLatest() );
211 $pageCounts = $cache->get( $memcKey );
212 $version = isset( $pageCounts['cacheversion'] ) ? $pageCounts['cacheversion'] : false;
213 if ( $pageCounts === false || $version !== self::CACHE_VERSION ) {
214 // Get page information that would be too "expensive" to retrieve by normal means
215 $pageCounts = $this->pageCounts( $title );
216 $pageCounts['cacheversion'] = self::CACHE_VERSION;
217
218 $cache->set( $memcKey, $pageCounts );
219 }
220
221 // Get page properties
222 $dbr = wfGetDB( DB_SLAVE );
223 $result = $dbr->select(
224 'page_props',
225 array( 'pp_propname', 'pp_value' ),
226 array( 'pp_page' => $id ),
227 __METHOD__
228 );
229
230 $pageProperties = array();
231 foreach ( $result as $row ) {
232 $pageProperties[$row->pp_propname] = $row->pp_value;
233 }
234
235 // Basic information
236 $pageInfo = array();
237 $pageInfo['header-basic'] = array();
238
239 // Display title
240 $displayTitle = $title->getPrefixedText();
241 if ( isset( $pageProperties['displaytitle'] ) ) {
242 $displayTitle = $pageProperties['displaytitle'];
243 }
244
245 $pageInfo['header-basic'][] = array(
246 $this->msg( 'pageinfo-display-title' ), $displayTitle
247 );
248
249 // Is it a redirect? If so, where to?
250 if ( $title->isRedirect() ) {
251 $pageInfo['header-basic'][] = array(
252 $this->msg( 'pageinfo-redirectsto' ),
253 Linker::link( $this->page->getRedirectTarget() ) .
254 $this->msg( 'word-separator' )->escaped() .
255 $this->msg( 'parentheses' )->rawParams( Linker::link(
256 $this->page->getRedirectTarget(),
257 $this->msg( 'pageinfo-redirectsto-info' )->escaped(),
258 array(),
259 array( 'action' => 'info' )
260 ) )->escaped()
261 );
262 }
263
264 // Default sort key
265 $sortKey = $title->getCategorySortkey();
266 if ( isset( $pageProperties['defaultsort'] ) ) {
267 $sortKey = $pageProperties['defaultsort'];
268 }
269
270 $sortKey = htmlspecialchars( $sortKey );
271 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-default-sort' ), $sortKey );
272
273 // Page length (in bytes)
274 $pageInfo['header-basic'][] = array(
275 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
276 );
277
278 // Page ID (number not localised, as it's a database ID)
279 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-article-id' ), $id );
280
281 // Language in which the page content is (supposed to be) written
282 $pageLang = $title->getPageLanguage()->getCode();
283
284 if ( $config->get( 'PageLanguageUseDB' )
285 && $this->getTitle()->userCan( 'pagelang', $this->getUser() )
286 ) {
287 // Link to Special:PageLanguage with pre-filled page title if user has permissions
288 $titleObj = SpecialPage::getTitleFor( 'PageLanguage', $title->getPrefixedText() );
289 $langDisp = Linker::link(
290 $titleObj,
291 $this->msg( 'pageinfo-language' )->escaped()
292 );
293 } else {
294 // Display just the message
295 $langDisp = $this->msg( 'pageinfo-language' )->escaped();
296 }
297
298 $pageInfo['header-basic'][] = array( $langDisp,
299 Language::fetchLanguageName( $pageLang, $lang->getCode() )
300 . ' ' . $this->msg( 'parentheses', $pageLang )->escaped() );
301
302 // Content model of the page
303 $pageInfo['header-basic'][] = array(
304 $this->msg( 'pageinfo-content-model' ),
305 htmlspecialchars( ContentHandler::getLocalizedName( $title->getContentModel() ) )
306 );
307
308 // Search engine status
309 $pOutput = new ParserOutput();
310 if ( isset( $pageProperties['noindex'] ) ) {
311 $pOutput->setIndexPolicy( 'noindex' );
312 }
313 if ( isset( $pageProperties['index'] ) ) {
314 $pOutput->setIndexPolicy( 'index' );
315 }
316
317 // Use robot policy logic
318 $policy = $this->page->getRobotPolicy( 'view', $pOutput );
319 $pageInfo['header-basic'][] = array(
320 // Messages: pageinfo-robot-index, pageinfo-robot-noindex
321 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
322 );
323
324 $unwatchedPageThreshold = $config->get( 'UnwatchedPageThreshold' );
325 if (
326 $user->isAllowed( 'unwatchedpages' ) ||
327 ( $unwatchedPageThreshold !== false &&
328 $pageCounts['watchers'] >= $unwatchedPageThreshold )
329 ) {
330 // Number of page watchers
331 $pageInfo['header-basic'][] = array(
332 $this->msg( 'pageinfo-watchers' ),
333 $lang->formatNum( $pageCounts['watchers'] )
334 );
335 if (
336 $config->get( 'ShowUpdatedMarker' ) &&
337 isset( $pageCounts['visitingWatchers'] )
338 ) {
339 $minToDisclose = $config->get( 'UnwatchedPageSecret' );
340 if ( $pageCounts['visitingWatchers'] > $minToDisclose ||
341 $user->isAllowed( 'unwatchedpages' ) ) {
342 $pageInfo['header-basic'][] = array(
343 $this->msg( 'pageinfo-visiting-watchers' ),
344 $lang->formatNum( $pageCounts['visitingWatchers'] )
345 );
346 } else {
347 $pageInfo['header-basic'][] = array(
348 $this->msg( 'pageinfo-visiting-watchers' ),
349 $this->msg( 'pageinfo-few-visiting-watchers' )
350 );
351 }
352 }
353 } elseif ( $unwatchedPageThreshold !== false ) {
354 $pageInfo['header-basic'][] = array(
355 $this->msg( 'pageinfo-watchers' ),
356 $this->msg( 'pageinfo-few-watchers' )->numParams( $unwatchedPageThreshold )
357 );
358 }
359
360 // Redirects to this page
361 $whatLinksHere = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
362 $pageInfo['header-basic'][] = array(
363 Linker::link(
364 $whatLinksHere,
365 $this->msg( 'pageinfo-redirects-name' )->escaped(),
366 array(),
367 array(
368 'hidelinks' => 1,
369 'hidetrans' => 1,
370 'hideimages' => $title->getNamespace() == NS_FILE
371 )
372 ),
373 $this->msg( 'pageinfo-redirects-value' )
374 ->numParams( count( $title->getRedirectsHere() ) )
375 );
376
377 // Is it counted as a content page?
378 if ( $this->page->isCountable() ) {
379 $pageInfo['header-basic'][] = array(
380 $this->msg( 'pageinfo-contentpage' ),
381 $this->msg( 'pageinfo-contentpage-yes' )
382 );
383 }
384
385 // Subpages of this page, if subpages are enabled for the current NS
386 if ( MWNamespace::hasSubpages( $title->getNamespace() ) ) {
387 $prefixIndex = SpecialPage::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
388 $pageInfo['header-basic'][] = array(
389 Linker::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
390 $this->msg( 'pageinfo-subpages-value' )
391 ->numParams(
392 $pageCounts['subpages']['total'],
393 $pageCounts['subpages']['redirects'],
394 $pageCounts['subpages']['nonredirects'] )
395 );
396 }
397
398 if ( $title->inNamespace( NS_CATEGORY ) ) {
399 $category = Category::newFromTitle( $title );
400
401 // $allCount is the total number of cat members,
402 // not the count of how many members are normal pages.
403 $allCount = (int)$category->getPageCount();
404 $subcatCount = (int)$category->getSubcatCount();
405 $fileCount = (int)$category->getFileCount();
406 $pagesCount = $allCount - $subcatCount - $fileCount;
407
408 $pageInfo['category-info'] = array(
409 array(
410 $this->msg( 'pageinfo-category-total' ),
411 $lang->formatNum( $allCount )
412 ),
413 array(
414 $this->msg( 'pageinfo-category-pages' ),
415 $lang->formatNum( $pagesCount )
416 ),
417 array(
418 $this->msg( 'pageinfo-category-subcats' ),
419 $lang->formatNum( $subcatCount )
420 ),
421 array(
422 $this->msg( 'pageinfo-category-files' ),
423 $lang->formatNum( $fileCount )
424 )
425 );
426 }
427
428 // Page protection
429 $pageInfo['header-restrictions'] = array();
430
431 // Is this page affected by the cascading protection of something which includes it?
432 if ( $title->isCascadeProtected() ) {
433 $cascadingFrom = '';
434 $sources = $title->getCascadeProtectionSources(); // Array deferencing is in PHP 5.4 :(
435
436 foreach ( $sources[0] as $sourceTitle ) {
437 $cascadingFrom .= Html::rawElement( 'li', array(), Linker::linkKnown( $sourceTitle ) );
438 }
439
440 $cascadingFrom = Html::rawElement( 'ul', array(), $cascadingFrom );
441 $pageInfo['header-restrictions'][] = array(
442 $this->msg( 'pageinfo-protect-cascading-from' ),
443 $cascadingFrom
444 );
445 }
446
447 // Is out protection set to cascade to other pages?
448 if ( $title->areRestrictionsCascading() ) {
449 $pageInfo['header-restrictions'][] = array(
450 $this->msg( 'pageinfo-protect-cascading' ),
451 $this->msg( 'pageinfo-protect-cascading-yes' )
452 );
453 }
454
455 // Page protection
456 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
457 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
458
459 if ( $protectionLevel == '' ) {
460 // Allow all users
461 $message = $this->msg( 'protect-default' )->escaped();
462 } else {
463 // Administrators only
464 // Messages: protect-level-autoconfirmed, protect-level-sysop
465 $message = $this->msg( "protect-level-$protectionLevel" );
466 if ( $message->isDisabled() ) {
467 // Require "$1" permission
468 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
469 } else {
470 $message = $message->escaped();
471 }
472 }
473 $expiry = $title->getRestrictionExpiry( $restrictionType );
474 $formattedexpiry = $this->msg( 'parentheses',
475 $this->getLanguage()->formatExpiry( $expiry ) )->escaped();
476 $message .= $this->msg( 'word-separator' )->escaped() . $formattedexpiry;
477
478 // Messages: restriction-edit, restriction-move, restriction-create,
479 // restriction-upload
480 $pageInfo['header-restrictions'][] = array(
481 $this->msg( "restriction-$restrictionType" ), $message
482 );
483 }
484
485 if ( !$this->page->exists() ) {
486 return $pageInfo;
487 }
488
489 // Edit history
490 $pageInfo['header-edits'] = array();
491
492 $firstRev = $this->page->getOldestRevision();
493 $lastRev = $this->page->getRevision();
494 $batch = new LinkBatch;
495
496 if ( $firstRev ) {
497 $firstRevUser = $firstRev->getUserText( Revision::FOR_THIS_USER );
498 if ( $firstRevUser !== '' ) {
499 $batch->add( NS_USER, $firstRevUser );
500 $batch->add( NS_USER_TALK, $firstRevUser );
501 }
502 }
503
504 if ( $lastRev ) {
505 $lastRevUser = $lastRev->getUserText( Revision::FOR_THIS_USER );
506 if ( $lastRevUser !== '' ) {
507 $batch->add( NS_USER, $lastRevUser );
508 $batch->add( NS_USER_TALK, $lastRevUser );
509 }
510 }
511
512 $batch->execute();
513
514 if ( $firstRev ) {
515 // Page creator
516 $pageInfo['header-edits'][] = array(
517 $this->msg( 'pageinfo-firstuser' ),
518 Linker::revUserTools( $firstRev )
519 );
520
521 // Date of page creation
522 $pageInfo['header-edits'][] = array(
523 $this->msg( 'pageinfo-firsttime' ),
524 Linker::linkKnown(
525 $title,
526 htmlspecialchars( $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ) ),
527 array(),
528 array( 'oldid' => $firstRev->getId() )
529 )
530 );
531 }
532
533 if ( $lastRev ) {
534 // Latest editor
535 $pageInfo['header-edits'][] = array(
536 $this->msg( 'pageinfo-lastuser' ),
537 Linker::revUserTools( $lastRev )
538 );
539
540 // Date of latest edit
541 $pageInfo['header-edits'][] = array(
542 $this->msg( 'pageinfo-lasttime' ),
543 Linker::linkKnown(
544 $title,
545 htmlspecialchars( $lang->userTimeAndDate( $this->page->getTimestamp(), $user ) ),
546 array(),
547 array( 'oldid' => $this->page->getLatest() )
548 )
549 );
550 }
551
552 // Total number of edits
553 $pageInfo['header-edits'][] = array(
554 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
555 );
556
557 // Total number of distinct authors
558 if ( $pageCounts['authors'] > 0 ) {
559 $pageInfo['header-edits'][] = array(
560 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
561 );
562 }
563
564 // Recent number of edits (within past 30 days)
565 $pageInfo['header-edits'][] = array(
566 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $config->get( 'RCMaxAge' ) ) ),
567 $lang->formatNum( $pageCounts['recent_edits'] )
568 );
569
570 // Recent number of distinct authors
571 $pageInfo['header-edits'][] = array(
572 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
573 );
574
575 // Array of MagicWord objects
576 $magicWords = MagicWord::getDoubleUnderscoreArray();
577
578 // Array of magic word IDs
579 $wordIDs = $magicWords->names;
580
581 // Array of IDs => localized magic words
582 $localizedWords = $wgContLang->getMagicWords();
583
584 $listItems = array();
585 foreach ( $pageProperties as $property => $value ) {
586 if ( in_array( $property, $wordIDs ) ) {
587 $listItems[] = Html::element( 'li', array(), $localizedWords[$property][1] );
588 }
589 }
590
591 $localizedList = Html::rawElement( 'ul', array(), implode( '', $listItems ) );
592 $hiddenCategories = $this->page->getHiddenCategories();
593
594 if (
595 count( $listItems ) > 0 ||
596 count( $hiddenCategories ) > 0 ||
597 $pageCounts['transclusion']['from'] > 0 ||
598 $pageCounts['transclusion']['to'] > 0
599 ) {
600 $options = array( 'LIMIT' => $config->get( 'PageInfoTransclusionLimit' ) );
601 $transcludedTemplates = $title->getTemplateLinksFrom( $options );
602 if ( $config->get( 'MiserMode' ) ) {
603 $transcludedTargets = array();
604 } else {
605 $transcludedTargets = $title->getTemplateLinksTo( $options );
606 }
607
608 // Page properties
609 $pageInfo['header-properties'] = array();
610
611 // Magic words
612 if ( count( $listItems ) > 0 ) {
613 $pageInfo['header-properties'][] = array(
614 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
615 $localizedList
616 );
617 }
618
619 // Hidden categories
620 if ( count( $hiddenCategories ) > 0 ) {
621 $pageInfo['header-properties'][] = array(
622 $this->msg( 'pageinfo-hidden-categories' )
623 ->numParams( count( $hiddenCategories ) ),
624 Linker::formatHiddenCategories( $hiddenCategories )
625 );
626 }
627
628 // Transcluded templates
629 if ( $pageCounts['transclusion']['from'] > 0 ) {
630 if ( $pageCounts['transclusion']['from'] > count( $transcludedTemplates ) ) {
631 $more = $this->msg( 'morenotlisted' )->escaped();
632 } else {
633 $more = null;
634 }
635
636 $pageInfo['header-properties'][] = array(
637 $this->msg( 'pageinfo-templates' )
638 ->numParams( $pageCounts['transclusion']['from'] ),
639 Linker::formatTemplates(
640 $transcludedTemplates,
641 false,
642 false,
643 $more )
644 );
645 }
646
647 if ( !$config->get( 'MiserMode' ) && $pageCounts['transclusion']['to'] > 0 ) {
648 if ( $pageCounts['transclusion']['to'] > count( $transcludedTargets ) ) {
649 $more = Linker::link(
650 $whatLinksHere,
651 $this->msg( 'moredotdotdot' )->escaped(),
652 array(),
653 array( 'hidelinks' => 1, 'hideredirs' => 1 )
654 );
655 } else {
656 $more = null;
657 }
658
659 $pageInfo['header-properties'][] = array(
660 $this->msg( 'pageinfo-transclusions' )
661 ->numParams( $pageCounts['transclusion']['to'] ),
662 Linker::formatTemplates(
663 $transcludedTargets,
664 false,
665 false,
666 $more )
667 );
668 }
669 }
670
671 return $pageInfo;
672 }
673
674 /**
675 * Returns page counts that would be too "expensive" to retrieve by normal means.
676 *
677 * @param Title $title Title to get counts for
678 * @return array
679 */
680 protected function pageCounts( Title $title ) {
681 $id = $title->getArticleID();
682 $config = $this->context->getConfig();
683
684 $dbrWatchlist = wfGetDB( DB_SLAVE, 'watchlist' );
685 $result = array();
686
687 // Number of page watchers
688 $watchers = (int)$dbrWatchlist->selectField(
689 'watchlist',
690 'COUNT(*)',
691 array(
692 'wl_namespace' => $title->getNamespace(),
693 'wl_title' => $title->getDBkey(),
694 ),
695 __METHOD__
696 );
697 $result['watchers'] = $watchers;
698
699 if ( $config->get( 'ShowUpdatedMarker' ) ) {
700 // Threshold: last visited about 26 weeks before latest edit
701 $updated = wfTimestamp( TS_UNIX, $this->page->getTimestamp() );
702 $age = $config->get( 'WatchersMaxAge' );
703 $threshold = $dbrWatchlist->timestamp( $updated - $age );
704 // Number of page watchers who also visited a "recent" edit
705 $visitingWatchers = (int)$dbrWatchlist->selectField(
706 'watchlist',
707 'COUNT(*)',
708 array(
709 'wl_namespace' => $title->getNamespace(),
710 'wl_title' => $title->getDBkey(),
711 'wl_notificationtimestamp >= ' . $dbrWatchlist->addQuotes( $threshold ) .
712 ' OR wl_notificationtimestamp IS NULL'
713 ),
714 __METHOD__
715 );
716 $result['visitingWatchers'] = $visitingWatchers;
717 }
718
719 $dbr = wfGetDB( DB_SLAVE );
720 // Total number of edits
721 $edits = (int)$dbr->selectField(
722 'revision',
723 'COUNT(*)',
724 array( 'rev_page' => $id ),
725 __METHOD__
726 );
727 $result['edits'] = $edits;
728
729 // Total number of distinct authors
730 if ( $config->get( 'MiserMode' ) ) {
731 $result['authors'] = 0;
732 } else {
733 $result['authors'] = (int)$dbr->selectField(
734 'revision',
735 'COUNT(DISTINCT rev_user_text)',
736 array( 'rev_page' => $id ),
737 __METHOD__
738 );
739 }
740
741 // "Recent" threshold defined by RCMaxAge setting
742 $threshold = $dbr->timestamp( time() - $config->get( 'RCMaxAge' ) );
743
744 // Recent number of edits
745 $edits = (int)$dbr->selectField(
746 'revision',
747 'COUNT(rev_page)',
748 array(
749 'rev_page' => $id,
750 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
751 ),
752 __METHOD__
753 );
754 $result['recent_edits'] = $edits;
755
756 // Recent number of distinct authors
757 $result['recent_authors'] = (int)$dbr->selectField(
758 'revision',
759 'COUNT(DISTINCT rev_user_text)',
760 array(
761 'rev_page' => $id,
762 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
763 ),
764 __METHOD__
765 );
766
767 // Subpages (if enabled)
768 if ( MWNamespace::hasSubpages( $title->getNamespace() ) ) {
769 $conds = array( 'page_namespace' => $title->getNamespace() );
770 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
771
772 // Subpages of this page (redirects)
773 $conds['page_is_redirect'] = 1;
774 $result['subpages']['redirects'] = (int)$dbr->selectField(
775 'page',
776 'COUNT(page_id)',
777 $conds,
778 __METHOD__ );
779
780 // Subpages of this page (non-redirects)
781 $conds['page_is_redirect'] = 0;
782 $result['subpages']['nonredirects'] = (int)$dbr->selectField(
783 'page',
784 'COUNT(page_id)',
785 $conds,
786 __METHOD__
787 );
788
789 // Subpages of this page (total)
790 $result['subpages']['total'] = $result['subpages']['redirects']
791 + $result['subpages']['nonredirects'];
792 }
793
794 // Counts for the number of transclusion links (to/from)
795 if ( $config->get( 'MiserMode' ) ) {
796 $result['transclusion']['to'] = 0;
797 } else {
798 $result['transclusion']['to'] = (int)$dbr->selectField(
799 'templatelinks',
800 'COUNT(tl_from)',
801 array(
802 'tl_namespace' => $title->getNamespace(),
803 'tl_title' => $title->getDBkey()
804 ),
805 __METHOD__
806 );
807 }
808
809 $result['transclusion']['from'] = (int)$dbr->selectField(
810 'templatelinks',
811 'COUNT(*)',
812 array( 'tl_from' => $title->getArticleID() ),
813 __METHOD__
814 );
815
816 return $result;
817 }
818
819 /**
820 * Returns the name that goes in the "<h1>" page title.
821 *
822 * @return string
823 */
824 protected function getPageTitle() {
825 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
826 }
827
828 /**
829 * Get a list of contributors of $article
830 * @return string Html
831 */
832 protected function getContributors() {
833 $contributors = $this->page->getContributors();
834 $real_names = array();
835 $user_names = array();
836 $anon_ips = array();
837
838 # Sift for real versus user names
839 /** @var $user User */
840 foreach ( $contributors as $user ) {
841 $page = $user->isAnon()
842 ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
843 : $user->getUserPage();
844
845 $hiddenPrefs = $this->context->getConfig()->get( 'HiddenPrefs' );
846 if ( $user->getID() == 0 ) {
847 $anon_ips[] = Linker::link( $page, htmlspecialchars( $user->getName() ) );
848 } elseif ( !in_array( 'realname', $hiddenPrefs ) && $user->getRealName() ) {
849 $real_names[] = Linker::link( $page, htmlspecialchars( $user->getRealName() ) );
850 } else {
851 $user_names[] = Linker::link( $page, htmlspecialchars( $user->getName() ) );
852 }
853 }
854
855 $lang = $this->getLanguage();
856
857 $real = $lang->listToText( $real_names );
858
859 # "ThisSite user(s) A, B and C"
860 if ( count( $user_names ) ) {
861 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
862 count( $user_names ) )->escaped();
863 } else {
864 $user = false;
865 }
866
867 if ( count( $anon_ips ) ) {
868 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
869 count( $anon_ips ) )->escaped();
870 } else {
871 $anon = false;
872 }
873
874 # This is the big list, all mooshed together. We sift for blank strings
875 $fulllist = array();
876 foreach ( array( $real, $user, $anon ) as $s ) {
877 if ( $s !== '' ) {
878 array_push( $fulllist, $s );
879 }
880 }
881
882 $count = count( $fulllist );
883
884 # "Based on work by ..."
885 return $count
886 ? $this->msg( 'othercontribs' )->rawParams(
887 $lang->listToText( $fulllist ) )->params( $count )->escaped()
888 : '';
889 }
890
891 /**
892 * Returns the description that goes below the "<h1>" tag.
893 *
894 * @return string
895 */
896 protected function getDescription() {
897 return '';
898 }
899 }