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