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
161 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
162 $table = $this->addRow( $table,
163 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
164 $this->msg( 'pageinfo-subpages-value' )
166 $pageInfo['subpages']['total'],
167 $pageInfo['subpages']['redirects'],
168 $pageInfo['subpages']['nonredirects'] )->escaped()
172 $content = $this->addTable( $content, $table );
173 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-restrictions' )->text() );
177 foreach ( $wgRestrictionTypes as $restrictionType ) {
178 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
179 if ( $protectionLevel == '' ) {
181 $message = $this->msg( 'protect-default' )->escaped();
183 // Administrators only
184 $message = $this->msg( "protect-level-$protectionLevel" );
185 if ( $message->isDisabled() ) {
186 // Require "$1" permission
187 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
189 $message = $message->escaped();
193 $table = $this->addRow( $table,
194 $this->msg( 'pageinfo-restriction',
195 $this->msg( "restriction-$restrictionType" )->plain()
201 $content = $this->addTable( $content, $table );
202 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-edits' )->text() );
206 $table = $this->addRow( $table,
207 $this->msg( 'pageinfo-firstuser' )->escaped(), $pageInfo['firstuser']
210 // Date of page creation
211 $table = $this->addRow( $table,
212 $this->msg( 'pageinfo-firsttime' )->escaped(), $lang->userTimeAndDate( $pageInfo['firsttime'], $user )
216 $table = $this->addRow( $table,
217 $this->msg( 'pageinfo-lastuser' )->escaped(), $pageInfo['lastuser']
220 // Date of latest edit
221 $table = $this->addRow( $table,
222 $this->msg( 'pageinfo-lasttime' )->escaped(), $lang->userTimeAndDate( $pageInfo['lasttime'], $user )
225 // Total number of edits
226 $table = $this->addRow( $table,
227 $this->msg( 'pageinfo-edits' )->escaped(), $lang->formatNum( $pageInfo['edits'] )
230 // Total number of distinct authors
231 $table = $this->addRow( $table,
232 $this->msg( 'pageinfo-authors' )->escaped(), $lang->formatNum( $pageInfo['authors'] )
235 // Recent number of edits (within past 30 days)
236 $table = $this->addRow( $table,
237 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) )->escaped(),
238 $lang->formatNum( $pageInfo['recent_edits'] )
241 // Recent number of distinct authors
242 $table = $this->addRow( $table,
243 $this->msg( 'pageinfo-recent-authors' )->escaped(), $lang->formatNum( $pageInfo['recent_authors'] )
246 $content = $this->addTable( $content, $table );
248 // Array of MagicWord objects
249 $magicWords = MagicWord
::getDoubleUnderscoreArray();
251 // Array of magic word IDs
252 $wordIDs = $magicWords->names
;
254 // Array of IDs => localized magic words
255 $localizedWords = $wgContLang->getMagicWords();
257 $listItems = array();
258 foreach ( $pageProperties as $property => $value ) {
259 if ( in_array( $property, $wordIDs ) ) {
260 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
264 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
265 $hiddenCategories = $this->page
->getHiddenCategories();
266 $transcludedTemplates = $title->getTemplateLinksFrom();
268 if ( count( $listItems ) > 0
269 ||
count( $hiddenCategories ) > 0
270 ||
count( $transcludedTemplates ) > 0 ) {
272 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-properties' )->text() );
276 if ( count( $listItems ) > 0 ) {
277 $table = $this->addRow( $table,
278 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) )->escaped(),
283 // Hide "This page is a member of # hidden categories explanation
284 $content .= Html
::element( 'style', array(),
285 '.mw-hiddenCategoriesExplanation { display: none; }' );
288 if ( count( $hiddenCategories ) > 0 ) {
289 $table = $this->addRow( $table,
290 $this->msg( 'pageinfo-hidden-categories' )
291 ->numParams( count( $hiddenCategories ) )->escaped(),
292 Linker
::formatHiddenCategories( $hiddenCategories )
296 // Hide "Templates used on this page:" explanation
297 $content .= Html
::element( 'style', array(),
298 '.mw-templatesUsedExplanation { display: none; }' );
300 // Transcluded templates
301 if ( count( $transcludedTemplates ) > 0 ) {
302 $table = $this->addRow( $table,
303 $this->msg( 'pageinfo-templates' )
304 ->numParams( count( $transcludedTemplates ) )->escaped(),
305 Linker
::formatTemplates( $transcludedTemplates )
309 $content = $this->addTable( $content, $table );
313 if ( !$this->msg( 'pageinfo-footer' )->isDisabled() ) {
314 $content .= $this->msg( 'pageinfo-footer' )->parse();
321 * Returns page information that would be too "expensive" to retrieve by normal means.
323 * @param $title Title object
324 * @param $canViewUnwatched bool
325 * @param $disableCounter bool
328 public static function pageCountInfo( $title, $canViewUnwatched, $disableCounter ) {
331 wfProfileIn( __METHOD__
);
332 $id = $title->getArticleID();
334 $dbr = wfGetDB( DB_SLAVE
);
337 if ( !$disableCounter ) {
339 $views = (int) $dbr->selectField(
342 array( 'page_id' => $id ),
345 $result['views'] = $views;
348 if ( $canViewUnwatched ) {
349 // Number of page watchers
350 $watchers = (int) $dbr->selectField(
354 'wl_namespace' => $title->getNamespace(),
355 'wl_title' => $title->getDBkey(),
359 $result['watchers'] = $watchers;
362 // Total number of edits
363 $edits = (int) $dbr->selectField(
366 array( 'rev_page' => $id ),
369 $result['edits'] = $edits;
371 // Total number of distinct authors
372 $authors = (int) $dbr->selectField(
374 'COUNT(DISTINCT rev_user_text)',
375 array( 'rev_page' => $id ),
378 $result['authors'] = $authors;
380 // "Recent" threshold defined by $wgRCMaxAge
381 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
383 // Recent number of edits
384 $edits = (int) $dbr->selectField(
389 "rev_timestamp >= $threshold"
393 $result['recent_edits'] = $edits;
395 // Recent number of distinct authors
396 $authors = (int) $dbr->selectField(
398 'COUNT(DISTINCT rev_user_text)',
401 "rev_timestamp >= $threshold"
405 $result['recent_authors'] = $authors;
407 $conds = array( 'page_namespace' => $title->getNamespace(), 'page_is_redirect' => 1 );
408 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
410 // Subpages of this page (redirects)
411 $result['subpages']['redirects'] = (int) $dbr->selectField(
417 // Subpages of this page (non-redirects)
418 $conds['page_is_redirect'] = 0;
419 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
426 // Subpages of this page (total)
427 $result['subpages']['total'] = $result['subpages']['redirects']
428 +
$result['subpages']['nonredirects'];
430 // Latest editor + date of latest edit
431 $options = array( 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 1 );
432 $row = $dbr->fetchRow( $dbr->select(
434 array( 'rev_user_text', 'rev_timestamp' ),
435 array( 'rev_page' => $id ),
440 $result['firstuser'] = $row['rev_user_text'];
441 $result['firsttime'] = $row['rev_timestamp'];
443 // Latest editor + date of latest edit
444 $options['ORDER BY'] = 'rev_timestamp DESC';
445 $row = $dbr->fetchRow( $dbr->select(
447 array( 'rev_user_text', 'rev_timestamp' ),
448 array( 'rev_page' => $id ),
453 $result['lastuser'] = $row['rev_user_text'];
454 $result['lasttime'] = $row['rev_timestamp'];
456 wfProfileOut( __METHOD__
);
461 * Adds a header to the content that will be added to the output.
463 * @param $content string The content that will be added to the output
464 * @param $header string The value of the header
465 * @return string The content with the header added
467 protected function addHeader( $content, $header ) {
468 return $content . Html
::element( 'h2', array(), $header );
472 * Adds a row to a table that will be added to the content.
474 * @param $table string The table that will be added to the content
475 * @param $name string The name of the row
476 * @param $value string The value of the row
477 * @return string The table with the row added
479 protected function addRow( $table, $name, $value ) {
480 return $table . Html
::rawElement( 'tr', array(),
481 Html
::rawElement( 'td', array(), $name ) .
482 Html
::rawElement( 'td', array(), $value )
487 * Adds a table to the content that will be added to the output.
489 * @param $content string The content that will be added to the output
490 * @param $table string The table
491 * @return string The content with the table added
493 protected function addTable( $content, $table ) {
494 return $content . Html
::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
499 * Returns the description that goes below the <h1> tag.
503 protected function getDescription() {
508 * Returns the name that goes in the <h1> page title.
512 protected function getPageTitle() {
513 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();