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
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 ) );
156 return Html
::rawElement( 'h2', array(), Html
::element( 'span', $spanAttribs, $header ) );
160 * Adds a row to a table that will be added to the content.
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
168 protected function addRow( $table, $name, $value, $id ) {
169 return $table . Html
::rawElement( 'tr', $id === null ?
array() : array( 'id' => 'mw-' . $id ),
170 Html
::rawElement( 'td', array( 'style' => 'vertical-align: top;' ), $name ) .
171 Html
::rawElement( 'td', array(), $value )
176 * Adds a table to the content that will be added to the output.
178 * @param string $content The content that will be added to the output
179 * @param string $table The table
180 * @return string The content with the table added
182 protected function addTable( $content, $table ) {
183 return $content . Html
::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
188 * Returns page information in an easily-manipulated format. Array keys are used so extensions
189 * may add additional information in arbitrary positions. Array values are arrays with one
190 * element to be rendered as a header, arrays with two elements to be rendered as a table row.
194 protected function pageInfo() {
195 global $wgContLang, $wgRCMaxAge, $wgMemc,
196 $wgUnwatchedPageThreshold, $wgPageInfoTransclusionLimit;
198 $user = $this->getUser();
199 $lang = $this->getLanguage();
200 $title = $this->getTitle();
201 $id = $title->getArticleID();
203 $memcKey = wfMemcKey( 'infoaction',
204 sha1( $title->getPrefixedText() ), $this->page
->getLatest() );
205 $pageCounts = $wgMemc->get( $memcKey );
206 $version = isset( $pageCounts['cacheversion'] ) ?
$pageCounts['cacheversion'] : false;
207 if ( $pageCounts === false ||
$version !== self
::CACHE_VERSION
) {
208 // Get page information that would be too "expensive" to retrieve by normal means
209 $pageCounts = self
::pageCounts( $title );
210 $pageCounts['cacheversion'] = self
::CACHE_VERSION
;
212 $wgMemc->set( $memcKey, $pageCounts );
215 // Get page properties
216 $dbr = wfGetDB( DB_SLAVE
);
217 $result = $dbr->select(
219 array( 'pp_propname', 'pp_value' ),
220 array( 'pp_page' => $id ),
224 $pageProperties = array();
225 foreach ( $result as $row ) {
226 $pageProperties[$row->pp_propname
] = $row->pp_value
;
231 $pageInfo['header-basic'] = array();
234 $displayTitle = $title->getPrefixedText();
235 if ( !empty( $pageProperties['displaytitle'] ) ) {
236 $displayTitle = $pageProperties['displaytitle'];
239 $pageInfo['header-basic'][] = array(
240 $this->msg( 'pageinfo-display-title' ), $displayTitle
243 // Is it a redirect? If so, where to?
244 if ( $title->isRedirect() ) {
245 $pageInfo['header-basic'][] = array(
246 $this->msg( 'pageinfo-redirectsto' ),
247 Linker
::link( $this->page
->getRedirectTarget() ) .
248 $this->msg( 'word-separator' )->text() .
249 $this->msg( 'parentheses', Linker
::link(
250 $this->page
->getRedirectTarget(),
251 $this->msg( 'pageinfo-redirectsto-info' )->escaped(),
253 array( 'action' => 'info' )
259 $sortKey = $title->getCategorySortkey();
260 if ( !empty( $pageProperties['defaultsort'] ) ) {
261 $sortKey = $pageProperties['defaultsort'];
264 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-default-sort' ), $sortKey );
266 // Page length (in bytes)
267 $pageInfo['header-basic'][] = array(
268 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
271 // Page ID (number not localised, as it's a database ID)
272 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-article-id' ), $id );
274 // Language in which the page content is (supposed to be) written
275 $pageLang = $title->getPageLanguage()->getCode();
276 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-language' ),
277 Language
::fetchLanguageName( $pageLang, $lang->getCode() )
278 . ' ' . $this->msg( 'parentheses', $pageLang ) );
280 // Search engine status
281 $pOutput = new ParserOutput();
282 if ( isset( $pageProperties['noindex'] ) ) {
283 $pOutput->setIndexPolicy( 'noindex' );
286 // Use robot policy logic
287 $policy = $this->page
->getRobotPolicy( 'view', $pOutput );
288 $pageInfo['header-basic'][] = array(
289 // Messages: pageinfo-robot-index, pageinfo-robot-noindex
290 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
293 if ( isset( $pageCounts['views'] ) ) {
295 $pageInfo['header-basic'][] = array(
296 $this->msg( 'pageinfo-views' ), $lang->formatNum( $pageCounts['views'] )
301 $user->isAllowed( 'unwatchedpages' ) ||
302 ( $wgUnwatchedPageThreshold !== false &&
303 $pageCounts['watchers'] >= $wgUnwatchedPageThreshold )
305 // Number of page watchers
306 $pageInfo['header-basic'][] = array(
307 $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
309 } elseif ( $wgUnwatchedPageThreshold !== false ) {
310 $pageInfo['header-basic'][] = array(
311 $this->msg( 'pageinfo-watchers' ),
312 $this->msg( 'pageinfo-few-watchers' )->numParams( $wgUnwatchedPageThreshold )
316 // Redirects to this page
317 $whatLinksHere = SpecialPage
::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
318 $pageInfo['header-basic'][] = array(
321 $this->msg( 'pageinfo-redirects-name' )->escaped(),
323 array( 'hidelinks' => 1, 'hidetrans' => 1 )
325 $this->msg( 'pageinfo-redirects-value' )
326 ->numParams( count( $title->getRedirectsHere() ) )
329 // Is it counted as a content page?
330 if ( $this->page
->isCountable() ) {
331 $pageInfo['header-basic'][] = array(
332 $this->msg( 'pageinfo-contentpage' ),
333 $this->msg( 'pageinfo-contentpage-yes' )
337 // Subpages of this page, if subpages are enabled for the current NS
338 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
339 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
340 $pageInfo['header-basic'][] = array(
341 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
342 $this->msg( 'pageinfo-subpages-value' )
344 $pageCounts['subpages']['total'],
345 $pageCounts['subpages']['redirects'],
346 $pageCounts['subpages']['nonredirects'] )
350 if ( $title->inNamespace( NS_CATEGORY
) ) {
351 $category = Category
::newFromTitle( $title );
352 $pageInfo['category-info'] = array(
354 $this->msg( 'pageinfo-category-pages' ),
355 $lang->formatNum( $category->getPageCount() )
358 $this->msg( 'pageinfo-category-subcats' ),
359 $lang->formatNum( $category->getSubcatCount() )
362 $this->msg( 'pageinfo-category-files' ),
363 $lang->formatNum( $category->getFileCount() )
369 $pageInfo['header-restrictions'] = array();
371 // Is this page effected by the cascading protection of something which includes it?
372 if ( $title->isCascadeProtected() ) {
374 $sources = $title->getCascadeProtectionSources(); // Array deferencing is in PHP 5.4 :(
376 foreach ( $sources[0] as $sourceTitle ) {
377 $cascadingFrom .= Html
::rawElement( 'li', array(), Linker
::linkKnown( $sourceTitle ) );
380 $cascadingFrom = Html
::rawElement( 'ul', array(), $cascadingFrom );
381 $pageInfo['header-restrictions'][] = array(
382 $this->msg( 'pageinfo-protect-cascading-from' ),
387 // Is out protection set to cascade to other pages?
388 if ( $title->areRestrictionsCascading() ) {
389 $pageInfo['header-restrictions'][] = array(
390 $this->msg( 'pageinfo-protect-cascading' ),
391 $this->msg( 'pageinfo-protect-cascading-yes' )
396 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
397 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
399 if ( $protectionLevel == '' ) {
401 $message = $this->msg( 'protect-default' )->escaped();
403 // Administrators only
404 // Messages: protect-level-autoconfirmed, protect-level-sysop
405 $message = $this->msg( "protect-level-$protectionLevel" );
406 if ( $message->isDisabled() ) {
407 // Require "$1" permission
408 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
410 $message = $message->escaped();
414 // Messages: restriction-edit, restriction-move, restriction-create,
415 // restriction-upload
416 $pageInfo['header-restrictions'][] = array(
417 $this->msg( "restriction-$restrictionType" ), $message
421 if ( !$this->page
->exists() ) {
426 $pageInfo['header-edits'] = array();
428 $firstRev = $this->page
->getOldestRevision();
429 $lastRev = $this->page
->getRevision();
430 $batch = new LinkBatch
;
433 $firstRevUser = $firstRev->getUserText( Revision
::FOR_THIS_USER
);
434 if ( $firstRevUser !== '' ) {
435 $batch->add( NS_USER
, $firstRevUser );
436 $batch->add( NS_USER_TALK
, $firstRevUser );
441 $lastRevUser = $lastRev->getUserText( Revision
::FOR_THIS_USER
);
442 if ( $lastRevUser !== '' ) {
443 $batch->add( NS_USER
, $lastRevUser );
444 $batch->add( NS_USER_TALK
, $lastRevUser );
452 $pageInfo['header-edits'][] = array(
453 $this->msg( 'pageinfo-firstuser' ),
454 Linker
::revUserTools( $firstRev )
457 // Date of page creation
458 $pageInfo['header-edits'][] = array(
459 $this->msg( 'pageinfo-firsttime' ),
462 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
464 array( 'oldid' => $firstRev->getId() )
471 $pageInfo['header-edits'][] = array(
472 $this->msg( 'pageinfo-lastuser' ),
473 Linker
::revUserTools( $lastRev )
476 // Date of latest edit
477 $pageInfo['header-edits'][] = array(
478 $this->msg( 'pageinfo-lasttime' ),
481 $lang->userTimeAndDate( $this->page
->getTimestamp(), $user ),
483 array( 'oldid' => $this->page
->getLatest() )
488 // Total number of edits
489 $pageInfo['header-edits'][] = array(
490 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
493 // Total number of distinct authors
494 $pageInfo['header-edits'][] = array(
495 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
498 // Recent number of edits (within past 30 days)
499 $pageInfo['header-edits'][] = array(
500 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
501 $lang->formatNum( $pageCounts['recent_edits'] )
504 // Recent number of distinct authors
505 $pageInfo['header-edits'][] = array(
506 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
509 // Array of MagicWord objects
510 $magicWords = MagicWord
::getDoubleUnderscoreArray();
512 // Array of magic word IDs
513 $wordIDs = $magicWords->names
;
515 // Array of IDs => localized magic words
516 $localizedWords = $wgContLang->getMagicWords();
518 $listItems = array();
519 foreach ( $pageProperties as $property => $value ) {
520 if ( in_array( $property, $wordIDs ) ) {
521 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
525 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
526 $hiddenCategories = $this->page
->getHiddenCategories();
529 count( $listItems ) > 0 ||
530 count( $hiddenCategories ) > 0 ||
531 $pageCounts['transclusion']['from'] > 0 ||
532 $pageCounts['transclusion']['to'] > 0
534 $options = array( 'LIMIT' => $wgPageInfoTransclusionLimit );
535 $transcludedTemplates = $title->getTemplateLinksFrom( $options );
536 $transcludedTargets = $title->getTemplateLinksTo( $options );
539 $pageInfo['header-properties'] = array();
542 if ( count( $listItems ) > 0 ) {
543 $pageInfo['header-properties'][] = array(
544 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
550 if ( count( $hiddenCategories ) > 0 ) {
551 $pageInfo['header-properties'][] = array(
552 $this->msg( 'pageinfo-hidden-categories' )
553 ->numParams( count( $hiddenCategories ) ),
554 Linker
::formatHiddenCategories( $hiddenCategories )
558 // Transcluded templates
559 if ( $pageCounts['transclusion']['from'] > 0 ) {
560 if ( $pageCounts['transclusion']['from'] > count( $transcludedTemplates ) ) {
561 $more = $this->msg( 'morenotlisted' )->escaped();
566 $pageInfo['header-properties'][] = array(
567 $this->msg( 'pageinfo-templates' )
568 ->numParams( $pageCounts['transclusion']['from'] ),
569 Linker
::formatTemplates(
570 $transcludedTemplates,
577 if ( $pageCounts['transclusion']['to'] > 0 ) {
578 if ( $pageCounts['transclusion']['to'] > count( $transcludedTargets ) ) {
579 $more = Linker
::link(
581 $this->msg( 'moredotdotdot' )->escaped(),
583 array( 'hidelinks' => 1, 'hideredirs' => 1 )
589 $pageInfo['header-properties'][] = array(
590 $this->msg( 'pageinfo-transclusions' )
591 ->numParams( $pageCounts['transclusion']['to'] ),
592 Linker
::formatTemplates(
605 * Returns page counts that would be too "expensive" to retrieve by normal means.
607 * @param Title $title Title to get counts for
610 protected static function pageCounts( Title
$title ) {
611 global $wgRCMaxAge, $wgDisableCounters;
613 wfProfileIn( __METHOD__
);
614 $id = $title->getArticleID();
616 $dbr = wfGetDB( DB_SLAVE
);
619 if ( !$wgDisableCounters ) {
621 $views = (int)$dbr->selectField(
624 array( 'page_id' => $id ),
627 $result['views'] = $views;
630 // Number of page watchers
631 $watchers = (int)$dbr->selectField(
635 'wl_namespace' => $title->getNamespace(),
636 'wl_title' => $title->getDBkey(),
640 $result['watchers'] = $watchers;
642 // Total number of edits
643 $edits = (int)$dbr->selectField(
646 array( 'rev_page' => $id ),
649 $result['edits'] = $edits;
651 // Total number of distinct authors
652 $authors = (int)$dbr->selectField(
654 'COUNT(DISTINCT rev_user_text)',
655 array( 'rev_page' => $id ),
658 $result['authors'] = $authors;
660 // "Recent" threshold defined by $wgRCMaxAge
661 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
663 // Recent number of edits
664 $edits = (int)$dbr->selectField(
669 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
673 $result['recent_edits'] = $edits;
675 // Recent number of distinct authors
676 $authors = (int)$dbr->selectField(
678 'COUNT(DISTINCT rev_user_text)',
681 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
685 $result['recent_authors'] = $authors;
687 // Subpages (if enabled)
688 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
689 $conds = array( 'page_namespace' => $title->getNamespace() );
690 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
692 // Subpages of this page (redirects)
693 $conds['page_is_redirect'] = 1;
694 $result['subpages']['redirects'] = (int)$dbr->selectField(
700 // Subpages of this page (non-redirects)
701 $conds['page_is_redirect'] = 0;
702 $result['subpages']['nonredirects'] = (int)$dbr->selectField(
709 // Subpages of this page (total)
710 $result['subpages']['total'] = $result['subpages']['redirects']
711 +
$result['subpages']['nonredirects'];
714 // Counts for the number of transclusion links (to/from)
715 $result['transclusion']['to'] = (int)$dbr->selectField(
719 'tl_namespace' => $title->getNamespace(),
720 'tl_title' => $title->getDBkey()
725 $result['transclusion']['from'] = (int)$dbr->selectField(
728 array( 'tl_from' => $title->getArticleID() ),
732 wfProfileOut( __METHOD__
);
737 * Returns the name that goes in the "<h1>" page title.
741 protected function getPageTitle() {
742 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
746 * Get a list of contributors of $article
747 * @return string: html
749 protected function getContributors() {
750 global $wgHiddenPrefs;
752 $contributors = $this->page
->getContributors();
753 $real_names = array();
754 $user_names = array();
757 # Sift for real versus user names
758 foreach ( $contributors as $user ) {
759 $page = $user->isAnon()
760 ? SpecialPage
::getTitleFor( 'Contributions', $user->getName() )
761 : $user->getUserPage();
763 if ( $user->getID() == 0 ) {
764 $anon_ips[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
765 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
766 $real_names[] = Linker
::link( $page, htmlspecialchars( $user->getRealName() ) );
768 $user_names[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
772 $lang = $this->getLanguage();
774 $real = $lang->listToText( $real_names );
776 # "ThisSite user(s) A, B and C"
777 if ( count( $user_names ) ) {
778 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
779 count( $user_names ) )->escaped();
784 if ( count( $anon_ips ) ) {
785 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
786 count( $anon_ips ) )->escaped();
791 # This is the big list, all mooshed together. We sift for blank strings
793 foreach ( array( $real, $user, $anon ) as $s ) {
795 array_push( $fulllist, $s );
799 $count = count( $fulllist );
800 # "Based on work by ..."
802 ?
$this->msg( 'othercontribs' )->rawParams(
803 $lang->listToText( $fulllist ) )->params( $count )->escaped()
808 * Returns the description that goes below the "<h1>" tag.
812 protected function getDescription() {