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, $wgRCMaxAge, $wgMemc, $wgMiserMode,
197 $wgUnwatchedPageThreshold, $wgPageInfoTransclusionLimit;
199 $user = $this->getUser();
200 $lang = $this->getLanguage();
201 $title = $this->getTitle();
202 $id = $title->getArticleID();
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 = self
::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 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-default-sort' ), $sortKey );
267 // Page length (in bytes)
268 $pageInfo['header-basic'][] = array(
269 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
272 // Page ID (number not localised, as it's a database ID)
273 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-article-id' ), $id );
275 // Language in which the page content is (supposed to be) written
276 $pageLang = $title->getPageLanguage()->getCode();
277 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-language' ),
278 Language
::fetchLanguageName( $pageLang, $lang->getCode() )
279 . ' ' . $this->msg( 'parentheses', $pageLang ) );
281 // Content model of the page
282 $pageInfo['header-basic'][] = array(
283 $this->msg( 'pageinfo-content-model' ),
284 ContentHandler
::getLocalizedName( $title->getContentModel() )
287 // Search engine status
288 $pOutput = new ParserOutput();
289 if ( isset( $pageProperties['noindex'] ) ) {
290 $pOutput->setIndexPolicy( 'noindex' );
292 if ( isset( $pageProperties['index'] ) ) {
293 $pOutput->setIndexPolicy( 'index' );
296 // Use robot policy logic
297 $policy = $this->page
->getRobotPolicy( 'view', $pOutput );
298 $pageInfo['header-basic'][] = array(
299 // Messages: pageinfo-robot-index, pageinfo-robot-noindex
300 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
303 if ( isset( $pageCounts['views'] ) ) {
305 $pageInfo['header-basic'][] = array(
306 $this->msg( 'pageinfo-views' ), $lang->formatNum( $pageCounts['views'] )
311 $user->isAllowed( 'unwatchedpages' ) ||
312 ( $wgUnwatchedPageThreshold !== false &&
313 $pageCounts['watchers'] >= $wgUnwatchedPageThreshold )
315 // Number of page watchers
316 $pageInfo['header-basic'][] = array(
317 $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
319 } elseif ( $wgUnwatchedPageThreshold !== false ) {
320 $pageInfo['header-basic'][] = array(
321 $this->msg( 'pageinfo-watchers' ),
322 $this->msg( 'pageinfo-few-watchers' )->numParams( $wgUnwatchedPageThreshold )
326 // Redirects to this page
327 $whatLinksHere = SpecialPage
::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
328 $pageInfo['header-basic'][] = array(
331 $this->msg( 'pageinfo-redirects-name' )->escaped(),
333 array( 'hidelinks' => 1, 'hidetrans' => 1 )
335 $this->msg( 'pageinfo-redirects-value' )
336 ->numParams( count( $title->getRedirectsHere() ) )
339 // Is it counted as a content page?
340 if ( $this->page
->isCountable() ) {
341 $pageInfo['header-basic'][] = array(
342 $this->msg( 'pageinfo-contentpage' ),
343 $this->msg( 'pageinfo-contentpage-yes' )
347 // Subpages of this page, if subpages are enabled for the current NS
348 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
349 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
350 $pageInfo['header-basic'][] = array(
351 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
352 $this->msg( 'pageinfo-subpages-value' )
354 $pageCounts['subpages']['total'],
355 $pageCounts['subpages']['redirects'],
356 $pageCounts['subpages']['nonredirects'] )
360 if ( $title->inNamespace( NS_CATEGORY
) ) {
361 $category = Category
::newFromTitle( $title );
362 $pageInfo['category-info'] = array(
364 $this->msg( 'pageinfo-category-pages' ),
365 $lang->formatNum( $category->getPageCount() )
368 $this->msg( 'pageinfo-category-subcats' ),
369 $lang->formatNum( $category->getSubcatCount() )
372 $this->msg( 'pageinfo-category-files' ),
373 $lang->formatNum( $category->getFileCount() )
379 $pageInfo['header-restrictions'] = array();
381 // Is this page effected by the cascading protection of something which includes it?
382 if ( $title->isCascadeProtected() ) {
384 $sources = $title->getCascadeProtectionSources(); // Array deferencing is in PHP 5.4 :(
386 foreach ( $sources[0] as $sourceTitle ) {
387 $cascadingFrom .= Html
::rawElement( 'li', array(), Linker
::linkKnown( $sourceTitle ) );
390 $cascadingFrom = Html
::rawElement( 'ul', array(), $cascadingFrom );
391 $pageInfo['header-restrictions'][] = array(
392 $this->msg( 'pageinfo-protect-cascading-from' ),
397 // Is out protection set to cascade to other pages?
398 if ( $title->areRestrictionsCascading() ) {
399 $pageInfo['header-restrictions'][] = array(
400 $this->msg( 'pageinfo-protect-cascading' ),
401 $this->msg( 'pageinfo-protect-cascading-yes' )
406 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
407 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
409 if ( $protectionLevel == '' ) {
411 $message = $this->msg( 'protect-default' )->escaped();
413 // Administrators only
414 // Messages: protect-level-autoconfirmed, protect-level-sysop
415 $message = $this->msg( "protect-level-$protectionLevel" );
416 if ( $message->isDisabled() ) {
417 // Require "$1" permission
418 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
420 $message = $message->escaped();
424 // Messages: restriction-edit, restriction-move, restriction-create,
425 // restriction-upload
426 $pageInfo['header-restrictions'][] = array(
427 $this->msg( "restriction-$restrictionType" ), $message
431 if ( !$this->page
->exists() ) {
436 $pageInfo['header-edits'] = array();
438 $firstRev = $this->page
->getOldestRevision();
439 $lastRev = $this->page
->getRevision();
440 $batch = new LinkBatch
;
443 $firstRevUser = $firstRev->getUserText( Revision
::FOR_THIS_USER
);
444 if ( $firstRevUser !== '' ) {
445 $batch->add( NS_USER
, $firstRevUser );
446 $batch->add( NS_USER_TALK
, $firstRevUser );
451 $lastRevUser = $lastRev->getUserText( Revision
::FOR_THIS_USER
);
452 if ( $lastRevUser !== '' ) {
453 $batch->add( NS_USER
, $lastRevUser );
454 $batch->add( NS_USER_TALK
, $lastRevUser );
462 $pageInfo['header-edits'][] = array(
463 $this->msg( 'pageinfo-firstuser' ),
464 Linker
::revUserTools( $firstRev )
467 // Date of page creation
468 $pageInfo['header-edits'][] = array(
469 $this->msg( 'pageinfo-firsttime' ),
472 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
474 array( 'oldid' => $firstRev->getId() )
481 $pageInfo['header-edits'][] = array(
482 $this->msg( 'pageinfo-lastuser' ),
483 Linker
::revUserTools( $lastRev )
486 // Date of latest edit
487 $pageInfo['header-edits'][] = array(
488 $this->msg( 'pageinfo-lasttime' ),
491 $lang->userTimeAndDate( $this->page
->getTimestamp(), $user ),
493 array( 'oldid' => $this->page
->getLatest() )
498 // Total number of edits
499 $pageInfo['header-edits'][] = array(
500 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
503 // Total number of distinct authors
504 $pageInfo['header-edits'][] = array(
505 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
508 // Recent number of edits (within past 30 days)
509 $pageInfo['header-edits'][] = array(
510 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
511 $lang->formatNum( $pageCounts['recent_edits'] )
514 // Recent number of distinct authors
515 $pageInfo['header-edits'][] = array(
516 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
519 // Array of MagicWord objects
520 $magicWords = MagicWord
::getDoubleUnderscoreArray();
522 // Array of magic word IDs
523 $wordIDs = $magicWords->names
;
525 // Array of IDs => localized magic words
526 $localizedWords = $wgContLang->getMagicWords();
528 $listItems = array();
529 foreach ( $pageProperties as $property => $value ) {
530 if ( in_array( $property, $wordIDs ) ) {
531 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
535 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
536 $hiddenCategories = $this->page
->getHiddenCategories();
539 count( $listItems ) > 0 ||
540 count( $hiddenCategories ) > 0 ||
541 $pageCounts['transclusion']['from'] > 0 ||
542 $pageCounts['transclusion']['to'] > 0
544 $options = array( 'LIMIT' => $wgPageInfoTransclusionLimit );
545 $transcludedTemplates = $title->getTemplateLinksFrom( $options );
546 if ( $wgMiserMode ) {
547 $transcludedTargets = array();
549 $transcludedTargets = $title->getTemplateLinksTo( $options );
553 $pageInfo['header-properties'] = array();
556 if ( count( $listItems ) > 0 ) {
557 $pageInfo['header-properties'][] = array(
558 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
564 if ( count( $hiddenCategories ) > 0 ) {
565 $pageInfo['header-properties'][] = array(
566 $this->msg( 'pageinfo-hidden-categories' )
567 ->numParams( count( $hiddenCategories ) ),
568 Linker
::formatHiddenCategories( $hiddenCategories )
572 // Transcluded templates
573 if ( $pageCounts['transclusion']['from'] > 0 ) {
574 if ( $pageCounts['transclusion']['from'] > count( $transcludedTemplates ) ) {
575 $more = $this->msg( 'morenotlisted' )->escaped();
580 $pageInfo['header-properties'][] = array(
581 $this->msg( 'pageinfo-templates' )
582 ->numParams( $pageCounts['transclusion']['from'] ),
583 Linker
::formatTemplates(
584 $transcludedTemplates,
591 if ( !$wgMiserMode && $pageCounts['transclusion']['to'] > 0 ) {
592 if ( $pageCounts['transclusion']['to'] > count( $transcludedTargets ) ) {
593 $more = Linker
::link(
595 $this->msg( 'moredotdotdot' )->escaped(),
597 array( 'hidelinks' => 1, 'hideredirs' => 1 )
603 $pageInfo['header-properties'][] = array(
604 $this->msg( 'pageinfo-transclusions' )
605 ->numParams( $pageCounts['transclusion']['to'] ),
606 Linker
::formatTemplates(
619 * Returns page counts that would be too "expensive" to retrieve by normal means.
621 * @param Title $title Title to get counts for
624 protected static function pageCounts( Title
$title ) {
625 global $wgRCMaxAge, $wgDisableCounters, $wgMiserMode;
627 wfProfileIn( __METHOD__
);
628 $id = $title->getArticleID();
630 $dbr = wfGetDB( DB_SLAVE
);
633 if ( !$wgDisableCounters ) {
635 $views = (int)$dbr->selectField(
638 array( 'page_id' => $id ),
641 $result['views'] = $views;
644 // Number of page watchers
645 $watchers = (int)$dbr->selectField(
649 'wl_namespace' => $title->getNamespace(),
650 'wl_title' => $title->getDBkey(),
654 $result['watchers'] = $watchers;
656 // Total number of edits
657 $edits = (int)$dbr->selectField(
660 array( 'rev_page' => $id ),
663 $result['edits'] = $edits;
665 // Total number of distinct authors
666 $authors = (int)$dbr->selectField(
668 'COUNT(DISTINCT rev_user_text)',
669 array( 'rev_page' => $id ),
672 $result['authors'] = $authors;
674 // "Recent" threshold defined by $wgRCMaxAge
675 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
677 // Recent number of edits
678 $edits = (int)$dbr->selectField(
683 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
687 $result['recent_edits'] = $edits;
689 // Recent number of distinct authors
690 $authors = (int)$dbr->selectField(
692 'COUNT(DISTINCT rev_user_text)',
695 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
699 $result['recent_authors'] = $authors;
701 // Subpages (if enabled)
702 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
703 $conds = array( 'page_namespace' => $title->getNamespace() );
704 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
706 // Subpages of this page (redirects)
707 $conds['page_is_redirect'] = 1;
708 $result['subpages']['redirects'] = (int)$dbr->selectField(
714 // Subpages of this page (non-redirects)
715 $conds['page_is_redirect'] = 0;
716 $result['subpages']['nonredirects'] = (int)$dbr->selectField(
723 // Subpages of this page (total)
724 $result['subpages']['total'] = $result['subpages']['redirects']
725 +
$result['subpages']['nonredirects'];
728 // Counts for the number of transclusion links (to/from)
729 if ( $wgMiserMode ) {
730 $result['transclusion']['to'] = 0;
732 $result['transclusion']['to'] = (int)$dbr->selectField(
736 'tl_namespace' => $title->getNamespace(),
737 'tl_title' => $title->getDBkey()
743 $result['transclusion']['from'] = (int)$dbr->selectField(
746 array( 'tl_from' => $title->getArticleID() ),
750 wfProfileOut( __METHOD__
);
756 * Returns the name that goes in the "<h1>" page title.
760 protected function getPageTitle() {
761 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
765 * Get a list of contributors of $article
766 * @return string Html
768 protected function getContributors() {
769 global $wgHiddenPrefs;
771 $contributors = $this->page
->getContributors();
772 $real_names = array();
773 $user_names = array();
776 # Sift for real versus user names
777 /** @var $user User */
778 foreach ( $contributors as $user ) {
779 $page = $user->isAnon()
780 ? SpecialPage
::getTitleFor( 'Contributions', $user->getName() )
781 : $user->getUserPage();
783 if ( $user->getID() == 0 ) {
784 $anon_ips[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
785 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
786 $real_names[] = Linker
::link( $page, htmlspecialchars( $user->getRealName() ) );
788 $user_names[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
792 $lang = $this->getLanguage();
794 $real = $lang->listToText( $real_names );
796 # "ThisSite user(s) A, B and C"
797 if ( count( $user_names ) ) {
798 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
799 count( $user_names ) )->escaped();
804 if ( count( $anon_ips ) ) {
805 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
806 count( $anon_ips ) )->escaped();
811 # This is the big list, all mooshed together. We sift for blank strings
813 foreach ( array( $real, $user, $anon ) as $s ) {
815 array_push( $fulllist, $s );
819 $count = count( $fulllist );
821 # "Based on work by ..."
823 ?
$this->msg( 'othercontribs' )->rawParams(
824 $lang->listToText( $fulllist ) )->params( $count )->escaped()
829 * Returns the description that goes below the "<h1>" tag.
833 protected function getDescription() {