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() );
207 $firstRev = $this->page
->getOldestRevision();
210 $table = $this->addRow( $table,
211 $this->msg( 'pageinfo-firstuser' )->escaped(),
212 $firstRev->getUserText( Revision
::FOR_THIS_USER
, $user )
215 // Date of page creation
216 $table = $this->addRow( $table,
217 $this->msg( 'pageinfo-firsttime' )->escaped(),
220 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
222 array( 'oldid' => $firstRev->getId() )
227 $table = $this->addRow( $table,
228 $this->msg( 'pageinfo-lastuser' )->escaped(),
229 $this->page
->getUserText( Revision
::FOR_THIS_USER
, $user )
232 // Date of latest edit
233 $table = $this->addRow( $table,
234 $this->msg( 'pageinfo-lasttime' )->escaped(),
237 $lang->userTimeAndDate( $this->page
->getTimestamp(), $user ),
239 array( 'oldid' => $this->page
->getLatest() )
243 // Total number of edits
244 $table = $this->addRow( $table,
245 $this->msg( 'pageinfo-edits' )->escaped(), $lang->formatNum( $pageInfo['edits'] )
248 // Total number of distinct authors
249 $table = $this->addRow( $table,
250 $this->msg( 'pageinfo-authors' )->escaped(), $lang->formatNum( $pageInfo['authors'] )
253 // Recent number of edits (within past 30 days)
254 $table = $this->addRow( $table,
255 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) )->escaped(),
256 $lang->formatNum( $pageInfo['recent_edits'] )
259 // Recent number of distinct authors
260 $table = $this->addRow( $table,
261 $this->msg( 'pageinfo-recent-authors' )->escaped(), $lang->formatNum( $pageInfo['recent_authors'] )
264 $content = $this->addTable( $content, $table );
266 // Array of MagicWord objects
267 $magicWords = MagicWord
::getDoubleUnderscoreArray();
269 // Array of magic word IDs
270 $wordIDs = $magicWords->names
;
272 // Array of IDs => localized magic words
273 $localizedWords = $wgContLang->getMagicWords();
275 $listItems = array();
276 foreach ( $pageProperties as $property => $value ) {
277 if ( in_array( $property, $wordIDs ) ) {
278 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
282 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
283 $hiddenCategories = $this->page
->getHiddenCategories();
284 $transcludedTemplates = $title->getTemplateLinksFrom();
286 if ( count( $listItems ) > 0
287 ||
count( $hiddenCategories ) > 0
288 ||
count( $transcludedTemplates ) > 0 ) {
290 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-properties' )->text() );
294 if ( count( $listItems ) > 0 ) {
295 $table = $this->addRow( $table,
296 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) )->escaped(),
301 // Hide "This page is a member of # hidden categories explanation
302 $content .= Html
::element( 'style', array(),
303 '.mw-hiddenCategoriesExplanation { display: none; }' );
306 if ( count( $hiddenCategories ) > 0 ) {
307 $table = $this->addRow( $table,
308 $this->msg( 'pageinfo-hidden-categories' )
309 ->numParams( count( $hiddenCategories ) )->escaped(),
310 Linker
::formatHiddenCategories( $hiddenCategories )
314 // Hide "Templates used on this page:" explanation
315 $content .= Html
::element( 'style', array(),
316 '.mw-templatesUsedExplanation { display: none; }' );
318 // Transcluded templates
319 if ( count( $transcludedTemplates ) > 0 ) {
320 $table = $this->addRow( $table,
321 $this->msg( 'pageinfo-templates' )
322 ->numParams( count( $transcludedTemplates ) )->escaped(),
323 Linker
::formatTemplates( $transcludedTemplates )
327 $content = $this->addTable( $content, $table );
331 if ( !$this->msg( 'pageinfo-footer' )->isDisabled() ) {
332 $content .= $this->msg( 'pageinfo-footer' )->parse();
339 * Returns page information that would be too "expensive" to retrieve by normal means.
341 * @param $title Title object
342 * @param $canViewUnwatched bool
343 * @param $disableCounter bool
346 public static function pageCountInfo( $title, $canViewUnwatched, $disableCounter ) {
349 wfProfileIn( __METHOD__
);
350 $id = $title->getArticleID();
352 $dbr = wfGetDB( DB_SLAVE
);
355 if ( !$disableCounter ) {
357 $views = (int) $dbr->selectField(
360 array( 'page_id' => $id ),
363 $result['views'] = $views;
366 if ( $canViewUnwatched ) {
367 // Number of page watchers
368 $watchers = (int) $dbr->selectField(
372 'wl_namespace' => $title->getNamespace(),
373 'wl_title' => $title->getDBkey(),
377 $result['watchers'] = $watchers;
380 // Total number of edits
381 $edits = (int) $dbr->selectField(
384 array( 'rev_page' => $id ),
387 $result['edits'] = $edits;
389 // Total number of distinct authors
390 $authors = (int) $dbr->selectField(
392 'COUNT(DISTINCT rev_user_text)',
393 array( 'rev_page' => $id ),
396 $result['authors'] = $authors;
398 // "Recent" threshold defined by $wgRCMaxAge
399 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
401 // Recent number of edits
402 $edits = (int) $dbr->selectField(
407 "rev_timestamp >= $threshold"
411 $result['recent_edits'] = $edits;
413 // Recent number of distinct authors
414 $authors = (int) $dbr->selectField(
416 'COUNT(DISTINCT rev_user_text)',
419 "rev_timestamp >= $threshold"
423 $result['recent_authors'] = $authors;
425 // Subpages (if enabled)
426 if ( MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
427 $conds = array( 'page_namespace' => $title->getNamespace() );
428 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
430 // Subpages of this page (redirects)
431 $conds['page_is_redirect'] = 1;
432 $result['subpages']['redirects'] = (int) $dbr->selectField(
438 // Subpages of this page (non-redirects)
439 $conds['page_is_redirect'] = 0;
440 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
447 // Subpages of this page (total)
448 $result['subpages']['total'] = $result['subpages']['redirects']
449 +
$result['subpages']['nonredirects'];
452 wfProfileOut( __METHOD__
);
457 * Adds a header to the content that will be added to the output.
459 * @param $content string The content that will be added to the output
460 * @param $header string The value of the header
461 * @return string The content with the header added
463 protected function addHeader( $content, $header ) {
464 return $content . Html
::element( 'h2', array(), $header );
468 * Adds a row to a table that will be added to the content.
470 * @param $table string The table that will be added to the content
471 * @param $name string The name of the row
472 * @param $value string The value of the row
473 * @return string The table with the row added
475 protected function addRow( $table, $name, $value ) {
476 return $table . Html
::rawElement( 'tr', array(),
477 Html
::rawElement( 'td', array( 'valign' => 'top' ), $name ) .
478 Html
::rawElement( 'td', array(), $value )
483 * Adds a table to the content that will be added to the output.
485 * @param $content string The content that will be added to the output
486 * @param $table string The table
487 * @return string The content with the table added
489 protected function addTable( $content, $table ) {
490 return $content . Html
::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
495 * Returns the description that goes below the <h1> tag.
499 protected function getDescription() {
504 * Returns the name that goes in the <h1> page title.
508 protected function getPageTitle() {
509 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();