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
192 // Is it a redirect? If so, where to?
193 if ( $title->isRedirect() ) {
194 $pageInfo['header-basic'][] = array(
195 $this->msg( 'pageinfo-redirectsto' ),
196 Linker
::link( $this->page
->getRedirectTarget() ) . ' ' .
197 $this->msg( 'parentheses', Linker
::link(
198 $this->page
->getRedirectTarget(),
199 $this->msg( 'pageinfo-redirectsto-info' ),
201 array( 'action' => 'info' )
207 $sortKey = $title->getCategorySortKey();
208 if ( !empty( $pageProperties['defaultsort'] ) ) {
209 $sortKey = $pageProperties['defaultsort'];
212 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-default-sort' ), $sortKey );
214 // Page length (in bytes)
215 $pageInfo['header-basic'][] = array(
216 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
219 // Page ID (number not localised, as it's a database ID)
220 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-article-id' ), $id );
222 // Search engine status
223 $pOutput = new ParserOutput();
224 if ( isset( $pageProperties['noindex'] ) ) {
225 $pOutput->setIndexPolicy( 'noindex' );
228 // Use robot policy logic
229 $policy = $this->page
->getRobotPolicy( 'view', $pOutput );
230 $pageInfo['header-basic'][] = array(
231 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
234 if ( !$wgDisableCounters ) {
236 $pageInfo['header-basic'][] = array(
237 $this->msg( 'pageinfo-views' ), $lang->formatNum( $pageCounts['views'] )
241 if ( $userCanViewUnwatchedPages ) {
242 // Number of page watchers
243 $pageInfo['header-basic'][] = array(
244 $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
248 // Redirects to this page
249 $whatLinksHere = SpecialPage
::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
250 $pageInfo['header-basic'][] = array(
253 $this->msg( 'pageinfo-redirects-name' )->escaped(),
255 array( 'hidelinks' => 1, 'hidetrans' => 1 )
257 $this->msg( 'pageinfo-redirects-value' )
258 ->numParams( count( $title->getRedirectsHere() ) )
261 // Is it counted as a content page?
262 if ( $this->page
->isCountable() ) {
263 $pageInfo['header-basic'][] = array(
264 $this->msg( 'pageinfo-contentpage' ),
265 $this->msg( 'pageinfo-contentpage-yes' )
269 // Subpages of this page, if subpages are enabled for the current NS
270 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
271 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
272 $pageInfo['header-basic'][] = array(
273 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
274 $this->msg( 'pageinfo-subpages-value' )
276 $pageCounts['subpages']['total'],
277 $pageCounts['subpages']['redirects'],
278 $pageCounts['subpages']['nonredirects'] )
283 $pageInfo['header-restrictions'] = array();
286 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
287 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
289 if ( $protectionLevel == '' ) {
291 $message = $this->msg( 'protect-default' )->escaped();
293 // Administrators only
294 $message = $this->msg( "protect-level-$protectionLevel" );
295 if ( $message->isDisabled() ) {
296 // Require "$1" permission
297 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
299 $message = $message->escaped();
303 $pageInfo['header-restrictions'][] = array(
304 $this->msg( "restriction-$restrictionType" ), $message
309 $pageInfo['header-edits'] = array();
311 $firstRev = $this->page
->getOldestRevision();
314 $pageInfo['header-edits'][] = array(
315 $this->msg( 'pageinfo-firstuser' ),
316 Linker
::revUserTools( $firstRev )
319 // Date of page creation
320 $pageInfo['header-edits'][] = array(
321 $this->msg( 'pageinfo-firsttime' ),
324 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
326 array( 'oldid' => $firstRev->getId() )
331 $pageInfo['header-edits'][] = array(
332 $this->msg( 'pageinfo-lastuser' ),
333 Linker
::revUserTools( $this->page
->getRevision() )
336 // Date of latest edit
337 $pageInfo['header-edits'][] = array(
338 $this->msg( 'pageinfo-lasttime' ),
341 $lang->userTimeAndDate( $this->page
->getTimestamp(), $user ),
343 array( 'oldid' => $this->page
->getLatest() )
347 // Total number of edits
348 $pageInfo['header-edits'][] = array(
349 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
352 // Total number of distinct authors
353 $pageInfo['header-edits'][] = array(
354 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
357 // Recent number of edits (within past 30 days)
358 $pageInfo['header-edits'][] = array(
359 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
360 $lang->formatNum( $pageCounts['recent_edits'] )
363 // Recent number of distinct authors
364 $pageInfo['header-edits'][] = array(
365 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
368 // Array of MagicWord objects
369 $magicWords = MagicWord
::getDoubleUnderscoreArray();
371 // Array of magic word IDs
372 $wordIDs = $magicWords->names
;
374 // Array of IDs => localized magic words
375 $localizedWords = $wgContLang->getMagicWords();
377 $listItems = array();
378 foreach ( $pageProperties as $property => $value ) {
379 if ( in_array( $property, $wordIDs ) ) {
380 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
384 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
385 $hiddenCategories = $this->page
->getHiddenCategories();
386 $transcludedTemplates = $title->getTemplateLinksFrom();
388 if ( count( $listItems ) > 0
389 ||
count( $hiddenCategories ) > 0
390 ||
count( $transcludedTemplates ) > 0 ) {
392 $pageInfo['header-properties'] = array();
395 if ( count( $listItems ) > 0 ) {
396 $pageInfo['header-properties'][] = array(
397 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
403 if ( count( $hiddenCategories ) > 0 ) {
404 $pageInfo['header-properties'][] = array(
405 $this->msg( 'pageinfo-hidden-categories' )
406 ->numParams( count( $hiddenCategories ) ),
407 Linker
::formatHiddenCategories( $hiddenCategories )
411 // Transcluded templates
412 if ( count( $transcludedTemplates ) > 0 ) {
413 $pageInfo['header-properties'][] = array(
414 $this->msg( 'pageinfo-templates' )
415 ->numParams( count( $transcludedTemplates ) ),
416 Linker
::formatTemplates( $transcludedTemplates )
425 * Returns page counts that would be too "expensive" to retrieve by normal means.
427 * @param $title Title object
428 * @param $canViewUnwatched bool
429 * @param $disableCounter bool
432 protected static function pageCounts( $title, $canViewUnwatched, $disableCounter ) {
435 wfProfileIn( __METHOD__
);
436 $id = $title->getArticleID();
438 $dbr = wfGetDB( DB_SLAVE
);
441 if ( !$disableCounter ) {
443 $views = (int) $dbr->selectField(
446 array( 'page_id' => $id ),
449 $result['views'] = $views;
452 if ( $canViewUnwatched ) {
453 // Number of page watchers
454 $watchers = (int) $dbr->selectField(
458 'wl_namespace' => $title->getNamespace(),
459 'wl_title' => $title->getDBkey(),
463 $result['watchers'] = $watchers;
466 // Total number of edits
467 $edits = (int) $dbr->selectField(
470 array( 'rev_page' => $id ),
473 $result['edits'] = $edits;
475 // Total number of distinct authors
476 $authors = (int) $dbr->selectField(
478 'COUNT(DISTINCT rev_user_text)',
479 array( 'rev_page' => $id ),
482 $result['authors'] = $authors;
484 // "Recent" threshold defined by $wgRCMaxAge
485 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
487 // Recent number of edits
488 $edits = (int) $dbr->selectField(
493 "rev_timestamp >= $threshold"
497 $result['recent_edits'] = $edits;
499 // Recent number of distinct authors
500 $authors = (int) $dbr->selectField(
502 'COUNT(DISTINCT rev_user_text)',
505 "rev_timestamp >= $threshold"
509 $result['recent_authors'] = $authors;
511 // Subpages (if enabled)
512 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
513 $conds = array( 'page_namespace' => $title->getNamespace() );
514 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
516 // Subpages of this page (redirects)
517 $conds['page_is_redirect'] = 1;
518 $result['subpages']['redirects'] = (int) $dbr->selectField(
524 // Subpages of this page (non-redirects)
525 $conds['page_is_redirect'] = 0;
526 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
533 // Subpages of this page (total)
534 $result['subpages']['total'] = $result['subpages']['redirects']
535 +
$result['subpages']['nonredirects'];
538 wfProfileOut( __METHOD__
);
543 * Returns the name that goes in the <h1> page title.
547 protected function getPageTitle() {
548 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
552 * Get a list of contributors of $article
553 * @return string: html
555 protected function getContributors() {
556 global $wgHiddenPrefs;
558 $contributors = $this->page
->getContributors();
559 $real_names = array();
560 $user_names = array();
563 # Sift for real versus user names
564 foreach ( $contributors as $user ) {
565 $page = $user->isAnon()
566 ? SpecialPage
::getTitleFor( 'Contributions', $user->getName() )
567 : $user->getUserPage();
569 if ( $user->getID() == 0 ) {
570 $anon_ips[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
571 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
572 $real_names[] = Linker
::link( $page, htmlspecialchars( $user->getRealName() ) );
574 $user_names[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
578 $lang = $this->getLanguage();
580 $real = $lang->listToText( $real_names );
582 # "ThisSite user(s) A, B and C"
583 if ( count( $user_names ) ) {
584 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
585 count( $user_names ) )->escaped();
590 if ( count( $anon_ips ) ) {
591 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
592 count( $anon_ips ) )->escaped();
597 # This is the big list, all mooshed together. We sift for blank strings
599 foreach ( array( $real, $user, $anon ) as $s ) {
601 array_push( $fulllist, $s );
605 $count = count( $fulllist );
606 # "Based on work by ..."
608 ?
$this->msg( 'othercontribs' )->rawParams(
609 $lang->listToText( $fulllist ) )->params( $count )->escaped()
614 * Returns the description that goes below the <h1> tag.
618 protected function getDescription() {