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 $memcKey = wfMemcKey( 'infoaction', $title->getPrefixedText(), $revision->getId() );
70 $wgMemc->delete( $memcKey );
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, $wgUnwatchedPageThreshold, $wgPageInfoTransclusionLimit;
190 $user = $this->getUser();
191 $lang = $this->getLanguage();
192 $title = $this->getTitle();
193 $id = $title->getArticleID();
195 $memcKey = wfMemcKey( 'infoaction', sha1( $title->getPrefixedText() ), $this->page
->getLatest() );
196 $pageCounts = $wgMemc->get( $memcKey );
197 if ( $pageCounts === false ) {
198 // Get page information that would be too "expensive" to retrieve by normal means
199 $pageCounts = self
::pageCounts( $title );
201 $wgMemc->set( $memcKey, $pageCounts );
204 // Get page properties
205 $dbr = wfGetDB( DB_SLAVE
);
206 $result = $dbr->select(
208 array( 'pp_propname', 'pp_value' ),
209 array( 'pp_page' => $id ),
213 $pageProperties = array();
214 foreach ( $result as $row ) {
215 $pageProperties[$row->pp_propname
] = $row->pp_value
;
220 $pageInfo['header-basic'] = array();
223 $displayTitle = $title->getPrefixedText();
224 if ( !empty( $pageProperties['displaytitle'] ) ) {
225 $displayTitle = $pageProperties['displaytitle'];
228 $pageInfo['header-basic'][] = array(
229 $this->msg( 'pageinfo-display-title' ), $displayTitle
232 // Is it a redirect? If so, where to?
233 if ( $title->isRedirect() ) {
234 $pageInfo['header-basic'][] = array(
235 $this->msg( 'pageinfo-redirectsto' ),
236 Linker
::link( $this->page
->getRedirectTarget() ) .
237 $this->msg( 'word-separator' )->text() .
238 $this->msg( 'parentheses', Linker
::link(
239 $this->page
->getRedirectTarget(),
240 $this->msg( 'pageinfo-redirectsto-info' )->escaped(),
242 array( 'action' => 'info' )
248 $sortKey = $title->getCategorySortkey();
249 if ( !empty( $pageProperties['defaultsort'] ) ) {
250 $sortKey = $pageProperties['defaultsort'];
253 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-default-sort' ), $sortKey );
255 // Page length (in bytes)
256 $pageInfo['header-basic'][] = array(
257 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
260 // Page ID (number not localised, as it's a database ID)
261 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-article-id' ), $id );
263 // Language in which the page content is (supposed to be) written
264 $pageLang = $title->getPageLanguage()->getCode();
265 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-language' ),
266 Language
::fetchLanguageName( $pageLang, $lang->getCode() )
267 . ' ' . $this->msg( 'parentheses', $pageLang ) );
269 // Search engine status
270 $pOutput = new ParserOutput();
271 if ( isset( $pageProperties['noindex'] ) ) {
272 $pOutput->setIndexPolicy( 'noindex' );
275 // Use robot policy logic
276 $policy = $this->page
->getRobotPolicy( 'view', $pOutput );
277 $pageInfo['header-basic'][] = array(
278 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
281 if ( isset( $pageCounts['views'] ) ) {
283 $pageInfo['header-basic'][] = array(
284 $this->msg( 'pageinfo-views' ), $lang->formatNum( $pageCounts['views'] )
289 $user->isAllowed( 'unwatchedpages' ) ||
290 ( $wgUnwatchedPageThreshold !== false &&
291 $pageCounts['watchers'] >= $wgUnwatchedPageThreshold )
293 // Number of page watchers
294 $pageInfo['header-basic'][] = array(
295 $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
297 } elseif ( $wgUnwatchedPageThreshold !== false ) {
298 $pageInfo['header-basic'][] = array(
299 $this->msg( 'pageinfo-watchers' ),
300 $this->msg( 'pageinfo-few-watchers' )->numParams( $wgUnwatchedPageThreshold )
304 // Redirects to this page
305 $whatLinksHere = SpecialPage
::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
306 $pageInfo['header-basic'][] = array(
309 $this->msg( 'pageinfo-redirects-name' )->escaped(),
311 array( 'hidelinks' => 1, 'hidetrans' => 1 )
313 $this->msg( 'pageinfo-redirects-value' )
314 ->numParams( count( $title->getRedirectsHere() ) )
317 // Is it counted as a content page?
318 if ( $this->page
->isCountable() ) {
319 $pageInfo['header-basic'][] = array(
320 $this->msg( 'pageinfo-contentpage' ),
321 $this->msg( 'pageinfo-contentpage-yes' )
325 // Subpages of this page, if subpages are enabled for the current NS
326 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
327 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
328 $pageInfo['header-basic'][] = array(
329 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
330 $this->msg( 'pageinfo-subpages-value' )
332 $pageCounts['subpages']['total'],
333 $pageCounts['subpages']['redirects'],
334 $pageCounts['subpages']['nonredirects'] )
338 if ( $title->inNamespace( NS_CATEGORY
) ) {
339 $category = Category
::newFromTitle( $title );
340 $pageInfo['category-info'] = array(
342 $this->msg( 'pageinfo-category-pages' ),
343 $lang->formatNum( $category->getPageCount() )
346 $this->msg( 'pageinfo-category-subcats' ),
347 $lang->formatNum( $category->getSubcatCount() )
350 $this->msg( 'pageinfo-category-files' ),
351 $lang->formatNum( $category->getFileCount() )
357 $pageInfo['header-restrictions'] = array();
359 // Is this page effected by the cascading protection of something which includes it?
360 if ( $title->isCascadeProtected() ) {
362 $sources = $title->getCascadeProtectionSources(); // Array deferencing is in PHP 5.4 :(
364 foreach ( $sources[0] as $sourceTitle ) {
365 $cascadingFrom .= Html
::rawElement( 'li', array(), Linker
::linkKnown( $sourceTitle ) );
368 $cascadingFrom = Html
::rawElement( 'ul', array(), $cascadingFrom );
369 $pageInfo['header-restrictions'][] = array(
370 $this->msg( 'pageinfo-protect-cascading-from' ),
375 // Is out protection set to cascade to other pages?
376 if ( $title->areRestrictionsCascading() ) {
377 $pageInfo['header-restrictions'][] = array(
378 $this->msg( 'pageinfo-protect-cascading' ),
379 $this->msg( 'pageinfo-protect-cascading-yes' )
384 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
385 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
387 if ( $protectionLevel == '' ) {
389 $message = $this->msg( 'protect-default' )->escaped();
391 // Administrators only
392 $message = $this->msg( "protect-level-$protectionLevel" );
393 if ( $message->isDisabled() ) {
394 // Require "$1" permission
395 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
397 $message = $message->escaped();
401 $pageInfo['header-restrictions'][] = array(
402 $this->msg( "restriction-$restrictionType" ), $message
406 if ( !$this->page
->exists() ) {
411 $pageInfo['header-edits'] = array();
413 $firstRev = $this->page
->getOldestRevision();
414 $lastRev = $this->page
->getRevision();
415 $batch = new LinkBatch
;
418 $firstRevUser = $firstRev->getUserText( Revision
::FOR_THIS_USER
);
419 if ( $firstRevUser !== '' ) {
420 $batch->add( NS_USER
, $firstRevUser );
421 $batch->add( NS_USER_TALK
, $firstRevUser );
426 $lastRevUser = $lastRev->getUserText( Revision
::FOR_THIS_USER
);
427 if ( $lastRevUser !== '' ) {
428 $batch->add( NS_USER
, $lastRevUser );
429 $batch->add( NS_USER_TALK
, $lastRevUser );
437 $pageInfo['header-edits'][] = array(
438 $this->msg( 'pageinfo-firstuser' ),
439 Linker
::revUserTools( $firstRev )
442 // Date of page creation
443 $pageInfo['header-edits'][] = array(
444 $this->msg( 'pageinfo-firsttime' ),
447 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
449 array( 'oldid' => $firstRev->getId() )
456 $pageInfo['header-edits'][] = array(
457 $this->msg( 'pageinfo-lastuser' ),
458 Linker
::revUserTools( $lastRev )
461 // Date of latest edit
462 $pageInfo['header-edits'][] = array(
463 $this->msg( 'pageinfo-lasttime' ),
466 $lang->userTimeAndDate( $this->page
->getTimestamp(), $user ),
468 array( 'oldid' => $this->page
->getLatest() )
473 // Total number of edits
474 $pageInfo['header-edits'][] = array(
475 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
478 // Total number of distinct authors
479 $pageInfo['header-edits'][] = array(
480 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
483 // Recent number of edits (within past 30 days)
484 $pageInfo['header-edits'][] = array(
485 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
486 $lang->formatNum( $pageCounts['recent_edits'] )
489 // Recent number of distinct authors
490 $pageInfo['header-edits'][] = array(
491 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
494 // Array of MagicWord objects
495 $magicWords = MagicWord
::getDoubleUnderscoreArray();
497 // Array of magic word IDs
498 $wordIDs = $magicWords->names
;
500 // Array of IDs => localized magic words
501 $localizedWords = $wgContLang->getMagicWords();
503 $listItems = array();
504 foreach ( $pageProperties as $property => $value ) {
505 if ( in_array( $property, $wordIDs ) ) {
506 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
510 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
511 $hiddenCategories = $this->page
->getHiddenCategories();
514 count( $listItems ) > 0 ||
515 count( $hiddenCategories ) > 0 ||
516 $pageCounts['transclusion']['from'] > 0 ||
517 $pageCounts['transclusion']['to'] > 0
519 $options = array( 'LIMIT' => $wgPageInfoTransclusionLimit );
520 $transcludedTemplates = $title->getTemplateLinksFrom( $options );
521 $transcludedTargets = $title->getTemplateLinksTo( $options );
524 $pageInfo['header-properties'] = array();
527 if ( count( $listItems ) > 0 ) {
528 $pageInfo['header-properties'][] = array(
529 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
535 if ( count( $hiddenCategories ) > 0 ) {
536 $pageInfo['header-properties'][] = array(
537 $this->msg( 'pageinfo-hidden-categories' )
538 ->numParams( count( $hiddenCategories ) ),
539 Linker
::formatHiddenCategories( $hiddenCategories )
543 // Transcluded templates
544 if ( $pageCounts['transclusion']['from'] > 0 ) {
545 if ( $pageCounts['transclusion']['from'] > count( $transcludedTemplates ) ) {
546 $more = $this->msg( 'morenotlisted' )->escaped();
551 $pageInfo['header-properties'][] = array(
552 $this->msg( 'pageinfo-templates' )
553 ->numParams( $pageCounts['transclusion']['from'] ),
554 Linker
::formatTemplates(
555 $transcludedTemplates,
562 if ( $pageCounts['transclusion']['to'] > 0 ) {
563 if ( $pageCounts['transclusion']['to'] > count( $transcludedTargets ) ) {
564 $more = Linker
::link(
566 $this->msg( 'moredotdotdot' )->escaped(),
568 array( 'hidelinks' => 1, 'hideredirs' => 1 )
574 $pageInfo['header-properties'][] = array(
575 $this->msg( 'pageinfo-transclusions' )
576 ->numParams( $pageCounts['transclusion']['to'] ),
577 Linker
::formatTemplates(
590 * Returns page counts that would be too "expensive" to retrieve by normal means.
592 * @param Title $title Title to get counts for
595 protected static function pageCounts( Title
$title ) {
596 global $wgRCMaxAge, $wgDisableCounters;
598 wfProfileIn( __METHOD__
);
599 $id = $title->getArticleID();
601 $dbr = wfGetDB( DB_SLAVE
);
604 if ( !$wgDisableCounters ) {
606 $views = (int) $dbr->selectField(
609 array( 'page_id' => $id ),
612 $result['views'] = $views;
615 // Number of page watchers
616 $watchers = (int) $dbr->selectField(
620 'wl_namespace' => $title->getNamespace(),
621 'wl_title' => $title->getDBkey(),
625 $result['watchers'] = $watchers;
627 // Total number of edits
628 $edits = (int) $dbr->selectField(
631 array( 'rev_page' => $id ),
634 $result['edits'] = $edits;
636 // Total number of distinct authors
637 $authors = (int) $dbr->selectField(
639 'COUNT(DISTINCT rev_user_text)',
640 array( 'rev_page' => $id ),
643 $result['authors'] = $authors;
645 // "Recent" threshold defined by $wgRCMaxAge
646 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
648 // Recent number of edits
649 $edits = (int) $dbr->selectField(
654 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
658 $result['recent_edits'] = $edits;
660 // Recent number of distinct authors
661 $authors = (int) $dbr->selectField(
663 'COUNT(DISTINCT rev_user_text)',
666 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
670 $result['recent_authors'] = $authors;
672 // Subpages (if enabled)
673 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
674 $conds = array( 'page_namespace' => $title->getNamespace() );
675 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
677 // Subpages of this page (redirects)
678 $conds['page_is_redirect'] = 1;
679 $result['subpages']['redirects'] = (int) $dbr->selectField(
685 // Subpages of this page (non-redirects)
686 $conds['page_is_redirect'] = 0;
687 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
694 // Subpages of this page (total)
695 $result['subpages']['total'] = $result['subpages']['redirects']
696 +
$result['subpages']['nonredirects'];
699 // Counts for the number of transclusion links (to/from)
700 $result['transclusion']['to'] = (int) $dbr->selectField(
704 'tl_namespace' => $title->getNamespace(),
705 'tl_title' => $title->getDBkey()
710 $result['transclusion']['from'] = (int) $dbr->selectField(
713 array( 'tl_from' => $title->getArticleID() ),
717 wfProfileOut( __METHOD__
);
722 * Returns the name that goes in the "<h1>" page title.
726 protected function getPageTitle() {
727 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
731 * Get a list of contributors of $article
732 * @return string: html
734 protected function getContributors() {
735 global $wgHiddenPrefs;
737 $contributors = $this->page
->getContributors();
738 $real_names = array();
739 $user_names = array();
742 # Sift for real versus user names
743 foreach ( $contributors as $user ) {
744 $page = $user->isAnon()
745 ? SpecialPage
::getTitleFor( 'Contributions', $user->getName() )
746 : $user->getUserPage();
748 if ( $user->getID() == 0 ) {
749 $anon_ips[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
750 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
751 $real_names[] = Linker
::link( $page, htmlspecialchars( $user->getRealName() ) );
753 $user_names[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
757 $lang = $this->getLanguage();
759 $real = $lang->listToText( $real_names );
761 # "ThisSite user(s) A, B and C"
762 if ( count( $user_names ) ) {
763 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
764 count( $user_names ) )->escaped();
769 if ( count( $anon_ips ) ) {
770 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
771 count( $anon_ips ) )->escaped();
776 # This is the big list, all mooshed together. We sift for blank strings
778 foreach ( array( $real, $user, $anon ) as $s ) {
780 array_push( $fulllist, $s );
784 $count = count( $fulllist );
785 # "Based on work by ..."
787 ?
$this->msg( 'othercontribs' )->rawParams(
788 $lang->listToText( $fulllist ) )->params( $count )->escaped()
793 * Returns the description that goes below the "<h1>" tag.
797 protected function getDescription() {