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