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