Merge "Add checkbox in Special:ListUsers to display only users in temporary user...
[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(
131 $this->msg( "pageinfo-${header}" )->text(),
132 "mw-pageinfo-${header}"
133 ) . "\n";
134 $table = "\n";
135 foreach ( $infoTable as $infoRow ) {
136 $name = ( $infoRow[0] instanceof Message ) ? $infoRow[0]->escaped() : $infoRow[0];
137 $value = ( $infoRow[1] instanceof Message ) ? $infoRow[1]->escaped() : $infoRow[1];
138 $id = ( $infoRow[0] instanceof Message ) ? $infoRow[0]->getKey() : null;
139 $table = $this->addRow( $table, $name, $value, $id ) . "\n";
140 }
141 $content = $this->addTable( $content, $table ) . "\n";
142 }
143
144 // Page footer
145 if ( !$this->msg( 'pageinfo-footer' )->isDisabled() ) {
146 $content .= $this->msg( 'pageinfo-footer' )->parse();
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 * @param string $canonicalId
157 * @return string The HTML.
158 */
159 protected function makeHeader( $header, $canonicalId ) {
160 $spanAttribs = [ 'class' => 'mw-headline', 'id' => Sanitizer::escapeIdForAttribute( $header ) ];
161 $h2Attribs = [ 'id' => Sanitizer::escapeIdForAttribute( $canonicalId ) ];
162
163 return Html::rawElement( 'h2', $h2Attribs, Html::element( 'span', $spanAttribs, $header ) );
164 }
165
166 /**
167 * Adds a row to a table that will be added to the content.
168 *
169 * @param string $table The table that will be added to the content
170 * @param string $name The name of the row
171 * @param string $value The value of the row
172 * @param string $id The ID to use for the 'tr' element
173 * @return string The table with the row added
174 */
175 protected function addRow( $table, $name, $value, $id ) {
176 return $table .
177 Html::rawElement(
178 'tr',
179 $id === null ? [] : [ 'id' => 'mw-' . $id ],
180 Html::rawElement( 'td', [ 'style' => 'vertical-align: top;' ], $name ) .
181 Html::rawElement( 'td', [], $value )
182 );
183 }
184
185 /**
186 * Adds a table to the content that will be added to the output.
187 *
188 * @param string $content The content that will be added to the output
189 * @param string $table
190 * @return string The content with the table added
191 */
192 protected function addTable( $content, $table ) {
193 return $content . Html::rawElement( 'table', [ 'class' => 'wikitable mw-page-info' ],
194 $table );
195 }
196
197 /**
198 * Returns an array of info groups (will be rendered as tables), keyed by group ID.
199 * Group IDs are arbitrary and used so that extensions may add additional information in
200 * arbitrary positions (and as message keys for section headers for the tables, prefixed
201 * with 'pageinfo-').
202 * Each info group is a non-associative array of info items (rendered as table rows).
203 * Each info item is an array with two elements: the first describes the type of
204 * information, the second the value for the current page. Both can be strings (will be
205 * interpreted as raw HTML) or messages (will be interpreted as plain text and escaped).
206 *
207 * @return array
208 */
209 protected function pageInfo() {
210 global $wgContLang;
211
212 $user = $this->getUser();
213 $lang = $this->getLanguage();
214 $title = $this->getTitle();
215 $id = $title->getArticleID();
216 $config = $this->context->getConfig();
217 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
218
219 $pageCounts = $this->pageCounts( $this->page );
220
221 $pageProperties = [];
222 $props = PageProps::getInstance()->getAllProperties( $title );
223 if ( isset( $props[$id] ) ) {
224 $pageProperties = $props[$id];
225 }
226
227 // Basic information
228 $pageInfo = [];
229 $pageInfo['header-basic'] = [];
230
231 // Display title
232 $displayTitle = $title->getPrefixedText();
233 if ( isset( $pageProperties['displaytitle'] ) ) {
234 $displayTitle = $pageProperties['displaytitle'];
235 }
236
237 $pageInfo['header-basic'][] = [
238 $this->msg( 'pageinfo-display-title' ), $displayTitle
239 ];
240
241 // Is it a redirect? If so, where to?
242 if ( $title->isRedirect() ) {
243 $pageInfo['header-basic'][] = [
244 $this->msg( 'pageinfo-redirectsto' ),
245 $linkRenderer->makeLink( $this->page->getRedirectTarget() ) .
246 $this->msg( 'word-separator' )->escaped() .
247 $this->msg( 'parentheses' )->rawParams( $linkRenderer->makeLink(
248 $this->page->getRedirectTarget(),
249 $this->msg( 'pageinfo-redirectsto-info' )->text(),
250 [],
251 [ 'action' => 'info' ]
252 ) )->escaped()
253 ];
254 }
255
256 // Default sort key
257 $sortKey = $title->getCategorySortkey();
258 if ( isset( $pageProperties['defaultsort'] ) ) {
259 $sortKey = $pageProperties['defaultsort'];
260 }
261
262 $sortKey = htmlspecialchars( $sortKey );
263 $pageInfo['header-basic'][] = [ $this->msg( 'pageinfo-default-sort' ), $sortKey ];
264
265 // Page length (in bytes)
266 $pageInfo['header-basic'][] = [
267 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
268 ];
269
270 // Page ID (number not localised, as it's a database ID)
271 $pageInfo['header-basic'][] = [ $this->msg( 'pageinfo-article-id' ), $id ];
272
273 // Language in which the page content is (supposed to be) written
274 $pageLang = $title->getPageLanguage()->getCode();
275
276 $pageLangHtml = $pageLang . ' - ' .
277 Language::fetchLanguageName( $pageLang, $lang->getCode() );
278 // Link to Special:PageLanguage with pre-filled page title if user has permissions
279 if ( $config->get( 'PageLanguageUseDB' )
280 && $title->userCan( 'pagelang', $user )
281 ) {
282 $pageLangHtml .= ' ' . $this->msg( 'parentheses' )->rawParams( $linkRenderer->makeLink(
283 SpecialPage::getTitleValueFor( 'PageLanguage', $title->getPrefixedText() ),
284 $this->msg( 'pageinfo-language-change' )->text()
285 ) )->escaped();
286 }
287
288 $pageInfo['header-basic'][] = [
289 $this->msg( 'pageinfo-language' )->escaped(),
290 $pageLangHtml
291 ];
292
293 // Content model of the page
294 $modelHtml = htmlspecialchars( ContentHandler::getLocalizedName( $title->getContentModel() ) );
295 // If the user can change it, add a link to Special:ChangeContentModel
296 if ( $config->get( 'ContentHandlerUseDB' )
297 && $title->userCan( 'editcontentmodel', $user )
298 ) {
299 $modelHtml .= ' ' . $this->msg( 'parentheses' )->rawParams( $linkRenderer->makeLink(
300 SpecialPage::getTitleValueFor( 'ChangeContentModel', $title->getPrefixedText() ),
301 $this->msg( 'pageinfo-content-model-change' )->text()
302 ) )->escaped();
303 }
304
305 $pageInfo['header-basic'][] = [
306 $this->msg( 'pageinfo-content-model' ),
307 $modelHtml
308 ];
309
310 if ( $title->inNamespace( NS_USER ) ) {
311 $pageUser = User::newFromName( $title->getRootText() );
312 if ( $pageUser && $pageUser->getId() && !$pageUser->isHidden() ) {
313 $pageInfo['header-basic'][] = [
314 $this->msg( 'pageinfo-user-id' ),
315 $pageUser->getId()
316 ];
317 }
318 }
319
320 // Search engine status
321 $pOutput = new ParserOutput();
322 if ( isset( $pageProperties['noindex'] ) ) {
323 $pOutput->setIndexPolicy( 'noindex' );
324 }
325 if ( isset( $pageProperties['index'] ) ) {
326 $pOutput->setIndexPolicy( 'index' );
327 }
328
329 // Use robot policy logic
330 $policy = $this->page->getRobotPolicy( 'view', $pOutput );
331 $pageInfo['header-basic'][] = [
332 // Messages: pageinfo-robot-index, pageinfo-robot-noindex
333 $this->msg( 'pageinfo-robot-policy' ),
334 $this->msg( "pageinfo-robot-${policy['index']}" )
335 ];
336
337 $unwatchedPageThreshold = $config->get( 'UnwatchedPageThreshold' );
338 if (
339 $user->isAllowed( 'unwatchedpages' ) ||
340 ( $unwatchedPageThreshold !== false &&
341 $pageCounts['watchers'] >= $unwatchedPageThreshold )
342 ) {
343 // Number of page watchers
344 $pageInfo['header-basic'][] = [
345 $this->msg( 'pageinfo-watchers' ),
346 $lang->formatNum( $pageCounts['watchers'] )
347 ];
348 if (
349 $config->get( 'ShowUpdatedMarker' ) &&
350 isset( $pageCounts['visitingWatchers'] )
351 ) {
352 $minToDisclose = $config->get( 'UnwatchedPageSecret' );
353 if ( $pageCounts['visitingWatchers'] > $minToDisclose ||
354 $user->isAllowed( 'unwatchedpages' ) ) {
355 $pageInfo['header-basic'][] = [
356 $this->msg( 'pageinfo-visiting-watchers' ),
357 $lang->formatNum( $pageCounts['visitingWatchers'] )
358 ];
359 } else {
360 $pageInfo['header-basic'][] = [
361 $this->msg( 'pageinfo-visiting-watchers' ),
362 $this->msg( 'pageinfo-few-visiting-watchers' )
363 ];
364 }
365 }
366 } elseif ( $unwatchedPageThreshold !== false ) {
367 $pageInfo['header-basic'][] = [
368 $this->msg( 'pageinfo-watchers' ),
369 $this->msg( 'pageinfo-few-watchers' )->numParams( $unwatchedPageThreshold )
370 ];
371 }
372
373 // Redirects to this page
374 $whatLinksHere = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
375 $pageInfo['header-basic'][] = [
376 $linkRenderer->makeLink(
377 $whatLinksHere,
378 $this->msg( 'pageinfo-redirects-name' )->text(),
379 [],
380 [
381 'hidelinks' => 1,
382 'hidetrans' => 1,
383 'hideimages' => $title->getNamespace() == NS_FILE
384 ]
385 ),
386 $this->msg( 'pageinfo-redirects-value' )
387 ->numParams( count( $title->getRedirectsHere() ) )
388 ];
389
390 // Is it counted as a content page?
391 if ( $this->page->isCountable() ) {
392 $pageInfo['header-basic'][] = [
393 $this->msg( 'pageinfo-contentpage' ),
394 $this->msg( 'pageinfo-contentpage-yes' )
395 ];
396 }
397
398 // Subpages of this page, if subpages are enabled for the current NS
399 if ( MWNamespace::hasSubpages( $title->getNamespace() ) ) {
400 $prefixIndex = SpecialPage::getTitleFor(
401 'Prefixindex', $title->getPrefixedText() . '/' );
402 $pageInfo['header-basic'][] = [
403 $linkRenderer->makeLink(
404 $prefixIndex,
405 $this->msg( 'pageinfo-subpages-name' )->text()
406 ),
407 $this->msg( 'pageinfo-subpages-value' )
408 ->numParams(
409 $pageCounts['subpages']['total'],
410 $pageCounts['subpages']['redirects'],
411 $pageCounts['subpages']['nonredirects'] )
412 ];
413 }
414
415 if ( $title->inNamespace( NS_CATEGORY ) ) {
416 $category = Category::newFromTitle( $title );
417
418 // $allCount is the total number of cat members,
419 // not the count of how many members are normal pages.
420 $allCount = (int)$category->getPageCount();
421 $subcatCount = (int)$category->getSubcatCount();
422 $fileCount = (int)$category->getFileCount();
423 $pagesCount = $allCount - $subcatCount - $fileCount;
424
425 $pageInfo['category-info'] = [
426 [
427 $this->msg( 'pageinfo-category-total' ),
428 $lang->formatNum( $allCount )
429 ],
430 [
431 $this->msg( 'pageinfo-category-pages' ),
432 $lang->formatNum( $pagesCount )
433 ],
434 [
435 $this->msg( 'pageinfo-category-subcats' ),
436 $lang->formatNum( $subcatCount )
437 ],
438 [
439 $this->msg( 'pageinfo-category-files' ),
440 $lang->formatNum( $fileCount )
441 ]
442 ];
443 }
444
445 // Display image SHA-1 value
446 if ( $title->inNamespace( NS_FILE ) ) {
447 $fileObj = wfFindFile( $title );
448 if ( $fileObj !== false ) {
449 // Convert the base-36 sha1 value obtained from database to base-16
450 $output = Wikimedia\base_convert( $fileObj->getSha1(), 36, 16, 40 );
451 $pageInfo['header-basic'][] = [
452 $this->msg( 'pageinfo-file-hash' ),
453 $output
454 ];
455 }
456 }
457
458 // Page protection
459 $pageInfo['header-restrictions'] = [];
460
461 // Is this page affected by the cascading protection of something which includes it?
462 if ( $title->isCascadeProtected() ) {
463 $cascadingFrom = '';
464 $sources = $title->getCascadeProtectionSources()[0];
465
466 foreach ( $sources as $sourceTitle ) {
467 $cascadingFrom .= Html::rawElement(
468 'li', [], $linkRenderer->makeKnownLink( $sourceTitle ) );
469 }
470
471 $cascadingFrom = Html::rawElement( 'ul', [], $cascadingFrom );
472 $pageInfo['header-restrictions'][] = [
473 $this->msg( 'pageinfo-protect-cascading-from' ),
474 $cascadingFrom
475 ];
476 }
477
478 // Is out protection set to cascade to other pages?
479 if ( $title->areRestrictionsCascading() ) {
480 $pageInfo['header-restrictions'][] = [
481 $this->msg( 'pageinfo-protect-cascading' ),
482 $this->msg( 'pageinfo-protect-cascading-yes' )
483 ];
484 }
485
486 // Page protection
487 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
488 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
489
490 if ( $protectionLevel == '' ) {
491 // Allow all users
492 $message = $this->msg( 'protect-default' )->escaped();
493 } else {
494 // Administrators only
495 // Messages: protect-level-autoconfirmed, protect-level-sysop
496 $message = $this->msg( "protect-level-$protectionLevel" );
497 if ( $message->isDisabled() ) {
498 // Require "$1" permission
499 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
500 } else {
501 $message = $message->escaped();
502 }
503 }
504 $expiry = $title->getRestrictionExpiry( $restrictionType );
505 $formattedexpiry = $this->msg( 'parentheses',
506 $lang->formatExpiry( $expiry ) )->escaped();
507 $message .= $this->msg( 'word-separator' )->escaped() . $formattedexpiry;
508
509 // Messages: restriction-edit, restriction-move, restriction-create,
510 // restriction-upload
511 $pageInfo['header-restrictions'][] = [
512 $this->msg( "restriction-$restrictionType" ), $message
513 ];
514 }
515
516 if ( !$this->page->exists() ) {
517 return $pageInfo;
518 }
519
520 // Edit history
521 $pageInfo['header-edits'] = [];
522
523 $firstRev = $this->page->getOldestRevision();
524 $lastRev = $this->page->getRevision();
525 $batch = new LinkBatch;
526
527 if ( $firstRev ) {
528 $firstRevUser = $firstRev->getUserText( Revision::FOR_THIS_USER );
529 if ( $firstRevUser !== '' ) {
530 $firstRevUserTitle = Title::makeTitle( NS_USER, $firstRevUser );
531 $batch->addObj( $firstRevUserTitle );
532 $batch->addObj( $firstRevUserTitle->getTalkPage() );
533 }
534 }
535
536 if ( $lastRev ) {
537 $lastRevUser = $lastRev->getUserText( Revision::FOR_THIS_USER );
538 if ( $lastRevUser !== '' ) {
539 $lastRevUserTitle = Title::makeTitle( NS_USER, $lastRevUser );
540 $batch->addObj( $lastRevUserTitle );
541 $batch->addObj( $lastRevUserTitle->getTalkPage() );
542 }
543 }
544
545 $batch->execute();
546
547 if ( $firstRev ) {
548 // Page creator
549 $pageInfo['header-edits'][] = [
550 $this->msg( 'pageinfo-firstuser' ),
551 Linker::revUserTools( $firstRev )
552 ];
553
554 // Date of page creation
555 $pageInfo['header-edits'][] = [
556 $this->msg( 'pageinfo-firsttime' ),
557 $linkRenderer->makeKnownLink(
558 $title,
559 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
560 [],
561 [ 'oldid' => $firstRev->getId() ]
562 )
563 ];
564 }
565
566 if ( $lastRev ) {
567 // Latest editor
568 $pageInfo['header-edits'][] = [
569 $this->msg( 'pageinfo-lastuser' ),
570 Linker::revUserTools( $lastRev )
571 ];
572
573 // Date of latest edit
574 $pageInfo['header-edits'][] = [
575 $this->msg( 'pageinfo-lasttime' ),
576 $linkRenderer->makeKnownLink(
577 $title,
578 $lang->userTimeAndDate( $this->page->getTimestamp(), $user ),
579 [],
580 [ 'oldid' => $this->page->getLatest() ]
581 )
582 ];
583 }
584
585 // Total number of edits
586 $pageInfo['header-edits'][] = [
587 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
588 ];
589
590 // Total number of distinct authors
591 if ( $pageCounts['authors'] > 0 ) {
592 $pageInfo['header-edits'][] = [
593 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
594 ];
595 }
596
597 // Recent number of edits (within past 30 days)
598 $pageInfo['header-edits'][] = [
599 $this->msg( 'pageinfo-recent-edits',
600 $lang->formatDuration( $config->get( 'RCMaxAge' ) ) ),
601 $lang->formatNum( $pageCounts['recent_edits'] )
602 ];
603
604 // Recent number of distinct authors
605 $pageInfo['header-edits'][] = [
606 $this->msg( 'pageinfo-recent-authors' ),
607 $lang->formatNum( $pageCounts['recent_authors'] )
608 ];
609
610 // Array of MagicWord objects
611 $magicWords = MagicWord::getDoubleUnderscoreArray();
612
613 // Array of magic word IDs
614 $wordIDs = $magicWords->names;
615
616 // Array of IDs => localized magic words
617 $localizedWords = $wgContLang->getMagicWords();
618
619 $listItems = [];
620 foreach ( $pageProperties as $property => $value ) {
621 if ( in_array( $property, $wordIDs ) ) {
622 $listItems[] = Html::element( 'li', [], $localizedWords[$property][1] );
623 }
624 }
625
626 $localizedList = Html::rawElement( 'ul', [], implode( '', $listItems ) );
627 $hiddenCategories = $this->page->getHiddenCategories();
628
629 if (
630 count( $listItems ) > 0 ||
631 count( $hiddenCategories ) > 0 ||
632 $pageCounts['transclusion']['from'] > 0 ||
633 $pageCounts['transclusion']['to'] > 0
634 ) {
635 $options = [ 'LIMIT' => $config->get( 'PageInfoTransclusionLimit' ) ];
636 $transcludedTemplates = $title->getTemplateLinksFrom( $options );
637 if ( $config->get( 'MiserMode' ) ) {
638 $transcludedTargets = [];
639 } else {
640 $transcludedTargets = $title->getTemplateLinksTo( $options );
641 }
642
643 // Page properties
644 $pageInfo['header-properties'] = [];
645
646 // Magic words
647 if ( count( $listItems ) > 0 ) {
648 $pageInfo['header-properties'][] = [
649 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
650 $localizedList
651 ];
652 }
653
654 // Hidden categories
655 if ( count( $hiddenCategories ) > 0 ) {
656 $pageInfo['header-properties'][] = [
657 $this->msg( 'pageinfo-hidden-categories' )
658 ->numParams( count( $hiddenCategories ) ),
659 Linker::formatHiddenCategories( $hiddenCategories )
660 ];
661 }
662
663 // Transcluded templates
664 if ( $pageCounts['transclusion']['from'] > 0 ) {
665 if ( $pageCounts['transclusion']['from'] > count( $transcludedTemplates ) ) {
666 $more = $this->msg( 'morenotlisted' )->escaped();
667 } else {
668 $more = null;
669 }
670
671 $templateListFormatter = new TemplatesOnThisPageFormatter(
672 $this->getContext(),
673 $linkRenderer
674 );
675
676 $pageInfo['header-properties'][] = [
677 $this->msg( 'pageinfo-templates' )
678 ->numParams( $pageCounts['transclusion']['from'] ),
679 $templateListFormatter->format( $transcludedTemplates, false, $more )
680 ];
681 }
682
683 if ( !$config->get( 'MiserMode' ) && $pageCounts['transclusion']['to'] > 0 ) {
684 if ( $pageCounts['transclusion']['to'] > count( $transcludedTargets ) ) {
685 $more = $linkRenderer->makeLink(
686 $whatLinksHere,
687 $this->msg( 'moredotdotdot' )->text(),
688 [],
689 [ 'hidelinks' => 1, 'hideredirs' => 1 ]
690 );
691 } else {
692 $more = null;
693 }
694
695 $templateListFormatter = new TemplatesOnThisPageFormatter(
696 $this->getContext(),
697 $linkRenderer
698 );
699
700 $pageInfo['header-properties'][] = [
701 $this->msg( 'pageinfo-transclusions' )
702 ->numParams( $pageCounts['transclusion']['to'] ),
703 $templateListFormatter->format( $transcludedTargets, false, $more )
704 ];
705 }
706 }
707
708 return $pageInfo;
709 }
710
711 /**
712 * Returns page counts that would be too "expensive" to retrieve by normal means.
713 *
714 * @param WikiPage|Article|Page $page
715 * @return array
716 */
717 protected function pageCounts( Page $page ) {
718 $fname = __METHOD__;
719 $config = $this->context->getConfig();
720 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
721
722 return $cache->getWithSetCallback(
723 self::getCacheKey( $cache, $page->getTitle(), $page->getLatest() ),
724 WANObjectCache::TTL_WEEK,
725 function ( $oldValue, &$ttl, &$setOpts ) use ( $page, $config, $fname ) {
726 global $wgActorTableSchemaMigrationStage;
727
728 $title = $page->getTitle();
729 $id = $title->getArticleID();
730
731 $dbr = wfGetDB( DB_REPLICA );
732 $dbrWatchlist = wfGetDB( DB_REPLICA, 'watchlist' );
733 $setOpts += Database::getCacheSetOptions( $dbr, $dbrWatchlist );
734
735 if ( $wgActorTableSchemaMigrationStage === MIGRATION_NEW ) {
736 $tables = [ 'revision_actor_temp' ];
737 $field = 'revactor_actor';
738 $pageField = 'revactor_page';
739 $tsField = 'revactor_timestamp';
740 $joins = [];
741 } elseif ( $wgActorTableSchemaMigrationStage === MIGRATION_OLD ) {
742 $tables = [ 'revision' ];
743 $field = 'rev_user_text';
744 $pageField = 'rev_page';
745 $tsField = 'rev_timestamp';
746 $joins = [];
747 } else {
748 $tables = [ 'revision', 'revision_actor_temp', 'actor' ];
749 $field = 'COALESCE( actor_name, rev_user_text)';
750 $pageField = 'rev_page';
751 $tsField = 'rev_timestamp';
752 $joins = [
753 'revision_actor_temp' => [ 'LEFT JOIN', 'revactor_rev = rev_id' ],
754 'actor' => [ 'LEFT JOIN', 'revactor_actor = actor_id' ],
755 ];
756 }
757
758 $watchedItemStore = MediaWikiServices::getInstance()->getWatchedItemStore();
759
760 $result = [];
761 $result['watchers'] = $watchedItemStore->countWatchers( $title );
762
763 if ( $config->get( 'ShowUpdatedMarker' ) ) {
764 $updated = wfTimestamp( TS_UNIX, $page->getTimestamp() );
765 $result['visitingWatchers'] = $watchedItemStore->countVisitingWatchers(
766 $title,
767 $updated - $config->get( 'WatchersMaxAge' )
768 );
769 }
770
771 // Total number of edits
772 $edits = (int)$dbr->selectField(
773 'revision',
774 'COUNT(*)',
775 [ 'rev_page' => $id ],
776 $fname
777 );
778 $result['edits'] = $edits;
779
780 // Total number of distinct authors
781 if ( $config->get( 'MiserMode' ) ) {
782 $result['authors'] = 0;
783 } else {
784 $result['authors'] = (int)$dbr->selectField(
785 $tables,
786 "COUNT(DISTINCT $field)",
787 [ $pageField => $id ],
788 $fname,
789 [],
790 $joins
791 );
792 }
793
794 // "Recent" threshold defined by RCMaxAge setting
795 $threshold = $dbr->timestamp( time() - $config->get( 'RCMaxAge' ) );
796
797 // Recent number of edits
798 $edits = (int)$dbr->selectField(
799 'revision',
800 'COUNT(rev_page)',
801 [
802 'rev_page' => $id,
803 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
804 ],
805 $fname
806 );
807 $result['recent_edits'] = $edits;
808
809 // Recent number of distinct authors
810 $result['recent_authors'] = (int)$dbr->selectField(
811 $tables,
812 "COUNT(DISTINCT $field)",
813 [
814 $pageField => $id,
815 "$tsField >= " . $dbr->addQuotes( $threshold )
816 ],
817 $fname,
818 [],
819 $joins
820 );
821
822 // Subpages (if enabled)
823 if ( MWNamespace::hasSubpages( $title->getNamespace() ) ) {
824 $conds = [ 'page_namespace' => $title->getNamespace() ];
825 $conds[] = 'page_title ' .
826 $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
827
828 // Subpages of this page (redirects)
829 $conds['page_is_redirect'] = 1;
830 $result['subpages']['redirects'] = (int)$dbr->selectField(
831 'page',
832 'COUNT(page_id)',
833 $conds,
834 $fname
835 );
836
837 // Subpages of this page (non-redirects)
838 $conds['page_is_redirect'] = 0;
839 $result['subpages']['nonredirects'] = (int)$dbr->selectField(
840 'page',
841 'COUNT(page_id)',
842 $conds,
843 $fname
844 );
845
846 // Subpages of this page (total)
847 $result['subpages']['total'] = $result['subpages']['redirects']
848 + $result['subpages']['nonredirects'];
849 }
850
851 // Counts for the number of transclusion links (to/from)
852 if ( $config->get( 'MiserMode' ) ) {
853 $result['transclusion']['to'] = 0;
854 } else {
855 $result['transclusion']['to'] = (int)$dbr->selectField(
856 'templatelinks',
857 'COUNT(tl_from)',
858 [
859 'tl_namespace' => $title->getNamespace(),
860 'tl_title' => $title->getDBkey()
861 ],
862 $fname
863 );
864 }
865
866 $result['transclusion']['from'] = (int)$dbr->selectField(
867 'templatelinks',
868 'COUNT(*)',
869 [ 'tl_from' => $title->getArticleID() ],
870 $fname
871 );
872
873 return $result;
874 }
875 );
876 }
877
878 /**
879 * Returns the name that goes in the "<h1>" page title.
880 *
881 * @return string
882 */
883 protected function getPageTitle() {
884 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
885 }
886
887 /**
888 * Get a list of contributors of $article
889 * @return string Html
890 */
891 protected function getContributors() {
892 $contributors = $this->page->getContributors();
893 $real_names = [];
894 $user_names = [];
895 $anon_ips = [];
896 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
897
898 # Sift for real versus user names
899 /** @var User $user */
900 foreach ( $contributors as $user ) {
901 $page = $user->isAnon()
902 ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
903 : $user->getUserPage();
904
905 $hiddenPrefs = $this->context->getConfig()->get( 'HiddenPrefs' );
906 if ( $user->getId() == 0 ) {
907 $anon_ips[] = $linkRenderer->makeLink( $page, $user->getName() );
908 } elseif ( !in_array( 'realname', $hiddenPrefs ) && $user->getRealName() ) {
909 $real_names[] = $linkRenderer->makeLink( $page, $user->getRealName() );
910 } else {
911 $user_names[] = $linkRenderer->makeLink( $page, $user->getName() );
912 }
913 }
914
915 $lang = $this->getLanguage();
916
917 $real = $lang->listToText( $real_names );
918
919 # "ThisSite user(s) A, B and C"
920 if ( count( $user_names ) ) {
921 $user = $this->msg( 'siteusers' )
922 ->rawParams( $lang->listToText( $user_names ) )
923 ->params( count( $user_names ) )->escaped();
924 } else {
925 $user = false;
926 }
927
928 if ( count( $anon_ips ) ) {
929 $anon = $this->msg( 'anonusers' )
930 ->rawParams( $lang->listToText( $anon_ips ) )
931 ->params( count( $anon_ips ) )->escaped();
932 } else {
933 $anon = false;
934 }
935
936 # This is the big list, all mooshed together. We sift for blank strings
937 $fulllist = [];
938 foreach ( [ $real, $user, $anon ] as $s ) {
939 if ( $s !== '' ) {
940 array_push( $fulllist, $s );
941 }
942 }
943
944 $count = count( $fulllist );
945
946 # "Based on work by ..."
947 return $count
948 ? $this->msg( 'othercontribs' )->rawParams(
949 $lang->listToText( $fulllist ) )->params( $count )->escaped()
950 : '';
951 }
952
953 /**
954 * Returns the description that goes below the "<h1>" tag.
955 *
956 * @return string
957 */
958 protected function getDescription() {
959 return '';
960 }
961
962 /**
963 * @param WANObjectCache $cache
964 * @param Title $title
965 * @param int $revId
966 * @return string
967 */
968 protected static function getCacheKey( WANObjectCache $cache, Title $title, $revId ) {
969 return $cache->makeKey( 'infoaction', md5( $title->getPrefixedText() ), $revId, self::VERSION );
970 }
971 }