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 ) {
66 $revision = WikiPage
::factory( $title )->getRevision();
67 if ( $revision !== null ) {
68 $memcKey = wfMemcKey( 'infoaction', $this->getPrefixedText(), $revision->getId() );
69 $wgMemc->delete( $memcKey );
74 * Shows page information on GET request.
76 * @return string Page information that will be added to the output
78 public function onView() {
82 $oldid = $this->page
->getOldID();
84 $revision = $this->page
->getRevisionFetched();
86 // Revision is missing
87 if ( $revision === null ) {
88 return $this->msg( 'missing-revision', $oldid )->parse();
91 // Revision is not current
92 if ( !$revision->isCurrent() ) {
93 return $this->msg( 'pageinfo-not-current' )->plain();
98 if ( !$this->msg( 'pageinfo-header' )->isDisabled() ) {
99 $content .= $this->msg( 'pageinfo-header' )->parse();
102 // Hide "This page is a member of # hidden categories" explanation
103 $content .= Html
::element( 'style', array(),
104 '.mw-hiddenCategoriesExplanation { display: none; }' ) . "\n";
106 // Hide "Templates used on this page" explanation
107 $content .= Html
::element( 'style', array(),
108 '.mw-templatesUsedExplanation { display: none; }' ) . "\n";
110 // Get page information
111 $pageInfo = $this->pageInfo();
113 // Allow extensions to add additional information
114 wfRunHooks( 'InfoAction', array( $this->getContext(), &$pageInfo ) );
116 // Render page information
117 foreach ( $pageInfo as $header => $infoTable ) {
118 $content .= $this->makeHeader( $this->msg( "pageinfo-${header}" )->escaped() ) . "\n";
120 foreach ( $infoTable as $infoRow ) {
121 $name = ( $infoRow[0] instanceof Message
) ?
$infoRow[0]->escaped() : $infoRow[0];
122 $value = ( $infoRow[1] instanceof Message
) ?
$infoRow[1]->escaped() : $infoRow[1];
123 $table = $this->addRow( $table, $name, $value ) . "\n";
125 $content = $this->addTable( $content, $table ) . "\n";
129 if ( !$this->msg( 'pageinfo-footer' )->isDisabled() ) {
130 $content .= $this->msg( 'pageinfo-footer' )->parse();
134 /*if ( $this->page->exists() ) {
135 $content .= Html::rawElement( 'div', array( 'id' => 'mw-credits' ), $this->getContributors() );
142 * Creates a header that can be added to the output.
144 * @param string $header The header text.
145 * @return string The HTML.
147 protected function makeHeader( $header ) {
148 $spanAttribs = array( 'class' => 'mw-headline', 'id' => Sanitizer
::escapeId( $header ) );
149 return Html
::rawElement( 'h2', array(), Html
::element( 'span', $spanAttribs, $header ) );
153 * Adds a row to a table that will be added to the content.
155 * @param string $table The table that will be added to the content
156 * @param string $name The name of the row
157 * @param string $value The value of the row
158 * @return string The table with the row added
160 protected function addRow( $table, $name, $value ) {
161 return $table . Html
::rawElement( 'tr', array(),
162 Html
::rawElement( 'td', array( 'style' => 'vertical-align: top;' ), $name ) .
163 Html
::rawElement( 'td', array(), $value )
168 * Adds a table to the content that will be added to the output.
170 * @param string $content The content that will be added to the output
171 * @param string $table The table
172 * @return string The content with the table added
174 protected function addTable( $content, $table ) {
175 return $content . Html
::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
180 * Returns page information in an easily-manipulated format. Array keys are used so extensions
181 * may add additional information in arbitrary positions. Array values are arrays with one
182 * element to be rendered as a header, arrays with two elements to be rendered as a table row.
186 protected function pageInfo() {
187 global $wgContLang, $wgRCMaxAge, $wgMemc, $wgUnwatchedPageThreshold, $wgPageInfoTransclusionLimit;
189 $user = $this->getUser();
190 $lang = $this->getLanguage();
191 $title = $this->getTitle();
192 $id = $title->getArticleID();
194 $memcKey = wfMemcKey( 'infoaction', sha1( $title->getPrefixedText() ), $this->page
->getLatest() );
195 $pageCounts = $wgMemc->get( $memcKey );
196 if ( $pageCounts === false ) {
197 // Get page information that would be too "expensive" to retrieve by normal means
198 $pageCounts = self
::pageCounts( $title );
200 $wgMemc->set( $memcKey, $pageCounts );
203 // Get page properties
204 $dbr = wfGetDB( DB_SLAVE
);
205 $result = $dbr->select(
207 array( 'pp_propname', 'pp_value' ),
208 array( 'pp_page' => $id ),
212 $pageProperties = array();
213 foreach ( $result as $row ) {
214 $pageProperties[$row->pp_propname
] = $row->pp_value
;
219 $pageInfo['header-basic'] = array();
222 $displayTitle = $title->getPrefixedText();
223 if ( !empty( $pageProperties['displaytitle'] ) ) {
224 $displayTitle = $pageProperties['displaytitle'];
227 $pageInfo['header-basic'][] = array(
228 $this->msg( 'pageinfo-display-title' ), $displayTitle
231 // Is it a redirect? If so, where to?
232 if ( $title->isRedirect() ) {
233 $pageInfo['header-basic'][] = array(
234 $this->msg( 'pageinfo-redirectsto' ),
235 Linker
::link( $this->page
->getRedirectTarget() ) .
236 $this->msg( 'word-separator' )->text() .
237 $this->msg( 'parentheses', Linker
::link(
238 $this->page
->getRedirectTarget(),
239 $this->msg( 'pageinfo-redirectsto-info' )->escaped(),
241 array( 'action' => 'info' )
247 $sortKey = $title->getCategorySortkey();
248 if ( !empty( $pageProperties['defaultsort'] ) ) {
249 $sortKey = $pageProperties['defaultsort'];
252 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-default-sort' ), $sortKey );
254 // Page length (in bytes)
255 $pageInfo['header-basic'][] = array(
256 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
259 // Page ID (number not localised, as it's a database ID)
260 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-article-id' ), $id );
262 // Language in which the page content is (supposed to be) written
263 $pageLang = $title->getPageLanguage()->getCode();
264 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-language' ),
265 Language
::fetchLanguageName( $pageLang, $lang->getCode() )
266 . ' ' . $this->msg( 'parentheses', $pageLang ) );
268 // Search engine status
269 $pOutput = new ParserOutput();
270 if ( isset( $pageProperties['noindex'] ) ) {
271 $pOutput->setIndexPolicy( 'noindex' );
274 // Use robot policy logic
275 $policy = $this->page
->getRobotPolicy( 'view', $pOutput );
276 $pageInfo['header-basic'][] = array(
277 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
280 if ( isset( $pageCounts['views'] ) ) {
282 $pageInfo['header-basic'][] = array(
283 $this->msg( 'pageinfo-views' ), $lang->formatNum( $pageCounts['views'] )
288 $user->isAllowed( 'unwatchedpages' ) ||
289 ( $wgUnwatchedPageThreshold !== false &&
290 $pageCounts['watchers'] >= $wgUnwatchedPageThreshold )
292 // Number of page watchers
293 $pageInfo['header-basic'][] = array(
294 $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
296 } elseif ( $wgUnwatchedPageThreshold !== false ) {
297 $pageInfo['header-basic'][] = array(
298 $this->msg( 'pageinfo-watchers' ),
299 $this->msg( 'pageinfo-few-watchers' )->numParams( $wgUnwatchedPageThreshold )
303 // Redirects to this page
304 $whatLinksHere = SpecialPage
::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
305 $pageInfo['header-basic'][] = array(
308 $this->msg( 'pageinfo-redirects-name' )->escaped(),
310 array( 'hidelinks' => 1, 'hidetrans' => 1 )
312 $this->msg( 'pageinfo-redirects-value' )
313 ->numParams( count( $title->getRedirectsHere() ) )
316 // Is it counted as a content page?
317 if ( $this->page
->isCountable() ) {
318 $pageInfo['header-basic'][] = array(
319 $this->msg( 'pageinfo-contentpage' ),
320 $this->msg( 'pageinfo-contentpage-yes' )
324 // Subpages of this page, if subpages are enabled for the current NS
325 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
326 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
327 $pageInfo['header-basic'][] = array(
328 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
329 $this->msg( 'pageinfo-subpages-value' )
331 $pageCounts['subpages']['total'],
332 $pageCounts['subpages']['redirects'],
333 $pageCounts['subpages']['nonredirects'] )
337 if ( $title->inNamespace( NS_CATEGORY
) ) {
338 $category = Category
::newFromTitle( $title );
339 $pageInfo['category-info'] = array(
341 $this->msg( 'pageinfo-category-pages' ),
342 $lang->formatNum( $category->getPageCount() )
345 $this->msg( 'pageinfo-category-subcats' ),
346 $lang->formatNum( $category->getSubcatCount() )
349 $this->msg( 'pageinfo-category-files' ),
350 $lang->formatNum( $category->getFileCount() )
356 $pageInfo['header-restrictions'] = array();
358 // Is this page effected by the cascading protection of something which includes it?
359 if ( $title->isCascadeProtected() ) {
361 $sources = $title->getCascadeProtectionSources(); // Array deferencing is in PHP 5.4 :(
363 foreach ( $sources[0] as $sourceTitle ) {
364 $cascadingFrom .= Html
::rawElement( 'li', array(), Linker
::linkKnown( $sourceTitle ) );
367 $cascadingFrom = Html
::rawElement( 'ul', array(), $cascadingFrom );
368 $pageInfo['header-restrictions'][] = array(
369 $this->msg( 'pageinfo-protect-cascading-from' ),
374 // Is out protection set to cascade to other pages?
375 if ( $title->areRestrictionsCascading() ) {
376 $pageInfo['header-restrictions'][] = array(
377 $this->msg( 'pageinfo-protect-cascading' ),
378 $this->msg( 'pageinfo-protect-cascading-yes' )
383 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
384 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
386 if ( $protectionLevel == '' ) {
388 $message = $this->msg( 'protect-default' )->escaped();
390 // Administrators only
391 $message = $this->msg( "protect-level-$protectionLevel" );
392 if ( $message->isDisabled() ) {
393 // Require "$1" permission
394 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
396 $message = $message->escaped();
400 $pageInfo['header-restrictions'][] = array(
401 $this->msg( "restriction-$restrictionType" ), $message
405 if ( !$this->page
->exists() ) {
410 $pageInfo['header-edits'] = array();
412 $firstRev = $this->page
->getOldestRevision();
413 $lastRev = $this->page
->getRevision();
414 $batch = new LinkBatch
;
417 $firstRevUser = $firstRev->getUserText( Revision
::FOR_THIS_USER
);
418 if ( $firstRevUser !== '' ) {
419 $batch->add( NS_USER
, $firstRevUser );
420 $batch->add( NS_USER_TALK
, $firstRevUser );
425 $lastRevUser = $lastRev->getUserText( Revision
::FOR_THIS_USER
);
426 if ( $lastRevUser !== '' ) {
427 $batch->add( NS_USER
, $lastRevUser );
428 $batch->add( NS_USER_TALK
, $lastRevUser );
436 $pageInfo['header-edits'][] = array(
437 $this->msg( 'pageinfo-firstuser' ),
438 Linker
::revUserTools( $firstRev )
441 // Date of page creation
442 $pageInfo['header-edits'][] = array(
443 $this->msg( 'pageinfo-firsttime' ),
446 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
448 array( 'oldid' => $firstRev->getId() )
455 $pageInfo['header-edits'][] = array(
456 $this->msg( 'pageinfo-lastuser' ),
457 Linker
::revUserTools( $lastRev )
460 // Date of latest edit
461 $pageInfo['header-edits'][] = array(
462 $this->msg( 'pageinfo-lasttime' ),
465 $lang->userTimeAndDate( $this->page
->getTimestamp(), $user ),
467 array( 'oldid' => $this->page
->getLatest() )
472 // Total number of edits
473 $pageInfo['header-edits'][] = array(
474 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
477 // Total number of distinct authors
478 $pageInfo['header-edits'][] = array(
479 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
482 // Recent number of edits (within past 30 days)
483 $pageInfo['header-edits'][] = array(
484 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
485 $lang->formatNum( $pageCounts['recent_edits'] )
488 // Recent number of distinct authors
489 $pageInfo['header-edits'][] = array(
490 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
493 // Array of MagicWord objects
494 $magicWords = MagicWord
::getDoubleUnderscoreArray();
496 // Array of magic word IDs
497 $wordIDs = $magicWords->names
;
499 // Array of IDs => localized magic words
500 $localizedWords = $wgContLang->getMagicWords();
502 $listItems = array();
503 foreach ( $pageProperties as $property => $value ) {
504 if ( in_array( $property, $wordIDs ) ) {
505 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
509 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
510 $hiddenCategories = $this->page
->getHiddenCategories();
513 count( $listItems ) > 0 ||
514 count( $hiddenCategories ) > 0 ||
515 $pageCounts['transclusion']['from'] > 0 ||
516 $pageCounts['transclusion']['to'] > 0
518 $options = array( 'LIMIT' => $wgPageInfoTransclusionLimit );
519 $transcludedTemplates = $title->getTemplateLinksFrom( $options );
520 $transcludedTargets = $title->getTemplateLinksTo( $options );
523 $pageInfo['header-properties'] = array();
526 if ( count( $listItems ) > 0 ) {
527 $pageInfo['header-properties'][] = array(
528 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
534 if ( count( $hiddenCategories ) > 0 ) {
535 $pageInfo['header-properties'][] = array(
536 $this->msg( 'pageinfo-hidden-categories' )
537 ->numParams( count( $hiddenCategories ) ),
538 Linker
::formatHiddenCategories( $hiddenCategories )
542 // Transcluded templates
543 if ( $pageCounts['transclusion']['from'] > 0 ) {
544 if ( $pageCounts['transclusion']['from'] > count( $transcludedTemplates ) ) {
545 $more = $this->msg( 'morenotlisted' )->escaped();
550 $pageInfo['header-properties'][] = array(
551 $this->msg( 'pageinfo-templates' )
552 ->numParams( $pageCounts['transclusion']['from'] ),
553 Linker
::formatTemplates(
554 $transcludedTemplates,
561 if ( $pageCounts['transclusion']['to'] > 0 ) {
562 if ( $pageCounts['transclusion']['to'] > count( $transcludedTargets ) ) {
563 $more = Linker
::link(
565 $this->msg( 'moredotdotdot' )->escaped(),
567 array( 'hidelinks' => 1, 'hideredirs' => 1 )
573 $pageInfo['header-properties'][] = array(
574 $this->msg( 'pageinfo-transclusions' )
575 ->numParams( $pageCounts['transclusion']['to'] ),
576 Linker
::formatTemplates(
589 * Returns page counts that would be too "expensive" to retrieve by normal means.
591 * @param Title $title Title to get counts for
594 protected static function pageCounts( Title
$title ) {
595 global $wgRCMaxAge, $wgDisableCounters;
597 wfProfileIn( __METHOD__
);
598 $id = $title->getArticleID();
600 $dbr = wfGetDB( DB_SLAVE
);
603 if ( !$wgDisableCounters ) {
605 $views = (int) $dbr->selectField(
608 array( 'page_id' => $id ),
611 $result['views'] = $views;
614 // Number of page watchers
615 $watchers = (int) $dbr->selectField(
619 'wl_namespace' => $title->getNamespace(),
620 'wl_title' => $title->getDBkey(),
624 $result['watchers'] = $watchers;
626 // Total number of edits
627 $edits = (int) $dbr->selectField(
630 array( 'rev_page' => $id ),
633 $result['edits'] = $edits;
635 // Total number of distinct authors
636 $authors = (int) $dbr->selectField(
638 'COUNT(DISTINCT rev_user_text)',
639 array( 'rev_page' => $id ),
642 $result['authors'] = $authors;
644 // "Recent" threshold defined by $wgRCMaxAge
645 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
647 // Recent number of edits
648 $edits = (int) $dbr->selectField(
653 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
657 $result['recent_edits'] = $edits;
659 // Recent number of distinct authors
660 $authors = (int) $dbr->selectField(
662 'COUNT(DISTINCT rev_user_text)',
665 "rev_timestamp >= " . $dbr->addQuotes( $threshold )
669 $result['recent_authors'] = $authors;
671 // Subpages (if enabled)
672 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
673 $conds = array( 'page_namespace' => $title->getNamespace() );
674 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
676 // Subpages of this page (redirects)
677 $conds['page_is_redirect'] = 1;
678 $result['subpages']['redirects'] = (int) $dbr->selectField(
684 // Subpages of this page (non-redirects)
685 $conds['page_is_redirect'] = 0;
686 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
693 // Subpages of this page (total)
694 $result['subpages']['total'] = $result['subpages']['redirects']
695 +
$result['subpages']['nonredirects'];
698 // Counts for the number of transclusion links (to/from)
699 $result['transclusion']['to'] = (int) $dbr->selectField(
703 'tl_namespace' => $title->getNamespace(),
704 'tl_title' => $title->getDBkey()
709 $result['transclusion']['from'] = (int) $dbr->selectField(
712 array( 'tl_from' => $title->getArticleID() ),
716 wfProfileOut( __METHOD__
);
721 * Returns the name that goes in the "<h1>" page title.
725 protected function getPageTitle() {
726 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
730 * Get a list of contributors of $article
731 * @return string: html
733 protected function getContributors() {
734 global $wgHiddenPrefs;
736 $contributors = $this->page
->getContributors();
737 $real_names = array();
738 $user_names = array();
741 # Sift for real versus user names
742 foreach ( $contributors as $user ) {
743 $page = $user->isAnon()
744 ? SpecialPage
::getTitleFor( 'Contributions', $user->getName() )
745 : $user->getUserPage();
747 if ( $user->getID() == 0 ) {
748 $anon_ips[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
749 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
750 $real_names[] = Linker
::link( $page, htmlspecialchars( $user->getRealName() ) );
752 $user_names[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
756 $lang = $this->getLanguage();
758 $real = $lang->listToText( $real_names );
760 # "ThisSite user(s) A, B and C"
761 if ( count( $user_names ) ) {
762 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
763 count( $user_names ) )->escaped();
768 if ( count( $anon_ips ) ) {
769 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
770 count( $anon_ips ) )->escaped();
775 # This is the big list, all mooshed together. We sift for blank strings
777 foreach ( array( $real, $user, $anon ) as $s ) {
779 array_push( $fulllist, $s );
783 $count = count( $fulllist );
784 # "Based on work by ..."
786 ?
$this->msg( 'othercontribs' )->rawParams(
787 $lang->listToText( $fulllist ) )->params( $count )->escaped()
792 * Returns the description that goes below the "<h1>" tag.
796 protected function getDescription() {