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 * Shows page information on GET request.
61 * @return string Page information that will be added to the output
63 public function onView() {
67 $oldid = $this->page
->getOldID();
69 $revision = $this->page
->getRevisionFetched();
71 // Revision is missing
72 if ( $revision === null ) {
73 return $this->msg( 'missing-revision', $oldid )->parse();
76 // Revision is not current
77 if ( !$revision->isCurrent() ) {
78 return $this->msg( 'pageinfo-not-current' )->plain();
83 if ( !$this->msg( 'pageinfo-header' )->isDisabled() ) {
84 $content .= $this->msg( 'pageinfo-header' )->parse();
87 // Hide "This page is a member of # hidden categories" explanation
88 $content .= Html
::element( 'style', array(),
89 '.mw-hiddenCategoriesExplanation { display: none; }' ) . "\n";
91 // Hide "Templates used on this page" explanation
92 $content .= Html
::element( 'style', array(),
93 '.mw-templatesUsedExplanation { display: none; }' ) . "\n";
95 // Get page information
96 $pageInfo = $this->pageInfo();
98 // Allow extensions to add additional information
99 wfRunHooks( 'InfoAction', array( $this->getContext(), &$pageInfo ) );
101 // Render page information
102 foreach ( $pageInfo as $header => $infoTable ) {
103 $content .= $this->makeHeader( $this->msg( "pageinfo-${header}" )->escaped() ) . "\n";
105 foreach ( $infoTable as $infoRow ) {
106 $name = ( $infoRow[0] instanceof Message
) ?
$infoRow[0]->escaped() : $infoRow[0];
107 $value = ( $infoRow[1] instanceof Message
) ?
$infoRow[1]->escaped() : $infoRow[1];
108 $table = $this->addRow( $table, $name, $value ) . "\n";
110 $content = $this->addTable( $content, $table ) . "\n";
114 if ( !$this->msg( 'pageinfo-footer' )->isDisabled() ) {
115 $content .= $this->msg( 'pageinfo-footer' )->parse();
119 /*if ( $this->page->exists() ) {
120 $content .= Html::rawElement( 'div', array( 'id' => 'mw-credits' ), $this->getContributors() );
127 * Creates a header that can be added to the output.
129 * @param string $header The header text.
130 * @return string The HTML.
132 protected function makeHeader( $header ) {
133 $spanAttribs = array( 'class' => 'mw-headline', 'id' => Sanitizer
::escapeId( $header ) );
134 return Html
::rawElement( 'h2', array(), Html
::element( 'span', $spanAttribs, $header ) );
138 * Adds a row to a table that will be added to the content.
140 * @param string $table The table that will be added to the content
141 * @param string $name The name of the row
142 * @param string $value The value of the row
143 * @return string The table with the row added
145 protected function addRow( $table, $name, $value ) {
146 return $table . Html
::rawElement( 'tr', array(),
147 Html
::rawElement( 'td', array( 'style' => 'vertical-align: top;' ), $name ) .
148 Html
::rawElement( 'td', array(), $value )
153 * Adds a table to the content that will be added to the output.
155 * @param string $content The content that will be added to the output
156 * @param string $table The table
157 * @return string The content with the table added
159 protected function addTable( $content, $table ) {
160 return $content . Html
::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
165 * Returns page information in an easily-manipulated format. Array keys are used so extensions
166 * may add additional information in arbitrary positions. Array values are arrays with one
167 * element to be rendered as a header, arrays with two elements to be rendered as a table row.
171 protected function pageInfo() {
172 global $wgContLang, $wgRCMaxAge, $wgMemc, $wgUnwatchedPageThreshold, $wgPageInfoTransclusionLimit;
174 $user = $this->getUser();
175 $lang = $this->getLanguage();
176 $title = $this->getTitle();
177 $id = $title->getArticleID();
179 $memcKey = wfMemcKey( 'infoaction', sha1( $title->getPrefixedText() ), $this->page
->getLatest() );
180 $pageCounts = $wgMemc->get( $memcKey );
181 if ( $pageCounts === false ) {
182 // Get page information that would be too "expensive" to retrieve by normal means
183 $pageCounts = self
::pageCounts( $title );
185 $wgMemc->set( $memcKey, $pageCounts );
188 // Get page properties
189 $dbr = wfGetDB( DB_SLAVE
);
190 $result = $dbr->select(
192 array( 'pp_propname', 'pp_value' ),
193 array( 'pp_page' => $id ),
197 $pageProperties = array();
198 foreach ( $result as $row ) {
199 $pageProperties[$row->pp_propname
] = $row->pp_value
;
204 $pageInfo['header-basic'] = array();
207 $displayTitle = $title->getPrefixedText();
208 if ( !empty( $pageProperties['displaytitle'] ) ) {
209 $displayTitle = $pageProperties['displaytitle'];
212 $pageInfo['header-basic'][] = array(
213 $this->msg( 'pageinfo-display-title' ), $displayTitle
216 // Is it a redirect? If so, where to?
217 if ( $title->isRedirect() ) {
218 $pageInfo['header-basic'][] = array(
219 $this->msg( 'pageinfo-redirectsto' ),
220 Linker
::link( $this->page
->getRedirectTarget() ) .
221 $this->msg( 'word-separator' )->text() .
222 $this->msg( 'parentheses', Linker
::link(
223 $this->page
->getRedirectTarget(),
224 $this->msg( 'pageinfo-redirectsto-info' )->escaped(),
226 array( 'action' => 'info' )
232 $sortKey = $title->getCategorySortkey();
233 if ( !empty( $pageProperties['defaultsort'] ) ) {
234 $sortKey = $pageProperties['defaultsort'];
237 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-default-sort' ), $sortKey );
239 // Page length (in bytes)
240 $pageInfo['header-basic'][] = array(
241 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
244 // Page ID (number not localised, as it's a database ID)
245 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-article-id' ), $id );
247 // Language in which the page content is (supposed to be) written
248 $pageLang = $title->getPageLanguage()->getCode();
249 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-language' ),
250 Language
::fetchLanguageName( $pageLang, $lang->getCode() )
251 . ' ' . $this->msg( 'parentheses', $pageLang ) );
253 // Search engine status
254 $pOutput = new ParserOutput();
255 if ( isset( $pageProperties['noindex'] ) ) {
256 $pOutput->setIndexPolicy( 'noindex' );
259 // Use robot policy logic
260 $policy = $this->page
->getRobotPolicy( 'view', $pOutput );
261 $pageInfo['header-basic'][] = array(
262 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
265 if ( isset( $pageCounts['views'] ) ) {
267 $pageInfo['header-basic'][] = array(
268 $this->msg( 'pageinfo-views' ), $lang->formatNum( $pageCounts['views'] )
273 $user->isAllowed( 'unwatchedpages' ) ||
274 ( $wgUnwatchedPageThreshold !== false &&
275 $pageCounts['watchers'] >= $wgUnwatchedPageThreshold )
277 // Number of page watchers
278 $pageInfo['header-basic'][] = array(
279 $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
281 } elseif ( $wgUnwatchedPageThreshold !== false ) {
282 $pageInfo['header-basic'][] = array(
283 $this->msg( 'pageinfo-watchers' ),
284 $this->msg( 'pageinfo-few-watchers' )->numParams( $wgUnwatchedPageThreshold )
288 // Redirects to this page
289 $whatLinksHere = SpecialPage
::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
290 $pageInfo['header-basic'][] = array(
293 $this->msg( 'pageinfo-redirects-name' )->escaped(),
295 array( 'hidelinks' => 1, 'hidetrans' => 1 )
297 $this->msg( 'pageinfo-redirects-value' )
298 ->numParams( count( $title->getRedirectsHere() ) )
301 // Is it counted as a content page?
302 if ( $this->page
->isCountable() ) {
303 $pageInfo['header-basic'][] = array(
304 $this->msg( 'pageinfo-contentpage' ),
305 $this->msg( 'pageinfo-contentpage-yes' )
309 // Subpages of this page, if subpages are enabled for the current NS
310 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
311 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
312 $pageInfo['header-basic'][] = array(
313 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
314 $this->msg( 'pageinfo-subpages-value' )
316 $pageCounts['subpages']['total'],
317 $pageCounts['subpages']['redirects'],
318 $pageCounts['subpages']['nonredirects'] )
322 if ( $title->inNamespace( NS_CATEGORY
) ) {
323 $category = Category
::newFromTitle( $title );
324 $pageInfo['category-info'] = array(
326 $this->msg( 'pageinfo-category-pages' ),
327 $lang->formatNum( $category->getPageCount() )
330 $this->msg( 'pageinfo-category-subcats' ),
331 $lang->formatNum( $category->getSubcatCount() )
334 $this->msg( 'pageinfo-category-files' ),
335 $lang->formatNum( $category->getFileCount() )
341 $pageInfo['header-restrictions'] = array();
343 // Is this page effected by the cascading protection of something which includes it?
344 if ( $title->isCascadeProtected() ) {
346 $sources = $title->getCascadeProtectionSources(); // Array deferencing is in PHP 5.4 :(
348 foreach ( $sources[0] as $sourceTitle ) {
349 $cascadingFrom .= Html
::rawElement( 'li', array(), Linker
::linkKnown( $sourceTitle ) );
352 $cascadingFrom = Html
::rawElement( 'ul', array(), $cascadingFrom );
353 $pageInfo['header-restrictions'][] = array(
354 $this->msg( 'pageinfo-protect-cascading-from' ),
359 // Is out protection set to cascade to other pages?
360 if ( $title->areRestrictionsCascading() ) {
361 $pageInfo['header-restrictions'][] = array(
362 $this->msg( 'pageinfo-protect-cascading' ),
363 $this->msg( 'pageinfo-protect-cascading-yes' )
368 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
369 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
371 if ( $protectionLevel == '' ) {
373 $message = $this->msg( 'protect-default' )->escaped();
375 // Administrators only
376 $message = $this->msg( "protect-level-$protectionLevel" );
377 if ( $message->isDisabled() ) {
378 // Require "$1" permission
379 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
381 $message = $message->escaped();
385 $pageInfo['header-restrictions'][] = array(
386 $this->msg( "restriction-$restrictionType" ), $message
390 if ( !$this->page
->exists() ) {
395 $pageInfo['header-edits'] = array();
397 $firstRev = $this->page
->getOldestRevision();
398 $lastRev = $this->page
->getRevision();
399 $batch = new LinkBatch
;
402 $firstRevUser = $firstRev->getUserText( Revision
::FOR_THIS_USER
);
403 if ( $firstRevUser !== '' ) {
404 $batch->add( NS_USER
, $firstRevUser );
405 $batch->add( NS_USER_TALK
, $firstRevUser );
410 $lastRevUser = $lastRev->getUserText( Revision
::FOR_THIS_USER
);
411 if ( $lastRevUser !== '' ) {
412 $batch->add( NS_USER
, $lastRevUser );
413 $batch->add( NS_USER_TALK
, $lastRevUser );
421 $pageInfo['header-edits'][] = array(
422 $this->msg( 'pageinfo-firstuser' ),
423 Linker
::revUserTools( $firstRev )
426 // Date of page creation
427 $pageInfo['header-edits'][] = array(
428 $this->msg( 'pageinfo-firsttime' ),
431 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
433 array( 'oldid' => $firstRev->getId() )
440 $pageInfo['header-edits'][] = array(
441 $this->msg( 'pageinfo-lastuser' ),
442 Linker
::revUserTools( $lastRev )
445 // Date of latest edit
446 $pageInfo['header-edits'][] = array(
447 $this->msg( 'pageinfo-lasttime' ),
450 $lang->userTimeAndDate( $this->page
->getTimestamp(), $user ),
452 array( 'oldid' => $this->page
->getLatest() )
457 // Total number of edits
458 $pageInfo['header-edits'][] = array(
459 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
462 // Total number of distinct authors
463 $pageInfo['header-edits'][] = array(
464 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
467 // Recent number of edits (within past 30 days)
468 $pageInfo['header-edits'][] = array(
469 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
470 $lang->formatNum( $pageCounts['recent_edits'] )
473 // Recent number of distinct authors
474 $pageInfo['header-edits'][] = array(
475 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
478 // Array of MagicWord objects
479 $magicWords = MagicWord
::getDoubleUnderscoreArray();
481 // Array of magic word IDs
482 $wordIDs = $magicWords->names
;
484 // Array of IDs => localized magic words
485 $localizedWords = $wgContLang->getMagicWords();
487 $listItems = array();
488 foreach ( $pageProperties as $property => $value ) {
489 if ( in_array( $property, $wordIDs ) ) {
490 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
494 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
495 $hiddenCategories = $this->page
->getHiddenCategories();
498 count( $listItems ) > 0 ||
499 count( $hiddenCategories ) > 0 ||
500 $pageCounts['transclusion']['from'] > 0 ||
501 $pageCounts['transclusion']['to'] > 0
503 $options = array( 'LIMIT' => $wgPageInfoTransclusionLimit );
504 $transcludedTemplates = $title->getTemplateLinksFrom( $options );
505 $transcludedTargets = $title->getTemplateLinksTo( $options );
508 $pageInfo['header-properties'] = array();
511 if ( count( $listItems ) > 0 ) {
512 $pageInfo['header-properties'][] = array(
513 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
519 if ( count( $hiddenCategories ) > 0 ) {
520 $pageInfo['header-properties'][] = array(
521 $this->msg( 'pageinfo-hidden-categories' )
522 ->numParams( count( $hiddenCategories ) ),
523 Linker
::formatHiddenCategories( $hiddenCategories )
527 // Transcluded templates
528 if ( $pageCounts['transclusion']['from'] > 0 ) {
529 if ( $pageCounts['transclusion']['from'] > count( $transcludedTemplates ) ) {
530 $more = $this->msg( 'morenotlisted' )->escaped();
535 $pageInfo['header-properties'][] = array(
536 $this->msg( 'pageinfo-templates' )
537 ->numParams( $pageCounts['transclusion']['from'] ),
538 Linker
::formatTemplates(
539 $transcludedTemplates,
546 if ( $pageCounts['transclusion']['to'] > 0 ) {
547 if ( $pageCounts['transclusion']['to'] > count( $transcludedTargets ) ) {
548 $more = Linker
::link(
550 $this->msg( 'moredotdotdot' )->escaped(),
552 array( 'hidelinks' => 1, 'hideredirs' => 1 )
558 $pageInfo['header-properties'][] = array(
559 $this->msg( 'pageinfo-transclusions' )
560 ->numParams( $pageCounts['transclusion']['to'] ),
561 Linker
::formatTemplates(
574 * Returns page counts that would be too "expensive" to retrieve by normal means.
576 * @param Title $title Title to get counts for
579 protected static function pageCounts( Title
$title ) {
580 global $wgRCMaxAge, $wgDisableCounters;
582 wfProfileIn( __METHOD__
);
583 $id = $title->getArticleID();
585 $dbr = wfGetDB( DB_SLAVE
);
588 if ( !$wgDisableCounters ) {
590 $views = (int) $dbr->selectField(
593 array( 'page_id' => $id ),
596 $result['views'] = $views;
599 // Number of page watchers
600 $watchers = (int) $dbr->selectField(
604 'wl_namespace' => $title->getNamespace(),
605 'wl_title' => $title->getDBkey(),
609 $result['watchers'] = $watchers;
611 // Total number of edits
612 $edits = (int) $dbr->selectField(
615 array( 'rev_page' => $id ),
618 $result['edits'] = $edits;
620 // Total number of distinct authors
621 $authors = (int) $dbr->selectField(
623 'COUNT(DISTINCT rev_user_text)',
624 array( 'rev_page' => $id ),
627 $result['authors'] = $authors;
629 // "Recent" threshold defined by $wgRCMaxAge
630 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
632 // Recent number of edits
633 $edits = (int) $dbr->selectField(
638 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
642 $result['recent_edits'] = $edits;
644 // Recent number of distinct authors
645 $authors = (int) $dbr->selectField(
647 'COUNT(DISTINCT rev_user_text)',
650 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
654 $result['recent_authors'] = $authors;
656 // Subpages (if enabled)
657 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
658 $conds = array( 'page_namespace' => $title->getNamespace() );
659 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
661 // Subpages of this page (redirects)
662 $conds['page_is_redirect'] = 1;
663 $result['subpages']['redirects'] = (int) $dbr->selectField(
669 // Subpages of this page (non-redirects)
670 $conds['page_is_redirect'] = 0;
671 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
678 // Subpages of this page (total)
679 $result['subpages']['total'] = $result['subpages']['redirects']
680 +
$result['subpages']['nonredirects'];
683 // Counts for the number of transclusion links (to/from)
684 $result['transclusion']['to'] = (int) $dbr->selectField(
688 'tl_namespace' => $title->getNamespace(),
689 'tl_title' => $title->getDBkey()
694 $result['transclusion']['from'] = (int) $dbr->selectField(
697 array( 'tl_from' => $title->getArticleID() ),
701 wfProfileOut( __METHOD__
);
706 * Returns the name that goes in the "<h1>" page title.
710 protected function getPageTitle() {
711 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
715 * Get a list of contributors of $article
716 * @return string: html
718 protected function getContributors() {
719 global $wgHiddenPrefs;
721 $contributors = $this->page
->getContributors();
722 $real_names = array();
723 $user_names = array();
726 # Sift for real versus user names
727 foreach ( $contributors as $user ) {
728 $page = $user->isAnon()
729 ? SpecialPage
::getTitleFor( 'Contributions', $user->getName() )
730 : $user->getUserPage();
732 if ( $user->getID() == 0 ) {
733 $anon_ips[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
734 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
735 $real_names[] = Linker
::link( $page, htmlspecialchars( $user->getRealName() ) );
737 $user_names[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
741 $lang = $this->getLanguage();
743 $real = $lang->listToText( $real_names );
745 # "ThisSite user(s) A, B and C"
746 if ( count( $user_names ) ) {
747 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
748 count( $user_names ) )->escaped();
753 if ( count( $anon_ips ) ) {
754 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
755 count( $anon_ips ) )->escaped();
760 # This is the big list, all mooshed together. We sift for blank strings
762 foreach ( array( $real, $user, $anon ) as $s ) {
764 array_push( $fulllist, $s );
768 $count = count( $fulllist );
769 # "Based on work by ..."
771 ?
$this->msg( 'othercontribs' )->rawParams(
772 $lang->listToText( $fulllist ) )->params( $count )->escaped()
777 * Returns the description that goes below the "<h1>" tag.
781 protected function getDescription() {