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
26 * Displays information about a page.
30 class InfoAction
extends FormlessAction
{
31 const CACHE_VERSION
= '2013-03-17';
34 * Returns the name of the action this object responds to.
36 * @return string Lowercase name
38 public function getName() {
43 * Whether this action can still be executed by a blocked user.
47 public function requiresUnblock() {
52 * Whether this action requires the wiki not to be locked.
56 public function requiresWrite() {
61 * Clear the info cache for a given Title.
64 * @param Title $title Title to clear cache for
66 public static function invalidateCache( Title
$title ) {
69 $revision = WikiPage
::factory( $title )->getRevision();
70 if ( $revision !== null ) {
71 $key = wfMemcKey( 'infoaction', sha1( $title->getPrefixedText() ), $revision->getId() );
72 $wgMemc->delete( $key );
77 * Shows page information on GET request.
79 * @return string Page information that will be added to the output
81 public function onView() {
85 $oldid = $this->page
->getOldID();
87 $revision = $this->page
->getRevisionFetched();
89 // Revision is missing
90 if ( $revision === null ) {
91 return $this->msg( 'missing-revision', $oldid )->parse();
94 // Revision is not current
95 if ( !$revision->isCurrent() ) {
96 return $this->msg( 'pageinfo-not-current' )->plain();
101 if ( !$this->msg( 'pageinfo-header' )->isDisabled() ) {
102 $content .= $this->msg( 'pageinfo-header' )->parse();
105 // Hide "This page is a member of # hidden categories" explanation
106 $content .= Html
::element( 'style', array(),
107 '.mw-hiddenCategoriesExplanation { display: none; }' ) . "\n";
109 // Hide "Templates used on this page" explanation
110 $content .= Html
::element( 'style', array(),
111 '.mw-templatesUsedExplanation { display: none; }' ) . "\n";
113 // Get page information
114 $pageInfo = $this->pageInfo();
116 // Allow extensions to add additional information
117 wfRunHooks( 'InfoAction', array( $this->getContext(), &$pageInfo ) );
119 // Render page information
120 foreach ( $pageInfo as $header => $infoTable ) {
122 // pageinfo-header-basic, pageinfo-header-edits, pageinfo-header-restrictions,
123 // pageinfo-header-properties, pageinfo-category-info
124 $content .= $this->makeHeader( $this->msg( "pageinfo-${header}" )->escaped() ) . "\n";
126 foreach ( $infoTable as $infoRow ) {
127 $name = ( $infoRow[0] instanceof Message
) ?
$infoRow[0]->escaped() : $infoRow[0];
128 $value = ( $infoRow[1] instanceof Message
) ?
$infoRow[1]->escaped() : $infoRow[1];
129 $id = ( $infoRow[0] instanceof Message
) ?
$infoRow[0]->getKey() : null;
130 $table = $this->addRow( $table, $name, $value, $id ) . "\n";
132 $content = $this->addTable( $content, $table ) . "\n";
136 if ( !$this->msg( 'pageinfo-footer' )->isDisabled() ) {
137 $content .= $this->msg( 'pageinfo-footer' )->parse();
141 /*if ( $this->page->exists() ) {
142 $content .= Html::rawElement( 'div', array( 'id' => 'mw-credits' ), $this->getContributors() );
149 * Creates a header that can be added to the output.
151 * @param string $header The header text.
152 * @return string The HTML.
154 protected function makeHeader( $header ) {
155 $spanAttribs = array( 'class' => 'mw-headline', 'id' => Sanitizer
::escapeId( $header ) );
157 return Html
::rawElement( 'h2', array(), Html
::element( 'span', $spanAttribs, $header ) );
161 * Adds a row to a table that will be added to the content.
163 * @param string $table The table that will be added to the content
164 * @param string $name The name of the row
165 * @param string $value The value of the row
166 * @param string $id The ID to use for the 'tr' element
167 * @return string The table with the row added
169 protected function addRow( $table, $name, $value, $id ) {
170 return $table . Html
::rawElement( 'tr', $id === null ?
array() : array( 'id' => 'mw-' . $id ),
171 Html
::rawElement( 'td', array( 'style' => 'vertical-align: top;' ), $name ) .
172 Html
::rawElement( 'td', array(), $value )
177 * Adds a table to the content that will be added to the output.
179 * @param string $content The content that will be added to the output
180 * @param string $table The table
181 * @return string The content with the table added
183 protected function addTable( $content, $table ) {
184 return $content . Html
::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
189 * Returns page information in an easily-manipulated format. Array keys are used so extensions
190 * may add additional information in arbitrary positions. Array values are arrays with one
191 * element to be rendered as a header, arrays with two elements to be rendered as a table row.
195 protected function pageInfo() {
196 global $wgContLang, $wgMemc;
198 $user = $this->getUser();
199 $lang = $this->getLanguage();
200 $title = $this->getTitle();
201 $id = $title->getArticleID();
202 $config = $this->context
->getConfig();
204 $memcKey = wfMemcKey( 'infoaction',
205 sha1( $title->getPrefixedText() ), $this->page
->getLatest() );
206 $pageCounts = $wgMemc->get( $memcKey );
207 $version = isset( $pageCounts['cacheversion'] ) ?
$pageCounts['cacheversion'] : false;
208 if ( $pageCounts === false ||
$version !== self
::CACHE_VERSION
) {
209 // Get page information that would be too "expensive" to retrieve by normal means
210 $pageCounts = $this->pageCounts( $title );
211 $pageCounts['cacheversion'] = self
::CACHE_VERSION
;
213 $wgMemc->set( $memcKey, $pageCounts );
216 // Get page properties
217 $dbr = wfGetDB( DB_SLAVE
);
218 $result = $dbr->select(
220 array( 'pp_propname', 'pp_value' ),
221 array( 'pp_page' => $id ),
225 $pageProperties = array();
226 foreach ( $result as $row ) {
227 $pageProperties[$row->pp_propname
] = $row->pp_value
;
232 $pageInfo['header-basic'] = array();
235 $displayTitle = $title->getPrefixedText();
236 if ( !empty( $pageProperties['displaytitle'] ) ) {
237 $displayTitle = $pageProperties['displaytitle'];
240 $pageInfo['header-basic'][] = array(
241 $this->msg( 'pageinfo-display-title' ), $displayTitle
244 // Is it a redirect? If so, where to?
245 if ( $title->isRedirect() ) {
246 $pageInfo['header-basic'][] = array(
247 $this->msg( 'pageinfo-redirectsto' ),
248 Linker
::link( $this->page
->getRedirectTarget() ) .
249 $this->msg( 'word-separator' )->text() .
250 $this->msg( 'parentheses', Linker
::link(
251 $this->page
->getRedirectTarget(),
252 $this->msg( 'pageinfo-redirectsto-info' )->escaped(),
254 array( 'action' => 'info' )
260 $sortKey = $title->getCategorySortkey();
261 if ( !empty( $pageProperties['defaultsort'] ) ) {
262 $sortKey = $pageProperties['defaultsort'];
265 $sortKey = htmlspecialchars( $sortKey );
266 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-default-sort' ), $sortKey );
268 // Page length (in bytes)
269 $pageInfo['header-basic'][] = array(
270 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
273 // Page ID (number not localised, as it's a database ID)
274 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-article-id' ), $id );
276 // Language in which the page content is (supposed to be) written
277 $pageLang = $title->getPageLanguage()->getCode();
279 if ( $config->get( 'PageLanguageUseDB' ) && $this->getTitle()->userCan( 'pagelang' ) ) {
280 // Link to Special:PageLanguage with pre-filled page title if user has permissions
281 $titleObj = SpecialPage
::getTitleFor( 'PageLanguage', $title->getPrefixedText() );
282 $langDisp = Linker
::link(
284 $this->msg( 'pageinfo-language' )->escaped()
287 // Display just the message
288 $langDisp = $this->msg( 'pageinfo-language' )->escaped();
291 $pageInfo['header-basic'][] = array( $langDisp,
292 Language
::fetchLanguageName( $pageLang, $lang->getCode() )
293 . ' ' . $this->msg( 'parentheses', $pageLang )->escaped() );
295 // Content model of the page
296 $pageInfo['header-basic'][] = array(
297 $this->msg( 'pageinfo-content-model' ),
298 ContentHandler
::getLocalizedName( $title->getContentModel() )
301 // Search engine status
302 $pOutput = new ParserOutput();
303 if ( isset( $pageProperties['noindex'] ) ) {
304 $pOutput->setIndexPolicy( 'noindex' );
306 if ( isset( $pageProperties['index'] ) ) {
307 $pOutput->setIndexPolicy( 'index' );
310 // Use robot policy logic
311 $policy = $this->page
->getRobotPolicy( 'view', $pOutput );
312 $pageInfo['header-basic'][] = array(
313 // Messages: pageinfo-robot-index, pageinfo-robot-noindex
314 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
317 $unwatchedPageThreshold = $config->get( 'UnwatchedPageThreshold' );
319 $user->isAllowed( 'unwatchedpages' ) ||
320 ( $unwatchedPageThreshold !== false &&
321 $pageCounts['watchers'] >= $unwatchedPageThreshold )
323 // Number of page watchers
324 $pageInfo['header-basic'][] = array(
325 $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
327 } elseif ( $unwatchedPageThreshold !== false ) {
328 $pageInfo['header-basic'][] = array(
329 $this->msg( 'pageinfo-watchers' ),
330 $this->msg( 'pageinfo-few-watchers' )->numParams( $unwatchedPageThreshold )
334 // Redirects to this page
335 $whatLinksHere = SpecialPage
::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
336 $pageInfo['header-basic'][] = array(
339 $this->msg( 'pageinfo-redirects-name' )->escaped(),
341 array( 'hidelinks' => 1, 'hidetrans' => 1 )
343 $this->msg( 'pageinfo-redirects-value' )
344 ->numParams( count( $title->getRedirectsHere() ) )
347 // Is it counted as a content page?
348 if ( $this->page
->isCountable() ) {
349 $pageInfo['header-basic'][] = array(
350 $this->msg( 'pageinfo-contentpage' ),
351 $this->msg( 'pageinfo-contentpage-yes' )
355 // Subpages of this page, if subpages are enabled for the current NS
356 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
357 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
358 $pageInfo['header-basic'][] = array(
359 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
360 $this->msg( 'pageinfo-subpages-value' )
362 $pageCounts['subpages']['total'],
363 $pageCounts['subpages']['redirects'],
364 $pageCounts['subpages']['nonredirects'] )
368 if ( $title->inNamespace( NS_CATEGORY
) ) {
369 $category = Category
::newFromTitle( $title );
370 $pageInfo['category-info'] = array(
372 $this->msg( 'pageinfo-category-pages' ),
373 $lang->formatNum( $category->getPageCount() )
376 $this->msg( 'pageinfo-category-subcats' ),
377 $lang->formatNum( $category->getSubcatCount() )
380 $this->msg( 'pageinfo-category-files' ),
381 $lang->formatNum( $category->getFileCount() )
387 $pageInfo['header-restrictions'] = array();
389 // Is this page effected by the cascading protection of something which includes it?
390 if ( $title->isCascadeProtected() ) {
392 $sources = $title->getCascadeProtectionSources(); // Array deferencing is in PHP 5.4 :(
394 foreach ( $sources[0] as $sourceTitle ) {
395 $cascadingFrom .= Html
::rawElement( 'li', array(), Linker
::linkKnown( $sourceTitle ) );
398 $cascadingFrom = Html
::rawElement( 'ul', array(), $cascadingFrom );
399 $pageInfo['header-restrictions'][] = array(
400 $this->msg( 'pageinfo-protect-cascading-from' ),
405 // Is out protection set to cascade to other pages?
406 if ( $title->areRestrictionsCascading() ) {
407 $pageInfo['header-restrictions'][] = array(
408 $this->msg( 'pageinfo-protect-cascading' ),
409 $this->msg( 'pageinfo-protect-cascading-yes' )
414 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
415 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
417 if ( $protectionLevel == '' ) {
419 $message = $this->msg( 'protect-default' )->escaped();
421 // Administrators only
422 // Messages: protect-level-autoconfirmed, protect-level-sysop
423 $message = $this->msg( "protect-level-$protectionLevel" );
424 if ( $message->isDisabled() ) {
425 // Require "$1" permission
426 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
428 $message = $message->escaped();
432 // Messages: restriction-edit, restriction-move, restriction-create,
433 // restriction-upload
434 $pageInfo['header-restrictions'][] = array(
435 $this->msg( "restriction-$restrictionType" ), $message
439 if ( !$this->page
->exists() ) {
444 $pageInfo['header-edits'] = array();
446 $firstRev = $this->page
->getOldestRevision();
447 $lastRev = $this->page
->getRevision();
448 $batch = new LinkBatch
;
451 $firstRevUser = $firstRev->getUserText( Revision
::FOR_THIS_USER
);
452 if ( $firstRevUser !== '' ) {
453 $batch->add( NS_USER
, $firstRevUser );
454 $batch->add( NS_USER_TALK
, $firstRevUser );
459 $lastRevUser = $lastRev->getUserText( Revision
::FOR_THIS_USER
);
460 if ( $lastRevUser !== '' ) {
461 $batch->add( NS_USER
, $lastRevUser );
462 $batch->add( NS_USER_TALK
, $lastRevUser );
470 $pageInfo['header-edits'][] = array(
471 $this->msg( 'pageinfo-firstuser' ),
472 Linker
::revUserTools( $firstRev )
475 // Date of page creation
476 $pageInfo['header-edits'][] = array(
477 $this->msg( 'pageinfo-firsttime' ),
480 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
482 array( 'oldid' => $firstRev->getId() )
489 $pageInfo['header-edits'][] = array(
490 $this->msg( 'pageinfo-lastuser' ),
491 Linker
::revUserTools( $lastRev )
494 // Date of latest edit
495 $pageInfo['header-edits'][] = array(
496 $this->msg( 'pageinfo-lasttime' ),
499 $lang->userTimeAndDate( $this->page
->getTimestamp(), $user ),
501 array( 'oldid' => $this->page
->getLatest() )
506 // Total number of edits
507 $pageInfo['header-edits'][] = array(
508 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
511 // Total number of distinct authors
512 $pageInfo['header-edits'][] = array(
513 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
516 // Recent number of edits (within past 30 days)
517 $pageInfo['header-edits'][] = array(
518 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $config->get( 'RCMaxAge' ) ) ),
519 $lang->formatNum( $pageCounts['recent_edits'] )
522 // Recent number of distinct authors
523 $pageInfo['header-edits'][] = array(
524 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
527 // Array of MagicWord objects
528 $magicWords = MagicWord
::getDoubleUnderscoreArray();
530 // Array of magic word IDs
531 $wordIDs = $magicWords->names
;
533 // Array of IDs => localized magic words
534 $localizedWords = $wgContLang->getMagicWords();
536 $listItems = array();
537 foreach ( $pageProperties as $property => $value ) {
538 if ( in_array( $property, $wordIDs ) ) {
539 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
543 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
544 $hiddenCategories = $this->page
->getHiddenCategories();
547 count( $listItems ) > 0 ||
548 count( $hiddenCategories ) > 0 ||
549 $pageCounts['transclusion']['from'] > 0 ||
550 $pageCounts['transclusion']['to'] > 0
552 $options = array( 'LIMIT' => $config->get( 'PageInfoTransclusionLimit' ) );
553 $transcludedTemplates = $title->getTemplateLinksFrom( $options );
554 if ( $config->get( 'MiserMode' ) ) {
555 $transcludedTargets = array();
557 $transcludedTargets = $title->getTemplateLinksTo( $options );
561 $pageInfo['header-properties'] = array();
564 if ( count( $listItems ) > 0 ) {
565 $pageInfo['header-properties'][] = array(
566 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
572 if ( count( $hiddenCategories ) > 0 ) {
573 $pageInfo['header-properties'][] = array(
574 $this->msg( 'pageinfo-hidden-categories' )
575 ->numParams( count( $hiddenCategories ) ),
576 Linker
::formatHiddenCategories( $hiddenCategories )
580 // Transcluded templates
581 if ( $pageCounts['transclusion']['from'] > 0 ) {
582 if ( $pageCounts['transclusion']['from'] > count( $transcludedTemplates ) ) {
583 $more = $this->msg( 'morenotlisted' )->escaped();
588 $pageInfo['header-properties'][] = array(
589 $this->msg( 'pageinfo-templates' )
590 ->numParams( $pageCounts['transclusion']['from'] ),
591 Linker
::formatTemplates(
592 $transcludedTemplates,
599 if ( !$config->get( 'MiserMode' ) && $pageCounts['transclusion']['to'] > 0 ) {
600 if ( $pageCounts['transclusion']['to'] > count( $transcludedTargets ) ) {
601 $more = Linker
::link(
603 $this->msg( 'moredotdotdot' )->escaped(),
605 array( 'hidelinks' => 1, 'hideredirs' => 1 )
611 $pageInfo['header-properties'][] = array(
612 $this->msg( 'pageinfo-transclusions' )
613 ->numParams( $pageCounts['transclusion']['to'] ),
614 Linker
::formatTemplates(
627 * Returns page counts that would be too "expensive" to retrieve by normal means.
629 * @param Title $title Title to get counts for
632 protected function pageCounts( Title
$title ) {
633 wfProfileIn( __METHOD__
);
634 $id = $title->getArticleID();
635 $config = $this->context
->getConfig();
637 $dbr = wfGetDB( DB_SLAVE
);
640 // Number of page watchers
641 $watchers = (int)$dbr->selectField(
645 'wl_namespace' => $title->getNamespace(),
646 'wl_title' => $title->getDBkey(),
650 $result['watchers'] = $watchers;
652 // Total number of edits
653 $edits = (int)$dbr->selectField(
656 array( 'rev_page' => $id ),
659 $result['edits'] = $edits;
661 // Total number of distinct authors
662 $authors = (int)$dbr->selectField(
664 'COUNT(DISTINCT rev_user_text)',
665 array( 'rev_page' => $id ),
668 $result['authors'] = $authors;
670 // "Recent" threshold defined by RCMaxAge setting
671 $threshold = $dbr->timestamp( time() - $config->get( 'RCMaxAge' ) );
673 // Recent number of edits
674 $edits = (int)$dbr->selectField(
679 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
683 $result['recent_edits'] = $edits;
685 // Recent number of distinct authors
686 $authors = (int)$dbr->selectField(
688 'COUNT(DISTINCT rev_user_text)',
691 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
695 $result['recent_authors'] = $authors;
697 // Subpages (if enabled)
698 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
699 $conds = array( 'page_namespace' => $title->getNamespace() );
700 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
702 // Subpages of this page (redirects)
703 $conds['page_is_redirect'] = 1;
704 $result['subpages']['redirects'] = (int)$dbr->selectField(
710 // Subpages of this page (non-redirects)
711 $conds['page_is_redirect'] = 0;
712 $result['subpages']['nonredirects'] = (int)$dbr->selectField(
719 // Subpages of this page (total)
720 $result['subpages']['total'] = $result['subpages']['redirects']
721 +
$result['subpages']['nonredirects'];
724 // Counts for the number of transclusion links (to/from)
725 if ( $config->get( 'MiserMode' ) ) {
726 $result['transclusion']['to'] = 0;
728 $result['transclusion']['to'] = (int)$dbr->selectField(
732 'tl_namespace' => $title->getNamespace(),
733 'tl_title' => $title->getDBkey()
739 $result['transclusion']['from'] = (int)$dbr->selectField(
742 array( 'tl_from' => $title->getArticleID() ),
746 wfProfileOut( __METHOD__
);
752 * Returns the name that goes in the "<h1>" page title.
756 protected function getPageTitle() {
757 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
761 * Get a list of contributors of $article
762 * @return string Html
764 protected function getContributors() {
765 $contributors = $this->page
->getContributors();
766 $real_names = array();
767 $user_names = array();
770 # Sift for real versus user names
771 /** @var $user User */
772 foreach ( $contributors as $user ) {
773 $page = $user->isAnon()
774 ? SpecialPage
::getTitleFor( 'Contributions', $user->getName() )
775 : $user->getUserPage();
777 $hiddenPrefs = $this->context
->getConfig()->get( 'HiddenPrefs' );
778 if ( $user->getID() == 0 ) {
779 $anon_ips[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
780 } elseif ( !in_array( 'realname', $hiddenPrefs ) && $user->getRealName() ) {
781 $real_names[] = Linker
::link( $page, htmlspecialchars( $user->getRealName() ) );
783 $user_names[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
787 $lang = $this->getLanguage();
789 $real = $lang->listToText( $real_names );
791 # "ThisSite user(s) A, B and C"
792 if ( count( $user_names ) ) {
793 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
794 count( $user_names ) )->escaped();
799 if ( count( $anon_ips ) ) {
800 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
801 count( $anon_ips ) )->escaped();
806 # This is the big list, all mooshed together. We sift for blank strings
808 foreach ( array( $real, $user, $anon ) as $s ) {
810 array_push( $fulllist, $s );
814 $count = count( $fulllist );
816 # "Based on work by ..."
818 ?
$this->msg( 'othercontribs' )->rawParams(
819 $lang->listToText( $fulllist ) )->params( $count )->escaped()
824 * Returns the description that goes below the "<h1>" tag.
828 protected function getDescription() {