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
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 ( isset( $pageCounts['views'] ) ) {
236 $pageInfo['header-basic'][] = array(
237 $this->msg( 'pageinfo-views' ), $lang->formatNum( $pageCounts['views'] )
241 if ( isset( $pageCounts['watchers'] ) ) {
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 // Subpages of this page, if subpages are enabled for the current NS
262 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
263 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
264 $pageInfo['header-basic'][] = array(
265 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
266 $this->msg( 'pageinfo-subpages-value' )
268 $pageCounts['subpages']['total'],
269 $pageCounts['subpages']['redirects'],
270 $pageCounts['subpages']['nonredirects'] )
275 $pageInfo['header-restrictions'] = array();
278 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
279 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
281 if ( $protectionLevel == '' ) {
283 $message = $this->msg( 'protect-default' )->escaped();
285 // Administrators only
286 $message = $this->msg( "protect-level-$protectionLevel" );
287 if ( $message->isDisabled() ) {
288 // Require "$1" permission
289 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
291 $message = $message->escaped();
295 $pageInfo['header-restrictions'][] = array(
296 $this->msg( "restriction-$restrictionType" ), $message
300 if ( !$this->page
->exists() ) {
305 $pageInfo['header-edits'] = array();
307 $firstRev = $this->page
->getOldestRevision();
310 $pageInfo['header-edits'][] = array(
311 $this->msg( 'pageinfo-firstuser' ),
312 Linker
::revUserTools( $firstRev )
315 // Date of page creation
316 $pageInfo['header-edits'][] = array(
317 $this->msg( 'pageinfo-firsttime' ),
320 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
322 array( 'oldid' => $firstRev->getId() )
327 $pageInfo['header-edits'][] = array(
328 $this->msg( 'pageinfo-lastuser' ),
329 Linker
::revUserTools( $this->page
->getRevision() )
332 // Date of latest edit
333 $pageInfo['header-edits'][] = array(
334 $this->msg( 'pageinfo-lasttime' ),
337 $lang->userTimeAndDate( $this->page
->getTimestamp(), $user ),
339 array( 'oldid' => $this->page
->getLatest() )
343 // Total number of edits
344 $pageInfo['header-edits'][] = array(
345 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
348 // Total number of distinct authors
349 $pageInfo['header-edits'][] = array(
350 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
353 // Recent number of edits (within past 30 days)
354 $pageInfo['header-edits'][] = array(
355 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
356 $lang->formatNum( $pageCounts['recent_edits'] )
359 // Recent number of distinct authors
360 $pageInfo['header-edits'][] = array(
361 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
364 // Array of MagicWord objects
365 $magicWords = MagicWord
::getDoubleUnderscoreArray();
367 // Array of magic word IDs
368 $wordIDs = $magicWords->names
;
370 // Array of IDs => localized magic words
371 $localizedWords = $wgContLang->getMagicWords();
373 $listItems = array();
374 foreach ( $pageProperties as $property => $value ) {
375 if ( in_array( $property, $wordIDs ) ) {
376 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
380 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
381 $hiddenCategories = $this->page
->getHiddenCategories();
382 $transcludedTemplates = $title->getTemplateLinksFrom();
384 if ( count( $listItems ) > 0
385 ||
count( $hiddenCategories ) > 0
386 ||
count( $transcludedTemplates ) > 0 ) {
388 $pageInfo['header-properties'] = array();
391 if ( count( $listItems ) > 0 ) {
392 $pageInfo['header-properties'][] = array(
393 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
399 if ( count( $hiddenCategories ) > 0 ) {
400 $pageInfo['header-properties'][] = array(
401 $this->msg( 'pageinfo-hidden-categories' )
402 ->numParams( count( $hiddenCategories ) ),
403 Linker
::formatHiddenCategories( $hiddenCategories )
407 // Transcluded templates
408 if ( count( $transcludedTemplates ) > 0 ) {
409 $pageInfo['header-properties'][] = array(
410 $this->msg( 'pageinfo-templates' )
411 ->numParams( count( $transcludedTemplates ) ),
412 Linker
::formatTemplates( $transcludedTemplates )
421 * Returns page counts that would be too "expensive" to retrieve by normal means.
423 * @param $title Title object
424 * @param $user User object
427 protected static function pageCounts( $title, $user ) {
428 global $wgRCMaxAge, $wgDisableCounters;
430 wfProfileIn( __METHOD__
);
431 $id = $title->getArticleID();
433 $dbr = wfGetDB( DB_SLAVE
);
436 if ( !$wgDisableCounters ) {
438 $views = (int) $dbr->selectField(
441 array( 'page_id' => $id ),
444 $result['views'] = $views;
447 if ( $user->isAllowed( 'unwatchedpages' ) ) {
448 // Number of page watchers
449 $watchers = (int) $dbr->selectField(
453 'wl_namespace' => $title->getNamespace(),
454 'wl_title' => $title->getDBkey(),
458 $result['watchers'] = $watchers;
461 // Total number of edits
462 $edits = (int) $dbr->selectField(
465 array( 'rev_page' => $id ),
468 $result['edits'] = $edits;
470 // Total number of distinct authors
471 $authors = (int) $dbr->selectField(
473 'COUNT(DISTINCT rev_user_text)',
474 array( 'rev_page' => $id ),
477 $result['authors'] = $authors;
479 // "Recent" threshold defined by $wgRCMaxAge
480 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
482 // Recent number of edits
483 $edits = (int) $dbr->selectField(
488 "rev_timestamp >= $threshold"
492 $result['recent_edits'] = $edits;
494 // Recent number of distinct authors
495 $authors = (int) $dbr->selectField(
497 'COUNT(DISTINCT rev_user_text)',
500 "rev_timestamp >= $threshold"
504 $result['recent_authors'] = $authors;
506 // Subpages (if enabled)
507 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
508 $conds = array( 'page_namespace' => $title->getNamespace() );
509 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
511 // Subpages of this page (redirects)
512 $conds['page_is_redirect'] = 1;
513 $result['subpages']['redirects'] = (int) $dbr->selectField(
519 // Subpages of this page (non-redirects)
520 $conds['page_is_redirect'] = 0;
521 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
528 // Subpages of this page (total)
529 $result['subpages']['total'] = $result['subpages']['redirects']
530 +
$result['subpages']['nonredirects'];
533 wfProfileOut( __METHOD__
);
538 * Returns the name that goes in the <h1> page title.
542 protected function getPageTitle() {
543 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
547 * Get a list of contributors of $article
548 * @return string: html
550 protected function getContributors() {
551 global $wgHiddenPrefs;
553 $contributors = $this->page
->getContributors();
554 $real_names = array();
555 $user_names = array();
558 # Sift for real versus user names
559 foreach ( $contributors as $user ) {
560 $page = $user->isAnon()
561 ? SpecialPage
::getTitleFor( 'Contributions', $user->getName() )
562 : $user->getUserPage();
564 if ( $user->getID() == 0 ) {
565 $anon_ips[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
566 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
567 $real_names[] = Linker
::link( $page, htmlspecialchars( $user->getRealName() ) );
569 $user_names[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
573 $lang = $this->getLanguage();
575 $real = $lang->listToText( $real_names );
577 # "ThisSite user(s) A, B and C"
578 if ( count( $user_names ) ) {
579 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
580 count( $user_names ) )->escaped();
585 if ( count( $anon_ips ) ) {
586 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
587 count( $anon_ips ) )->escaped();
592 # This is the big list, all mooshed together. We sift for blank strings
594 foreach ( array( $real, $user, $anon ) as $s ) {
596 array_push( $fulllist, $s );
600 $count = count( $fulllist );
601 # "Based on work by ..."
603 ?
$this->msg( 'othercontribs' )->rawParams(
604 $lang->listToText( $fulllist ) )->params( $count )->escaped()
609 * Returns the description that goes below the <h1> tag.
613 protected function getDescription() {