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 // Content model of the page
281 $pageInfo['header-basic'][] = array(
282 $this->msg( 'pageinfo-content-model' ),
283 ContentHandler
::getLocalizedName( $title->getContentModel() )
286 // Search engine status
287 $pOutput = new ParserOutput();
288 if ( isset( $pageProperties['noindex'] ) ) {
289 $pOutput->setIndexPolicy( 'noindex' );
292 // Use robot policy logic
293 $policy = $this->page
->getRobotPolicy( 'view', $pOutput );
294 $pageInfo['header-basic'][] = array(
295 // Messages: pageinfo-robot-index, pageinfo-robot-noindex
296 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
299 if ( isset( $pageCounts['views'] ) ) {
301 $pageInfo['header-basic'][] = array(
302 $this->msg( 'pageinfo-views' ), $lang->formatNum( $pageCounts['views'] )
307 $user->isAllowed( 'unwatchedpages' ) ||
308 ( $wgUnwatchedPageThreshold !== false &&
309 $pageCounts['watchers'] >= $wgUnwatchedPageThreshold )
311 // Number of page watchers
312 $pageInfo['header-basic'][] = array(
313 $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
315 } elseif ( $wgUnwatchedPageThreshold !== false ) {
316 $pageInfo['header-basic'][] = array(
317 $this->msg( 'pageinfo-watchers' ),
318 $this->msg( 'pageinfo-few-watchers' )->numParams( $wgUnwatchedPageThreshold )
322 // Redirects to this page
323 $whatLinksHere = SpecialPage
::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
324 $pageInfo['header-basic'][] = array(
327 $this->msg( 'pageinfo-redirects-name' )->escaped(),
329 array( 'hidelinks' => 1, 'hidetrans' => 1 )
331 $this->msg( 'pageinfo-redirects-value' )
332 ->numParams( count( $title->getRedirectsHere() ) )
335 // Is it counted as a content page?
336 if ( $this->page
->isCountable() ) {
337 $pageInfo['header-basic'][] = array(
338 $this->msg( 'pageinfo-contentpage' ),
339 $this->msg( 'pageinfo-contentpage-yes' )
343 // Subpages of this page, if subpages are enabled for the current NS
344 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
345 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
346 $pageInfo['header-basic'][] = array(
347 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
348 $this->msg( 'pageinfo-subpages-value' )
350 $pageCounts['subpages']['total'],
351 $pageCounts['subpages']['redirects'],
352 $pageCounts['subpages']['nonredirects'] )
356 if ( $title->inNamespace( NS_CATEGORY
) ) {
357 $category = Category
::newFromTitle( $title );
358 $pageInfo['category-info'] = array(
360 $this->msg( 'pageinfo-category-pages' ),
361 $lang->formatNum( $category->getPageCount() )
364 $this->msg( 'pageinfo-category-subcats' ),
365 $lang->formatNum( $category->getSubcatCount() )
368 $this->msg( 'pageinfo-category-files' ),
369 $lang->formatNum( $category->getFileCount() )
375 $pageInfo['header-restrictions'] = array();
377 // Is this page effected by the cascading protection of something which includes it?
378 if ( $title->isCascadeProtected() ) {
380 $sources = $title->getCascadeProtectionSources(); // Array deferencing is in PHP 5.4 :(
382 foreach ( $sources[0] as $sourceTitle ) {
383 $cascadingFrom .= Html
::rawElement( 'li', array(), Linker
::linkKnown( $sourceTitle ) );
386 $cascadingFrom = Html
::rawElement( 'ul', array(), $cascadingFrom );
387 $pageInfo['header-restrictions'][] = array(
388 $this->msg( 'pageinfo-protect-cascading-from' ),
393 // Is out protection set to cascade to other pages?
394 if ( $title->areRestrictionsCascading() ) {
395 $pageInfo['header-restrictions'][] = array(
396 $this->msg( 'pageinfo-protect-cascading' ),
397 $this->msg( 'pageinfo-protect-cascading-yes' )
402 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
403 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
405 if ( $protectionLevel == '' ) {
407 $message = $this->msg( 'protect-default' )->escaped();
409 // Administrators only
410 // Messages: protect-level-autoconfirmed, protect-level-sysop
411 $message = $this->msg( "protect-level-$protectionLevel" );
412 if ( $message->isDisabled() ) {
413 // Require "$1" permission
414 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
416 $message = $message->escaped();
420 // Messages: restriction-edit, restriction-move, restriction-create,
421 // restriction-upload
422 $pageInfo['header-restrictions'][] = array(
423 $this->msg( "restriction-$restrictionType" ), $message
427 if ( !$this->page
->exists() ) {
432 $pageInfo['header-edits'] = array();
434 $firstRev = $this->page
->getOldestRevision();
435 $lastRev = $this->page
->getRevision();
436 $batch = new LinkBatch
;
439 $firstRevUser = $firstRev->getUserText( Revision
::FOR_THIS_USER
);
440 if ( $firstRevUser !== '' ) {
441 $batch->add( NS_USER
, $firstRevUser );
442 $batch->add( NS_USER_TALK
, $firstRevUser );
447 $lastRevUser = $lastRev->getUserText( Revision
::FOR_THIS_USER
);
448 if ( $lastRevUser !== '' ) {
449 $batch->add( NS_USER
, $lastRevUser );
450 $batch->add( NS_USER_TALK
, $lastRevUser );
458 $pageInfo['header-edits'][] = array(
459 $this->msg( 'pageinfo-firstuser' ),
460 Linker
::revUserTools( $firstRev )
463 // Date of page creation
464 $pageInfo['header-edits'][] = array(
465 $this->msg( 'pageinfo-firsttime' ),
468 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
470 array( 'oldid' => $firstRev->getId() )
477 $pageInfo['header-edits'][] = array(
478 $this->msg( 'pageinfo-lastuser' ),
479 Linker
::revUserTools( $lastRev )
482 // Date of latest edit
483 $pageInfo['header-edits'][] = array(
484 $this->msg( 'pageinfo-lasttime' ),
487 $lang->userTimeAndDate( $this->page
->getTimestamp(), $user ),
489 array( 'oldid' => $this->page
->getLatest() )
494 // Total number of edits
495 $pageInfo['header-edits'][] = array(
496 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
499 // Total number of distinct authors
500 $pageInfo['header-edits'][] = array(
501 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
504 // Recent number of edits (within past 30 days)
505 $pageInfo['header-edits'][] = array(
506 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
507 $lang->formatNum( $pageCounts['recent_edits'] )
510 // Recent number of distinct authors
511 $pageInfo['header-edits'][] = array(
512 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
515 // Array of MagicWord objects
516 $magicWords = MagicWord
::getDoubleUnderscoreArray();
518 // Array of magic word IDs
519 $wordIDs = $magicWords->names
;
521 // Array of IDs => localized magic words
522 $localizedWords = $wgContLang->getMagicWords();
524 $listItems = array();
525 foreach ( $pageProperties as $property => $value ) {
526 if ( in_array( $property, $wordIDs ) ) {
527 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
531 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
532 $hiddenCategories = $this->page
->getHiddenCategories();
535 count( $listItems ) > 0 ||
536 count( $hiddenCategories ) > 0 ||
537 $pageCounts['transclusion']['from'] > 0 ||
538 $pageCounts['transclusion']['to'] > 0
540 $options = array( 'LIMIT' => $wgPageInfoTransclusionLimit );
541 $transcludedTemplates = $title->getTemplateLinksFrom( $options );
542 $transcludedTargets = $title->getTemplateLinksTo( $options );
545 $pageInfo['header-properties'] = array();
548 if ( count( $listItems ) > 0 ) {
549 $pageInfo['header-properties'][] = array(
550 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
556 if ( count( $hiddenCategories ) > 0 ) {
557 $pageInfo['header-properties'][] = array(
558 $this->msg( 'pageinfo-hidden-categories' )
559 ->numParams( count( $hiddenCategories ) ),
560 Linker
::formatHiddenCategories( $hiddenCategories )
564 // Transcluded templates
565 if ( $pageCounts['transclusion']['from'] > 0 ) {
566 if ( $pageCounts['transclusion']['from'] > count( $transcludedTemplates ) ) {
567 $more = $this->msg( 'morenotlisted' )->escaped();
572 $pageInfo['header-properties'][] = array(
573 $this->msg( 'pageinfo-templates' )
574 ->numParams( $pageCounts['transclusion']['from'] ),
575 Linker
::formatTemplates(
576 $transcludedTemplates,
583 if ( $pageCounts['transclusion']['to'] > 0 ) {
584 if ( $pageCounts['transclusion']['to'] > count( $transcludedTargets ) ) {
585 $more = Linker
::link(
587 $this->msg( 'moredotdotdot' )->escaped(),
589 array( 'hidelinks' => 1, 'hideredirs' => 1 )
595 $pageInfo['header-properties'][] = array(
596 $this->msg( 'pageinfo-transclusions' )
597 ->numParams( $pageCounts['transclusion']['to'] ),
598 Linker
::formatTemplates(
611 * Returns page counts that would be too "expensive" to retrieve by normal means.
613 * @param Title $title Title to get counts for
616 protected static function pageCounts( Title
$title ) {
617 global $wgRCMaxAge, $wgDisableCounters;
619 wfProfileIn( __METHOD__
);
620 $id = $title->getArticleID();
622 $dbr = wfGetDB( DB_SLAVE
);
625 if ( !$wgDisableCounters ) {
627 $views = (int)$dbr->selectField(
630 array( 'page_id' => $id ),
633 $result['views'] = $views;
636 // Number of page watchers
637 $watchers = (int)$dbr->selectField(
641 'wl_namespace' => $title->getNamespace(),
642 'wl_title' => $title->getDBkey(),
646 $result['watchers'] = $watchers;
648 // Total number of edits
649 $edits = (int)$dbr->selectField(
652 array( 'rev_page' => $id ),
655 $result['edits'] = $edits;
657 // Total number of distinct authors
658 $authors = (int)$dbr->selectField(
660 'COUNT(DISTINCT rev_user_text)',
661 array( 'rev_page' => $id ),
664 $result['authors'] = $authors;
666 // "Recent" threshold defined by $wgRCMaxAge
667 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
669 // Recent number of edits
670 $edits = (int)$dbr->selectField(
675 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
679 $result['recent_edits'] = $edits;
681 // Recent number of distinct authors
682 $authors = (int)$dbr->selectField(
684 'COUNT(DISTINCT rev_user_text)',
687 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
691 $result['recent_authors'] = $authors;
693 // Subpages (if enabled)
694 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
695 $conds = array( 'page_namespace' => $title->getNamespace() );
696 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
698 // Subpages of this page (redirects)
699 $conds['page_is_redirect'] = 1;
700 $result['subpages']['redirects'] = (int)$dbr->selectField(
706 // Subpages of this page (non-redirects)
707 $conds['page_is_redirect'] = 0;
708 $result['subpages']['nonredirects'] = (int)$dbr->selectField(
715 // Subpages of this page (total)
716 $result['subpages']['total'] = $result['subpages']['redirects']
717 +
$result['subpages']['nonredirects'];
720 // Counts for the number of transclusion links (to/from)
721 $result['transclusion']['to'] = (int)$dbr->selectField(
725 'tl_namespace' => $title->getNamespace(),
726 'tl_title' => $title->getDBkey()
731 $result['transclusion']['from'] = (int)$dbr->selectField(
734 array( 'tl_from' => $title->getArticleID() ),
738 wfProfileOut( __METHOD__
);
743 * Returns the name that goes in the "<h1>" page title.
747 protected function getPageTitle() {
748 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
752 * Get a list of contributors of $article
753 * @return string: html
755 protected function getContributors() {
756 global $wgHiddenPrefs;
758 $contributors = $this->page
->getContributors();
759 $real_names = array();
760 $user_names = array();
763 # Sift for real versus user names
764 foreach ( $contributors as $user ) {
765 $page = $user->isAnon()
766 ? SpecialPage
::getTitleFor( 'Contributions', $user->getName() )
767 : $user->getUserPage();
769 if ( $user->getID() == 0 ) {
770 $anon_ips[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
771 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
772 $real_names[] = Linker
::link( $page, htmlspecialchars( $user->getRealName() ) );
774 $user_names[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
778 $lang = $this->getLanguage();
780 $real = $lang->listToText( $real_names );
782 # "ThisSite user(s) A, B and C"
783 if ( count( $user_names ) ) {
784 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
785 count( $user_names ) )->escaped();
790 if ( count( $anon_ips ) ) {
791 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
792 count( $anon_ips ) )->escaped();
797 # This is the big list, all mooshed together. We sift for blank strings
799 foreach ( array( $real, $user, $anon ) as $s ) {
801 array_push( $fulllist, $s );
805 $count = count( $fulllist );
806 # "Based on work by ..."
808 ?
$this->msg( 'othercontribs' )->rawParams(
809 $lang->listToText( $fulllist ) )->params( $count )->escaped()
814 * Returns the description that goes below the "<h1>" tag.
818 protected function getDescription() {