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 $pageInfo = $this->pageInfo();
77 // Allow extensions to add additional information
78 wfRunHooks( 'InfoAction', array( &$pageInfo ) );
80 // Render page information
81 foreach ( $pageInfo as $header => $infoTable ) {
82 $content .= $this->makeHeader( $this->msg( "pageinfo-${header}" )->escaped() );
84 foreach ( $infoTable as $infoRow ) {
85 $name = ( $infoRow[0] instanceof Message
) ?
$infoRow[0]->escaped() : $infoRow[0];
86 $value = ( $infoRow[1] instanceof Message
) ?
$infoRow[1]->escaped() : $infoRow[1];
87 $table = $this->addRow( $table, $name, $value );
89 $content = $this->addTable( $content, $table );
93 if ( !$this->msg( 'pageinfo-footer' )->isDisabled() ) {
94 $content .= $this->msg( 'pageinfo-footer' )->parse();
98 /*if ( $this->page->exists() ) {
99 $content .= Html::rawElement( 'div', array( 'id' => 'mw-credits' ), $this->getContributors() );
106 * Creates a header that can be added to the output.
108 * @param $header The header text.
109 * @return string The HTML.
111 protected function makeHeader( $header ) {
113 $spanAttribs = array( 'class' => 'mw-headline', 'id' => $wgParser->guessSectionNameFromWikiText( $header ) );
114 return Html
::rawElement( 'h2', array(), Html
::element( 'span', $spanAttribs, $header ) );
118 * Adds a row to a table that will be added to the content.
120 * @param $table string The table that will be added to the content
121 * @param $name string The name of the row
122 * @param $value string The value of the row
123 * @return string The table with the row added
125 protected function addRow( $table, $name, $value ) {
126 return $table . Html
::rawElement( 'tr', array(),
127 Html
::rawElement( 'td', array( 'style' => 'vertical-align: top;' ), $name ) .
128 Html
::rawElement( 'td', array(), $value )
133 * Adds a table to the content that will be added to the output.
135 * @param $content string The content that will be added to the output
136 * @param $table string The table
137 * @return string The content with the table added
139 protected function addTable( $content, $table ) {
140 return $content . Html
::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
145 * Returns page information in an easily-manipulated format. Array keys are used so extensions
146 * may add additional information in arbitrary positions. Array values are arrays with one
147 * element to be rendered as a header, arrays with two elements to be rendered as a table row.
149 protected function pageInfo() {
150 global $wgContLang, $wgDisableCounters, $wgRCMaxAge;
152 $user = $this->getUser();
153 $lang = $this->getLanguage();
154 $title = $this->getTitle();
155 $id = $title->getArticleID();
157 // Get page information that would be too "expensive" to retrieve by normal means
158 $userCanViewUnwatchedPages = $user->isAllowed( 'unwatchedpages' );
159 $pageCounts = self
::pageCounts( $title, $userCanViewUnwatchedPages, $wgDisableCounters );
161 // Get page properties
162 $dbr = wfGetDB( DB_SLAVE
);
163 $result = $dbr->select(
165 array( 'pp_propname', 'pp_value' ),
166 array( 'pp_page' => $id ),
170 $pageProperties = array();
171 foreach ( $result as $row ) {
172 $pageProperties[$row->pp_propname
] = $row->pp_value
;
177 $pageInfo['header-basic'] = array();
180 $displayTitle = $title->getPrefixedText();
181 if ( !empty( $pageProperties['displaytitle'] ) ) {
182 $displayTitle = $pageProperties['displaytitle'];
185 $pageInfo['header-basic'][] = array(
186 $this->msg( 'pageinfo-display-title' ), $displayTitle
190 $sortKey = $title->getCategorySortKey();
191 if ( !empty( $pageProperties['defaultsort'] ) ) {
192 $sortKey = $pageProperties['defaultsort'];
195 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-default-sort' ), $sortKey );
197 // Page length (in bytes)
198 $pageInfo['header-basic'][] = array(
199 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
202 // Page ID (number not localised, as it's a database ID)
203 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-article-id' ), $id );
205 // Search engine status
206 $pOutput = new ParserOutput();
207 if ( isset( $pageProperties['noindex'] ) ) {
208 $pOutput->setIndexPolicy( 'noindex' );
211 // Use robot policy logic
212 $policy = $this->page
->getRobotPolicy( 'view', $pOutput );
213 $pageInfo['header-basic'][] = array(
214 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
217 if ( !$wgDisableCounters ) {
219 $pageInfo['header-basic'][] = array(
220 $this->msg( 'pageinfo-views' ), $lang->formatNum( $pageCounts['views'] )
224 if ( $userCanViewUnwatchedPages ) {
225 // Number of page watchers
226 $pageInfo['header-basic'][] = array(
227 $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
231 // Redirects to this page
232 $whatLinksHere = SpecialPage
::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
233 $pageInfo['header-basic'][] = array(
236 $this->msg( 'pageinfo-redirects-name' )->escaped(),
238 array( 'hidelinks' => 1, 'hidetrans' => 1 )
240 $this->msg( 'pageinfo-redirects-value' )
241 ->numParams( count( $title->getRedirectsHere() ) )
244 // Subpages of this page, if subpages are enabled for the current NS
245 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
246 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
247 $pageInfo['header-basic'][] = array(
248 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
249 $this->msg( 'pageinfo-subpages-value' )
251 $pageCounts['subpages']['total'],
252 $pageCounts['subpages']['redirects'],
253 $pageCounts['subpages']['nonredirects'] )
258 $pageInfo['header-restrictions'] = array();
261 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
262 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
264 if ( $protectionLevel == '' ) {
266 $message = $this->msg( 'protect-default' )->escaped();
268 // Administrators only
269 $message = $this->msg( "protect-level-$protectionLevel" );
270 if ( $message->isDisabled() ) {
271 // Require "$1" permission
272 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
274 $message = $message->escaped();
278 $pageInfo['header-restrictions'][] = array(
279 $this->msg( "restriction-$restrictionType" ), $message
284 $pageInfo['header-edits'] = array();
286 $firstRev = $this->page
->getOldestRevision();
289 $pageInfo['header-edits'][] = array(
290 $this->msg( 'pageinfo-firstuser' ),
291 Linker
::revUserTools( $firstRev )
294 // Date of page creation
295 $pageInfo['header-edits'][] = array(
296 $this->msg( 'pageinfo-firsttime' ),
299 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
301 array( 'oldid' => $firstRev->getId() )
306 $pageInfo['header-edits'][] = array(
307 $this->msg( 'pageinfo-lastuser' ),
308 Linker
::revUserTools( $this->page
->getRevision() )
311 // Date of latest edit
312 $pageInfo['header-edits'][] = array(
313 $this->msg( 'pageinfo-lasttime' ),
316 $lang->userTimeAndDate( $this->page
->getTimestamp(), $user ),
318 array( 'oldid' => $this->page
->getLatest() )
322 // Total number of edits
323 $pageInfo['header-edits'][] = array(
324 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
327 // Total number of distinct authors
328 $pageInfo['header-edits'][] = array(
329 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
332 // Recent number of edits (within past 30 days)
333 $pageInfo['header-edits'][] = array(
334 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
335 $lang->formatNum( $pageCounts['recent_edits'] )
338 // Recent number of distinct authors
339 $pageInfo['header-edits'][] = array(
340 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
343 // Array of MagicWord objects
344 $magicWords = MagicWord
::getDoubleUnderscoreArray();
346 // Array of magic word IDs
347 $wordIDs = $magicWords->names
;
349 // Array of IDs => localized magic words
350 $localizedWords = $wgContLang->getMagicWords();
352 $listItems = array();
353 foreach ( $pageProperties as $property => $value ) {
354 if ( in_array( $property, $wordIDs ) ) {
355 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
359 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
360 $hiddenCategories = $this->page
->getHiddenCategories();
361 $transcludedTemplates = $title->getTemplateLinksFrom();
363 if ( count( $listItems ) > 0
364 ||
count( $hiddenCategories ) > 0
365 ||
count( $transcludedTemplates ) > 0 ) {
367 $pageInfo['header-properties'] = array();
370 if ( count( $listItems ) > 0 ) {
371 $pageInfo['header-properties'][] = array(
372 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
378 if ( count( $hiddenCategories ) > 0 ) {
379 $pageInfo['header-properties'][] = array(
380 $this->msg( 'pageinfo-hidden-categories' )
381 ->numParams( count( $hiddenCategories ) ),
382 Linker
::formatHiddenCategories( $hiddenCategories )
386 // Transcluded templates
387 if ( count( $transcludedTemplates ) > 0 ) {
388 $pageInfo['header-properties'][] = array(
389 $this->msg( 'pageinfo-templates' )
390 ->numParams( count( $transcludedTemplates ) ),
391 Linker
::formatTemplates( $transcludedTemplates )
400 * Returns page counts that would be too "expensive" to retrieve by normal means.
402 * @param $title Title object
403 * @param $canViewUnwatched bool
404 * @param $disableCounter bool
407 protected static function pageCounts( $title, $canViewUnwatched, $disableCounter ) {
410 wfProfileIn( __METHOD__
);
411 $id = $title->getArticleID();
413 $dbr = wfGetDB( DB_SLAVE
);
416 if ( !$disableCounter ) {
418 $views = (int) $dbr->selectField(
421 array( 'page_id' => $id ),
424 $result['views'] = $views;
427 if ( $canViewUnwatched ) {
428 // Number of page watchers
429 $watchers = (int) $dbr->selectField(
433 'wl_namespace' => $title->getNamespace(),
434 'wl_title' => $title->getDBkey(),
438 $result['watchers'] = $watchers;
441 // Total number of edits
442 $edits = (int) $dbr->selectField(
445 array( 'rev_page' => $id ),
448 $result['edits'] = $edits;
450 // Total number of distinct authors
451 $authors = (int) $dbr->selectField(
453 'COUNT(DISTINCT rev_user_text)',
454 array( 'rev_page' => $id ),
457 $result['authors'] = $authors;
459 // "Recent" threshold defined by $wgRCMaxAge
460 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
462 // Recent number of edits
463 $edits = (int) $dbr->selectField(
468 "rev_timestamp >= $threshold"
472 $result['recent_edits'] = $edits;
474 // Recent number of distinct authors
475 $authors = (int) $dbr->selectField(
477 'COUNT(DISTINCT rev_user_text)',
480 "rev_timestamp >= $threshold"
484 $result['recent_authors'] = $authors;
486 // Subpages (if enabled)
487 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
488 $conds = array( 'page_namespace' => $title->getNamespace() );
489 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
491 // Subpages of this page (redirects)
492 $conds['page_is_redirect'] = 1;
493 $result['subpages']['redirects'] = (int) $dbr->selectField(
499 // Subpages of this page (non-redirects)
500 $conds['page_is_redirect'] = 0;
501 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
508 // Subpages of this page (total)
509 $result['subpages']['total'] = $result['subpages']['redirects']
510 +
$result['subpages']['nonredirects'];
513 wfProfileOut( __METHOD__
);
518 * Returns the name that goes in the <h1> page title.
522 protected function getPageTitle() {
523 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
527 * Get a list of contributors of $article
528 * @return string: html
530 protected function getContributors() {
531 global $wgHiddenPrefs;
533 $contributors = $this->page
->getContributors();
534 $real_names = array();
535 $user_names = array();
538 # Sift for real versus user names
539 foreach ( $contributors as $user ) {
540 $page = $user->isAnon()
541 ? SpecialPage
::getTitleFor( 'Contributions', $user->getName() )
542 : $user->getUserPage();
544 if ( $user->getID() == 0 ) {
545 $anon_ips[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
546 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
547 $real_names[] = Linker
::link( $page, htmlspecialchars( $user->getRealName() ) );
549 $user_names[] = Linker
::link( $page, htmlspecialchars( $user->getName() ) );
553 $lang = $this->getLanguage();
555 $real = $lang->listToText( $real_names );
557 # "ThisSite user(s) A, B and C"
558 if ( count( $user_names ) ) {
559 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
560 count( $user_names ) )->escaped();
565 if ( count( $anon_ips ) ) {
566 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
567 count( $anon_ips ) )->escaped();
572 # This is the big list, all mooshed together. We sift for blank strings
574 foreach ( array( $real, $user, $anon ) as $s ) {
576 array_push( $fulllist, $s );
580 $count = count( $fulllist );
581 # "Based on work by ..."
583 ?
$this->msg( 'othercontribs' )->rawParams(
584 $lang->listToText( $fulllist ) )->params( $count )->escaped()
589 * Returns the description that goes below the <h1> tag.
593 protected function getDescription() {