Use a LinkBatch for the creator/last editor user (talk) pages in InfoAction
[mediawiki.git] / includes / actions / InfoAction.php
bloba627125510d277cd7f6a5844477a8b3764be5fa9
1 <?php
2 /**
3 * Displays information about a page.
5 * Copyright © 2011 Alexandre Emsenhuber
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.
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.
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
21 * @file
22 * @ingroup Actions
25 class InfoAction extends FormlessAction {
26 /**
27 * Returns the name of the action this object responds to.
29 * @return string lowercase
31 public function getName() {
32 return 'info';
35 /**
36 * Whether this action can still be executed by a blocked user.
38 * @return bool
40 public function requiresUnblock() {
41 return false;
44 /**
45 * Whether this action requires the wiki not to be locked.
47 * @return bool
49 public function requiresWrite() {
50 return false;
53 /**
54 * Shows page information on GET request.
56 * @return string Page information that will be added to the output
58 public function onView() {
59 $content = '';
61 // Validate revision
62 $oldid = $this->page->getOldID();
63 if ( $oldid ) {
64 $revision = $this->page->getRevisionFetched();
66 // Revision is missing
67 if ( $revision === null ) {
68 return $this->msg( 'missing-revision', $oldid )->parse();
71 // Revision is not current
72 if ( !$revision->isCurrent() ) {
73 return $this->msg( 'pageinfo-not-current' )->plain();
77 // Page header
78 if ( !$this->msg( 'pageinfo-header' )->isDisabled() ) {
79 $content .= $this->msg( 'pageinfo-header' )->parse();
82 // Hide "This page is a member of # hidden categories" explanation
83 $content .= Html::element( 'style', array(),
84 '.mw-hiddenCategoriesExplanation { display: none; }' );
86 // Hide "Templates used on this page" explanation
87 $content .= Html::element( 'style', array(),
88 '.mw-templatesUsedExplanation { display: none; }' );
90 // Get page information
91 $pageInfo = $this->pageInfo();
93 // Allow extensions to add additional information
94 wfRunHooks( 'InfoAction', array( $this->getContext(), &$pageInfo ) );
96 // Render page information
97 foreach ( $pageInfo as $header => $infoTable ) {
98 $content .= $this->makeHeader( $this->msg( "pageinfo-${header}" )->escaped() );
99 $table = '';
100 foreach ( $infoTable as $infoRow ) {
101 $name = ( $infoRow[0] instanceof Message ) ? $infoRow[0]->escaped() : $infoRow[0];
102 $value = ( $infoRow[1] instanceof Message ) ? $infoRow[1]->escaped() : $infoRow[1];
103 $table = $this->addRow( $table, $name, $value );
105 $content = $this->addTable( $content, $table );
108 // Page footer
109 if ( !$this->msg( 'pageinfo-footer' )->isDisabled() ) {
110 $content .= $this->msg( 'pageinfo-footer' )->parse();
113 // Page credits
114 /*if ( $this->page->exists() ) {
115 $content .= Html::rawElement( 'div', array( 'id' => 'mw-credits' ), $this->getContributors() );
118 return $content;
122 * Creates a header that can be added to the output.
124 * @param $header The header text.
125 * @return string The HTML.
127 protected function makeHeader( $header ) {
128 global $wgParser;
129 $spanAttribs = array( 'class' => 'mw-headline', 'id' => $wgParser->guessSectionNameFromWikiText( $header ) );
130 return Html::rawElement( 'h2', array(), Html::element( 'span', $spanAttribs, $header ) );
134 * Adds a row to a table that will be added to the content.
136 * @param $table string The table that will be added to the content
137 * @param $name string The name of the row
138 * @param $value string The value of the row
139 * @return string The table with the row added
141 protected function addRow( $table, $name, $value ) {
142 return $table . Html::rawElement( 'tr', array(),
143 Html::rawElement( 'td', array( 'style' => 'vertical-align: top;' ), $name ) .
144 Html::rawElement( 'td', array(), $value )
149 * Adds a table to the content that will be added to the output.
151 * @param $content string The content that will be added to the output
152 * @param $table string The table
153 * @return string The content with the table added
155 protected function addTable( $content, $table ) {
156 return $content . Html::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
157 $table );
161 * Returns page information in an easily-manipulated format. Array keys are used so extensions
162 * may add additional information in arbitrary positions. Array values are arrays with one
163 * element to be rendered as a header, arrays with two elements to be rendered as a table row.
165 * @return array
167 protected function pageInfo() {
168 global $wgContLang, $wgRCMaxAge, $wgMemc, $wgUnwatchedPageThreshold, $wgPageInfoTransclusionLimit;
170 $user = $this->getUser();
171 $lang = $this->getLanguage();
172 $title = $this->getTitle();
173 $id = $title->getArticleID();
175 $memcKey = wfMemcKey( 'infoaction', $title->getPrefixedText(), $this->page->getLatest() );
176 $pageCounts = $wgMemc->get( $memcKey );
177 if ( $pageCounts === false ) {
178 // Get page information that would be too "expensive" to retrieve by normal means
179 $pageCounts = self::pageCounts( $title );
181 $wgMemc->set( $memcKey, $pageCounts );
184 // Get page properties
185 $dbr = wfGetDB( DB_SLAVE );
186 $result = $dbr->select(
187 'page_props',
188 array( 'pp_propname', 'pp_value' ),
189 array( 'pp_page' => $id ),
190 __METHOD__
193 $pageProperties = array();
194 foreach ( $result as $row ) {
195 $pageProperties[$row->pp_propname] = $row->pp_value;
198 // Basic information
199 $pageInfo = array();
200 $pageInfo['header-basic'] = array();
202 // Display title
203 $displayTitle = $title->getPrefixedText();
204 if ( !empty( $pageProperties['displaytitle'] ) ) {
205 $displayTitle = $pageProperties['displaytitle'];
208 $pageInfo['header-basic'][] = array(
209 $this->msg( 'pageinfo-display-title' ), $displayTitle
212 // Is it a redirect? If so, where to?
213 if ( $title->isRedirect() ) {
214 $pageInfo['header-basic'][] = array(
215 $this->msg( 'pageinfo-redirectsto' ),
216 Linker::link( $this->page->getRedirectTarget() ) .
217 $this->msg( 'word-separator' )->text() .
218 $this->msg( 'parentheses', Linker::link(
219 $this->page->getRedirectTarget(),
220 $this->msg( 'pageinfo-redirectsto-info' )->escaped(),
221 array(),
222 array( 'action' => 'info' )
223 ) )->text()
227 // Default sort key
228 $sortKey = $title->getCategorySortKey();
229 if ( !empty( $pageProperties['defaultsort'] ) ) {
230 $sortKey = $pageProperties['defaultsort'];
233 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-default-sort' ), $sortKey );
235 // Page length (in bytes)
236 $pageInfo['header-basic'][] = array(
237 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
240 // Page ID (number not localised, as it's a database ID)
241 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-article-id' ), $id );
243 // Language in which the page content is (supposed to be) written
244 $pageLang = $title->getPageLanguage()->getCode();
245 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-language' ),
246 Language::fetchLanguageName( $pageLang, $lang->getCode() )
247 . ' ' . $this->msg( 'parentheses', $pageLang ) );
249 // Search engine status
250 $pOutput = new ParserOutput();
251 if ( isset( $pageProperties['noindex'] ) ) {
252 $pOutput->setIndexPolicy( 'noindex' );
255 // Use robot policy logic
256 $policy = $this->page->getRobotPolicy( 'view', $pOutput );
257 $pageInfo['header-basic'][] = array(
258 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
261 if ( isset( $pageCounts['views'] ) ) {
262 // Number of views
263 $pageInfo['header-basic'][] = array(
264 $this->msg( 'pageinfo-views' ), $lang->formatNum( $pageCounts['views'] )
268 if (
269 $user->isAllowed( 'unwatchedpages' ) ||
270 ( $wgUnwatchedPageThreshold !== false &&
271 $pageCounts['watchers'] >= $wgUnwatchedPageThreshold )
273 // Number of page watchers
274 $pageInfo['header-basic'][] = array(
275 $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
279 // Redirects to this page
280 $whatLinksHere = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
281 $pageInfo['header-basic'][] = array(
282 Linker::link(
283 $whatLinksHere,
284 $this->msg( 'pageinfo-redirects-name' )->escaped(),
285 array(),
286 array( 'hidelinks' => 1, 'hidetrans' => 1 )
288 $this->msg( 'pageinfo-redirects-value' )
289 ->numParams( count( $title->getRedirectsHere() ) )
292 // Is it counted as a content page?
293 if ( $this->page->isCountable() ) {
294 $pageInfo['header-basic'][] = array(
295 $this->msg( 'pageinfo-contentpage' ),
296 $this->msg( 'pageinfo-contentpage-yes' )
300 // Subpages of this page, if subpages are enabled for the current NS
301 if ( MWNamespace::hasSubpages( $title->getNamespace() ) ) {
302 $prefixIndex = SpecialPage::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
303 $pageInfo['header-basic'][] = array(
304 Linker::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
305 $this->msg( 'pageinfo-subpages-value' )
306 ->numParams(
307 $pageCounts['subpages']['total'],
308 $pageCounts['subpages']['redirects'],
309 $pageCounts['subpages']['nonredirects'] )
313 if ( $title->inNamespace( NS_CATEGORY ) ) {
314 $category = Category::newFromTitle( $title );
315 $pageInfo['category-info'] = array(
316 array(
317 $this->msg( 'pageinfo-category-pages' ),
318 $lang->formatNum( $category->getPageCount() )
320 array(
321 $this->msg( 'pageinfo-category-subcats' ),
322 $lang->formatNum( $category->getSubcatCount() )
324 array(
325 $this->msg( 'pageinfo-category-files' ),
326 $lang->formatNum( $category->getFileCount() )
331 // Page protection
332 $pageInfo['header-restrictions'] = array();
334 // Is this page effected by the cascading protection of something which includes it?
335 if ( $title->isCascadeProtected() ) {
336 $cascadingFrom = '';
337 $sources = $title->getCascadeProtectionSources(); // Array deferencing is in PHP 5.4 :(
339 foreach ( $sources[0] as $sourceTitle ) {
340 $cascadingFrom .= Html::rawElement( 'li', array(), Linker::linkKnown( $sourceTitle ) );
343 $cascadingFrom = Html::rawElement( 'ul', array(), $cascadingFrom );
344 $pageInfo['header-restrictions'][] = array(
345 $this->msg( 'pageinfo-protect-cascading-from' ),
346 $cascadingFrom
350 // Is out protection set to cascade to other pages?
351 if ( $title->areRestrictionsCascading() ) {
352 $pageInfo['header-restrictions'][] = array(
353 $this->msg( 'pageinfo-protect-cascading' ),
354 $this->msg( 'pageinfo-protect-cascading-yes' )
358 // Page protection
359 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
360 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
362 if ( $protectionLevel == '' ) {
363 // Allow all users
364 $message = $this->msg( 'protect-default' )->escaped();
365 } else {
366 // Administrators only
367 $message = $this->msg( "protect-level-$protectionLevel" );
368 if ( $message->isDisabled() ) {
369 // Require "$1" permission
370 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
371 } else {
372 $message = $message->escaped();
376 $pageInfo['header-restrictions'][] = array(
377 $this->msg( "restriction-$restrictionType" ), $message
381 if ( !$this->page->exists() ) {
382 return $pageInfo;
385 // Edit history
386 $pageInfo['header-edits'] = array();
388 $firstRev = $this->page->getOldestRevision();
389 $lastRev = $this->page->getRevision();
390 $batch = new LinkBatch;
392 $firstRevUser = $firstRev->getUserText( Revision::FOR_THIS_USER );
393 if ( $firstRevUser !== '' ) {
394 $batch->add( NS_USER, $firstRevUser );
395 $batch->add( NS_USER_TALK, $firstRevUser );
398 $lastRevUser = $lastRev->getUserText( Revision::FOR_THIS_USER );
399 if ( $lastRevUser !== '' ) {
400 $batch->add( NS_USER, $lastRevUser );
401 $batch->add( NS_USER_TALK, $lastRevUser );
404 $batch->execute();
406 // Page creator
407 $pageInfo['header-edits'][] = array(
408 $this->msg( 'pageinfo-firstuser' ),
409 Linker::revUserTools( $firstRev )
412 // Date of page creation
413 $pageInfo['header-edits'][] = array(
414 $this->msg( 'pageinfo-firsttime' ),
415 Linker::linkKnown(
416 $title,
417 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
418 array(),
419 array( 'oldid' => $firstRev->getId() )
423 // Latest editor
424 $pageInfo['header-edits'][] = array(
425 $this->msg( 'pageinfo-lastuser' ),
426 Linker::revUserTools( $lastRev )
429 // Date of latest edit
430 $pageInfo['header-edits'][] = array(
431 $this->msg( 'pageinfo-lasttime' ),
432 Linker::linkKnown(
433 $title,
434 $lang->userTimeAndDate( $this->page->getTimestamp(), $user ),
435 array(),
436 array( 'oldid' => $this->page->getLatest() )
440 // Total number of edits
441 $pageInfo['header-edits'][] = array(
442 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
445 // Total number of distinct authors
446 $pageInfo['header-edits'][] = array(
447 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
450 // Recent number of edits (within past 30 days)
451 $pageInfo['header-edits'][] = array(
452 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
453 $lang->formatNum( $pageCounts['recent_edits'] )
456 // Recent number of distinct authors
457 $pageInfo['header-edits'][] = array(
458 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
461 // Array of MagicWord objects
462 $magicWords = MagicWord::getDoubleUnderscoreArray();
464 // Array of magic word IDs
465 $wordIDs = $magicWords->names;
467 // Array of IDs => localized magic words
468 $localizedWords = $wgContLang->getMagicWords();
470 $listItems = array();
471 foreach ( $pageProperties as $property => $value ) {
472 if ( in_array( $property, $wordIDs ) ) {
473 $listItems[] = Html::element( 'li', array(), $localizedWords[$property][1] );
477 $localizedList = Html::rawElement( 'ul', array(), implode( '', $listItems ) );
478 $hiddenCategories = $this->page->getHiddenCategories();
480 if (
481 count( $listItems ) > 0 ||
482 count( $hiddenCategories ) > 0 ||
483 $pageCounts['transclusion']['from'] > 0 ||
484 $pageCounts['transclusion']['to'] > 0
486 $options = array( 'LIMIT' => $wgPageInfoTransclusionLimit );
487 $transcludedTemplates = $title->getTemplateLinksFrom( $options );
488 $transcludedTargets = $title->getTemplateLinksTo( $options );
490 // Page properties
491 $pageInfo['header-properties'] = array();
493 // Magic words
494 if ( count( $listItems ) > 0 ) {
495 $pageInfo['header-properties'][] = array(
496 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
497 $localizedList
501 // Hidden categories
502 if ( count( $hiddenCategories ) > 0 ) {
503 $pageInfo['header-properties'][] = array(
504 $this->msg( 'pageinfo-hidden-categories' )
505 ->numParams( count( $hiddenCategories ) ),
506 Linker::formatHiddenCategories( $hiddenCategories )
510 // Transcluded templates
511 if ( $pageCounts['transclusion']['from'] > 0 ) {
512 if ( $pageCounts['transclusion']['from'] > count( $transcludedTemplates ) ) {
513 $more = $this->msg( 'morenotlisted' )->escaped();
514 } else {
515 $more = null;
518 $pageInfo['header-properties'][] = array(
519 $this->msg( 'pageinfo-templates' )
520 ->numParams( $pageCounts['transclusion']['from'] ),
521 Linker::formatTemplates(
522 $transcludedTemplates,
523 false,
524 false,
525 $more )
529 if ( $pageCounts['transclusion']['to'] > 0 ) {
530 if ( $pageCounts['transclusion']['to'] > count( $transcludedTargets ) ) {
531 $more = Linker::link(
532 $whatLinksHere,
533 $this->msg( 'moredotdotdot' )->escaped(),
534 array(),
535 array( 'hidelinks' => 1, 'hideredirs' => 1 )
537 } else {
538 $more = null;
541 $pageInfo['header-properties'][] = array(
542 $this->msg( 'pageinfo-transclusions' )
543 ->numParams( $pageCounts['transclusion']['to'] ),
544 Linker::formatTemplates(
545 $transcludedTargets,
546 false,
547 false,
548 $more )
553 return $pageInfo;
557 * Returns page counts that would be too "expensive" to retrieve by normal means.
559 * @param Title $title Title to get counts for
560 * @return array
562 protected static function pageCounts( Title $title ) {
563 global $wgRCMaxAge, $wgDisableCounters;
565 wfProfileIn( __METHOD__ );
566 $id = $title->getArticleID();
568 $dbr = wfGetDB( DB_SLAVE );
569 $result = array();
571 if ( !$wgDisableCounters ) {
572 // Number of views
573 $views = (int) $dbr->selectField(
574 'page',
575 'page_counter',
576 array( 'page_id' => $id ),
577 __METHOD__
579 $result['views'] = $views;
582 // Number of page watchers
583 $watchers = (int) $dbr->selectField(
584 'watchlist',
585 'COUNT(*)',
586 array(
587 'wl_namespace' => $title->getNamespace(),
588 'wl_title' => $title->getDBkey(),
590 __METHOD__
592 $result['watchers'] = $watchers;
594 // Total number of edits
595 $edits = (int) $dbr->selectField(
596 'revision',
597 'COUNT(rev_page)',
598 array( 'rev_page' => $id ),
599 __METHOD__
601 $result['edits'] = $edits;
603 // Total number of distinct authors
604 $authors = (int) $dbr->selectField(
605 'revision',
606 'COUNT(DISTINCT rev_user_text)',
607 array( 'rev_page' => $id ),
608 __METHOD__
610 $result['authors'] = $authors;
612 // "Recent" threshold defined by $wgRCMaxAge
613 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
615 // Recent number of edits
616 $edits = (int) $dbr->selectField(
617 'revision',
618 'COUNT(rev_page)',
619 array(
620 'rev_page' => $id,
621 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
623 __METHOD__
625 $result['recent_edits'] = $edits;
627 // Recent number of distinct authors
628 $authors = (int) $dbr->selectField(
629 'revision',
630 'COUNT(DISTINCT rev_user_text)',
631 array(
632 'rev_page' => $id,
633 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
635 __METHOD__
637 $result['recent_authors'] = $authors;
639 // Subpages (if enabled)
640 if ( MWNamespace::hasSubpages( $title->getNamespace() ) ) {
641 $conds = array( 'page_namespace' => $title->getNamespace() );
642 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
644 // Subpages of this page (redirects)
645 $conds['page_is_redirect'] = 1;
646 $result['subpages']['redirects'] = (int) $dbr->selectField(
647 'page',
648 'COUNT(page_id)',
649 $conds,
650 __METHOD__ );
652 // Subpages of this page (non-redirects)
653 $conds['page_is_redirect'] = 0;
654 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
655 'page',
656 'COUNT(page_id)',
657 $conds,
658 __METHOD__
661 // Subpages of this page (total)
662 $result['subpages']['total'] = $result['subpages']['redirects']
663 + $result['subpages']['nonredirects'];
666 // Counts for the number of transclusion links (to/from)
667 $result['transclusion']['to'] = (int) $dbr->selectField(
668 'templatelinks',
669 'COUNT(tl_from)',
670 array(
671 'tl_namespace' => $title->getNamespace(),
672 'tl_title' => $title->getDBkey()
674 __METHOD__
677 $result['transclusion']['from'] = (int) $dbr->selectField(
678 'templatelinks',
679 'COUNT(*)',
680 array( 'tl_from' => $title->getArticleID() ),
681 __METHOD__
684 wfProfileOut( __METHOD__ );
685 return $result;
689 * Returns the name that goes in the "<h1>" page title.
691 * @return string
693 protected function getPageTitle() {
694 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
698 * Get a list of contributors of $article
699 * @return string: html
701 protected function getContributors() {
702 global $wgHiddenPrefs;
704 $contributors = $this->page->getContributors();
705 $real_names = array();
706 $user_names = array();
707 $anon_ips = array();
709 # Sift for real versus user names
710 foreach ( $contributors as $user ) {
711 $page = $user->isAnon()
712 ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
713 : $user->getUserPage();
715 if ( $user->getID() == 0 ) {
716 $anon_ips[] = Linker::link( $page, htmlspecialchars( $user->getName() ) );
717 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
718 $real_names[] = Linker::link( $page, htmlspecialchars( $user->getRealName() ) );
719 } else {
720 $user_names[] = Linker::link( $page, htmlspecialchars( $user->getName() ) );
724 $lang = $this->getLanguage();
726 $real = $lang->listToText( $real_names );
728 # "ThisSite user(s) A, B and C"
729 if ( count( $user_names ) ) {
730 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
731 count( $user_names ) )->escaped();
732 } else {
733 $user = false;
736 if ( count( $anon_ips ) ) {
737 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
738 count( $anon_ips ) )->escaped();
739 } else {
740 $anon = false;
743 # This is the big list, all mooshed together. We sift for blank strings
744 $fulllist = array();
745 foreach ( array( $real, $user, $anon ) as $s ) {
746 if ( $s !== '' ) {
747 array_push( $fulllist, $s );
751 $count = count( $fulllist );
752 # "Based on work by ..."
753 return $count
754 ? $this->msg( 'othercontribs' )->rawParams(
755 $lang->listToText( $fulllist ) )->params( $count )->escaped()
756 : '';
760 * Returns the description that goes below the "<h1>" tag.
762 * @return string
764 protected function getDescription() {
765 return '';