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; }' ) . "\n";
86 // Hide "Templates used on this page" explanation
87 $content .= Html
::element( 'style', array(),
88 '.mw-templatesUsedExplanation { display: none; }' ) . "\n";
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() ) . "\n";
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 ) . "\n";
105 $content = $this->addTable( $content, $table ) . "\n";
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 ) {
128 $spanAttribs = array( 'class' => 'mw-headline', 'id' => Sanitizer
::escapeId( $header ) );
129 return Html
::rawElement( 'h2', array(), Html
::element( 'span', $spanAttribs, $header ) );
133 * Adds a row to a table that will be added to the content.
135 * @param $table string The table that will be added to the content
136 * @param $name string The name of the row
137 * @param $value string The value of the row
138 * @return string The table with the row added
140 protected function addRow( $table, $name, $value ) {
141 return $table . Html
::rawElement( 'tr', array(),
142 Html
::rawElement( 'td', array( 'style' => 'vertical-align: top;' ), $name ) .
143 Html
::rawElement( 'td', array(), $value )
148 * Adds a table to the content that will be added to the output.
150 * @param $content string The content that will be added to the output
151 * @param $table string The table
152 * @return string The content with the table added
154 protected function addTable( $content, $table ) {
155 return $content . Html
::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
160 * Returns page information in an easily-manipulated format. Array keys are used so extensions
161 * may add additional information in arbitrary positions. Array values are arrays with one
162 * element to be rendered as a header, arrays with two elements to be rendered as a table row.
166 protected function pageInfo() {
167 global $wgContLang, $wgRCMaxAge, $wgMemc, $wgUnwatchedPageThreshold, $wgPageInfoTransclusionLimit;
169 $user = $this->getUser();
170 $lang = $this->getLanguage();
171 $title = $this->getTitle();
172 $id = $title->getArticleID();
174 $memcKey = wfMemcKey( 'infoaction', sha1( $title->getPrefixedText() ), $this->page
->getLatest() );
175 $pageCounts = $wgMemc->get( $memcKey );
176 if ( $pageCounts === false ) {
177 // Get page information that would be too "expensive" to retrieve by normal means
178 $pageCounts = self
::pageCounts( $title );
180 $wgMemc->set( $memcKey, $pageCounts );
183 // Get page properties
184 $dbr = wfGetDB( DB_SLAVE
);
185 $result = $dbr->select(
187 array( 'pp_propname', 'pp_value' ),
188 array( 'pp_page' => $id ),
192 $pageProperties = array();
193 foreach ( $result as $row ) {
194 $pageProperties[$row->pp_propname
] = $row->pp_value
;
199 $pageInfo['header-basic'] = array();
202 $displayTitle = $title->getPrefixedText();
203 if ( !empty( $pageProperties['displaytitle'] ) ) {
204 $displayTitle = $pageProperties['displaytitle'];
207 $pageInfo['header-basic'][] = array(
208 $this->msg( 'pageinfo-display-title' ), $displayTitle
211 // Is it a redirect? If so, where to?
212 if ( $title->isRedirect() ) {
213 $pageInfo['header-basic'][] = array(
214 $this->msg( 'pageinfo-redirectsto' ),
215 Linker
::link( $this->page
->getRedirectTarget() ) .
216 $this->msg( 'word-separator' )->text() .
217 $this->msg( 'parentheses', Linker
::link(
218 $this->page
->getRedirectTarget(),
219 $this->msg( 'pageinfo-redirectsto-info' )->escaped(),
221 array( 'action' => 'info' )
227 $sortKey = $title->getCategorySortKey();
228 if ( !empty( $pageProperties['defaultsort'] ) ) {
229 $sortKey = $pageProperties['defaultsort'];
232 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-default-sort' ), $sortKey );
234 // Page length (in bytes)
235 $pageInfo['header-basic'][] = array(
236 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
239 // Page ID (number not localised, as it's a database ID)
240 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-article-id' ), $id );
242 // Language in which the page content is (supposed to be) written
243 $pageLang = $title->getPageLanguage()->getCode();
244 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-language' ),
245 Language
::fetchLanguageName( $pageLang, $lang->getCode() )
246 . ' ' . $this->msg( 'parentheses', $pageLang ) );
248 // Search engine status
249 $pOutput = new ParserOutput();
250 if ( isset( $pageProperties['noindex'] ) ) {
251 $pOutput->setIndexPolicy( 'noindex' );
254 // Use robot policy logic
255 $policy = $this->page
->getRobotPolicy( 'view', $pOutput );
256 $pageInfo['header-basic'][] = array(
257 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
260 if ( isset( $pageCounts['views'] ) ) {
262 $pageInfo['header-basic'][] = array(
263 $this->msg( 'pageinfo-views' ), $lang->formatNum( $pageCounts['views'] )
268 $user->isAllowed( 'unwatchedpages' ) ||
269 ( $wgUnwatchedPageThreshold !== false &&
270 $pageCounts['watchers'] >= $wgUnwatchedPageThreshold )
272 // Number of page watchers
273 $pageInfo['header-basic'][] = array(
274 $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
276 } elseif ( $wgUnwatchedPageThreshold !== false ) {
277 $pageInfo['header-basic'][] = array(
278 $this->msg( 'pageinfo-watchers' ),
279 $this->msg( 'pageinfo-few-watchers' )->numParams( $wgUnwatchedPageThreshold )
283 // Redirects to this page
284 $whatLinksHere = SpecialPage
::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
285 $pageInfo['header-basic'][] = array(
288 $this->msg( 'pageinfo-redirects-name' )->escaped(),
290 array( 'hidelinks' => 1, 'hidetrans' => 1 )
292 $this->msg( 'pageinfo-redirects-value' )
293 ->numParams( count( $title->getRedirectsHere() ) )
296 // Is it counted as a content page?
297 if ( $this->page
->isCountable() ) {
298 $pageInfo['header-basic'][] = array(
299 $this->msg( 'pageinfo-contentpage' ),
300 $this->msg( 'pageinfo-contentpage-yes' )
304 // Subpages of this page, if subpages are enabled for the current NS
305 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
306 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
307 $pageInfo['header-basic'][] = array(
308 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
309 $this->msg( 'pageinfo-subpages-value' )
311 $pageCounts['subpages']['total'],
312 $pageCounts['subpages']['redirects'],
313 $pageCounts['subpages']['nonredirects'] )
317 if ( $title->inNamespace( NS_CATEGORY
) ) {
318 $category = Category
::newFromTitle( $title );
319 $pageInfo['category-info'] = array(
321 $this->msg( 'pageinfo-category-pages' ),
322 $lang->formatNum( $category->getPageCount() )
325 $this->msg( 'pageinfo-category-subcats' ),
326 $lang->formatNum( $category->getSubcatCount() )
329 $this->msg( 'pageinfo-category-files' ),
330 $lang->formatNum( $category->getFileCount() )
336 $pageInfo['header-restrictions'] = array();
338 // Is this page effected by the cascading protection of something which includes it?
339 if ( $title->isCascadeProtected() ) {
341 $sources = $title->getCascadeProtectionSources(); // Array deferencing is in PHP 5.4 :(
343 foreach ( $sources[0] as $sourceTitle ) {
344 $cascadingFrom .= Html
::rawElement( 'li', array(), Linker
::linkKnown( $sourceTitle ) );
347 $cascadingFrom = Html
::rawElement( 'ul', array(), $cascadingFrom );
348 $pageInfo['header-restrictions'][] = array(
349 $this->msg( 'pageinfo-protect-cascading-from' ),
354 // Is out protection set to cascade to other pages?
355 if ( $title->areRestrictionsCascading() ) {
356 $pageInfo['header-restrictions'][] = array(
357 $this->msg( 'pageinfo-protect-cascading' ),
358 $this->msg( 'pageinfo-protect-cascading-yes' )
363 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
364 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
366 if ( $protectionLevel == '' ) {
368 $message = $this->msg( 'protect-default' )->escaped();
370 // Administrators only
371 $message = $this->msg( "protect-level-$protectionLevel" );
372 if ( $message->isDisabled() ) {
373 // Require "$1" permission
374 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
376 $message = $message->escaped();
380 $pageInfo['header-restrictions'][] = array(
381 $this->msg( "restriction-$restrictionType" ), $message
385 if ( !$this->page
->exists() ) {
390 $pageInfo['header-edits'] = array();
392 $firstRev = $this->page
->getOldestRevision();
393 $lastRev = $this->page
->getRevision();
394 $batch = new LinkBatch
;
397 $firstRevUser = $firstRev->getUserText( Revision
::FOR_THIS_USER
);
398 if ( $firstRevUser !== '' ) {
399 $batch->add( NS_USER
, $firstRevUser );
400 $batch->add( NS_USER_TALK
, $firstRevUser );
405 $lastRevUser = $lastRev->getUserText( Revision
::FOR_THIS_USER
);
406 if ( $lastRevUser !== '' ) {
407 $batch->add( NS_USER
, $lastRevUser );
408 $batch->add( NS_USER_TALK
, $lastRevUser );
416 $pageInfo['header-edits'][] = array(
417 $this->msg( 'pageinfo-firstuser' ),
418 Linker
::revUserTools( $firstRev )
421 // Date of page creation
422 $pageInfo['header-edits'][] = array(
423 $this->msg( 'pageinfo-firsttime' ),
426 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
428 array( 'oldid' => $firstRev->getId() )
435 $pageInfo['header-edits'][] = array(
436 $this->msg( 'pageinfo-lastuser' ),
437 Linker
::revUserTools( $lastRev )
440 // Date of latest edit
441 $pageInfo['header-edits'][] = array(
442 $this->msg( 'pageinfo-lasttime' ),
445 $lang->userTimeAndDate( $this->page
->getTimestamp(), $user ),
447 array( 'oldid' => $this->page
->getLatest() )
452 // Total number of edits
453 $pageInfo['header-edits'][] = array(
454 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
457 // Total number of distinct authors
458 $pageInfo['header-edits'][] = array(
459 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
462 // Recent number of edits (within past 30 days)
463 $pageInfo['header-edits'][] = array(
464 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
465 $lang->formatNum( $pageCounts['recent_edits'] )
468 // Recent number of distinct authors
469 $pageInfo['header-edits'][] = array(
470 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
473 // Array of MagicWord objects
474 $magicWords = MagicWord
::getDoubleUnderscoreArray();
476 // Array of magic word IDs
477 $wordIDs = $magicWords->names
;
479 // Array of IDs => localized magic words
480 $localizedWords = $wgContLang->getMagicWords();
482 $listItems = array();
483 foreach ( $pageProperties as $property => $value ) {
484 if ( in_array( $property, $wordIDs ) ) {
485 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
489 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
490 $hiddenCategories = $this->page
->getHiddenCategories();
493 count( $listItems ) > 0 ||
494 count( $hiddenCategories ) > 0 ||
495 $pageCounts['transclusion']['from'] > 0 ||
496 $pageCounts['transclusion']['to'] > 0
498 $options = array( 'LIMIT' => $wgPageInfoTransclusionLimit );
499 $transcludedTemplates = $title->getTemplateLinksFrom( $options );
500 $transcludedTargets = $title->getTemplateLinksTo( $options );
503 $pageInfo['header-properties'] = array();
506 if ( count( $listItems ) > 0 ) {
507 $pageInfo['header-properties'][] = array(
508 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
514 if ( count( $hiddenCategories ) > 0 ) {
515 $pageInfo['header-properties'][] = array(
516 $this->msg( 'pageinfo-hidden-categories' )
517 ->numParams( count( $hiddenCategories ) ),
518 Linker
::formatHiddenCategories( $hiddenCategories )
522 // Transcluded templates
523 if ( $pageCounts['transclusion']['from'] > 0 ) {
524 if ( $pageCounts['transclusion']['from'] > count( $transcludedTemplates ) ) {
525 $more = $this->msg( 'morenotlisted' )->escaped();
530 $pageInfo['header-properties'][] = array(
531 $this->msg( 'pageinfo-templates' )
532 ->numParams( $pageCounts['transclusion']['from'] ),
533 Linker
::formatTemplates(
534 $transcludedTemplates,
541 if ( $pageCounts['transclusion']['to'] > 0 ) {
542 if ( $pageCounts['transclusion']['to'] > count( $transcludedTargets ) ) {
543 $more = Linker
::link(
545 $this->msg( 'moredotdotdot' )->escaped(),
547 array( 'hidelinks' => 1, 'hideredirs' => 1 )
553 $pageInfo['header-properties'][] = array(
554 $this->msg( 'pageinfo-transclusions' )
555 ->numParams( $pageCounts['transclusion']['to'] ),
556 Linker
::formatTemplates(
569 * Returns page counts that would be too "expensive" to retrieve by normal means.
571 * @param Title $title Title to get counts for
574 protected static function pageCounts( Title
$title ) {
575 global $wgRCMaxAge, $wgDisableCounters;
577 wfProfileIn( __METHOD__
);
578 $id = $title->getArticleID();
580 $dbr = wfGetDB( DB_SLAVE
);
583 if ( !$wgDisableCounters ) {
585 $views = (int) $dbr->selectField(
588 array( 'page_id' => $id ),
591 $result['views'] = $views;
594 // Number of page watchers
595 $watchers = (int) $dbr->selectField(
599 'wl_namespace' => $title->getNamespace(),
600 'wl_title' => $title->getDBkey(),
604 $result['watchers'] = $watchers;
606 // Total number of edits
607 $edits = (int) $dbr->selectField(
610 array( 'rev_page' => $id ),
613 $result['edits'] = $edits;
615 // Total number of distinct authors
616 $authors = (int) $dbr->selectField(
618 'COUNT(DISTINCT rev_user_text)',
619 array( 'rev_page' => $id ),
622 $result['authors'] = $authors;
624 // "Recent" threshold defined by $wgRCMaxAge
625 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
627 // Recent number of edits
628 $edits = (int) $dbr->selectField(
633 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
637 $result['recent_edits'] = $edits;
639 // Recent number of distinct authors
640 $authors = (int) $dbr->selectField(
642 'COUNT(DISTINCT rev_user_text)',
645 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
649 $result['recent_authors'] = $authors;
651 // Subpages (if enabled)
652 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
653 $conds = array( 'page_namespace' => $title->getNamespace() );
654 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
656 // Subpages of this page (redirects)
657 $conds['page_is_redirect'] = 1;
658 $result['subpages']['redirects'] = (int) $dbr->selectField(
664 // Subpages of this page (non-redirects)
665 $conds['page_is_redirect'] = 0;
666 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
673 // Subpages of this page (total)
674 $result['subpages']['total'] = $result['subpages']['redirects']
675 +
$result['subpages']['nonredirects'];
678 // Counts for the number of transclusion links (to/from)
679 $result['transclusion']['to'] = (int) $dbr->selectField(
683 'tl_namespace' => $title->getNamespace(),
684 'tl_title' => $title->getDBkey()
689 $result['transclusion']['from'] = (int) $dbr->selectField(
692 array( 'tl_from' => $title->getArticleID() ),
696 wfProfileOut( __METHOD__
);
701 * Returns the name that goes in the "<h1>" page title.
705 protected function getPageTitle() {
706 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
710 * Get a list of contributors of $article
711 * @return string: html
713 protected function getContributors() {
714 global $wgHiddenPrefs;
716 $contributors = $this->page
->getContributors();
717 $real_names = array();
718 $user_names = array();
721 # Sift for real versus user names
722 foreach ( $contributors as $user ) {
723 $page = $user->isAnon()
724 ? SpecialPage
::getTitleFor( 'Contributions', $user->getName() )
725 : $user->getUserPage();
727 if ( $user->getID() == 0 ) {
728 $anon_ips[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
729 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
730 $real_names[] = Linker
::link( $page, htmlspecialchars( $user->getRealName() ) );
732 $user_names[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
736 $lang = $this->getLanguage();
738 $real = $lang->listToText( $real_names );
740 # "ThisSite user(s) A, B and C"
741 if ( count( $user_names ) ) {
742 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
743 count( $user_names ) )->escaped();
748 if ( count( $anon_ips ) ) {
749 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
750 count( $anon_ips ) )->escaped();
755 # This is the big list, all mooshed together. We sift for blank strings
757 foreach ( array( $real, $user, $anon ) as $s ) {
759 array_push( $fulllist, $s );
763 $count = count( $fulllist );
764 # "Based on work by ..."
766 ?
$this->msg( 'othercontribs' )->rawParams(
767 $lang->listToText( $fulllist ) )->params( $count )->escaped()
772 * Returns the description that goes below the "<h1>" tag.
776 protected function getDescription() {