ebedde3003bbf546529adb3b4bdd19eb32473c07
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 ) {
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
;
396 $firstRevUser = $firstRev->getUserText( Revision
::FOR_THIS_USER
);
397 if ( $firstRevUser !== '' ) {
398 $batch->add( NS_USER
, $firstRevUser );
399 $batch->add( NS_USER_TALK
, $firstRevUser );
402 $lastRevUser = $lastRev->getUserText( Revision
::FOR_THIS_USER
);
403 if ( $lastRevUser !== '' ) {
404 $batch->add( NS_USER
, $lastRevUser );
405 $batch->add( NS_USER_TALK
, $lastRevUser );
411 $pageInfo['header-edits'][] = array(
412 $this->msg( 'pageinfo-firstuser' ),
413 Linker
::revUserTools( $firstRev )
416 // Date of page creation
417 $pageInfo['header-edits'][] = array(
418 $this->msg( 'pageinfo-firsttime' ),
421 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
423 array( 'oldid' => $firstRev->getId() )
428 $pageInfo['header-edits'][] = array(
429 $this->msg( 'pageinfo-lastuser' ),
430 Linker
::revUserTools( $lastRev )
433 // Date of latest edit
434 $pageInfo['header-edits'][] = array(
435 $this->msg( 'pageinfo-lasttime' ),
438 $lang->userTimeAndDate( $this->page
->getTimestamp(), $user ),
440 array( 'oldid' => $this->page
->getLatest() )
444 // Total number of edits
445 $pageInfo['header-edits'][] = array(
446 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
449 // Total number of distinct authors
450 $pageInfo['header-edits'][] = array(
451 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
454 // Recent number of edits (within past 30 days)
455 $pageInfo['header-edits'][] = array(
456 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
457 $lang->formatNum( $pageCounts['recent_edits'] )
460 // Recent number of distinct authors
461 $pageInfo['header-edits'][] = array(
462 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
465 // Array of MagicWord objects
466 $magicWords = MagicWord
::getDoubleUnderscoreArray();
468 // Array of magic word IDs
469 $wordIDs = $magicWords->names
;
471 // Array of IDs => localized magic words
472 $localizedWords = $wgContLang->getMagicWords();
474 $listItems = array();
475 foreach ( $pageProperties as $property => $value ) {
476 if ( in_array( $property, $wordIDs ) ) {
477 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
481 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
482 $hiddenCategories = $this->page
->getHiddenCategories();
485 count( $listItems ) > 0 ||
486 count( $hiddenCategories ) > 0 ||
487 $pageCounts['transclusion']['from'] > 0 ||
488 $pageCounts['transclusion']['to'] > 0
490 $options = array( 'LIMIT' => $wgPageInfoTransclusionLimit );
491 $transcludedTemplates = $title->getTemplateLinksFrom( $options );
492 $transcludedTargets = $title->getTemplateLinksTo( $options );
495 $pageInfo['header-properties'] = array();
498 if ( count( $listItems ) > 0 ) {
499 $pageInfo['header-properties'][] = array(
500 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
506 if ( count( $hiddenCategories ) > 0 ) {
507 $pageInfo['header-properties'][] = array(
508 $this->msg( 'pageinfo-hidden-categories' )
509 ->numParams( count( $hiddenCategories ) ),
510 Linker
::formatHiddenCategories( $hiddenCategories )
514 // Transcluded templates
515 if ( $pageCounts['transclusion']['from'] > 0 ) {
516 if ( $pageCounts['transclusion']['from'] > count( $transcludedTemplates ) ) {
517 $more = $this->msg( 'morenotlisted' )->escaped();
522 $pageInfo['header-properties'][] = array(
523 $this->msg( 'pageinfo-templates' )
524 ->numParams( $pageCounts['transclusion']['from'] ),
525 Linker
::formatTemplates(
526 $transcludedTemplates,
533 if ( $pageCounts['transclusion']['to'] > 0 ) {
534 if ( $pageCounts['transclusion']['to'] > count( $transcludedTargets ) ) {
535 $more = Linker
::link(
537 $this->msg( 'moredotdotdot' )->escaped(),
539 array( 'hidelinks' => 1, 'hideredirs' => 1 )
545 $pageInfo['header-properties'][] = array(
546 $this->msg( 'pageinfo-transclusions' )
547 ->numParams( $pageCounts['transclusion']['to'] ),
548 Linker
::formatTemplates(
561 * Returns page counts that would be too "expensive" to retrieve by normal means.
563 * @param Title $title Title to get counts for
566 protected static function pageCounts( Title
$title ) {
567 global $wgRCMaxAge, $wgDisableCounters;
569 wfProfileIn( __METHOD__
);
570 $id = $title->getArticleID();
572 $dbr = wfGetDB( DB_SLAVE
);
575 if ( !$wgDisableCounters ) {
577 $views = (int) $dbr->selectField(
580 array( 'page_id' => $id ),
583 $result['views'] = $views;
586 // Number of page watchers
587 $watchers = (int) $dbr->selectField(
591 'wl_namespace' => $title->getNamespace(),
592 'wl_title' => $title->getDBkey(),
596 $result['watchers'] = $watchers;
598 // Total number of edits
599 $edits = (int) $dbr->selectField(
602 array( 'rev_page' => $id ),
605 $result['edits'] = $edits;
607 // Total number of distinct authors
608 $authors = (int) $dbr->selectField(
610 'COUNT(DISTINCT rev_user_text)',
611 array( 'rev_page' => $id ),
614 $result['authors'] = $authors;
616 // "Recent" threshold defined by $wgRCMaxAge
617 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
619 // Recent number of edits
620 $edits = (int) $dbr->selectField(
625 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
629 $result['recent_edits'] = $edits;
631 // Recent number of distinct authors
632 $authors = (int) $dbr->selectField(
634 'COUNT(DISTINCT rev_user_text)',
637 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
641 $result['recent_authors'] = $authors;
643 // Subpages (if enabled)
644 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
645 $conds = array( 'page_namespace' => $title->getNamespace() );
646 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
648 // Subpages of this page (redirects)
649 $conds['page_is_redirect'] = 1;
650 $result['subpages']['redirects'] = (int) $dbr->selectField(
656 // Subpages of this page (non-redirects)
657 $conds['page_is_redirect'] = 0;
658 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
665 // Subpages of this page (total)
666 $result['subpages']['total'] = $result['subpages']['redirects']
667 +
$result['subpages']['nonredirects'];
670 // Counts for the number of transclusion links (to/from)
671 $result['transclusion']['to'] = (int) $dbr->selectField(
675 'tl_namespace' => $title->getNamespace(),
676 'tl_title' => $title->getDBkey()
681 $result['transclusion']['from'] = (int) $dbr->selectField(
684 array( 'tl_from' => $title->getArticleID() ),
688 wfProfileOut( __METHOD__
);
693 * Returns the name that goes in the "<h1>" page title.
697 protected function getPageTitle() {
698 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
702 * Get a list of contributors of $article
703 * @return string: html
705 protected function getContributors() {
706 global $wgHiddenPrefs;
708 $contributors = $this->page
->getContributors();
709 $real_names = array();
710 $user_names = array();
713 # Sift for real versus user names
714 foreach ( $contributors as $user ) {
715 $page = $user->isAnon()
716 ? SpecialPage
::getTitleFor( 'Contributions', $user->getName() )
717 : $user->getUserPage();
719 if ( $user->getID() == 0 ) {
720 $anon_ips[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
721 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
722 $real_names[] = Linker
::link( $page, htmlspecialchars( $user->getRealName() ) );
724 $user_names[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
728 $lang = $this->getLanguage();
730 $real = $lang->listToText( $real_names );
732 # "ThisSite user(s) A, B and C"
733 if ( count( $user_names ) ) {
734 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
735 count( $user_names ) )->escaped();
740 if ( count( $anon_ips ) ) {
741 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
742 count( $anon_ips ) )->escaped();
747 # This is the big list, all mooshed together. We sift for blank strings
749 foreach ( array( $real, $user, $anon ) as $s ) {
751 array_push( $fulllist, $s );
755 $count = count( $fulllist );
756 # "Based on work by ..."
758 ?
$this->msg( 'othercontribs' )->rawParams(
759 $lang->listToText( $fulllist ) )->params( $count )->escaped()
764 * Returns the description that goes below the "<h1>" tag.
768 protected function getDescription() {