e09cd205684a8cb0b7e13938368f6ef3ed5fecff
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' ),
294 Linker
::revUserTools( $firstRev )
297 // Date of page creation
298 $pageInfo['header-edits'][] = array(
299 $this->msg( 'pageinfo-firsttime' ),
302 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
304 array( 'oldid' => $firstRev->getId() )
309 $pageInfo['header-edits'][] = array(
310 $this->msg( 'pageinfo-lastuser' ),
311 Linker
::revUserTools( $this->page
->getRevision() )
314 // Date of latest edit
315 $pageInfo['header-edits'][] = array(
316 $this->msg( 'pageinfo-lasttime' ),
319 $lang->userTimeAndDate( $this->page
->getTimestamp(), $user ),
321 array( 'oldid' => $this->page
->getLatest() )
325 // Total number of edits
326 $pageInfo['header-edits'][] = array(
327 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
330 // Total number of distinct authors
331 $pageInfo['header-edits'][] = array(
332 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
335 // Recent number of edits (within past 30 days)
336 $pageInfo['header-edits'][] = array(
337 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
338 $lang->formatNum( $pageCounts['recent_edits'] )
341 // Recent number of distinct authors
342 $pageInfo['header-edits'][] = array(
343 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
346 // Array of MagicWord objects
347 $magicWords = MagicWord
::getDoubleUnderscoreArray();
349 // Array of magic word IDs
350 $wordIDs = $magicWords->names
;
352 // Array of IDs => localized magic words
353 $localizedWords = $wgContLang->getMagicWords();
355 $listItems = array();
356 foreach ( $pageProperties as $property => $value ) {
357 if ( in_array( $property, $wordIDs ) ) {
358 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
362 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
363 $hiddenCategories = $this->page
->getHiddenCategories();
364 $transcludedTemplates = $title->getTemplateLinksFrom();
366 if ( count( $listItems ) > 0
367 ||
count( $hiddenCategories ) > 0
368 ||
count( $transcludedTemplates ) > 0 ) {
370 $pageInfo['header-properties'] = array();
373 if ( count( $listItems ) > 0 ) {
374 $pageInfo['header-properties'][] = array(
375 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
381 if ( count( $hiddenCategories ) > 0 ) {
382 $pageInfo['header-properties'][] = array(
383 $this->msg( 'pageinfo-hidden-categories' )
384 ->numParams( count( $hiddenCategories ) ),
385 Linker
::formatHiddenCategories( $hiddenCategories )
389 // Transcluded templates
390 if ( count( $transcludedTemplates ) > 0 ) {
391 $pageInfo['header-properties'][] = array(
392 $this->msg( 'pageinfo-templates' )
393 ->numParams( count( $transcludedTemplates ) ),
394 Linker
::formatTemplates( $transcludedTemplates )
403 * Returns page counts that would be too "expensive" to retrieve by normal means.
405 * @param $title Title object
406 * @param $canViewUnwatched bool
407 * @param $disableCounter bool
410 protected static function pageCounts( $title, $canViewUnwatched, $disableCounter ) {
413 wfProfileIn( __METHOD__
);
414 $id = $title->getArticleID();
416 $dbr = wfGetDB( DB_SLAVE
);
419 if ( !$disableCounter ) {
421 $views = (int) $dbr->selectField(
424 array( 'page_id' => $id ),
427 $result['views'] = $views;
430 if ( $canViewUnwatched ) {
431 // Number of page watchers
432 $watchers = (int) $dbr->selectField(
436 'wl_namespace' => $title->getNamespace(),
437 'wl_title' => $title->getDBkey(),
441 $result['watchers'] = $watchers;
444 // Total number of edits
445 $edits = (int) $dbr->selectField(
448 array( 'rev_page' => $id ),
451 $result['edits'] = $edits;
453 // Total number of distinct authors
454 $authors = (int) $dbr->selectField(
456 'COUNT(DISTINCT rev_user_text)',
457 array( 'rev_page' => $id ),
460 $result['authors'] = $authors;
462 // "Recent" threshold defined by $wgRCMaxAge
463 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
465 // Recent number of edits
466 $edits = (int) $dbr->selectField(
471 "rev_timestamp >= $threshold"
475 $result['recent_edits'] = $edits;
477 // Recent number of distinct authors
478 $authors = (int) $dbr->selectField(
480 'COUNT(DISTINCT rev_user_text)',
483 "rev_timestamp >= $threshold"
487 $result['recent_authors'] = $authors;
489 // Subpages (if enabled)
490 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
491 $conds = array( 'page_namespace' => $title->getNamespace() );
492 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
494 // Subpages of this page (redirects)
495 $conds['page_is_redirect'] = 1;
496 $result['subpages']['redirects'] = (int) $dbr->selectField(
502 // Subpages of this page (non-redirects)
503 $conds['page_is_redirect'] = 0;
504 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
511 // Subpages of this page (total)
512 $result['subpages']['total'] = $result['subpages']['redirects']
513 +
$result['subpages']['nonredirects'];
516 wfProfileOut( __METHOD__
);
521 * Returns the name that goes in the <h1> page title.
525 protected function getPageTitle() {
526 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
530 * Get a list of contributors of $article
531 * @return string: html
533 protected function getContributors() {
534 global $wgHiddenPrefs;
536 $contributors = $this->page
->getContributors();
537 $real_names = array();
538 $user_names = array();
541 # Sift for real versus user names
542 foreach ( $contributors as $user ) {
543 $page = $user->isAnon()
544 ? SpecialPage
::getTitleFor( 'Contributions', $user->getName() )
545 : $user->getUserPage();
547 if ( $user->getID() == 0 ) {
548 $anon_ips[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
549 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
550 $real_names[] = Linker
::link( $page, htmlspecialchars( $user->getRealName() ) );
552 $user_names[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
556 $lang = $this->getLanguage();
558 $real = $lang->listToText( $real_names );
560 # "ThisSite user(s) A, B and C"
561 if ( count( $user_names ) ) {
562 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
563 count( $user_names ) )->escaped();
568 if ( count( $anon_ips ) ) {
569 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
570 count( $anon_ips ) )->escaped();
575 # This is the big list, all mooshed together. We sift for blank strings
577 foreach ( array( $real, $user, $anon ) as $s ) {
579 array_push( $fulllist, $s );
583 $count = count( $fulllist );
584 # "Based on work by ..."
586 ?
$this->msg( 'othercontribs' )->rawParams(
587 $lang->listToText( $fulllist ) )->params( $count )->escaped()
592 * Returns the description that goes below the <h1> tag.
596 protected function getDescription() {