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 if ( !$this->msg( 'pageinfo-header' )->isDisabled() ) {
63 $content .= $this->msg( 'pageinfo-header' )->parse();
66 // Hide "This page is a member of # hidden categories" explanation
67 $content .= Html
::element( 'style', array(),
68 '.mw-hiddenCategoriesExplanation { display: none; }' );
70 // Hide "Templates used on this page" explanation
71 $content .= Html
::element( 'style', array(),
72 '.mw-templatesUsedExplanation { display: none; }' );
74 // Get page information
75 $title = $this->getTitle();
76 $pageInfo = $this->pageInfo( $title );
78 // Allow extensions to add additional information
79 wfRunHooks( 'InfoAction', array( &$pageInfo ) );
81 // Render page information
82 foreach ( $pageInfo as $header => $infoTable ) {
83 $content .= $this->makeHeader( $this->msg( "pageinfo-${header}" )->escaped() );
85 foreach ( $infoTable as $infoRow ) {
86 $name = ( $infoRow[0] instanceof Message
) ?
$infoRow[0]->escaped() : $infoRow[0];
87 $value = ( $infoRow[1] instanceof Message
) ?
$infoRow[1]->escaped() : $infoRow[1];
88 $table = $this->addRow( $table, $name, $value );
90 $content = $this->addTable( $content, $table );
94 if ( !$this->msg( 'pageinfo-footer' )->isDisabled() ) {
95 $content .= $this->msg( 'pageinfo-footer' )->parse();
99 if ( $title->exists() ) {
100 $content .= Html
::rawElement( 'div', array( 'id' => 'mw-credits' ), $this->getContributors() );
107 * Creates a header that can be added to the output.
109 * @param $header The header text.
110 * @return string The HTML.
112 protected function makeHeader( $header ) {
114 $spanAttribs = array( 'class' => 'mw-headline', 'id' => $wgParser->guessSectionNameFromWikiText( $header ) );
115 return Html
::rawElement( 'h2', array(), Html
::element( 'span', $spanAttribs, $header ) );
119 * Adds a row to a table that will be added to the content.
121 * @param $table string The table that will be added to the content
122 * @param $name string The name of the row
123 * @param $value string The value of the row
124 * @return string The table with the row added
126 protected function addRow( $table, $name, $value ) {
127 return $table . Html
::rawElement( 'tr', array(),
128 Html
::rawElement( 'td', array( 'style' => 'vertical-align: top;' ), $name ) .
129 Html
::rawElement( 'td', array(), $value )
134 * Adds a table to the content that will be added to the output.
136 * @param $content string The content that will be added to the output
137 * @param $table string The table
138 * @return string The content with the table added
140 protected function addTable( $content, $table ) {
141 return $content . Html
::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
146 * Returns page information in an easily-manipulated format. Array keys are used so extensions
147 * may add additional information in arbitrary positions. Array values are arrays with one
148 * element to be rendered as a header, arrays with two elements to be rendered as a table row.
150 * @param $title Title object
152 protected function pageInfo( $title ) {
153 global $wgContLang, $wgDisableCounters, $wgRCMaxAge;
155 $user = $this->getUser();
156 $lang = $this->getLanguage();
157 $title = $this->getTitle();
158 $id = $title->getArticleID();
160 // Get page information that would be too "expensive" to retrieve by normal means
161 $userCanViewUnwatchedPages = $user->isAllowed( 'unwatchedpages' );
162 $pageCounts = self
::pageCounts( $title, $userCanViewUnwatchedPages, $wgDisableCounters );
164 // Get page properties
165 $dbr = wfGetDB( DB_SLAVE
);
166 $result = $dbr->select(
168 array( 'pp_propname', 'pp_value' ),
169 array( 'pp_page' => $id ),
173 $pageProperties = array();
174 foreach ( $result as $row ) {
175 $pageProperties[$row->pp_propname
] = $row->pp_value
;
180 $pageInfo['header-basic'] = array();
183 $displayTitle = $title->getPrefixedText();
184 if ( !empty( $pageProperties['displaytitle'] ) ) {
185 $displayTitle = $pageProperties['displaytitle'];
188 $pageInfo['header-basic'][] = array(
189 $this->msg( 'pageinfo-display-title' ), $displayTitle
193 $sortKey = $title->getCategorySortKey();
194 if ( !empty( $pageProperties['defaultsort'] ) ) {
195 $sortKey = $pageProperties['defaultsort'];
198 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-default-sort' ), $sortKey );
200 // Page length (in bytes)
201 $pageInfo['header-basic'][] = array(
202 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
205 // Page ID (number not localised, as it's a database ID)
206 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-article-id' ), $id );
208 // Search engine status
209 $pOutput = new ParserOutput();
210 if ( isset( $pageProperties['noindex'] ) ) {
211 $pOutput->setIndexPolicy( 'noindex' );
214 // Use robot policy logic
215 $policy = $this->page
->getRobotPolicy( 'view', $pOutput );
216 $pageInfo['header-basic'][] = array(
217 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
220 if ( !$wgDisableCounters ) {
222 $pageInfo['header-basic'][] = array(
223 $this->msg( 'pageinfo-views' ), $lang->formatNum( $pageCounts['views'] )
227 if ( $userCanViewUnwatchedPages ) {
228 // Number of page watchers
229 $pageInfo['header-basic'][] = array(
230 $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
234 // Redirects to this page
235 $whatLinksHere = SpecialPage
::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
236 $pageInfo['header-basic'][] = array(
239 $this->msg( 'pageinfo-redirects-name' )->escaped(),
241 array( 'hidelinks' => 1, 'hidetrans' => 1 )
243 $this->msg( 'pageinfo-redirects-value' )
244 ->numParams( count( $title->getRedirectsHere() ) )
247 // Subpages of this page, if subpages are enabled for the current NS
248 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
249 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
250 $pageInfo['header-basic'][] = array(
251 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
252 $this->msg( 'pageinfo-subpages-value' )
254 $pageCounts['subpages']['total'],
255 $pageCounts['subpages']['redirects'],
256 $pageCounts['subpages']['nonredirects'] )
261 $pageInfo['header-restrictions'] = array();
264 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
265 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
267 if ( $protectionLevel == '' ) {
269 $message = $this->msg( 'protect-default' )->escaped();
271 // Administrators only
272 $message = $this->msg( "protect-level-$protectionLevel" );
273 if ( $message->isDisabled() ) {
274 // Require "$1" permission
275 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
277 $message = $message->escaped();
281 $pageInfo['header-restrictions'][] = array(
282 $this->msg( "restriction-$restrictionType" ), $message
287 $pageInfo['header-edits'] = array();
289 $firstRev = $this->page
->getOldestRevision();
292 $pageInfo['header-edits'][] = array(
293 $this->msg( 'pageinfo-firstuser' ),
295 $firstRev->getUser( Revision
::FOR_THIS_USER
, $user ),
296 $firstRev->getUserText( Revision
::FOR_THIS_USER
, $user )
300 // Date of page creation
301 $pageInfo['header-edits'][] = array(
302 $this->msg( 'pageinfo-firsttime' ),
305 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
307 array( 'oldid' => $firstRev->getId() )
312 $pageInfo['header-edits'][] = array(
313 $this->msg( 'pageinfo-lastuser' ),
315 $this->page
->getUser( Revision
::FOR_THIS_USER
, $user ),
316 $this->page
->getUserText( Revision
::FOR_THIS_USER
, $user )
320 // Date of latest edit
321 $pageInfo['header-edits'][] = array(
322 $this->msg( 'pageinfo-lasttime' ),
325 $lang->userTimeAndDate( $this->page
->getTimestamp(), $user ),
327 array( 'oldid' => $this->page
->getLatest() )
331 // Total number of edits
332 $pageInfo['header-edits'][] = array(
333 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
336 // Total number of distinct authors
337 $pageInfo['header-edits'][] = array(
338 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
341 // Recent number of edits (within past 30 days)
342 $pageInfo['header-edits'][] = array(
343 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
344 $lang->formatNum( $pageCounts['recent_edits'] )
347 // Recent number of distinct authors
348 $pageInfo['header-edits'][] = array(
349 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
352 // Array of MagicWord objects
353 $magicWords = MagicWord
::getDoubleUnderscoreArray();
355 // Array of magic word IDs
356 $wordIDs = $magicWords->names
;
358 // Array of IDs => localized magic words
359 $localizedWords = $wgContLang->getMagicWords();
361 $listItems = array();
362 foreach ( $pageProperties as $property => $value ) {
363 if ( in_array( $property, $wordIDs ) ) {
364 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
368 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
369 $hiddenCategories = $this->page
->getHiddenCategories();
370 $transcludedTemplates = $title->getTemplateLinksFrom();
372 if ( count( $listItems ) > 0
373 ||
count( $hiddenCategories ) > 0
374 ||
count( $transcludedTemplates ) > 0 ) {
376 $pageInfo['header-properties'] = array();
379 if ( count( $listItems ) > 0 ) {
380 $pageInfo['header-properties'][] = array(
381 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
387 if ( count( $hiddenCategories ) > 0 ) {
388 $pageInfo['header-properties'][] = array(
389 $this->msg( 'pageinfo-hidden-categories' )
390 ->numParams( count( $hiddenCategories ) ),
391 Linker
::formatHiddenCategories( $hiddenCategories )
395 // Transcluded templates
396 if ( count( $transcludedTemplates ) > 0 ) {
397 $pageInfo['header-properties'][] = array(
398 $this->msg( 'pageinfo-templates' )
399 ->numParams( count( $transcludedTemplates ) ),
400 Linker
::formatTemplates( $transcludedTemplates )
409 * Returns page counts that would be too "expensive" to retrieve by normal means.
411 * @param $title Title object
412 * @param $canViewUnwatched bool
413 * @param $disableCounter bool
416 protected static function pageCounts( $title, $canViewUnwatched, $disableCounter ) {
419 wfProfileIn( __METHOD__
);
420 $id = $title->getArticleID();
422 $dbr = wfGetDB( DB_SLAVE
);
425 if ( !$disableCounter ) {
427 $views = (int) $dbr->selectField(
430 array( 'page_id' => $id ),
433 $result['views'] = $views;
436 if ( $canViewUnwatched ) {
437 // Number of page watchers
438 $watchers = (int) $dbr->selectField(
442 'wl_namespace' => $title->getNamespace(),
443 'wl_title' => $title->getDBkey(),
447 $result['watchers'] = $watchers;
450 // Total number of edits
451 $edits = (int) $dbr->selectField(
454 array( 'rev_page' => $id ),
457 $result['edits'] = $edits;
459 // Total number of distinct authors
460 $authors = (int) $dbr->selectField(
462 'COUNT(DISTINCT rev_user_text)',
463 array( 'rev_page' => $id ),
466 $result['authors'] = $authors;
468 // "Recent" threshold defined by $wgRCMaxAge
469 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
471 // Recent number of edits
472 $edits = (int) $dbr->selectField(
477 "rev_timestamp >= $threshold"
481 $result['recent_edits'] = $edits;
483 // Recent number of distinct authors
484 $authors = (int) $dbr->selectField(
486 'COUNT(DISTINCT rev_user_text)',
489 "rev_timestamp >= $threshold"
493 $result['recent_authors'] = $authors;
495 // Subpages (if enabled)
496 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
497 $conds = array( 'page_namespace' => $title->getNamespace() );
498 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
500 // Subpages of this page (redirects)
501 $conds['page_is_redirect'] = 1;
502 $result['subpages']['redirects'] = (int) $dbr->selectField(
508 // Subpages of this page (non-redirects)
509 $conds['page_is_redirect'] = 0;
510 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
517 // Subpages of this page (total)
518 $result['subpages']['total'] = $result['subpages']['redirects']
519 +
$result['subpages']['nonredirects'];
522 wfProfileOut( __METHOD__
);
527 * Returns the name that goes in the <h1> page title.
531 protected function getPageTitle() {
532 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
536 * Get a list of contributors of $article
537 * @return string: html
539 protected function getContributors() {
540 global $wgHiddenPrefs;
542 $contributors = $this->page
->getContributors();
543 $real_names = array();
544 $user_names = array();
547 # Sift for real versus user names
548 foreach ( $contributors as $user ) {
549 $page = $user->isAnon()
550 ? SpecialPage
::getTitleFor( 'Contributions', $user->getName() )
551 : $user->getUserPage();
553 if ( $user->getID() == 0 ) {
554 $anon_ips[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
555 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
556 $real_names[] = Linker
::link( $page, htmlspecialchars( $user->getRealName() ) );
558 $user_names[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
562 $lang = $this->getLanguage();
564 $real = $lang->listToText( $real_names );
566 # "ThisSite user(s) A, B and C"
567 if ( count( $user_names ) ) {
568 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
569 count( $user_names ) )->escaped();
574 if ( count( $anon_ips ) ) {
575 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
576 count( $anon_ips ) )->escaped();
581 # This is the big list, all mooshed together. We sift for blank strings
583 foreach ( array( $real, $user, $anon ) as $s ) {
585 array_push( $fulllist, $s );
589 $count = count( $fulllist );
590 # "Based on work by ..."
592 ?
$this->msg( 'othercontribs' )->rawParams(
593 $lang->listToText( $fulllist ) )->params( $count )->escaped()
598 * Returns the description that goes below the <h1> tag.
602 protected function getDescription() {