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() {
59 global $wgContLang, $wgDisableCounters, $wgRCMaxAge, $wgRestrictionTypes;
61 $user = $this->getUser();
62 $lang = $this->getLanguage();
63 $title = $this->getTitle();
64 $id = $title->getArticleID();
66 // Get page information that would be too "expensive" to retrieve by normal means
67 $userCanViewUnwatchedPages = $user->isAllowed( 'unwatchedpages' );
68 $pageInfo = self
::pageCountInfo( $title, $userCanViewUnwatchedPages, $wgDisableCounters );
70 // Get page properties
71 $dbr = wfGetDB( DB_SLAVE
);
72 $result = $dbr->select(
74 array( 'pp_propname', 'pp_value' ),
75 array( 'pp_page' => $id ),
79 $pageProperties = array();
80 foreach ( $result as $row ) {
81 $pageProperties[$row->pp_propname
] = $row->pp_value
;
88 if ( !$this->msg( 'pageinfo-header' )->isDisabled() ) {
89 $content .= $this->msg( 'pageinfo-header ' )->parse();
93 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-basic' )->text() );
96 $displayTitle = $title->getPrefixedText();
97 if ( !empty( $pageProperties['displaytitle'] ) ) {
98 $displayTitle = $pageProperties['displaytitle'];
101 $table = $this->addRow( $table,
102 $this->msg( 'pageinfo-display-title' )->escaped(), $displayTitle );
105 $sortKey = $title->getCategorySortKey();
106 if ( !empty( $pageProperties['defaultsort'] ) ) {
107 $sortKey = $pageProperties['defaultsort'];
110 $table = $this->addRow( $table,
111 $this->msg( 'pageinfo-default-sort' )->escaped(), $sortKey );
113 // Page length (in bytes)
114 $table = $this->addRow( $table,
115 $this->msg( 'pageinfo-length' )->escaped(), $lang->formatNum( $title->getLength() ) );
117 // Page ID (number not localised, as it's a database ID.)
118 $table = $this->addRow( $table,
119 $this->msg( 'pageinfo-article-id' )->escaped(), $id );
121 // Search engine status
122 $pOutput = new ParserOutput();
123 if ( isset( $pageProperties['noindex'] ) ) {
124 $pOutput->setIndexPolicy( 'noindex' );
127 // Use robot policy logic
128 $policy = $this->page
->getRobotPolicy( 'view', $pOutput );
129 $table = $this->addRow( $table,
130 $this->msg( 'pageinfo-robot-policy' )->escaped(),
131 $this->msg( "pageinfo-robot-${policy['index']}" )->escaped()
134 if ( !$wgDisableCounters ) {
136 $table = $this->addRow( $table,
137 $this->msg( 'pageinfo-views' )->escaped(), $lang->formatNum( $pageInfo['views'] )
141 if ( $userCanViewUnwatchedPages ) {
142 // Number of page watchers
143 $table = $this->addRow( $table,
144 $this->msg( 'pageinfo-watchers' )->escaped(), $lang->formatNum( $pageInfo['watchers'] ) );
147 // Redirects to this page
148 $whatLinksHere = SpecialPage
::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
149 $table = $this->addRow( $table,
152 $this->msg( 'pageinfo-redirects-name' )->escaped(),
154 array( 'hidelinks' => 1, 'hidetrans' => 1 )
156 $this->msg( 'pageinfo-redirects-value' )
157 ->numParams( count( $title->getRedirectsHere() ) )->escaped()
160 // Subpages of this page, if subpages are enabled for the current NS
161 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
162 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
163 $table = $this->addRow( $table,
164 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
165 $this->msg( 'pageinfo-subpages-value' )
167 $pageInfo['subpages']['total'],
168 $pageInfo['subpages']['redirects'],
169 $pageInfo['subpages']['nonredirects'] )->escaped()
174 $content = $this->addTable( $content, $table );
175 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-restrictions' )->text() );
179 foreach ( $wgRestrictionTypes as $restrictionType ) {
180 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
181 if ( $protectionLevel == '' ) {
183 $message = $this->msg( 'protect-default' )->escaped();
185 // Administrators only
186 $message = $this->msg( "protect-level-$protectionLevel" );
187 if ( $message->isDisabled() ) {
188 // Require "$1" permission
189 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
191 $message = $message->escaped();
195 $table = $this->addRow( $table,
196 $this->msg( 'pageinfo-restriction',
197 $this->msg( "restriction-$restrictionType" )->plain()
203 $content = $this->addTable( $content, $table );
204 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-edits' )->text() );
208 $table = $this->addRow( $table,
209 $this->msg( 'pageinfo-firstuser' )->escaped(), $pageInfo['firstuser']
212 // Date of page creation
213 $table = $this->addRow( $table,
214 $this->msg( 'pageinfo-firsttime' )->escaped(), $lang->userTimeAndDate( $pageInfo['firsttime'], $user )
218 $table = $this->addRow( $table,
219 $this->msg( 'pageinfo-lastuser' )->escaped(), $pageInfo['lastuser']
222 // Date of latest edit
223 $table = $this->addRow( $table,
224 $this->msg( 'pageinfo-lasttime' )->escaped(), $lang->userTimeAndDate( $pageInfo['lasttime'], $user )
227 // Total number of edits
228 $table = $this->addRow( $table,
229 $this->msg( 'pageinfo-edits' )->escaped(), $lang->formatNum( $pageInfo['edits'] )
232 // Total number of distinct authors
233 $table = $this->addRow( $table,
234 $this->msg( 'pageinfo-authors' )->escaped(), $lang->formatNum( $pageInfo['authors'] )
237 // Recent number of edits (within past 30 days)
238 $table = $this->addRow( $table,
239 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) )->escaped(),
240 $lang->formatNum( $pageInfo['recent_edits'] )
243 // Recent number of distinct authors
244 $table = $this->addRow( $table,
245 $this->msg( 'pageinfo-recent-authors' )->escaped(), $lang->formatNum( $pageInfo['recent_authors'] )
248 $content = $this->addTable( $content, $table );
250 // Array of MagicWord objects
251 $magicWords = MagicWord
::getDoubleUnderscoreArray();
253 // Array of magic word IDs
254 $wordIDs = $magicWords->names
;
256 // Array of IDs => localized magic words
257 $localizedWords = $wgContLang->getMagicWords();
259 $listItems = array();
260 foreach ( $pageProperties as $property => $value ) {
261 if ( in_array( $property, $wordIDs ) ) {
262 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
266 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
267 $hiddenCategories = $this->page
->getHiddenCategories();
268 $transcludedTemplates = $title->getTemplateLinksFrom();
270 if ( count( $listItems ) > 0
271 ||
count( $hiddenCategories ) > 0
272 ||
count( $transcludedTemplates ) > 0 ) {
274 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-properties' )->text() );
278 if ( count( $listItems ) > 0 ) {
279 $table = $this->addRow( $table,
280 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) )->escaped(),
285 // Hide "This page is a member of # hidden categories explanation
286 $content .= Html
::element( 'style', array(),
287 '.mw-hiddenCategoriesExplanation { display: none; }' );
290 if ( count( $hiddenCategories ) > 0 ) {
291 $table = $this->addRow( $table,
292 $this->msg( 'pageinfo-hidden-categories' )
293 ->numParams( count( $hiddenCategories ) )->escaped(),
294 Linker
::formatHiddenCategories( $hiddenCategories )
298 // Hide "Templates used on this page:" explanation
299 $content .= Html
::element( 'style', array(),
300 '.mw-templatesUsedExplanation { display: none; }' );
302 // Transcluded templates
303 if ( count( $transcludedTemplates ) > 0 ) {
304 $table = $this->addRow( $table,
305 $this->msg( 'pageinfo-templates' )
306 ->numParams( count( $transcludedTemplates ) )->escaped(),
307 Linker
::formatTemplates( $transcludedTemplates )
311 $content = $this->addTable( $content, $table );
315 if ( !$this->msg( 'pageinfo-footer' )->isDisabled() ) {
316 $content .= $this->msg( 'pageinfo-footer' )->parse();
323 * Returns page information that would be too "expensive" to retrieve by normal means.
325 * @param $title Title object
326 * @param $canViewUnwatched bool
327 * @param $disableCounter bool
330 public static function pageCountInfo( $title, $canViewUnwatched, $disableCounter ) {
333 wfProfileIn( __METHOD__
);
334 $id = $title->getArticleID();
336 $dbr = wfGetDB( DB_SLAVE
);
339 if ( !$disableCounter ) {
341 $views = (int) $dbr->selectField(
344 array( 'page_id' => $id ),
347 $result['views'] = $views;
350 if ( $canViewUnwatched ) {
351 // Number of page watchers
352 $watchers = (int) $dbr->selectField(
356 'wl_namespace' => $title->getNamespace(),
357 'wl_title' => $title->getDBkey(),
361 $result['watchers'] = $watchers;
364 // Total number of edits
365 $edits = (int) $dbr->selectField(
368 array( 'rev_page' => $id ),
371 $result['edits'] = $edits;
373 // Total number of distinct authors
374 $authors = (int) $dbr->selectField(
376 'COUNT(DISTINCT rev_user_text)',
377 array( 'rev_page' => $id ),
380 $result['authors'] = $authors;
382 // "Recent" threshold defined by $wgRCMaxAge
383 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
385 // Recent number of edits
386 $edits = (int) $dbr->selectField(
391 "rev_timestamp >= $threshold"
395 $result['recent_edits'] = $edits;
397 // Recent number of distinct authors
398 $authors = (int) $dbr->selectField(
400 'COUNT(DISTINCT rev_user_text)',
403 "rev_timestamp >= $threshold"
407 $result['recent_authors'] = $authors;
409 // Subpages (if enabled)
410 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
411 $conds = array( 'page_namespace' => $title->getNamespace() );
412 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
414 // Subpages of this page (redirects)
415 $conds['page_is_redirect'] = 1;
416 $result['subpages']['redirects'] = (int) $dbr->selectField(
422 // Subpages of this page (non-redirects)
423 $conds['page_is_redirect'] = 0;
424 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
431 // Subpages of this page (total)
432 $result['subpages']['total'] = $result['subpages']['redirects']
433 +
$result['subpages']['nonredirects'];
436 // Latest editor + date of latest edit
437 $options = array( 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 1 );
438 $row = $dbr->fetchRow( $dbr->select(
440 array( 'rev_user_text', 'rev_timestamp' ),
441 array( 'rev_page' => $id ),
446 $result['firstuser'] = $row['rev_user_text'];
447 $result['firsttime'] = $row['rev_timestamp'];
449 // Latest editor + date of latest edit
450 $options['ORDER BY'] = 'rev_timestamp DESC';
451 $row = $dbr->fetchRow( $dbr->select(
453 array( 'rev_user_text', 'rev_timestamp' ),
454 array( 'rev_page' => $id ),
459 $result['lastuser'] = $row['rev_user_text'];
460 $result['lasttime'] = $row['rev_timestamp'];
462 wfProfileOut( __METHOD__
);
467 * Adds a header to the content that will be added to the output.
469 * @param $content string The content that will be added to the output
470 * @param $header string The value of the header
471 * @return string The content with the header added
473 protected function addHeader( $content, $header ) {
474 return $content . Html
::element( 'h2', array(), $header );
478 * Adds a row to a table that will be added to the content.
480 * @param $table string The table that will be added to the content
481 * @param $name string The name of the row
482 * @param $value string The value of the row
483 * @return string The table with the row added
485 protected function addRow( $table, $name, $value ) {
486 return $table . Html
::rawElement( 'tr', array(),
487 Html
::rawElement( 'td', array( 'valign' => 'top' ), $name ) .
488 Html
::rawElement( 'td', array(), $value )
493 * Adds a table to the content that will be added to the output.
495 * @param $content string The content that will be added to the output
496 * @param $table string The table
497 * @return string The content with the table added
499 protected function addTable( $content, $table ) {
500 return $content . Html
::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
505 * Returns the description that goes below the <h1> tag.
509 protected function getDescription() {
514 * Returns the name that goes in the <h1> page title.
518 protected function getPageTitle() {
519 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();