d61f5b665d986948e3554a402e0cdff3e6eee546
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 $wgDisableCounters, $wgRCMaxAge, $wgRestrictionTypes;
61 $lang = $this->getLanguage();
62 $title = $this->getTitle();
64 $article = new Article( $title );
65 $id = $title->getArticleID();
67 // Get page information that would be too "expensive" to retrieve by normal means
68 $userCanViewUnwatchedPages = $this->getUser()->isAllowed( 'unwatchedpages' );
69 $pageInfo = self
::pageCountInfo( $title, $userCanViewUnwatchedPages, $wgDisableCounters );
71 // Get page properties
72 $dbr = wfGetDB( DB_SLAVE
);
73 $result = $dbr->select(
75 array( 'pp_propname', 'pp_value' ),
76 array( 'pp_page' => $id ),
80 $pageProperties = array();
81 foreach ( $result as $row ) {
82 $pageProperties[$row->pp_propname
] = $row->pp_value
;
89 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-basic' )->text() );
92 $displayTitle = $title->getPrefixedText();
93 if ( !empty( $pageProperties['displaytitle'] ) ) {
94 $displayTitle = $pageProperties['displaytitle'];
97 $table = $this->addRow( $table,
98 $this->msg( 'pageinfo-display-title' )->escaped(), $displayTitle );
101 $sortKey = $title->getCategorySortKey();
102 if ( !empty( $pageProperties['defaultsort'] ) ) {
103 $sortKey = $pageProperties['defaultsort'];
106 $table = $this->addRow( $table,
107 $this->msg( 'pageinfo-default-sort' )->escaped(), $sortKey );
109 // Page length (in bytes)
110 $table = $this->addRow( $table,
111 $this->msg( 'pageinfo-length' )->escaped(), $lang->formatNum( $title->getLength() ) );
113 // Page ID (number not localised, as it's a database ID.)
114 $table = $this->addRow( $table,
115 $this->msg( 'pageinfo-article-id' )->escaped(), $id );
117 // Search engine status
118 $pOutput = new ParserOutput();
119 if ( isset( $pageProperties['noindex'] ) ) {
120 $pOutput->setIndexPolicy( 'noindex' );
123 // Use robot policy logic
124 $policy = $article->getRobotPolicy( 'view', $pOutput );
125 // @todo FIXME: Hard coded English text.
126 $table = $this->addRow( $table,
127 $this->msg( 'pageinfo-robot-policy' )->escaped(), "Marked as '" . $policy['index'] . "'"
130 if ( !$wgDisableCounters ) {
132 $table = $this->addRow( $table,
133 $this->msg( 'pageinfo-views' )->escaped(), $lang->formatNum( $pageInfo['views'] )
137 if ( $userCanViewUnwatchedPages ) {
138 // Number of page watchers
139 $table = $this->addRow( $table,
140 $this->msg( 'pageinfo-watchers' )->escaped(), $lang->formatNum( $pageInfo['watchers'] ) );
143 // Redirects to this page
144 $whatLinksHere = SpecialPage
::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
145 $table = $this->addRow( $table,
148 $this->msg( 'pageinfo-redirects-name' )->escaped(),
150 array( 'hidelinks' => 1, 'hidetrans' => 1 )
152 $this->msg( 'pageinfo-redirects-value' )
153 ->numParams( count( $title->getRedirectsHere() ) )->escaped()
156 // Subpages of this page
157 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
158 $table = $this->addRow( $table,
159 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
160 $this->msg( 'pageinfo-subpages-value' )
162 $pageInfo['subpages']['total'],
163 $pageInfo['subpages']['redirects'],
164 $pageInfo['subpages']['nonredirects'] )->escaped()
168 $content = $this->addTable( $content, $table );
169 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-restrictions' )->text() );
173 foreach ( $wgRestrictionTypes as $restrictionType ) {
174 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
175 if ( $protectionLevel == '' ) {
177 $message = $this->msg( 'protect-default' )->escaped();
179 // Administrators only
180 $message = $this->msg( "protect-level-$protectionLevel" );
181 if ( $message->isDisabled() ) {
182 // Require "$1" permission
183 $message = $this->msg( "protect-fallback", $protectionLevel )->escaped();
185 $message = $message->escaped();
189 $table = $this->addRow( $table,
190 $this->msg( 'pageinfo-restriction', $restrictionType )->escaped(), $message
195 $content = $this->addTable( $content, $table );
196 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-edits' )->text() );
200 $table = $this->addRow( $table,
201 $this->msg( 'pageinfo-firstuser' )->escaped(), $pageInfo['firstuser']
204 // Date of page creation
205 $table = $this->addRow( $table,
206 $this->msg( 'pageinfo-firsttime' )->escaped(), $lang->timeanddate( $pageInfo['firsttime'] )
210 $table = $this->addRow( $table,
211 $this->msg( 'pageinfo-lastuser' )->escaped(), $pageInfo['lastuser']
214 // Date of latest edit
215 $table = $this->addRow( $table,
216 $this->msg( 'pageinfo-lasttime' )->escaped(), $lang->timeanddate( $pageInfo['lasttime'] )
219 // Total number of edits
220 $table = $this->addRow( $table,
221 $this->msg( 'pageinfo-edits' )->escaped(), $lang->formatNum( $pageInfo['edits'] )
224 // Total number of distinct authors
225 $table = $this->addRow( $table,
226 $this->msg( 'pageinfo-authors' )->escaped(), $lang->formatNum( $pageInfo['authors'] )
229 // Recent number of edits (within past 30 days)
230 $table = $this->addRow( $table,
231 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) )->escaped(),
232 $lang->formatNum( $pageInfo['recent_edits'] )
235 // Recent number of distinct authors
236 $table = $this->addRow( $table,
237 $this->msg( 'pageinfo-recent-authors' )->escaped(), $lang->formatNum( $pageInfo['recent_authors'] )
240 $content = $this->addTable( $content, $table );
242 // Array of MagicWord objects
243 $magicWords = MagicWord
::getDoubleUnderscoreArray();
245 // Array of magic word IDs
246 $wordIDs = $magicWords->names
;
248 // @todo FIXME: Should report in content language, as localised
249 // magic words cannot be used.
250 // Array of IDs => localized magic words
251 $localizedWords = $lang->getMagicWords();
253 $listItems = array();
254 foreach ( $pageProperties as $property => $value ) {
255 if ( in_array( $property, $wordIDs ) ) {
256 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
260 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
261 $hiddenCategories = $article->getHiddenCategories();
262 $transcludedTemplates = $title->getTemplateLinksFrom();
264 if ( count( $listItems ) > 0
265 ||
count( $hiddenCategories ) > 0
266 ||
count( $transcludedTemplates ) > 0 ) {
268 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-properties' )->text() );
272 if ( count( $listItems ) > 0 ) {
273 $table = $this->addRow( $table,
274 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) )->escaped(),
279 // Hide "This page is a member of # hidden categories explanation
280 $content .= Html
::element( 'style', array(),
281 '.mw-hiddenCategoriesExplanation { display: none; }' );
284 if ( count( $hiddenCategories ) > 0 ) {
285 $table = $this->addRow( $table,
286 $this->msg( 'pageinfo-hidden-categories' )
287 ->numParams( count( $hiddenCategories ) )->escaped(),
288 Linker
::formatHiddenCategories( $hiddenCategories )
292 // Hide "Templates used on this page:" explanation
293 $content .= Html
::element( 'style', array(),
294 '.mw-templatesUsedExplanation { display: none; }' );
296 // Transcluded templates
297 if ( count( $transcludedTemplates ) > 0 ) {
298 $table = $this->addRow( $table,
299 $this->msg( 'pageinfo-templates' )
300 ->numParams( count( $transcludedTemplates ) )->escaped(),
301 Linker
::formatTemplates( $transcludedTemplates )
305 $content = $this->addTable( $content, $table );
312 * Returns page information that would be too "expensive" to retrieve by normal means.
314 * @param $title Title object
315 * @param $canViewUnwatched bool
316 * @param $disableCounter bool
319 public static function pageCountInfo( $title, $canViewUnwatched, $disableCounter ) {
322 wfProfileIn( __METHOD__
);
323 $id = $title->getArticleID();
325 $dbr = wfGetDB( DB_SLAVE
);
328 if ( !$disableCounter ) {
330 $views = (int) $dbr->selectField(
333 array( 'page_id' => $id ),
336 $result['views'] = $views;
339 if ( $canViewUnwatched ) {
340 // Number of page watchers
341 $watchers = (int) $dbr->selectField(
345 'wl_namespace' => $title->getNamespace(),
346 'wl_title' => $title->getDBkey(),
350 $result['watchers'] = $watchers;
353 // Total number of edits
354 $edits = (int) $dbr->selectField(
357 array( 'rev_page' => $id ),
360 $result['edits'] = $edits;
362 // Total number of distinct authors
363 $authors = (int) $dbr->selectField(
365 'COUNT(DISTINCT rev_user_text)',
366 array( 'rev_page' => $id ),
369 $result['authors'] = $authors;
371 // "Recent" threshold defined by $wgRCMaxAge
372 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
374 // Recent number of edits
375 $edits = (int) $dbr->selectField(
380 "rev_timestamp >= $threshold"
384 $result['recent_edits'] = $edits;
386 // Recent number of distinct authors
387 $authors = (int) $dbr->selectField(
389 'COUNT(DISTINCT rev_user_text)',
392 "rev_timestamp >= $threshold"
396 $result['recent_authors'] = $authors;
398 $conds = array( 'page_namespace' => $title->getNamespace(), 'page_is_redirect' => 1 );
399 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
401 // Subpages of this page (redirects)
402 $result['subpages']['redirects'] = (int) $dbr->selectField(
408 // Subpages of this page (non-redirects)
409 $conds['page_is_redirect'] = 0;
410 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
417 // Subpages of this page (total)
418 $result['subpages']['total'] = $result['subpages']['redirects']
419 +
$result['subpages']['nonredirects'];
421 // Latest editor + date of latest edit
422 $options = array( 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 1 );
423 $row = $dbr->fetchRow( $dbr->select(
425 array( 'rev_user_text', 'rev_timestamp' ),
426 array( 'rev_page' => $id ),
431 $result['firstuser'] = $row['rev_user_text'];
432 $result['firsttime'] = $row['rev_timestamp'];
434 // Latest editor + date of latest edit
435 $options['ORDER BY'] = 'rev_timestamp DESC';
436 $row = $dbr->fetchRow( $dbr->select(
438 array( 'rev_user_text', 'rev_timestamp' ),
439 array( 'rev_page' => $id ),
444 $result['lastuser'] = $row['rev_user_text'];
445 $result['lasttime'] = $row['rev_timestamp'];
447 wfProfileOut( __METHOD__
);
452 * Adds a header to the content that will be added to the output.
454 * @param $content string The content that will be added to the output
455 * @param $header string The value of the header
456 * @return string The content with the header added
458 protected function addHeader( $content, $header ) {
459 return $content . Html
::element( 'h2', array(), $header );
463 * Adds a row to a table that will be added to the content.
465 * @param $table string The table that will be added to the content
466 * @param $name string The name of the row
467 * @param $value string The value of the row
468 * @return string The table with the row added
470 protected function addRow( $table, $name, $value ) {
471 return $table . Html
::rawElement( 'tr', array(),
472 Html
::rawElement( 'td', array(), $name ) .
473 Html
::rawElement( 'td', array(), $value )
478 * Adds a table to the content that will be added to the output.
480 * @param $content string The content that will be added to the output
481 * @param $table string The table
482 * @return string The content with the table added
484 protected function addTable( $content, $table ) {
485 return $content . Html
::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
490 * Returns the description that goes below the <h1> tag.
494 protected function getDescription() {
499 * Returns the name that goes in the <h1> page title.
503 protected function getPageTitle() {
504 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();