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
{
32 * Returns the name of the action this object responds to.
34 * @return string lowercase
36 public function getName() {
41 * Whether this action can still be executed by a blocked user.
45 public function requiresUnblock() {
50 * Whether this action requires the wiki not to be locked.
54 public function requiresWrite() {
59 * Clear the info cache for a given Title.
62 * @param Title $title Title to clear cache for
64 public static function invalidateCache( Title
$title ) {
67 $revision = WikiPage
::factory( $title )->getRevision();
68 if ( $revision !== null ) {
69 $key = wfMemcKey( 'infoaction', sha1( $title->getPrefixedText() ), $revision->getId() );
70 $wgMemc->delete( $key );
75 * Shows page information on GET request.
77 * @return string Page information that will be added to the output
79 public function onView() {
83 $oldid = $this->page
->getOldID();
85 $revision = $this->page
->getRevisionFetched();
87 // Revision is missing
88 if ( $revision === null ) {
89 return $this->msg( 'missing-revision', $oldid )->parse();
92 // Revision is not current
93 if ( !$revision->isCurrent() ) {
94 return $this->msg( 'pageinfo-not-current' )->plain();
99 if ( !$this->msg( 'pageinfo-header' )->isDisabled() ) {
100 $content .= $this->msg( 'pageinfo-header' )->parse();
103 // Hide "This page is a member of # hidden categories" explanation
104 $content .= Html
::element( 'style', array(),
105 '.mw-hiddenCategoriesExplanation { display: none; }' ) . "\n";
107 // Hide "Templates used on this page" explanation
108 $content .= Html
::element( 'style', array(),
109 '.mw-templatesUsedExplanation { display: none; }' ) . "\n";
111 // Get page information
112 $pageInfo = $this->pageInfo();
114 // Allow extensions to add additional information
115 wfRunHooks( 'InfoAction', array( $this->getContext(), &$pageInfo ) );
117 // Render page information
118 foreach ( $pageInfo as $header => $infoTable ) {
119 $content .= $this->makeHeader( $this->msg( "pageinfo-${header}" )->escaped() ) . "\n";
121 foreach ( $infoTable as $infoRow ) {
122 $name = ( $infoRow[0] instanceof Message
) ?
$infoRow[0]->escaped() : $infoRow[0];
123 $value = ( $infoRow[1] instanceof Message
) ?
$infoRow[1]->escaped() : $infoRow[1];
124 $table = $this->addRow( $table, $name, $value ) . "\n";
126 $content = $this->addTable( $content, $table ) . "\n";
130 if ( !$this->msg( 'pageinfo-footer' )->isDisabled() ) {
131 $content .= $this->msg( 'pageinfo-footer' )->parse();
135 /*if ( $this->page->exists() ) {
136 $content .= Html::rawElement( 'div', array( 'id' => 'mw-credits' ), $this->getContributors() );
143 * Creates a header that can be added to the output.
145 * @param string $header The header text.
146 * @return string The HTML.
148 protected function makeHeader( $header ) {
149 $spanAttribs = array( 'class' => 'mw-headline', 'id' => Sanitizer
::escapeId( $header ) );
150 return Html
::rawElement( 'h2', array(), Html
::element( 'span', $spanAttribs, $header ) );
154 * Adds a row to a table that will be added to the content.
156 * @param string $table The table that will be added to the content
157 * @param string $name The name of the row
158 * @param string $value The value of the row
159 * @return string The table with the row added
161 protected function addRow( $table, $name, $value ) {
162 return $table . Html
::rawElement( 'tr', array(),
163 Html
::rawElement( 'td', array( 'style' => 'vertical-align: top;' ), $name ) .
164 Html
::rawElement( 'td', array(), $value )
169 * Adds a table to the content that will be added to the output.
171 * @param string $content The content that will be added to the output
172 * @param string $table The table
173 * @return string The content with the table added
175 protected function addTable( $content, $table ) {
176 return $content . Html
::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
181 * Returns page information in an easily-manipulated format. Array keys are used so extensions
182 * may add additional information in arbitrary positions. Array values are arrays with one
183 * element to be rendered as a header, arrays with two elements to be rendered as a table row.
187 protected function pageInfo() {
188 global $wgContLang, $wgRCMaxAge, $wgMemc,
189 $wgUnwatchedPageThreshold, $wgPageInfoTransclusionLimit;
191 $user = $this->getUser();
192 $lang = $this->getLanguage();
193 $title = $this->getTitle();
194 $id = $title->getArticleID();
196 $memcKey = wfMemcKey( 'infoaction',
197 sha1( $title->getPrefixedText() ), $this->page
->getLatest() );
198 $pageCounts = $wgMemc->get( $memcKey );
199 if ( $pageCounts === false ) {
200 // Get page information that would be too "expensive" to retrieve by normal means
201 $pageCounts = self
::pageCounts( $title );
203 $wgMemc->set( $memcKey, $pageCounts );
206 // Get page properties
207 $dbr = wfGetDB( DB_SLAVE
);
208 $result = $dbr->select(
210 array( 'pp_propname', 'pp_value' ),
211 array( 'pp_page' => $id ),
215 $pageProperties = array();
216 foreach ( $result as $row ) {
217 $pageProperties[$row->pp_propname
] = $row->pp_value
;
222 $pageInfo['header-basic'] = array();
225 $displayTitle = $title->getPrefixedText();
226 if ( !empty( $pageProperties['displaytitle'] ) ) {
227 $displayTitle = $pageProperties['displaytitle'];
230 $pageInfo['header-basic'][] = array(
231 $this->msg( 'pageinfo-display-title' ), $displayTitle
234 // Is it a redirect? If so, where to?
235 if ( $title->isRedirect() ) {
236 $pageInfo['header-basic'][] = array(
237 $this->msg( 'pageinfo-redirectsto' ),
238 Linker
::link( $this->page
->getRedirectTarget() ) .
239 $this->msg( 'word-separator' )->text() .
240 $this->msg( 'parentheses', Linker
::link(
241 $this->page
->getRedirectTarget(),
242 $this->msg( 'pageinfo-redirectsto-info' )->escaped(),
244 array( 'action' => 'info' )
250 $sortKey = $title->getCategorySortkey();
251 if ( !empty( $pageProperties['defaultsort'] ) ) {
252 $sortKey = $pageProperties['defaultsort'];
255 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-default-sort' ), $sortKey );
257 // Page length (in bytes)
258 $pageInfo['header-basic'][] = array(
259 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
262 // Page ID (number not localised, as it's a database ID)
263 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-article-id' ), $id );
265 // Language in which the page content is (supposed to be) written
266 $pageLang = $title->getPageLanguage()->getCode();
267 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-language' ),
268 Language
::fetchLanguageName( $pageLang, $lang->getCode() )
269 . ' ' . $this->msg( 'parentheses', $pageLang ) );
271 // Search engine status
272 $pOutput = new ParserOutput();
273 if ( isset( $pageProperties['noindex'] ) ) {
274 $pOutput->setIndexPolicy( 'noindex' );
277 // Use robot policy logic
278 $policy = $this->page
->getRobotPolicy( 'view', $pOutput );
279 $pageInfo['header-basic'][] = array(
280 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
283 if ( isset( $pageCounts['views'] ) ) {
285 $pageInfo['header-basic'][] = array(
286 $this->msg( 'pageinfo-views' ), $lang->formatNum( $pageCounts['views'] )
291 $user->isAllowed( 'unwatchedpages' ) ||
292 ( $wgUnwatchedPageThreshold !== false &&
293 $pageCounts['watchers'] >= $wgUnwatchedPageThreshold )
295 // Number of page watchers
296 $pageInfo['header-basic'][] = array(
297 $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
299 } elseif ( $wgUnwatchedPageThreshold !== false ) {
300 $pageInfo['header-basic'][] = array(
301 $this->msg( 'pageinfo-watchers' ),
302 $this->msg( 'pageinfo-few-watchers' )->numParams( $wgUnwatchedPageThreshold )
306 // Redirects to this page
307 $whatLinksHere = SpecialPage
::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
308 $pageInfo['header-basic'][] = array(
311 $this->msg( 'pageinfo-redirects-name' )->escaped(),
313 array( 'hidelinks' => 1, 'hidetrans' => 1 )
315 $this->msg( 'pageinfo-redirects-value' )
316 ->numParams( count( $title->getRedirectsHere() ) )
319 // Is it counted as a content page?
320 if ( $this->page
->isCountable() ) {
321 $pageInfo['header-basic'][] = array(
322 $this->msg( 'pageinfo-contentpage' ),
323 $this->msg( 'pageinfo-contentpage-yes' )
327 // Subpages of this page, if subpages are enabled for the current NS
328 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
329 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
330 $pageInfo['header-basic'][] = array(
331 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
332 $this->msg( 'pageinfo-subpages-value' )
334 $pageCounts['subpages']['total'],
335 $pageCounts['subpages']['redirects'],
336 $pageCounts['subpages']['nonredirects'] )
340 if ( $title->inNamespace( NS_CATEGORY
) ) {
341 $category = Category
::newFromTitle( $title );
342 $pageInfo['category-info'] = array(
344 $this->msg( 'pageinfo-category-pages' ),
345 $lang->formatNum( $category->getPageCount() )
348 $this->msg( 'pageinfo-category-subcats' ),
349 $lang->formatNum( $category->getSubcatCount() )
352 $this->msg( 'pageinfo-category-files' ),
353 $lang->formatNum( $category->getFileCount() )
359 $pageInfo['header-restrictions'] = array();
361 // Is this page effected by the cascading protection of something which includes it?
362 if ( $title->isCascadeProtected() ) {
364 $sources = $title->getCascadeProtectionSources(); // Array deferencing is in PHP 5.4 :(
366 foreach ( $sources[0] as $sourceTitle ) {
367 $cascadingFrom .= Html
::rawElement( 'li', array(), Linker
::linkKnown( $sourceTitle ) );
370 $cascadingFrom = Html
::rawElement( 'ul', array(), $cascadingFrom );
371 $pageInfo['header-restrictions'][] = array(
372 $this->msg( 'pageinfo-protect-cascading-from' ),
377 // Is out protection set to cascade to other pages?
378 if ( $title->areRestrictionsCascading() ) {
379 $pageInfo['header-restrictions'][] = array(
380 $this->msg( 'pageinfo-protect-cascading' ),
381 $this->msg( 'pageinfo-protect-cascading-yes' )
386 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
387 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
389 if ( $protectionLevel == '' ) {
391 $message = $this->msg( 'protect-default' )->escaped();
393 // Administrators only
394 $message = $this->msg( "protect-level-$protectionLevel" );
395 if ( $message->isDisabled() ) {
396 // Require "$1" permission
397 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
399 $message = $message->escaped();
403 $pageInfo['header-restrictions'][] = array(
404 $this->msg( "restriction-$restrictionType" ), $message
408 if ( !$this->page
->exists() ) {
413 $pageInfo['header-edits'] = array();
415 $firstRev = $this->page
->getOldestRevision();
416 $lastRev = $this->page
->getRevision();
417 $batch = new LinkBatch
;
420 $firstRevUser = $firstRev->getUserText( Revision
::FOR_THIS_USER
);
421 if ( $firstRevUser !== '' ) {
422 $batch->add( NS_USER
, $firstRevUser );
423 $batch->add( NS_USER_TALK
, $firstRevUser );
428 $lastRevUser = $lastRev->getUserText( Revision
::FOR_THIS_USER
);
429 if ( $lastRevUser !== '' ) {
430 $batch->add( NS_USER
, $lastRevUser );
431 $batch->add( NS_USER_TALK
, $lastRevUser );
439 $pageInfo['header-edits'][] = array(
440 $this->msg( 'pageinfo-firstuser' ),
441 Linker
::revUserTools( $firstRev )
444 // Date of page creation
445 $pageInfo['header-edits'][] = array(
446 $this->msg( 'pageinfo-firsttime' ),
449 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
451 array( 'oldid' => $firstRev->getId() )
458 $pageInfo['header-edits'][] = array(
459 $this->msg( 'pageinfo-lastuser' ),
460 Linker
::revUserTools( $lastRev )
463 // Date of latest edit
464 $pageInfo['header-edits'][] = array(
465 $this->msg( 'pageinfo-lasttime' ),
468 $lang->userTimeAndDate( $this->page
->getTimestamp(), $user ),
470 array( 'oldid' => $this->page
->getLatest() )
475 // Total number of edits
476 $pageInfo['header-edits'][] = array(
477 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
480 // Total number of distinct authors
481 $pageInfo['header-edits'][] = array(
482 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
485 // Recent number of edits (within past 30 days)
486 $pageInfo['header-edits'][] = array(
487 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
488 $lang->formatNum( $pageCounts['recent_edits'] )
491 // Recent number of distinct authors
492 $pageInfo['header-edits'][] = array(
493 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
496 // Array of MagicWord objects
497 $magicWords = MagicWord
::getDoubleUnderscoreArray();
499 // Array of magic word IDs
500 $wordIDs = $magicWords->names
;
502 // Array of IDs => localized magic words
503 $localizedWords = $wgContLang->getMagicWords();
505 $listItems = array();
506 foreach ( $pageProperties as $property => $value ) {
507 if ( in_array( $property, $wordIDs ) ) {
508 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
512 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
513 $hiddenCategories = $this->page
->getHiddenCategories();
516 count( $listItems ) > 0 ||
517 count( $hiddenCategories ) > 0 ||
518 $pageCounts['transclusion']['from'] > 0 ||
519 $pageCounts['transclusion']['to'] > 0
521 $options = array( 'LIMIT' => $wgPageInfoTransclusionLimit );
522 $transcludedTemplates = $title->getTemplateLinksFrom( $options );
523 $transcludedTargets = $title->getTemplateLinksTo( $options );
526 $pageInfo['header-properties'] = array();
529 if ( count( $listItems ) > 0 ) {
530 $pageInfo['header-properties'][] = array(
531 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
537 if ( count( $hiddenCategories ) > 0 ) {
538 $pageInfo['header-properties'][] = array(
539 $this->msg( 'pageinfo-hidden-categories' )
540 ->numParams( count( $hiddenCategories ) ),
541 Linker
::formatHiddenCategories( $hiddenCategories )
545 // Transcluded templates
546 if ( $pageCounts['transclusion']['from'] > 0 ) {
547 if ( $pageCounts['transclusion']['from'] > count( $transcludedTemplates ) ) {
548 $more = $this->msg( 'morenotlisted' )->escaped();
553 $pageInfo['header-properties'][] = array(
554 $this->msg( 'pageinfo-templates' )
555 ->numParams( $pageCounts['transclusion']['from'] ),
556 Linker
::formatTemplates(
557 $transcludedTemplates,
564 if ( $pageCounts['transclusion']['to'] > 0 ) {
565 if ( $pageCounts['transclusion']['to'] > count( $transcludedTargets ) ) {
566 $more = Linker
::link(
568 $this->msg( 'moredotdotdot' )->escaped(),
570 array( 'hidelinks' => 1, 'hideredirs' => 1 )
576 $pageInfo['header-properties'][] = array(
577 $this->msg( 'pageinfo-transclusions' )
578 ->numParams( $pageCounts['transclusion']['to'] ),
579 Linker
::formatTemplates(
592 * Returns page counts that would be too "expensive" to retrieve by normal means.
594 * @param Title $title Title to get counts for
597 protected static function pageCounts( Title
$title ) {
598 global $wgRCMaxAge, $wgDisableCounters;
600 wfProfileIn( __METHOD__
);
601 $id = $title->getArticleID();
603 $dbr = wfGetDB( DB_SLAVE
);
606 if ( !$wgDisableCounters ) {
608 $views = (int) $dbr->selectField(
611 array( 'page_id' => $id ),
614 $result['views'] = $views;
617 // Number of page watchers
618 $watchers = (int) $dbr->selectField(
622 'wl_namespace' => $title->getNamespace(),
623 'wl_title' => $title->getDBkey(),
627 $result['watchers'] = $watchers;
629 // Total number of edits
630 $edits = (int) $dbr->selectField(
633 array( 'rev_page' => $id ),
636 $result['edits'] = $edits;
638 // Total number of distinct authors
639 $authors = (int) $dbr->selectField(
641 'COUNT(DISTINCT rev_user_text)',
642 array( 'rev_page' => $id ),
645 $result['authors'] = $authors;
647 // "Recent" threshold defined by $wgRCMaxAge
648 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
650 // Recent number of edits
651 $edits = (int) $dbr->selectField(
656 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
660 $result['recent_edits'] = $edits;
662 // Recent number of distinct authors
663 $authors = (int) $dbr->selectField(
665 'COUNT(DISTINCT rev_user_text)',
668 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
672 $result['recent_authors'] = $authors;
674 // Subpages (if enabled)
675 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
676 $conds = array( 'page_namespace' => $title->getNamespace() );
677 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
679 // Subpages of this page (redirects)
680 $conds['page_is_redirect'] = 1;
681 $result['subpages']['redirects'] = (int) $dbr->selectField(
687 // Subpages of this page (non-redirects)
688 $conds['page_is_redirect'] = 0;
689 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
696 // Subpages of this page (total)
697 $result['subpages']['total'] = $result['subpages']['redirects']
698 +
$result['subpages']['nonredirects'];
701 // Counts for the number of transclusion links (to/from)
702 $result['transclusion']['to'] = (int) $dbr->selectField(
706 'tl_namespace' => $title->getNamespace(),
707 'tl_title' => $title->getDBkey()
712 $result['transclusion']['from'] = (int) $dbr->selectField(
715 array( 'tl_from' => $title->getArticleID() ),
719 wfProfileOut( __METHOD__
);
724 * Returns the name that goes in the "<h1>" page title.
728 protected function getPageTitle() {
729 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
733 * Get a list of contributors of $article
734 * @return string: html
736 protected function getContributors() {
737 global $wgHiddenPrefs;
739 $contributors = $this->page
->getContributors();
740 $real_names = array();
741 $user_names = array();
744 # Sift for real versus user names
745 foreach ( $contributors as $user ) {
746 $page = $user->isAnon()
747 ? SpecialPage
::getTitleFor( 'Contributions', $user->getName() )
748 : $user->getUserPage();
750 if ( $user->getID() == 0 ) {
751 $anon_ips[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
752 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
753 $real_names[] = Linker
::link( $page, htmlspecialchars( $user->getRealName() ) );
755 $user_names[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
759 $lang = $this->getLanguage();
761 $real = $lang->listToText( $real_names );
763 # "ThisSite user(s) A, B and C"
764 if ( count( $user_names ) ) {
765 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
766 count( $user_names ) )->escaped();
771 if ( count( $anon_ips ) ) {
772 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
773 count( $anon_ips ) )->escaped();
778 # This is the big list, all mooshed together. We sift for blank strings
780 foreach ( array( $real, $user, $anon ) as $s ) {
782 array_push( $fulllist, $s );
786 $count = count( $fulllist );
787 # "Based on work by ..."
789 ?
$this->msg( 'othercontribs' )->rawParams(
790 $lang->listToText( $fulllist ) )->params( $count )->escaped()
795 * Returns the description that goes below the "<h1>" tag.
799 protected function getDescription() {