SpecialNewFiles: Swap from/to date serverside
[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 $linkRenderer->makeLink(
394 $prefixIndex,
395 $this->msg( 'pageinfo-subpages-name' )->text()
396 ),
397 $this->msg( 'pageinfo-subpages-value' )
398 ->numParams(
399 $pageCounts['subpages']['total'],
400 $pageCounts['subpages']['redirects'],
401 $pageCounts['subpages']['nonredirects'] )
402 ];
403 }
404
405 if ( $title->inNamespace( NS_CATEGORY ) ) {
406 $category = Category::newFromTitle( $title );
407
408 // $allCount is the total number of cat members,
409 // not the count of how many members are normal pages.
410 $allCount = (int)$category->getPageCount();
411 $subcatCount = (int)$category->getSubcatCount();
412 $fileCount = (int)$category->getFileCount();
413 $pagesCount = $allCount - $subcatCount - $fileCount;
414
415 $pageInfo['category-info'] = [
416 [
417 $this->msg( 'pageinfo-category-total' ),
418 $lang->formatNum( $allCount )
419 ],
420 [
421 $this->msg( 'pageinfo-category-pages' ),
422 $lang->formatNum( $pagesCount )
423 ],
424 [
425 $this->msg( 'pageinfo-category-subcats' ),
426 $lang->formatNum( $subcatCount )
427 ],
428 [
429 $this->msg( 'pageinfo-category-files' ),
430 $lang->formatNum( $fileCount )
431 ]
432 ];
433 }
434
435 // Page protection
436 $pageInfo['header-restrictions'] = [];
437
438 // Is this page affected by the cascading protection of something which includes it?
439 if ( $title->isCascadeProtected() ) {
440 $cascadingFrom = '';
441 $sources = $title->getCascadeProtectionSources()[0];
442
443 foreach ( $sources as $sourceTitle ) {
444 $cascadingFrom .= Html::rawElement(
445 'li', [], $linkRenderer->makeKnownLink( $sourceTitle ) );
446 }
447
448 $cascadingFrom = Html::rawElement( 'ul', [], $cascadingFrom );
449 $pageInfo['header-restrictions'][] = [
450 $this->msg( 'pageinfo-protect-cascading-from' ),
451 $cascadingFrom
452 ];
453 }
454
455 // Is out protection set to cascade to other pages?
456 if ( $title->areRestrictionsCascading() ) {
457 $pageInfo['header-restrictions'][] = [
458 $this->msg( 'pageinfo-protect-cascading' ),
459 $this->msg( 'pageinfo-protect-cascading-yes' )
460 ];
461 }
462
463 // Page protection
464 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
465 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
466
467 if ( $protectionLevel == '' ) {
468 // Allow all users
469 $message = $this->msg( 'protect-default' )->escaped();
470 } else {
471 // Administrators only
472 // Messages: protect-level-autoconfirmed, protect-level-sysop
473 $message = $this->msg( "protect-level-$protectionLevel" );
474 if ( $message->isDisabled() ) {
475 // Require "$1" permission
476 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
477 } else {
478 $message = $message->escaped();
479 }
480 }
481 $expiry = $title->getRestrictionExpiry( $restrictionType );
482 $formattedexpiry = $this->msg( 'parentheses',
483 $lang->formatExpiry( $expiry ) )->escaped();
484 $message .= $this->msg( 'word-separator' )->escaped() . $formattedexpiry;
485
486 // Messages: restriction-edit, restriction-move, restriction-create,
487 // restriction-upload
488 $pageInfo['header-restrictions'][] = [
489 $this->msg( "restriction-$restrictionType" ), $message
490 ];
491 }
492
493 if ( !$this->page->exists() ) {
494 return $pageInfo;
495 }
496
497 // Edit history
498 $pageInfo['header-edits'] = [];
499
500 $firstRev = $this->page->getOldestRevision();
501 $lastRev = $this->page->getRevision();
502 $batch = new LinkBatch;
503
504 if ( $firstRev ) {
505 $firstRevUser = $firstRev->getUserText( Revision::FOR_THIS_USER );
506 if ( $firstRevUser !== '' ) {
507 $firstRevUserTitle = Title::makeTitle( NS_USER, $firstRevUser );
508 $batch->addObj( $firstRevUserTitle );
509 $batch->addObj( $firstRevUserTitle->getTalkPage() );
510 }
511 }
512
513 if ( $lastRev ) {
514 $lastRevUser = $lastRev->getUserText( Revision::FOR_THIS_USER );
515 if ( $lastRevUser !== '' ) {
516 $lastRevUserTitle = Title::makeTitle( NS_USER, $lastRevUser );
517 $batch->addObj( $lastRevUserTitle );
518 $batch->addObj( $lastRevUserTitle->getTalkPage() );
519 }
520 }
521
522 $batch->execute();
523
524 if ( $firstRev ) {
525 // Page creator
526 $pageInfo['header-edits'][] = [
527 $this->msg( 'pageinfo-firstuser' ),
528 Linker::revUserTools( $firstRev )
529 ];
530
531 // Date of page creation
532 $pageInfo['header-edits'][] = [
533 $this->msg( 'pageinfo-firsttime' ),
534 $linkRenderer->makeKnownLink(
535 $title,
536 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
537 [],
538 [ 'oldid' => $firstRev->getId() ]
539 )
540 ];
541 }
542
543 if ( $lastRev ) {
544 // Latest editor
545 $pageInfo['header-edits'][] = [
546 $this->msg( 'pageinfo-lastuser' ),
547 Linker::revUserTools( $lastRev )
548 ];
549
550 // Date of latest edit
551 $pageInfo['header-edits'][] = [
552 $this->msg( 'pageinfo-lasttime' ),
553 $linkRenderer->makeKnownLink(
554 $title,
555 $lang->userTimeAndDate( $this->page->getTimestamp(), $user ),
556 [],
557 [ 'oldid' => $this->page->getLatest() ]
558 )
559 ];
560 }
561
562 // Total number of edits
563 $pageInfo['header-edits'][] = [
564 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
565 ];
566
567 // Total number of distinct authors
568 if ( $pageCounts['authors'] > 0 ) {
569 $pageInfo['header-edits'][] = [
570 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
571 ];
572 }
573
574 // Recent number of edits (within past 30 days)
575 $pageInfo['header-edits'][] = [
576 $this->msg( 'pageinfo-recent-edits',
577 $lang->formatDuration( $config->get( 'RCMaxAge' ) ) ),
578 $lang->formatNum( $pageCounts['recent_edits'] )
579 ];
580
581 // Recent number of distinct authors
582 $pageInfo['header-edits'][] = [
583 $this->msg( 'pageinfo-recent-authors' ),
584 $lang->formatNum( $pageCounts['recent_authors'] )
585 ];
586
587 // Array of MagicWord objects
588 $magicWords = MagicWord::getDoubleUnderscoreArray();
589
590 // Array of magic word IDs
591 $wordIDs = $magicWords->names;
592
593 // Array of IDs => localized magic words
594 $localizedWords = $wgContLang->getMagicWords();
595
596 $listItems = [];
597 foreach ( $pageProperties as $property => $value ) {
598 if ( in_array( $property, $wordIDs ) ) {
599 $listItems[] = Html::element( 'li', [], $localizedWords[$property][1] );
600 }
601 }
602
603 $localizedList = Html::rawElement( 'ul', [], implode( '', $listItems ) );
604 $hiddenCategories = $this->page->getHiddenCategories();
605
606 if (
607 count( $listItems ) > 0 ||
608 count( $hiddenCategories ) > 0 ||
609 $pageCounts['transclusion']['from'] > 0 ||
610 $pageCounts['transclusion']['to'] > 0
611 ) {
612 $options = [ 'LIMIT' => $config->get( 'PageInfoTransclusionLimit' ) ];
613 $transcludedTemplates = $title->getTemplateLinksFrom( $options );
614 if ( $config->get( 'MiserMode' ) ) {
615 $transcludedTargets = [];
616 } else {
617 $transcludedTargets = $title->getTemplateLinksTo( $options );
618 }
619
620 // Page properties
621 $pageInfo['header-properties'] = [];
622
623 // Magic words
624 if ( count( $listItems ) > 0 ) {
625 $pageInfo['header-properties'][] = [
626 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
627 $localizedList
628 ];
629 }
630
631 // Hidden categories
632 if ( count( $hiddenCategories ) > 0 ) {
633 $pageInfo['header-properties'][] = [
634 $this->msg( 'pageinfo-hidden-categories' )
635 ->numParams( count( $hiddenCategories ) ),
636 Linker::formatHiddenCategories( $hiddenCategories )
637 ];
638 }
639
640 // Transcluded templates
641 if ( $pageCounts['transclusion']['from'] > 0 ) {
642 if ( $pageCounts['transclusion']['from'] > count( $transcludedTemplates ) ) {
643 $more = $this->msg( 'morenotlisted' )->escaped();
644 } else {
645 $more = null;
646 }
647
648 $templateListFormatter = new TemplatesOnThisPageFormatter(
649 $this->getContext(),
650 $linkRenderer
651 );
652
653 $pageInfo['header-properties'][] = [
654 $this->msg( 'pageinfo-templates' )
655 ->numParams( $pageCounts['transclusion']['from'] ),
656 $templateListFormatter->format( $transcludedTemplates, false, $more )
657 ];
658 }
659
660 if ( !$config->get( 'MiserMode' ) && $pageCounts['transclusion']['to'] > 0 ) {
661 if ( $pageCounts['transclusion']['to'] > count( $transcludedTargets ) ) {
662 $more = $linkRenderer->makeLink(
663 $whatLinksHere,
664 $this->msg( 'moredotdotdot' )->text(),
665 [],
666 [ 'hidelinks' => 1, 'hideredirs' => 1 ]
667 );
668 } else {
669 $more = null;
670 }
671
672 $templateListFormatter = new TemplatesOnThisPageFormatter(
673 $this->getContext(),
674 $linkRenderer
675 );
676
677 $pageInfo['header-properties'][] = [
678 $this->msg( 'pageinfo-transclusions' )
679 ->numParams( $pageCounts['transclusion']['to'] ),
680 $templateListFormatter->format( $transcludedTargets, false, $more )
681 ];
682 }
683 }
684
685 return $pageInfo;
686 }
687
688 /**
689 * Returns page counts that would be too "expensive" to retrieve by normal means.
690 *
691 * @param WikiPage|Article|Page $page
692 * @return array
693 */
694 protected function pageCounts( Page $page ) {
695 $fname = __METHOD__;
696 $config = $this->context->getConfig();
697 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
698
699 return $cache->getWithSetCallback(
700 self::getCacheKey( $cache, $page->getTitle(), $page->getLatest() ),
701 WANObjectCache::TTL_WEEK,
702 function ( $oldValue, &$ttl, &$setOpts ) use ( $page, $config, $fname ) {
703 $title = $page->getTitle();
704 $id = $title->getArticleID();
705
706 $dbr = wfGetDB( DB_REPLICA );
707 $dbrWatchlist = wfGetDB( DB_REPLICA, 'watchlist' );
708 $setOpts += Database::getCacheSetOptions( $dbr, $dbrWatchlist );
709
710 $watchedItemStore = MediaWikiServices::getInstance()->getWatchedItemStore();
711
712 $result = [];
713 $result['watchers'] = $watchedItemStore->countWatchers( $title );
714
715 if ( $config->get( 'ShowUpdatedMarker' ) ) {
716 $updated = wfTimestamp( TS_UNIX, $page->getTimestamp() );
717 $result['visitingWatchers'] = $watchedItemStore->countVisitingWatchers(
718 $title,
719 $updated - $config->get( 'WatchersMaxAge' )
720 );
721 }
722
723 // Total number of edits
724 $edits = (int)$dbr->selectField(
725 'revision',
726 'COUNT(*)',
727 [ 'rev_page' => $id ],
728 $fname
729 );
730 $result['edits'] = $edits;
731
732 // Total number of distinct authors
733 if ( $config->get( 'MiserMode' ) ) {
734 $result['authors'] = 0;
735 } else {
736 $result['authors'] = (int)$dbr->selectField(
737 'revision',
738 'COUNT(DISTINCT rev_user_text)',
739 [ 'rev_page' => $id ],
740 $fname
741 );
742 }
743
744 // "Recent" threshold defined by RCMaxAge setting
745 $threshold = $dbr->timestamp( time() - $config->get( 'RCMaxAge' ) );
746
747 // Recent number of edits
748 $edits = (int)$dbr->selectField(
749 'revision',
750 'COUNT(rev_page)',
751 [
752 'rev_page' => $id,
753 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
754 ],
755 $fname
756 );
757 $result['recent_edits'] = $edits;
758
759 // Recent number of distinct authors
760 $result['recent_authors'] = (int)$dbr->selectField(
761 'revision',
762 'COUNT(DISTINCT rev_user_text)',
763 [
764 'rev_page' => $id,
765 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
766 ],
767 $fname
768 );
769
770 // Subpages (if enabled)
771 if ( MWNamespace::hasSubpages( $title->getNamespace() ) ) {
772 $conds = [ 'page_namespace' => $title->getNamespace() ];
773 $conds[] = 'page_title ' .
774 $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
775
776 // Subpages of this page (redirects)
777 $conds['page_is_redirect'] = 1;
778 $result['subpages']['redirects'] = (int)$dbr->selectField(
779 'page',
780 'COUNT(page_id)',
781 $conds,
782 $fname
783 );
784
785 // Subpages of this page (non-redirects)
786 $conds['page_is_redirect'] = 0;
787 $result['subpages']['nonredirects'] = (int)$dbr->selectField(
788 'page',
789 'COUNT(page_id)',
790 $conds,
791 $fname
792 );
793
794 // Subpages of this page (total)
795 $result['subpages']['total'] = $result['subpages']['redirects']
796 + $result['subpages']['nonredirects'];
797 }
798
799 // Counts for the number of transclusion links (to/from)
800 if ( $config->get( 'MiserMode' ) ) {
801 $result['transclusion']['to'] = 0;
802 } else {
803 $result['transclusion']['to'] = (int)$dbr->selectField(
804 'templatelinks',
805 'COUNT(tl_from)',
806 [
807 'tl_namespace' => $title->getNamespace(),
808 'tl_title' => $title->getDBkey()
809 ],
810 $fname
811 );
812 }
813
814 $result['transclusion']['from'] = (int)$dbr->selectField(
815 'templatelinks',
816 'COUNT(*)',
817 [ 'tl_from' => $title->getArticleID() ],
818 $fname
819 );
820
821 return $result;
822 }
823 );
824 }
825
826 /**
827 * Returns the name that goes in the "<h1>" page title.
828 *
829 * @return string
830 */
831 protected function getPageTitle() {
832 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
833 }
834
835 /**
836 * Get a list of contributors of $article
837 * @return string Html
838 */
839 protected function getContributors() {
840 $contributors = $this->page->getContributors();
841 $real_names = [];
842 $user_names = [];
843 $anon_ips = [];
844 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
845
846 # Sift for real versus user names
847 /** @var $user User */
848 foreach ( $contributors as $user ) {
849 $page = $user->isAnon()
850 ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
851 : $user->getUserPage();
852
853 $hiddenPrefs = $this->context->getConfig()->get( 'HiddenPrefs' );
854 if ( $user->getId() == 0 ) {
855 $anon_ips[] = $linkRenderer->makeLink( $page, $user->getName() );
856 } elseif ( !in_array( 'realname', $hiddenPrefs ) && $user->getRealName() ) {
857 $real_names[] = $linkRenderer->makeLink( $page, $user->getRealName() );
858 } else {
859 $user_names[] = $linkRenderer->makeLink( $page, $user->getName() );
860 }
861 }
862
863 $lang = $this->getLanguage();
864
865 $real = $lang->listToText( $real_names );
866
867 # "ThisSite user(s) A, B and C"
868 if ( count( $user_names ) ) {
869 $user = $this->msg( 'siteusers' )
870 ->rawParams( $lang->listToText( $user_names ) )
871 ->params( count( $user_names ) )->escaped();
872 } else {
873 $user = false;
874 }
875
876 if ( count( $anon_ips ) ) {
877 $anon = $this->msg( 'anonusers' )
878 ->rawParams( $lang->listToText( $anon_ips ) )
879 ->params( count( $anon_ips ) )->escaped();
880 } else {
881 $anon = false;
882 }
883
884 # This is the big list, all mooshed together. We sift for blank strings
885 $fulllist = [];
886 foreach ( [ $real, $user, $anon ] as $s ) {
887 if ( $s !== '' ) {
888 array_push( $fulllist, $s );
889 }
890 }
891
892 $count = count( $fulllist );
893
894 # "Based on work by ..."
895 return $count
896 ? $this->msg( 'othercontribs' )->rawParams(
897 $lang->listToText( $fulllist ) )->params( $count )->escaped()
898 : '';
899 }
900
901 /**
902 * Returns the description that goes below the "<h1>" tag.
903 *
904 * @return string
905 */
906 protected function getDescription() {
907 return '';
908 }
909
910 /**
911 * @param WANObjectCache $cache
912 * @param Title $title
913 * @param int $revId
914 * @return string
915 */
916 protected static function getCacheKey( WANObjectCache $cache, Title $title, $revId ) {
917 return $cache->makeKey( 'infoaction', md5( $title->getPrefixedText() ), $revId, self::VERSION );
918 }
919 }