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 $id = ( $infoRow[0] instanceof Message
) ?
$infoRow[0]->getKey() : null;
125 $table = $this->addRow( $table, $name, $value, $id ) . "\n";
127 $content = $this->addTable( $content, $table ) . "\n";
131 if ( !$this->msg( 'pageinfo-footer' )->isDisabled() ) {
132 $content .= $this->msg( 'pageinfo-footer' )->parse();
136 /*if ( $this->page->exists() ) {
137 $content .= Html::rawElement( 'div', array( 'id' => 'mw-credits' ), $this->getContributors() );
144 * Creates a header that can be added to the output.
146 * @param string $header The header text.
147 * @return string The HTML.
149 protected function makeHeader( $header ) {
150 $spanAttribs = array( 'class' => 'mw-headline', 'id' => Sanitizer
::escapeId( $header ) );
151 return Html
::rawElement( 'h2', array(), Html
::element( 'span', $spanAttribs, $header ) );
155 * Adds a row to a table that will be added to the content.
157 * @param string $table The table that will be added to the content
158 * @param string $name The name of the row
159 * @param string $value The value of the row
160 * @param string $id The ID to use for the 'tr' element
161 * @return string The table with the row added
163 protected function addRow( $table, $name, $value, $id ) {
164 return $table . Html
::rawElement( 'tr', $id === null ?
array() : array( 'id' => 'mw-' . $id ),
165 Html
::rawElement( 'td', array( 'style' => 'vertical-align: top;' ), $name ) .
166 Html
::rawElement( 'td', array(), $value )
171 * Adds a table to the content that will be added to the output.
173 * @param string $content The content that will be added to the output
174 * @param string $table The table
175 * @return string The content with the table added
177 protected function addTable( $content, $table ) {
178 return $content . Html
::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
183 * Returns page information in an easily-manipulated format. Array keys are used so extensions
184 * may add additional information in arbitrary positions. Array values are arrays with one
185 * element to be rendered as a header, arrays with two elements to be rendered as a table row.
189 protected function pageInfo() {
190 global $wgContLang, $wgRCMaxAge, $wgMemc,
191 $wgUnwatchedPageThreshold, $wgPageInfoTransclusionLimit;
193 $user = $this->getUser();
194 $lang = $this->getLanguage();
195 $title = $this->getTitle();
196 $id = $title->getArticleID();
198 $memcKey = wfMemcKey( 'infoaction',
199 sha1( $title->getPrefixedText() ), $this->page
->getLatest() );
200 $pageCounts = $wgMemc->get( $memcKey );
201 if ( $pageCounts === false ) {
202 // Get page information that would be too "expensive" to retrieve by normal means
203 $pageCounts = self
::pageCounts( $title );
205 $wgMemc->set( $memcKey, $pageCounts );
208 // Get page properties
209 $dbr = wfGetDB( DB_SLAVE
);
210 $result = $dbr->select(
212 array( 'pp_propname', 'pp_value' ),
213 array( 'pp_page' => $id ),
217 $pageProperties = array();
218 foreach ( $result as $row ) {
219 $pageProperties[$row->pp_propname
] = $row->pp_value
;
224 $pageInfo['header-basic'] = array();
227 $displayTitle = $title->getPrefixedText();
228 if ( !empty( $pageProperties['displaytitle'] ) ) {
229 $displayTitle = $pageProperties['displaytitle'];
232 $pageInfo['header-basic'][] = array(
233 $this->msg( 'pageinfo-display-title' ), $displayTitle
236 // Is it a redirect? If so, where to?
237 if ( $title->isRedirect() ) {
238 $pageInfo['header-basic'][] = array(
239 $this->msg( 'pageinfo-redirectsto' ),
240 Linker
::link( $this->page
->getRedirectTarget() ) .
241 $this->msg( 'word-separator' )->text() .
242 $this->msg( 'parentheses', Linker
::link(
243 $this->page
->getRedirectTarget(),
244 $this->msg( 'pageinfo-redirectsto-info' )->escaped(),
246 array( 'action' => 'info' )
252 $sortKey = $title->getCategorySortkey();
253 if ( !empty( $pageProperties['defaultsort'] ) ) {
254 $sortKey = $pageProperties['defaultsort'];
257 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-default-sort' ), $sortKey );
259 // Page length (in bytes)
260 $pageInfo['header-basic'][] = array(
261 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
264 // Page ID (number not localised, as it's a database ID)
265 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-article-id' ), $id );
267 // Language in which the page content is (supposed to be) written
268 $pageLang = $title->getPageLanguage()->getCode();
269 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-language' ),
270 Language
::fetchLanguageName( $pageLang, $lang->getCode() )
271 . ' ' . $this->msg( 'parentheses', $pageLang ) );
273 // Search engine status
274 $pOutput = new ParserOutput();
275 if ( isset( $pageProperties['noindex'] ) ) {
276 $pOutput->setIndexPolicy( 'noindex' );
279 // Use robot policy logic
280 $policy = $this->page
->getRobotPolicy( 'view', $pOutput );
281 $pageInfo['header-basic'][] = array(
282 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
285 if ( isset( $pageCounts['views'] ) ) {
287 $pageInfo['header-basic'][] = array(
288 $this->msg( 'pageinfo-views' ), $lang->formatNum( $pageCounts['views'] )
293 $user->isAllowed( 'unwatchedpages' ) ||
294 ( $wgUnwatchedPageThreshold !== false &&
295 $pageCounts['watchers'] >= $wgUnwatchedPageThreshold )
297 // Number of page watchers
298 $pageInfo['header-basic'][] = array(
299 $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
301 } elseif ( $wgUnwatchedPageThreshold !== false ) {
302 $pageInfo['header-basic'][] = array(
303 $this->msg( 'pageinfo-watchers' ),
304 $this->msg( 'pageinfo-few-watchers' )->numParams( $wgUnwatchedPageThreshold )
308 // Redirects to this page
309 $whatLinksHere = SpecialPage
::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
310 $pageInfo['header-basic'][] = array(
313 $this->msg( 'pageinfo-redirects-name' )->escaped(),
315 array( 'hidelinks' => 1, 'hidetrans' => 1 )
317 $this->msg( 'pageinfo-redirects-value' )
318 ->numParams( count( $title->getRedirectsHere() ) )
321 // Is it counted as a content page?
322 if ( $this->page
->isCountable() ) {
323 $pageInfo['header-basic'][] = array(
324 $this->msg( 'pageinfo-contentpage' ),
325 $this->msg( 'pageinfo-contentpage-yes' )
329 // Subpages of this page, if subpages are enabled for the current NS
330 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
331 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
332 $pageInfo['header-basic'][] = array(
333 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
334 $this->msg( 'pageinfo-subpages-value' )
336 $pageCounts['subpages']['total'],
337 $pageCounts['subpages']['redirects'],
338 $pageCounts['subpages']['nonredirects'] )
342 if ( $title->inNamespace( NS_CATEGORY
) ) {
343 $category = Category
::newFromTitle( $title );
344 $pageInfo['category-info'] = array(
346 $this->msg( 'pageinfo-category-pages' ),
347 $lang->formatNum( $category->getPageCount() )
350 $this->msg( 'pageinfo-category-subcats' ),
351 $lang->formatNum( $category->getSubcatCount() )
354 $this->msg( 'pageinfo-category-files' ),
355 $lang->formatNum( $category->getFileCount() )
361 $pageInfo['header-restrictions'] = array();
363 // Is this page effected by the cascading protection of something which includes it?
364 if ( $title->isCascadeProtected() ) {
366 $sources = $title->getCascadeProtectionSources(); // Array deferencing is in PHP 5.4 :(
368 foreach ( $sources[0] as $sourceTitle ) {
369 $cascadingFrom .= Html
::rawElement( 'li', array(), Linker
::linkKnown( $sourceTitle ) );
372 $cascadingFrom = Html
::rawElement( 'ul', array(), $cascadingFrom );
373 $pageInfo['header-restrictions'][] = array(
374 $this->msg( 'pageinfo-protect-cascading-from' ),
379 // Is out protection set to cascade to other pages?
380 if ( $title->areRestrictionsCascading() ) {
381 $pageInfo['header-restrictions'][] = array(
382 $this->msg( 'pageinfo-protect-cascading' ),
383 $this->msg( 'pageinfo-protect-cascading-yes' )
388 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
389 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
391 if ( $protectionLevel == '' ) {
393 $message = $this->msg( 'protect-default' )->escaped();
395 // Administrators only
396 $message = $this->msg( "protect-level-$protectionLevel" );
397 if ( $message->isDisabled() ) {
398 // Require "$1" permission
399 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
401 $message = $message->escaped();
405 $pageInfo['header-restrictions'][] = array(
406 $this->msg( "restriction-$restrictionType" ), $message
410 if ( !$this->page
->exists() ) {
415 $pageInfo['header-edits'] = array();
417 $firstRev = $this->page
->getOldestRevision();
418 $lastRev = $this->page
->getRevision();
419 $batch = new LinkBatch
;
422 $firstRevUser = $firstRev->getUserText( Revision
::FOR_THIS_USER
);
423 if ( $firstRevUser !== '' ) {
424 $batch->add( NS_USER
, $firstRevUser );
425 $batch->add( NS_USER_TALK
, $firstRevUser );
430 $lastRevUser = $lastRev->getUserText( Revision
::FOR_THIS_USER
);
431 if ( $lastRevUser !== '' ) {
432 $batch->add( NS_USER
, $lastRevUser );
433 $batch->add( NS_USER_TALK
, $lastRevUser );
441 $pageInfo['header-edits'][] = array(
442 $this->msg( 'pageinfo-firstuser' ),
443 Linker
::revUserTools( $firstRev )
446 // Date of page creation
447 $pageInfo['header-edits'][] = array(
448 $this->msg( 'pageinfo-firsttime' ),
451 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
453 array( 'oldid' => $firstRev->getId() )
460 $pageInfo['header-edits'][] = array(
461 $this->msg( 'pageinfo-lastuser' ),
462 Linker
::revUserTools( $lastRev )
465 // Date of latest edit
466 $pageInfo['header-edits'][] = array(
467 $this->msg( 'pageinfo-lasttime' ),
470 $lang->userTimeAndDate( $this->page
->getTimestamp(), $user ),
472 array( 'oldid' => $this->page
->getLatest() )
477 // Total number of edits
478 $pageInfo['header-edits'][] = array(
479 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
482 // Total number of distinct authors
483 $pageInfo['header-edits'][] = array(
484 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
487 // Recent number of edits (within past 30 days)
488 $pageInfo['header-edits'][] = array(
489 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
490 $lang->formatNum( $pageCounts['recent_edits'] )
493 // Recent number of distinct authors
494 $pageInfo['header-edits'][] = array(
495 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
498 // Array of MagicWord objects
499 $magicWords = MagicWord
::getDoubleUnderscoreArray();
501 // Array of magic word IDs
502 $wordIDs = $magicWords->names
;
504 // Array of IDs => localized magic words
505 $localizedWords = $wgContLang->getMagicWords();
507 $listItems = array();
508 foreach ( $pageProperties as $property => $value ) {
509 if ( in_array( $property, $wordIDs ) ) {
510 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
514 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
515 $hiddenCategories = $this->page
->getHiddenCategories();
518 count( $listItems ) > 0 ||
519 count( $hiddenCategories ) > 0 ||
520 $pageCounts['transclusion']['from'] > 0 ||
521 $pageCounts['transclusion']['to'] > 0
523 $options = array( 'LIMIT' => $wgPageInfoTransclusionLimit );
524 $transcludedTemplates = $title->getTemplateLinksFrom( $options );
525 $transcludedTargets = $title->getTemplateLinksTo( $options );
528 $pageInfo['header-properties'] = array();
531 if ( count( $listItems ) > 0 ) {
532 $pageInfo['header-properties'][] = array(
533 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
539 if ( count( $hiddenCategories ) > 0 ) {
540 $pageInfo['header-properties'][] = array(
541 $this->msg( 'pageinfo-hidden-categories' )
542 ->numParams( count( $hiddenCategories ) ),
543 Linker
::formatHiddenCategories( $hiddenCategories )
547 // Transcluded templates
548 if ( $pageCounts['transclusion']['from'] > 0 ) {
549 if ( $pageCounts['transclusion']['from'] > count( $transcludedTemplates ) ) {
550 $more = $this->msg( 'morenotlisted' )->escaped();
555 $pageInfo['header-properties'][] = array(
556 $this->msg( 'pageinfo-templates' )
557 ->numParams( $pageCounts['transclusion']['from'] ),
558 Linker
::formatTemplates(
559 $transcludedTemplates,
566 if ( $pageCounts['transclusion']['to'] > 0 ) {
567 if ( $pageCounts['transclusion']['to'] > count( $transcludedTargets ) ) {
568 $more = Linker
::link(
570 $this->msg( 'moredotdotdot' )->escaped(),
572 array( 'hidelinks' => 1, 'hideredirs' => 1 )
578 $pageInfo['header-properties'][] = array(
579 $this->msg( 'pageinfo-transclusions' )
580 ->numParams( $pageCounts['transclusion']['to'] ),
581 Linker
::formatTemplates(
594 * Returns page counts that would be too "expensive" to retrieve by normal means.
596 * @param Title $title Title to get counts for
599 protected static function pageCounts( Title
$title ) {
600 global $wgRCMaxAge, $wgDisableCounters;
602 wfProfileIn( __METHOD__
);
603 $id = $title->getArticleID();
605 $dbr = wfGetDB( DB_SLAVE
);
608 if ( !$wgDisableCounters ) {
610 $views = (int) $dbr->selectField(
613 array( 'page_id' => $id ),
616 $result['views'] = $views;
619 // Number of page watchers
620 $watchers = (int) $dbr->selectField(
624 'wl_namespace' => $title->getNamespace(),
625 'wl_title' => $title->getDBkey(),
629 $result['watchers'] = $watchers;
631 // Total number of edits
632 $edits = (int) $dbr->selectField(
635 array( 'rev_page' => $id ),
638 $result['edits'] = $edits;
640 // Total number of distinct authors
641 $authors = (int) $dbr->selectField(
643 'COUNT(DISTINCT rev_user_text)',
644 array( 'rev_page' => $id ),
647 $result['authors'] = $authors;
649 // "Recent" threshold defined by $wgRCMaxAge
650 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
652 // Recent number of edits
653 $edits = (int) $dbr->selectField(
658 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
662 $result['recent_edits'] = $edits;
664 // Recent number of distinct authors
665 $authors = (int) $dbr->selectField(
667 'COUNT(DISTINCT rev_user_text)',
670 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
674 $result['recent_authors'] = $authors;
676 // Subpages (if enabled)
677 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
678 $conds = array( 'page_namespace' => $title->getNamespace() );
679 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
681 // Subpages of this page (redirects)
682 $conds['page_is_redirect'] = 1;
683 $result['subpages']['redirects'] = (int) $dbr->selectField(
689 // Subpages of this page (non-redirects)
690 $conds['page_is_redirect'] = 0;
691 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
698 // Subpages of this page (total)
699 $result['subpages']['total'] = $result['subpages']['redirects']
700 +
$result['subpages']['nonredirects'];
703 // Counts for the number of transclusion links (to/from)
704 $result['transclusion']['to'] = (int) $dbr->selectField(
708 'tl_namespace' => $title->getNamespace(),
709 'tl_title' => $title->getDBkey()
714 $result['transclusion']['from'] = (int) $dbr->selectField(
717 array( 'tl_from' => $title->getArticleID() ),
721 wfProfileOut( __METHOD__
);
726 * Returns the name that goes in the "<h1>" page title.
730 protected function getPageTitle() {
731 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
735 * Get a list of contributors of $article
736 * @return string: html
738 protected function getContributors() {
739 global $wgHiddenPrefs;
741 $contributors = $this->page
->getContributors();
742 $real_names = array();
743 $user_names = array();
746 # Sift for real versus user names
747 foreach ( $contributors as $user ) {
748 $page = $user->isAnon()
749 ? SpecialPage
::getTitleFor( 'Contributions', $user->getName() )
750 : $user->getUserPage();
752 if ( $user->getID() == 0 ) {
753 $anon_ips[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
754 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
755 $real_names[] = Linker
::link( $page, htmlspecialchars( $user->getRealName() ) );
757 $user_names[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
761 $lang = $this->getLanguage();
763 $real = $lang->listToText( $real_names );
765 # "ThisSite user(s) A, B and C"
766 if ( count( $user_names ) ) {
767 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
768 count( $user_names ) )->escaped();
773 if ( count( $anon_ips ) ) {
774 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
775 count( $anon_ips ) )->escaped();
780 # This is the big list, all mooshed together. We sift for blank strings
782 foreach ( array( $real, $user, $anon ) as $s ) {
784 array_push( $fulllist, $s );
788 $count = count( $fulllist );
789 # "Based on work by ..."
791 ?
$this->msg( 'othercontribs' )->rawParams(
792 $lang->listToText( $fulllist ) )->params( $count )->escaped()
797 * Returns the description that goes below the "<h1>" tag.
801 protected function getDescription() {