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 ) {
129 $spanAttribs = array( 'class' => 'mw-headline', 'id' => $wgParser->guessSectionNameFromWikiText( $header ) );
130 return Html
::rawElement( 'h2', array(), Html
::element( 'span', $spanAttribs, $header ) );
134 * Adds a row to a table that will be added to the content.
136 * @param $table string The table that will be added to the content
137 * @param $name string The name of the row
138 * @param $value string The value of the row
139 * @return string The table with the row added
141 protected function addRow( $table, $name, $value ) {
142 return $table . Html
::rawElement( 'tr', array(),
143 Html
::rawElement( 'td', array( 'style' => 'vertical-align: top;' ), $name ) .
144 Html
::rawElement( 'td', array(), $value )
149 * Adds a table to the content that will be added to the output.
151 * @param $content string The content that will be added to the output
152 * @param $table string The table
153 * @return string The content with the table added
155 protected function addTable( $content, $table ) {
156 return $content . Html
::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
161 * Returns page information in an easily-manipulated format. Array keys are used so extensions
162 * may add additional information in arbitrary positions. Array values are arrays with one
163 * element to be rendered as a header, arrays with two elements to be rendered as a table row.
167 protected function pageInfo() {
168 global $wgContLang, $wgRCMaxAge;
170 $user = $this->getUser();
171 $lang = $this->getLanguage();
172 $title = $this->getTitle();
173 $id = $title->getArticleID();
175 // Get page information that would be too "expensive" to retrieve by normal means
176 $pageCounts = self
::pageCounts( $title, $user );
178 // Get page properties
179 $dbr = wfGetDB( DB_SLAVE
);
180 $result = $dbr->select(
182 array( 'pp_propname', 'pp_value' ),
183 array( 'pp_page' => $id ),
187 $pageProperties = array();
188 foreach ( $result as $row ) {
189 $pageProperties[$row->pp_propname
] = $row->pp_value
;
194 $pageInfo['header-basic'] = array();
197 $displayTitle = $title->getPrefixedText();
198 if ( !empty( $pageProperties['displaytitle'] ) ) {
199 $displayTitle = $pageProperties['displaytitle'];
202 $pageInfo['header-basic'][] = array(
203 $this->msg( 'pageinfo-display-title' ), $displayTitle
206 // Is it a redirect? If so, where to?
207 if ( $title->isRedirect() ) {
208 $pageInfo['header-basic'][] = array(
209 $this->msg( 'pageinfo-redirectsto' ),
210 Linker
::link( $this->page
->getRedirectTarget() ) .
211 $this->msg( 'word-separator' )->text() .
212 $this->msg( 'parentheses', Linker
::link(
213 $this->page
->getRedirectTarget(),
214 $this->msg( 'pageinfo-redirectsto-info' )->escaped(),
216 array( 'action' => 'info' )
222 $sortKey = $title->getCategorySortKey();
223 if ( !empty( $pageProperties['defaultsort'] ) ) {
224 $sortKey = $pageProperties['defaultsort'];
227 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-default-sort' ), $sortKey );
229 // Page length (in bytes)
230 $pageInfo['header-basic'][] = array(
231 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
234 // Page ID (number not localised, as it's a database ID)
235 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-article-id' ), $id );
237 // Search engine status
238 $pOutput = new ParserOutput();
239 if ( isset( $pageProperties['noindex'] ) ) {
240 $pOutput->setIndexPolicy( 'noindex' );
243 // Use robot policy logic
244 $policy = $this->page
->getRobotPolicy( 'view', $pOutput );
245 $pageInfo['header-basic'][] = array(
246 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
249 if ( isset( $pageCounts['views'] ) ) {
251 $pageInfo['header-basic'][] = array(
252 $this->msg( 'pageinfo-views' ), $lang->formatNum( $pageCounts['views'] )
256 if ( isset( $pageCounts['watchers'] ) ) {
257 // Number of page watchers
258 $pageInfo['header-basic'][] = array(
259 $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
263 // Redirects to this page
264 $whatLinksHere = SpecialPage
::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
265 $pageInfo['header-basic'][] = array(
268 $this->msg( 'pageinfo-redirects-name' )->escaped(),
270 array( 'hidelinks' => 1, 'hidetrans' => 1 )
272 $this->msg( 'pageinfo-redirects-value' )
273 ->numParams( count( $title->getRedirectsHere() ) )
276 // Is it counted as a content page?
277 if ( $this->page
->isCountable() ) {
278 $pageInfo['header-basic'][] = array(
279 $this->msg( 'pageinfo-contentpage' ),
280 $this->msg( 'pageinfo-contentpage-yes' )
284 // Subpages of this page, if subpages are enabled for the current NS
285 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
286 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
287 $pageInfo['header-basic'][] = array(
288 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
289 $this->msg( 'pageinfo-subpages-value' )
291 $pageCounts['subpages']['total'],
292 $pageCounts['subpages']['redirects'],
293 $pageCounts['subpages']['nonredirects'] )
298 $pageInfo['header-restrictions'] = array();
300 // Is this page effected by the cascading protection of something which includes it?
301 if ( $title->isCascadeProtected() ) {
303 $sources = $title->getCascadeProtectionSources(); // Array deferencing is in PHP 5.4 :(
305 foreach ( $sources[0] as $sourceTitle ) {
306 $cascadingFrom .= Html
::rawElement( 'li', array(), Linker
::linkKnown( $sourceTitle ) );
309 $cascadingFrom = Html
::rawElement( 'ul', array(), $cascadingFrom );
310 $pageInfo['header-restrictions'][] = array(
311 $this->msg( 'pageinfo-protect-cascading-from' ),
316 // Is out protection set to cascade to other pages?
317 if ( $title->areRestrictionsCascading() ) {
318 $pageInfo['header-restrictions'][] = array(
319 $this->msg( 'pageinfo-protect-cascading' ),
320 $this->msg( 'pageinfo-protect-cascading-yes' )
325 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
326 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
328 if ( $protectionLevel == '' ) {
330 $message = $this->msg( 'protect-default' )->escaped();
332 // Administrators only
333 $message = $this->msg( "protect-level-$protectionLevel" );
334 if ( $message->isDisabled() ) {
335 // Require "$1" permission
336 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
338 $message = $message->escaped();
342 $pageInfo['header-restrictions'][] = array(
343 $this->msg( "restriction-$restrictionType" ), $message
347 if ( !$this->page
->exists() ) {
352 $pageInfo['header-edits'] = array();
354 $firstRev = $this->page
->getOldestRevision();
357 $pageInfo['header-edits'][] = array(
358 $this->msg( 'pageinfo-firstuser' ),
359 Linker
::revUserTools( $firstRev )
362 // Date of page creation
363 $pageInfo['header-edits'][] = array(
364 $this->msg( 'pageinfo-firsttime' ),
367 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
369 array( 'oldid' => $firstRev->getId() )
374 $pageInfo['header-edits'][] = array(
375 $this->msg( 'pageinfo-lastuser' ),
376 Linker
::revUserTools( $this->page
->getRevision() )
379 // Date of latest edit
380 $pageInfo['header-edits'][] = array(
381 $this->msg( 'pageinfo-lasttime' ),
384 $lang->userTimeAndDate( $this->page
->getTimestamp(), $user ),
386 array( 'oldid' => $this->page
->getLatest() )
390 // Total number of edits
391 $pageInfo['header-edits'][] = array(
392 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
395 // Total number of distinct authors
396 $pageInfo['header-edits'][] = array(
397 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
400 // Recent number of edits (within past 30 days)
401 $pageInfo['header-edits'][] = array(
402 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
403 $lang->formatNum( $pageCounts['recent_edits'] )
406 // Recent number of distinct authors
407 $pageInfo['header-edits'][] = array(
408 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
411 // Array of MagicWord objects
412 $magicWords = MagicWord
::getDoubleUnderscoreArray();
414 // Array of magic word IDs
415 $wordIDs = $magicWords->names
;
417 // Array of IDs => localized magic words
418 $localizedWords = $wgContLang->getMagicWords();
420 $listItems = array();
421 foreach ( $pageProperties as $property => $value ) {
422 if ( in_array( $property, $wordIDs ) ) {
423 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
427 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
428 $hiddenCategories = $this->page
->getHiddenCategories();
429 $transcludedTemplates = $title->getTemplateLinksFrom();
431 if ( count( $listItems ) > 0
432 ||
count( $hiddenCategories ) > 0
433 ||
count( $transcludedTemplates ) > 0 ) {
435 $pageInfo['header-properties'] = array();
438 if ( count( $listItems ) > 0 ) {
439 $pageInfo['header-properties'][] = array(
440 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
446 if ( count( $hiddenCategories ) > 0 ) {
447 $pageInfo['header-properties'][] = array(
448 $this->msg( 'pageinfo-hidden-categories' )
449 ->numParams( count( $hiddenCategories ) ),
450 Linker
::formatHiddenCategories( $hiddenCategories )
454 // Transcluded templates
455 if ( count( $transcludedTemplates ) > 0 ) {
456 $pageInfo['header-properties'][] = array(
457 $this->msg( 'pageinfo-templates' )
458 ->numParams( count( $transcludedTemplates ) ),
459 Linker
::formatTemplates( $transcludedTemplates )
468 * Returns page counts that would be too "expensive" to retrieve by normal means.
470 * @param $title Title object
471 * @param $user User object
474 protected static function pageCounts( $title, $user ) {
475 global $wgRCMaxAge, $wgDisableCounters;
477 wfProfileIn( __METHOD__
);
478 $id = $title->getArticleID();
480 $dbr = wfGetDB( DB_SLAVE
);
483 if ( !$wgDisableCounters ) {
485 $views = (int) $dbr->selectField(
488 array( 'page_id' => $id ),
491 $result['views'] = $views;
494 if ( $user->isAllowed( 'unwatchedpages' ) ) {
495 // Number of page watchers
496 $watchers = (int) $dbr->selectField(
500 'wl_namespace' => $title->getNamespace(),
501 'wl_title' => $title->getDBkey(),
505 $result['watchers'] = $watchers;
508 // Total number of edits
509 $edits = (int) $dbr->selectField(
512 array( 'rev_page' => $id ),
515 $result['edits'] = $edits;
517 // Total number of distinct authors
518 $authors = (int) $dbr->selectField(
520 'COUNT(DISTINCT rev_user_text)',
521 array( 'rev_page' => $id ),
524 $result['authors'] = $authors;
526 // "Recent" threshold defined by $wgRCMaxAge
527 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
529 // Recent number of edits
530 $edits = (int) $dbr->selectField(
535 "rev_timestamp >= $threshold"
539 $result['recent_edits'] = $edits;
541 // Recent number of distinct authors
542 $authors = (int) $dbr->selectField(
544 'COUNT(DISTINCT rev_user_text)',
547 "rev_timestamp >= $threshold"
551 $result['recent_authors'] = $authors;
553 // Subpages (if enabled)
554 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
555 $conds = array( 'page_namespace' => $title->getNamespace() );
556 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
558 // Subpages of this page (redirects)
559 $conds['page_is_redirect'] = 1;
560 $result['subpages']['redirects'] = (int) $dbr->selectField(
566 // Subpages of this page (non-redirects)
567 $conds['page_is_redirect'] = 0;
568 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
575 // Subpages of this page (total)
576 $result['subpages']['total'] = $result['subpages']['redirects']
577 +
$result['subpages']['nonredirects'];
580 wfProfileOut( __METHOD__
);
585 * Returns the name that goes in the "<h1>" page title.
589 protected function getPageTitle() {
590 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
594 * Get a list of contributors of $article
595 * @return string: html
597 protected function getContributors() {
598 global $wgHiddenPrefs;
600 $contributors = $this->page
->getContributors();
601 $real_names = array();
602 $user_names = array();
605 # Sift for real versus user names
606 foreach ( $contributors as $user ) {
607 $page = $user->isAnon()
608 ? SpecialPage
::getTitleFor( 'Contributions', $user->getName() )
609 : $user->getUserPage();
611 if ( $user->getID() == 0 ) {
612 $anon_ips[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
613 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
614 $real_names[] = Linker
::link( $page, htmlspecialchars( $user->getRealName() ) );
616 $user_names[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
620 $lang = $this->getLanguage();
622 $real = $lang->listToText( $real_names );
624 # "ThisSite user(s) A, B and C"
625 if ( count( $user_names ) ) {
626 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
627 count( $user_names ) )->escaped();
632 if ( count( $anon_ips ) ) {
633 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
634 count( $anon_ips ) )->escaped();
639 # This is the big list, all mooshed together. We sift for blank strings
641 foreach ( array( $real, $user, $anon ) as $s ) {
643 array_push( $fulllist, $s );
647 $count = count( $fulllist );
648 # "Based on work by ..."
650 ?
$this->msg( 'othercontribs' )->rawParams(
651 $lang->listToText( $fulllist ) )->params( $count )->escaped()
656 * Returns the description that goes below the "<h1>" tag.
660 protected function getDescription() {