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
25 class InfoAction
extends FormlessAction
{
27 * Returns the name of the action this object responds to.
29 * @return string lowercase
31 public function getName() {
36 * Whether this action can still be executed by a blocked user.
40 public function requiresUnblock() {
45 * Whether this action requires the wiki not to be locked.
49 public function requiresWrite() {
54 * Shows page information on GET request.
56 * @return string Page information that will be added to the output
58 public function onView() {
62 $oldid = $this->page
->getOldID();
64 $revision = $this->page
->getRevisionFetched();
66 // Revision is missing
67 if ( $revision === null ) {
68 return $this->msg( 'missing-revision', $oldid )->parse();
71 // Revision is not current
72 if ( !$revision->isCurrent() ) {
73 return $this->msg( 'pageinfo-not-current' )->plain();
78 if ( !$this->msg( 'pageinfo-header' )->isDisabled() ) {
79 $content .= $this->msg( 'pageinfo-header' )->parse();
82 // Hide "This page is a member of # hidden categories" explanation
83 $content .= Html
::element( 'style', array(),
84 '.mw-hiddenCategoriesExplanation { display: none; }' );
86 // Hide "Templates used on this page" explanation
87 $content .= Html
::element( 'style', array(),
88 '.mw-templatesUsedExplanation { display: none; }' );
90 // Get page information
91 $pageInfo = $this->pageInfo();
93 // Allow extensions to add additional information
94 wfRunHooks( 'InfoAction', array( $this->getContext(), &$pageInfo ) );
96 // Render page information
97 foreach ( $pageInfo as $header => $infoTable ) {
98 $content .= $this->makeHeader( $this->msg( "pageinfo-${header}" )->escaped() );
100 foreach ( $infoTable as $infoRow ) {
101 $name = ( $infoRow[0] instanceof Message
) ?
$infoRow[0]->escaped() : $infoRow[0];
102 $value = ( $infoRow[1] instanceof Message
) ?
$infoRow[1]->escaped() : $infoRow[1];
103 $table = $this->addRow( $table, $name, $value );
105 $content = $this->addTable( $content, $table );
109 if ( !$this->msg( 'pageinfo-footer' )->isDisabled() ) {
110 $content .= $this->msg( 'pageinfo-footer' )->parse();
114 /*if ( $this->page->exists() ) {
115 $content .= Html::rawElement( 'div', array( 'id' => 'mw-credits' ), $this->getContributors() );
122 * Creates a header that can be added to the output.
124 * @param $header The header text.
125 * @return string The HTML.
127 protected function makeHeader( $header ) {
129 $spanAttribs = array( 'class' => 'mw-headline', 'id' => $wgParser->guessSectionNameFromWikiText( $header ) );
130 return Html
::rawElement( 'h2', array(), Html
::element( 'span', $spanAttribs, $header ) );
134 * Adds a row to a table that will be added to the content.
136 * @param $table string The table that will be added to the content
137 * @param $name string The name of the row
138 * @param $value string The value of the row
139 * @return string The table with the row added
141 protected function addRow( $table, $name, $value ) {
142 return $table . Html
::rawElement( 'tr', array(),
143 Html
::rawElement( 'td', array( 'style' => 'vertical-align: top;' ), $name ) .
144 Html
::rawElement( 'td', array(), $value )
149 * Adds a table to the content that will be added to the output.
151 * @param $content string The content that will be added to the output
152 * @param $table string The table
153 * @return string The content with the table added
155 protected function addTable( $content, $table ) {
156 return $content . Html
::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
161 * Returns page information in an easily-manipulated format. Array keys are used so extensions
162 * may add additional information in arbitrary positions. Array values are arrays with one
163 * element to be rendered as a header, arrays with two elements to be rendered as a table row.
167 protected function pageInfo() {
168 global $wgContLang, $wgRCMaxAge, $wgMemc, $wgUnwatchedPageThreshold, $wgPageInfoTransclusionLimit;
170 $user = $this->getUser();
171 $lang = $this->getLanguage();
172 $title = $this->getTitle();
173 $id = $title->getArticleID();
175 $memcKey = wfMemcKey( 'infoaction', sha1( $title->getPrefixedText() ), $this->page
->getLatest() );
176 $pageCounts = $wgMemc->get( $memcKey );
177 if ( $pageCounts === false ) {
178 // Get page information that would be too "expensive" to retrieve by normal means
179 $pageCounts = self
::pageCounts( $title );
181 $wgMemc->set( $memcKey, $pageCounts );
184 // Get page properties
185 $dbr = wfGetDB( DB_SLAVE
);
186 $result = $dbr->select(
188 array( 'pp_propname', 'pp_value' ),
189 array( 'pp_page' => $id ),
193 $pageProperties = array();
194 foreach ( $result as $row ) {
195 $pageProperties[$row->pp_propname
] = $row->pp_value
;
200 $pageInfo['header-basic'] = array();
203 $displayTitle = $title->getPrefixedText();
204 if ( !empty( $pageProperties['displaytitle'] ) ) {
205 $displayTitle = $pageProperties['displaytitle'];
208 $pageInfo['header-basic'][] = array(
209 $this->msg( 'pageinfo-display-title' ), $displayTitle
212 // Is it a redirect? If so, where to?
213 if ( $title->isRedirect() ) {
214 $pageInfo['header-basic'][] = array(
215 $this->msg( 'pageinfo-redirectsto' ),
216 Linker
::link( $this->page
->getRedirectTarget() ) .
217 $this->msg( 'word-separator' )->text() .
218 $this->msg( 'parentheses', Linker
::link(
219 $this->page
->getRedirectTarget(),
220 $this->msg( 'pageinfo-redirectsto-info' )->escaped(),
222 array( 'action' => 'info' )
228 $sortKey = $title->getCategorySortKey();
229 if ( !empty( $pageProperties['defaultsort'] ) ) {
230 $sortKey = $pageProperties['defaultsort'];
233 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-default-sort' ), $sortKey );
235 // Page length (in bytes)
236 $pageInfo['header-basic'][] = array(
237 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
240 // Page ID (number not localised, as it's a database ID)
241 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-article-id' ), $id );
243 // Language in which the page content is (supposed to be) written
244 $pageLang = $title->getPageLanguage()->getCode();
245 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-language' ),
246 Language
::fetchLanguageName( $pageLang, $lang->getCode() )
247 . ' ' . $this->msg( 'parentheses', $pageLang ) );
249 // Search engine status
250 $pOutput = new ParserOutput();
251 if ( isset( $pageProperties['noindex'] ) ) {
252 $pOutput->setIndexPolicy( 'noindex' );
255 // Use robot policy logic
256 $policy = $this->page
->getRobotPolicy( 'view', $pOutput );
257 $pageInfo['header-basic'][] = array(
258 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
261 if ( isset( $pageCounts['views'] ) ) {
263 $pageInfo['header-basic'][] = array(
264 $this->msg( 'pageinfo-views' ), $lang->formatNum( $pageCounts['views'] )
269 $user->isAllowed( 'unwatchedpages' ) ||
270 ( $wgUnwatchedPageThreshold !== false &&
271 $pageCounts['watchers'] >= $wgUnwatchedPageThreshold )
273 // Number of page watchers
274 $pageInfo['header-basic'][] = array(
275 $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
279 // Redirects to this page
280 $whatLinksHere = SpecialPage
::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
281 $pageInfo['header-basic'][] = array(
284 $this->msg( 'pageinfo-redirects-name' )->escaped(),
286 array( 'hidelinks' => 1, 'hidetrans' => 1 )
288 $this->msg( 'pageinfo-redirects-value' )
289 ->numParams( count( $title->getRedirectsHere() ) )
292 // Is it counted as a content page?
293 if ( $this->page
->isCountable() ) {
294 $pageInfo['header-basic'][] = array(
295 $this->msg( 'pageinfo-contentpage' ),
296 $this->msg( 'pageinfo-contentpage-yes' )
300 // Subpages of this page, if subpages are enabled for the current NS
301 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
302 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
303 $pageInfo['header-basic'][] = array(
304 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
305 $this->msg( 'pageinfo-subpages-value' )
307 $pageCounts['subpages']['total'],
308 $pageCounts['subpages']['redirects'],
309 $pageCounts['subpages']['nonredirects'] )
313 if ( $title->inNamespace( NS_CATEGORY
) ) {
314 $category = Category
::newFromTitle( $title );
315 $pageInfo['category-info'] = array(
317 $this->msg( 'pageinfo-category-pages' ),
318 $lang->formatNum( $category->getPageCount() )
321 $this->msg( 'pageinfo-category-subcats' ),
322 $lang->formatNum( $category->getSubcatCount() )
325 $this->msg( 'pageinfo-category-files' ),
326 $lang->formatNum( $category->getFileCount() )
332 $pageInfo['header-restrictions'] = array();
334 // Is this page effected by the cascading protection of something which includes it?
335 if ( $title->isCascadeProtected() ) {
337 $sources = $title->getCascadeProtectionSources(); // Array deferencing is in PHP 5.4 :(
339 foreach ( $sources[0] as $sourceTitle ) {
340 $cascadingFrom .= Html
::rawElement( 'li', array(), Linker
::linkKnown( $sourceTitle ) );
343 $cascadingFrom = Html
::rawElement( 'ul', array(), $cascadingFrom );
344 $pageInfo['header-restrictions'][] = array(
345 $this->msg( 'pageinfo-protect-cascading-from' ),
350 // Is out protection set to cascade to other pages?
351 if ( $title->areRestrictionsCascading() ) {
352 $pageInfo['header-restrictions'][] = array(
353 $this->msg( 'pageinfo-protect-cascading' ),
354 $this->msg( 'pageinfo-protect-cascading-yes' )
359 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
360 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
362 if ( $protectionLevel == '' ) {
364 $message = $this->msg( 'protect-default' )->escaped();
366 // Administrators only
367 $message = $this->msg( "protect-level-$protectionLevel" );
368 if ( $message->isDisabled() ) {
369 // Require "$1" permission
370 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
372 $message = $message->escaped();
376 $pageInfo['header-restrictions'][] = array(
377 $this->msg( "restriction-$restrictionType" ), $message
381 if ( !$this->page
->exists() ) {
386 $pageInfo['header-edits'] = array();
388 $firstRev = $this->page
->getOldestRevision();
391 $pageInfo['header-edits'][] = array(
392 $this->msg( 'pageinfo-firstuser' ),
393 Linker
::revUserTools( $firstRev )
396 // Date of page creation
397 $pageInfo['header-edits'][] = array(
398 $this->msg( 'pageinfo-firsttime' ),
401 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
403 array( 'oldid' => $firstRev->getId() )
408 $pageInfo['header-edits'][] = array(
409 $this->msg( 'pageinfo-lastuser' ),
410 Linker
::revUserTools( $this->page
->getRevision() )
413 // Date of latest edit
414 $pageInfo['header-edits'][] = array(
415 $this->msg( 'pageinfo-lasttime' ),
418 $lang->userTimeAndDate( $this->page
->getTimestamp(), $user ),
420 array( 'oldid' => $this->page
->getLatest() )
424 // Total number of edits
425 $pageInfo['header-edits'][] = array(
426 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
429 // Total number of distinct authors
430 $pageInfo['header-edits'][] = array(
431 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
434 // Recent number of edits (within past 30 days)
435 $pageInfo['header-edits'][] = array(
436 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
437 $lang->formatNum( $pageCounts['recent_edits'] )
440 // Recent number of distinct authors
441 $pageInfo['header-edits'][] = array(
442 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
445 // Array of MagicWord objects
446 $magicWords = MagicWord
::getDoubleUnderscoreArray();
448 // Array of magic word IDs
449 $wordIDs = $magicWords->names
;
451 // Array of IDs => localized magic words
452 $localizedWords = $wgContLang->getMagicWords();
454 $listItems = array();
455 foreach ( $pageProperties as $property => $value ) {
456 if ( in_array( $property, $wordIDs ) ) {
457 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
461 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
462 $hiddenCategories = $this->page
->getHiddenCategories();
465 count( $listItems ) > 0 ||
466 count( $hiddenCategories ) > 0 ||
467 $pageCounts['transclusion']['from'] > 0 ||
468 $pageCounts['transclusion']['to'] > 0
470 $options = array( 'LIMIT' => $wgPageInfoTransclusionLimit );
471 $transcludedTemplates = $title->getTemplateLinksFrom( $options );
472 $transcludedTargets = $title->getTemplateLinksTo( $options );
475 $pageInfo['header-properties'] = array();
478 if ( count( $listItems ) > 0 ) {
479 $pageInfo['header-properties'][] = array(
480 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
486 if ( count( $hiddenCategories ) > 0 ) {
487 $pageInfo['header-properties'][] = array(
488 $this->msg( 'pageinfo-hidden-categories' )
489 ->numParams( count( $hiddenCategories ) ),
490 Linker
::formatHiddenCategories( $hiddenCategories )
494 // Transcluded templates
495 if ( $pageCounts['transclusion']['from'] > 0 ) {
496 if ( $pageCounts['transclusion']['from'] > count( $transcludedTemplates ) ) {
497 $more = $this->msg( 'morenotlisted' )->escaped();
502 $pageInfo['header-properties'][] = array(
503 $this->msg( 'pageinfo-templates' )
504 ->numParams( $pageCounts['transclusion']['from'] ),
505 Linker
::formatTemplates(
506 $transcludedTemplates,
513 if ( $pageCounts['transclusion']['to'] > 0 ) {
514 if ( $pageCounts['transclusion']['to'] > count( $transcludedTargets ) ) {
515 $more = Linker
::link(
517 $this->msg( 'moredotdotdot' )->escaped(),
519 array( 'hidelinks' => 1, 'hideredirs' => 1 )
525 $pageInfo['header-properties'][] = array(
526 $this->msg( 'pageinfo-transclusions' )
527 ->numParams( $pageCounts['transclusion']['to'] ),
528 Linker
::formatTemplates(
541 * Returns page counts that would be too "expensive" to retrieve by normal means.
543 * @param Title $title Title to get counts for
546 protected static function pageCounts( Title
$title ) {
547 global $wgRCMaxAge, $wgDisableCounters;
549 wfProfileIn( __METHOD__
);
550 $id = $title->getArticleID();
552 $dbr = wfGetDB( DB_SLAVE
);
555 if ( !$wgDisableCounters ) {
557 $views = (int) $dbr->selectField(
560 array( 'page_id' => $id ),
563 $result['views'] = $views;
566 // Number of page watchers
567 $watchers = (int) $dbr->selectField(
571 'wl_namespace' => $title->getNamespace(),
572 'wl_title' => $title->getDBkey(),
576 $result['watchers'] = $watchers;
578 // Total number of edits
579 $edits = (int) $dbr->selectField(
582 array( 'rev_page' => $id ),
585 $result['edits'] = $edits;
587 // Total number of distinct authors
588 $authors = (int) $dbr->selectField(
590 'COUNT(DISTINCT rev_user_text)',
591 array( 'rev_page' => $id ),
594 $result['authors'] = $authors;
596 // "Recent" threshold defined by $wgRCMaxAge
597 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
599 // Recent number of edits
600 $edits = (int) $dbr->selectField(
605 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
609 $result['recent_edits'] = $edits;
611 // Recent number of distinct authors
612 $authors = (int) $dbr->selectField(
614 'COUNT(DISTINCT rev_user_text)',
617 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
621 $result['recent_authors'] = $authors;
623 // Subpages (if enabled)
624 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
625 $conds = array( 'page_namespace' => $title->getNamespace() );
626 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
628 // Subpages of this page (redirects)
629 $conds['page_is_redirect'] = 1;
630 $result['subpages']['redirects'] = (int) $dbr->selectField(
636 // Subpages of this page (non-redirects)
637 $conds['page_is_redirect'] = 0;
638 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
645 // Subpages of this page (total)
646 $result['subpages']['total'] = $result['subpages']['redirects']
647 +
$result['subpages']['nonredirects'];
650 // Counts for the number of transclusion links (to/from)
651 $result['transclusion']['to'] = (int) $dbr->selectField(
655 'tl_namespace' => $title->getNamespace(),
656 'tl_title' => $title->getDBkey()
661 $result['transclusion']['from'] = (int) $dbr->selectField(
664 array( 'tl_from' => $title->getArticleID() ),
668 wfProfileOut( __METHOD__
);
673 * Returns the name that goes in the "<h1>" page title.
677 protected function getPageTitle() {
678 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
682 * Get a list of contributors of $article
683 * @return string: html
685 protected function getContributors() {
686 global $wgHiddenPrefs;
688 $contributors = $this->page
->getContributors();
689 $real_names = array();
690 $user_names = array();
693 # Sift for real versus user names
694 foreach ( $contributors as $user ) {
695 $page = $user->isAnon()
696 ? SpecialPage
::getTitleFor( 'Contributions', $user->getName() )
697 : $user->getUserPage();
699 if ( $user->getID() == 0 ) {
700 $anon_ips[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
701 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
702 $real_names[] = Linker
::link( $page, htmlspecialchars( $user->getRealName() ) );
704 $user_names[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
708 $lang = $this->getLanguage();
710 $real = $lang->listToText( $real_names );
712 # "ThisSite user(s) A, B and C"
713 if ( count( $user_names ) ) {
714 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
715 count( $user_names ) )->escaped();
720 if ( count( $anon_ips ) ) {
721 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
722 count( $anon_ips ) )->escaped();
727 # This is the big list, all mooshed together. We sift for blank strings
729 foreach ( array( $real, $user, $anon ) as $s ) {
731 array_push( $fulllist, $s );
735 $count = count( $fulllist );
736 # "Based on work by ..."
738 ?
$this->msg( 'othercontribs' )->rawParams(
739 $lang->listToText( $fulllist ) )->params( $count )->escaped()
744 * Returns the description that goes below the "<h1>" tag.
748 protected function getDescription() {