19a0b06ea47450b19034a3f70484cd1a3bac6f4c
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 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-basic' )->text() );
91 $displayTitle = $title->getPrefixedText();
92 if ( !empty( $pageProperties['displaytitle'] ) ) {
93 $displayTitle = $pageProperties['displaytitle'];
96 $table = $this->addRow( $table,
97 $this->msg( 'pageinfo-display-title' )->escaped(), $displayTitle );
100 $sortKey = $title->getCategorySortKey();
101 if ( !empty( $pageProperties['defaultsort'] ) ) {
102 $sortKey = $pageProperties['defaultsort'];
105 $table = $this->addRow( $table,
106 $this->msg( 'pageinfo-default-sort' )->escaped(), $sortKey );
108 // Page length (in bytes)
109 $table = $this->addRow( $table,
110 $this->msg( 'pageinfo-length' )->escaped(), $lang->formatNum( $title->getLength() ) );
112 // Page ID (number not localised, as it's a database ID.)
113 $table = $this->addRow( $table,
114 $this->msg( 'pageinfo-article-id' )->escaped(), $id );
116 // Search engine status
117 $pOutput = new ParserOutput();
118 if ( isset( $pageProperties['noindex'] ) ) {
119 $pOutput->setIndexPolicy( 'noindex' );
122 // Use robot policy logic
123 $policy = $this->page
->getRobotPolicy( 'view', $pOutput );
124 $table = $this->addRow( $table,
125 $this->msg( 'pageinfo-robot-policy' )->escaped(),
126 $this->msg( "pageinfo-robot-${policy['index']}" )->escaped()
129 if ( !$wgDisableCounters ) {
131 $table = $this->addRow( $table,
132 $this->msg( 'pageinfo-views' )->escaped(), $lang->formatNum( $pageInfo['views'] )
136 if ( $userCanViewUnwatchedPages ) {
137 // Number of page watchers
138 $table = $this->addRow( $table,
139 $this->msg( 'pageinfo-watchers' )->escaped(), $lang->formatNum( $pageInfo['watchers'] ) );
142 // Redirects to this page
143 $whatLinksHere = SpecialPage
::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
144 $table = $this->addRow( $table,
147 $this->msg( 'pageinfo-redirects-name' )->escaped(),
149 array( 'hidelinks' => 1, 'hidetrans' => 1 )
151 $this->msg( 'pageinfo-redirects-value' )
152 ->numParams( count( $title->getRedirectsHere() ) )->escaped()
155 // Subpages of this page
156 $prefixIndex = SpecialPage
::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
157 $table = $this->addRow( $table,
158 Linker
::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
159 $this->msg( 'pageinfo-subpages-value' )
161 $pageInfo['subpages']['total'],
162 $pageInfo['subpages']['redirects'],
163 $pageInfo['subpages']['nonredirects'] )->escaped()
167 $content = $this->addTable( $content, $table );
168 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-restrictions' )->text() );
172 foreach ( $wgRestrictionTypes as $restrictionType ) {
173 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
174 if ( $protectionLevel == '' ) {
176 $message = $this->msg( 'protect-default' )->escaped();
178 // Administrators only
179 $message = $this->msg( "protect-level-$protectionLevel" );
180 if ( $message->isDisabled() ) {
181 // Require "$1" permission
182 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
184 $message = $message->escaped();
188 $table = $this->addRow( $table,
189 $this->msg( 'pageinfo-restriction',
190 $this->msg( "restriction-$restrictionType" )->plain()
196 $content = $this->addTable( $content, $table );
197 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-edits' )->text() );
201 $table = $this->addRow( $table,
202 $this->msg( 'pageinfo-firstuser' )->escaped(), $pageInfo['firstuser']
205 // Date of page creation
206 $table = $this->addRow( $table,
207 $this->msg( 'pageinfo-firsttime' )->escaped(), $lang->userTimeAndDate( $pageInfo['firsttime'], $user )
211 $table = $this->addRow( $table,
212 $this->msg( 'pageinfo-lastuser' )->escaped(), $pageInfo['lastuser']
215 // Date of latest edit
216 $table = $this->addRow( $table,
217 $this->msg( 'pageinfo-lasttime' )->escaped(), $lang->userTimeAndDate( $pageInfo['lasttime'], $user )
220 // Total number of edits
221 $table = $this->addRow( $table,
222 $this->msg( 'pageinfo-edits' )->escaped(), $lang->formatNum( $pageInfo['edits'] )
225 // Total number of distinct authors
226 $table = $this->addRow( $table,
227 $this->msg( 'pageinfo-authors' )->escaped(), $lang->formatNum( $pageInfo['authors'] )
230 // Recent number of edits (within past 30 days)
231 $table = $this->addRow( $table,
232 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) )->escaped(),
233 $lang->formatNum( $pageInfo['recent_edits'] )
236 // Recent number of distinct authors
237 $table = $this->addRow( $table,
238 $this->msg( 'pageinfo-recent-authors' )->escaped(), $lang->formatNum( $pageInfo['recent_authors'] )
241 $content = $this->addTable( $content, $table );
243 // Array of MagicWord objects
244 $magicWords = MagicWord
::getDoubleUnderscoreArray();
246 // Array of magic word IDs
247 $wordIDs = $magicWords->names
;
249 // Array of IDs => localized magic words
250 $localizedWords = $wgContLang->getMagicWords();
252 $listItems = array();
253 foreach ( $pageProperties as $property => $value ) {
254 if ( in_array( $property, $wordIDs ) ) {
255 $listItems[] = Html
::element( 'li', array(), $localizedWords[$property][1] );
259 $localizedList = Html
::rawElement( 'ul', array(), implode( '', $listItems ) );
260 $hiddenCategories = $this->page
->getHiddenCategories();
261 $transcludedTemplates = $title->getTemplateLinksFrom();
263 if ( count( $listItems ) > 0
264 ||
count( $hiddenCategories ) > 0
265 ||
count( $transcludedTemplates ) > 0 ) {
267 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-properties' )->text() );
271 if ( count( $listItems ) > 0 ) {
272 $table = $this->addRow( $table,
273 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) )->escaped(),
278 // Hide "This page is a member of # hidden categories explanation
279 $content .= Html
::element( 'style', array(),
280 '.mw-hiddenCategoriesExplanation { display: none; }' );
283 if ( count( $hiddenCategories ) > 0 ) {
284 $table = $this->addRow( $table,
285 $this->msg( 'pageinfo-hidden-categories' )
286 ->numParams( count( $hiddenCategories ) )->escaped(),
287 Linker
::formatHiddenCategories( $hiddenCategories )
291 // Hide "Templates used on this page:" explanation
292 $content .= Html
::element( 'style', array(),
293 '.mw-templatesUsedExplanation { display: none; }' );
295 // Transcluded templates
296 if ( count( $transcludedTemplates ) > 0 ) {
297 $table = $this->addRow( $table,
298 $this->msg( 'pageinfo-templates' )
299 ->numParams( count( $transcludedTemplates ) )->escaped(),
300 Linker
::formatTemplates( $transcludedTemplates )
304 $content = $this->addTable( $content, $table );
311 * Returns page information that would be too "expensive" to retrieve by normal means.
313 * @param $title Title object
314 * @param $canViewUnwatched bool
315 * @param $disableCounter bool
318 public static function pageCountInfo( $title, $canViewUnwatched, $disableCounter ) {
321 wfProfileIn( __METHOD__
);
322 $id = $title->getArticleID();
324 $dbr = wfGetDB( DB_SLAVE
);
327 if ( !$disableCounter ) {
329 $views = (int) $dbr->selectField(
332 array( 'page_id' => $id ),
335 $result['views'] = $views;
338 if ( $canViewUnwatched ) {
339 // Number of page watchers
340 $watchers = (int) $dbr->selectField(
344 'wl_namespace' => $title->getNamespace(),
345 'wl_title' => $title->getDBkey(),
349 $result['watchers'] = $watchers;
352 // Total number of edits
353 $edits = (int) $dbr->selectField(
356 array( 'rev_page' => $id ),
359 $result['edits'] = $edits;
361 // Total number of distinct authors
362 $authors = (int) $dbr->selectField(
364 'COUNT(DISTINCT rev_user_text)',
365 array( 'rev_page' => $id ),
368 $result['authors'] = $authors;
370 // "Recent" threshold defined by $wgRCMaxAge
371 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
373 // Recent number of edits
374 $edits = (int) $dbr->selectField(
379 "rev_timestamp >= $threshold"
383 $result['recent_edits'] = $edits;
385 // Recent number of distinct authors
386 $authors = (int) $dbr->selectField(
388 'COUNT(DISTINCT rev_user_text)',
391 "rev_timestamp >= $threshold"
395 $result['recent_authors'] = $authors;
397 $conds = array( 'page_namespace' => $title->getNamespace(), 'page_is_redirect' => 1 );
398 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
400 // Subpages of this page (redirects)
401 $result['subpages']['redirects'] = (int) $dbr->selectField(
407 // Subpages of this page (non-redirects)
408 $conds['page_is_redirect'] = 0;
409 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
416 // Subpages of this page (total)
417 $result['subpages']['total'] = $result['subpages']['redirects']
418 +
$result['subpages']['nonredirects'];
420 // Latest editor + date of latest edit
421 $options = array( 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 1 );
422 $row = $dbr->fetchRow( $dbr->select(
424 array( 'rev_user_text', 'rev_timestamp' ),
425 array( 'rev_page' => $id ),
430 $result['firstuser'] = $row['rev_user_text'];
431 $result['firsttime'] = $row['rev_timestamp'];
433 // Latest editor + date of latest edit
434 $options['ORDER BY'] = 'rev_timestamp DESC';
435 $row = $dbr->fetchRow( $dbr->select(
437 array( 'rev_user_text', 'rev_timestamp' ),
438 array( 'rev_page' => $id ),
443 $result['lastuser'] = $row['rev_user_text'];
444 $result['lasttime'] = $row['rev_timestamp'];
446 wfProfileOut( __METHOD__
);
451 * Adds a header to the content that will be added to the output.
453 * @param $content string The content that will be added to the output
454 * @param $header string The value of the header
455 * @return string The content with the header added
457 protected function addHeader( $content, $header ) {
458 return $content . Html
::element( 'h2', array(), $header );
462 * Adds a row to a table that will be added to the content.
464 * @param $table string The table that will be added to the content
465 * @param $name string The name of the row
466 * @param $value string The value of the row
467 * @return string The table with the row added
469 protected function addRow( $table, $name, $value ) {
470 return $table . Html
::rawElement( 'tr', array(),
471 Html
::rawElement( 'td', array(), $name ) .
472 Html
::rawElement( 'td', array(), $value )
477 * Adds a table to the content that will be added to the output.
479 * @param $content string The content that will be added to the output
480 * @param $table string The table
481 * @return string The content with the table added
483 protected function addTable( $content, $table ) {
484 return $content . Html
::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
489 * Returns the description that goes below the <h1> tag.
493 protected function getDescription() {
498 * Returns the name that goes in the <h1> page title.
502 protected function getPageTitle() {
503 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();