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( $this->getContext(), &$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.
151 protected function pageInfo() {
152 global $wgContLang, $wgRCMaxAge;
154 $user = $this->getUser();
155 $lang = $this->getLanguage();
156 $title = $this->getTitle();
157 $id = $title->getArticleID();
159 // Get page information that would be too "expensive" to retrieve by normal means
160 $pageCounts = self
::pageCounts( $title, $user );
162 // Get page properties
163 $dbr = wfGetDB( DB_SLAVE
);
164 $result = $dbr->select(
166 array( 'pp_propname', 'pp_value' ),
167 array( 'pp_page' => $id ),
171 $pageProperties = array();
172 foreach ( $result as $row ) {
173 $pageProperties[$row->pp_propname
] = $row->pp_value
;
178 $pageInfo['header-basic'] = array();
181 $displayTitle = $title->getPrefixedText();
182 if ( !empty( $pageProperties['displaytitle'] ) ) {
183 $displayTitle = $pageProperties['displaytitle'];
186 $pageInfo['header-basic'][] = array(
187 $this->msg( 'pageinfo-display-title' ), $displayTitle
191 $sortKey = $title->getCategorySortKey();
192 if ( !empty( $pageProperties['defaultsort'] ) ) {
193 $sortKey = $pageProperties['defaultsort'];
196 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-default-sort' ), $sortKey );
198 // Page length (in bytes)
199 $pageInfo['header-basic'][] = array(
200 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
203 // Page ID (number not localised, as it's a database ID)
204 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-article-id' ), $id );
206 // Search engine status
207 $pOutput = new ParserOutput();
208 if ( isset( $pageProperties['noindex'] ) ) {
209 $pOutput->setIndexPolicy( 'noindex' );
212 // Use robot policy logic
213 $policy = $this->page
->getRobotPolicy( 'view', $pOutput );
214 $pageInfo['header-basic'][] = array(
215 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
218 if ( isset( $pageCounts['views'] ) ) {
220 $pageInfo['header-basic'][] = array(
221 $this->msg( 'pageinfo-views' ), $lang->formatNum( $pageCounts['views'] )
225 if ( isset( $pageCounts['watchers'] ) ) {
226 // Number of page watchers
227 $pageInfo['header-basic'][] = array(
228 $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
232 // Redirects to this page
233 $whatLinksHere = SpecialPage
::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
234 $pageInfo['header-basic'][] = array(
237 $this->msg( 'pageinfo-redirects-name' )->escaped(),
239 array( 'hidelinks' => 1, 'hidetrans' => 1 )
241 $this->msg( 'pageinfo-redirects-value' )
242 ->numParams( count( $title->getRedirectsHere() ) )
245 // Subpages of this page, if subpages are enabled for the current NS
246 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
247 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
248 $pageInfo['header-basic'][] = array(
249 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
250 $this->msg( 'pageinfo-subpages-value' )
252 $pageCounts['subpages']['total'],
253 $pageCounts['subpages']['redirects'],
254 $pageCounts['subpages']['nonredirects'] )
259 $pageInfo['header-restrictions'] = array();
262 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
263 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
265 if ( $protectionLevel == '' ) {
267 $message = $this->msg( 'protect-default' )->escaped();
269 // Administrators only
270 $message = $this->msg( "protect-level-$protectionLevel" );
271 if ( $message->isDisabled() ) {
272 // Require "$1" permission
273 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
275 $message = $message->escaped();
279 $pageInfo['header-restrictions'][] = array(
280 $this->msg( "restriction-$restrictionType" ), $message
285 $pageInfo['header-edits'] = array();
287 $firstRev = $this->page
->getOldestRevision();
290 $pageInfo['header-edits'][] = array(
291 $this->msg( 'pageinfo-firstuser' ),
292 Linker
::revUserTools( $firstRev )
295 // Date of page creation
296 $pageInfo['header-edits'][] = array(
297 $this->msg( 'pageinfo-firsttime' ),
300 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
302 array( 'oldid' => $firstRev->getId() )
307 $pageInfo['header-edits'][] = array(
308 $this->msg( 'pageinfo-lastuser' ),
309 Linker
::revUserTools( $this->page
->getRevision() )
312 // Date of latest edit
313 $pageInfo['header-edits'][] = array(
314 $this->msg( 'pageinfo-lasttime' ),
317 $lang->userTimeAndDate( $this->page
->getTimestamp(), $user ),
319 array( 'oldid' => $this->page
->getLatest() )
323 // Total number of edits
324 $pageInfo['header-edits'][] = array(
325 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
328 // Total number of distinct authors
329 $pageInfo['header-edits'][] = array(
330 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
333 // Recent number of edits (within past 30 days)
334 $pageInfo['header-edits'][] = array(
335 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
336 $lang->formatNum( $pageCounts['recent_edits'] )
339 // Recent number of distinct authors
340 $pageInfo['header-edits'][] = array(
341 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
344 // Array of MagicWord objects
345 $magicWords = MagicWord
::getDoubleUnderscoreArray();
347 // Array of magic word IDs
348 $wordIDs = $magicWords->names
;
350 // Array of IDs => localized magic words
351 $localizedWords = $wgContLang->getMagicWords();
353 $listItems = array();
354 foreach ( $pageProperties as $property => $value ) {
355 if ( in_array( $property, $wordIDs ) ) {
356 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
360 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
361 $hiddenCategories = $this->page
->getHiddenCategories();
362 $transcludedTemplates = $title->getTemplateLinksFrom();
364 if ( count( $listItems ) > 0
365 ||
count( $hiddenCategories ) > 0
366 ||
count( $transcludedTemplates ) > 0 ) {
368 $pageInfo['header-properties'] = array();
371 if ( count( $listItems ) > 0 ) {
372 $pageInfo['header-properties'][] = array(
373 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
379 if ( count( $hiddenCategories ) > 0 ) {
380 $pageInfo['header-properties'][] = array(
381 $this->msg( 'pageinfo-hidden-categories' )
382 ->numParams( count( $hiddenCategories ) ),
383 Linker
::formatHiddenCategories( $hiddenCategories )
387 // Transcluded templates
388 if ( count( $transcludedTemplates ) > 0 ) {
389 $pageInfo['header-properties'][] = array(
390 $this->msg( 'pageinfo-templates' )
391 ->numParams( count( $transcludedTemplates ) ),
392 Linker
::formatTemplates( $transcludedTemplates )
401 * Returns page counts that would be too "expensive" to retrieve by normal means.
403 * @param $title Title object
404 * @param $user User object
407 protected static function pageCounts( $title, $user ) {
408 global $wgRCMaxAge, $wgDisableCounters;
410 wfProfileIn( __METHOD__
);
411 $id = $title->getArticleID();
413 $dbr = wfGetDB( DB_SLAVE
);
416 if ( !$wgDisableCounters ) {
418 $views = (int) $dbr->selectField(
421 array( 'page_id' => $id ),
424 $result['views'] = $views;
427 if ( $user->isAllowed( 'unwatchedpages' ) ) {
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() {