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