Merge "Follow-up I0b781c11 (2a55449): use User::getAutomaticGroups()."
[lhc/web/wiklou.git] / includes / actions / InfoAction.php
1 <?php
2 /**
3 * Displays information about a page.
4 *
5 * Copyright © 2011 Alexandre Emsenhuber
6 *
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.
11 *
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.
16 *
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
20 *
21 * @file
22 * @ingroup Actions
23 */
24
25 class InfoAction extends FormlessAction {
26 /**
27 * Returns the name of the action this object responds to.
28 *
29 * @return string lowercase
30 */
31 public function getName() {
32 return 'info';
33 }
34
35 /**
36 * Whether this action can still be executed by a blocked user.
37 *
38 * @return bool
39 */
40 public function requiresUnblock() {
41 return false;
42 }
43
44 /**
45 * Whether this action requires the wiki not to be locked.
46 *
47 * @return bool
48 */
49 public function requiresWrite() {
50 return false;
51 }
52
53 /**
54 * Shows page information on GET request.
55 *
56 * @return string Page information that will be added to the output
57 */
58 public function onView() {
59 global $wgContLang, $wgDisableCounters, $wgRCMaxAge, $wgRestrictionTypes;
60
61 $user = $this->getUser();
62 $lang = $this->getLanguage();
63 $title = $this->getTitle();
64 $id = $title->getArticleID();
65
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 );
69
70 // Get page properties
71 $dbr = wfGetDB( DB_SLAVE );
72 $result = $dbr->select(
73 'page_props',
74 array( 'pp_propname', 'pp_value' ),
75 array( 'pp_page' => $id ),
76 __METHOD__
77 );
78
79 $pageProperties = array();
80 foreach ( $result as $row ) {
81 $pageProperties[$row->pp_propname] = $row->pp_value;
82 }
83
84 $content = '';
85 $table = '';
86
87 // Header
88 if ( !$this->msg( 'pageinfo-header' )->isDisabled() ) {
89 $content .= $this->msg( 'pageinfo-header ' )->parse();
90 }
91
92 // Basic information
93 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-basic' )->text() );
94
95 // Display title
96 $displayTitle = $title->getPrefixedText();
97 if ( !empty( $pageProperties['displaytitle'] ) ) {
98 $displayTitle = $pageProperties['displaytitle'];
99 }
100
101 $table = $this->addRow( $table,
102 $this->msg( 'pageinfo-display-title' )->escaped(), $displayTitle );
103
104 // Default sort key
105 $sortKey = $title->getCategorySortKey();
106 if ( !empty( $pageProperties['defaultsort'] ) ) {
107 $sortKey = $pageProperties['defaultsort'];
108 }
109
110 $table = $this->addRow( $table,
111 $this->msg( 'pageinfo-default-sort' )->escaped(), $sortKey );
112
113 // Page length (in bytes)
114 $table = $this->addRow( $table,
115 $this->msg( 'pageinfo-length' )->escaped(), $lang->formatNum( $title->getLength() ) );
116
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 );
120
121 // Search engine status
122 $pOutput = new ParserOutput();
123 if ( isset( $pageProperties['noindex'] ) ) {
124 $pOutput->setIndexPolicy( 'noindex' );
125 }
126
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()
132 );
133
134 if ( !$wgDisableCounters ) {
135 // Number of views
136 $table = $this->addRow( $table,
137 $this->msg( 'pageinfo-views' )->escaped(), $lang->formatNum( $pageInfo['views'] )
138 );
139 }
140
141 if ( $userCanViewUnwatchedPages ) {
142 // Number of page watchers
143 $table = $this->addRow( $table,
144 $this->msg( 'pageinfo-watchers' )->escaped(), $lang->formatNum( $pageInfo['watchers'] ) );
145 }
146
147 // Redirects to this page
148 $whatLinksHere = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
149 $table = $this->addRow( $table,
150 Linker::link(
151 $whatLinksHere,
152 $this->msg( 'pageinfo-redirects-name' )->escaped(),
153 array(),
154 array( 'hidelinks' => 1, 'hidetrans' => 1 )
155 ),
156 $this->msg( 'pageinfo-redirects-value' )
157 ->numParams( count( $title->getRedirectsHere() ) )->escaped()
158 );
159
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' )
165 ->numParams(
166 $pageInfo['subpages']['total'],
167 $pageInfo['subpages']['redirects'],
168 $pageInfo['subpages']['nonredirects'] )->escaped()
169 );
170
171 // Page protection
172 $content = $this->addTable( $content, $table );
173 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-restrictions' )->text() );
174 $table = '';
175
176 // Page protection
177 foreach ( $wgRestrictionTypes as $restrictionType ) {
178 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
179 if ( $protectionLevel == '' ) {
180 // Allow all users
181 $message = $this->msg( 'protect-default' )->escaped();
182 } else {
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();
188 } else {
189 $message = $message->escaped();
190 }
191 }
192
193 $table = $this->addRow( $table,
194 $this->msg( 'pageinfo-restriction',
195 $this->msg( "restriction-$restrictionType" )->plain()
196 )->parse(), $message
197 );
198 }
199
200 // Edit history
201 $content = $this->addTable( $content, $table );
202 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-edits' )->text() );
203 $table = '';
204
205 // Page creator
206 $table = $this->addRow( $table,
207 $this->msg( 'pageinfo-firstuser' )->escaped(), $pageInfo['firstuser']
208 );
209
210 // Date of page creation
211 $table = $this->addRow( $table,
212 $this->msg( 'pageinfo-firsttime' )->escaped(), $lang->userTimeAndDate( $pageInfo['firsttime'], $user )
213 );
214
215 // Latest editor
216 $table = $this->addRow( $table,
217 $this->msg( 'pageinfo-lastuser' )->escaped(), $pageInfo['lastuser']
218 );
219
220 // Date of latest edit
221 $table = $this->addRow( $table,
222 $this->msg( 'pageinfo-lasttime' )->escaped(), $lang->userTimeAndDate( $pageInfo['lasttime'], $user )
223 );
224
225 // Total number of edits
226 $table = $this->addRow( $table,
227 $this->msg( 'pageinfo-edits' )->escaped(), $lang->formatNum( $pageInfo['edits'] )
228 );
229
230 // Total number of distinct authors
231 $table = $this->addRow( $table,
232 $this->msg( 'pageinfo-authors' )->escaped(), $lang->formatNum( $pageInfo['authors'] )
233 );
234
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'] )
239 );
240
241 // Recent number of distinct authors
242 $table = $this->addRow( $table,
243 $this->msg( 'pageinfo-recent-authors' )->escaped(), $lang->formatNum( $pageInfo['recent_authors'] )
244 );
245
246 $content = $this->addTable( $content, $table );
247
248 // Array of MagicWord objects
249 $magicWords = MagicWord::getDoubleUnderscoreArray();
250
251 // Array of magic word IDs
252 $wordIDs = $magicWords->names;
253
254 // Array of IDs => localized magic words
255 $localizedWords = $wgContLang->getMagicWords();
256
257 $listItems = array();
258 foreach ( $pageProperties as $property => $value ) {
259 if ( in_array( $property, $wordIDs ) ) {
260 $listItems[] = Html::element( 'li', array(), $localizedWords[$property][1] );
261 }
262 }
263
264 $localizedList = Html::rawElement( 'ul', array(), implode( '', $listItems ) );
265 $hiddenCategories = $this->page->getHiddenCategories();
266 $transcludedTemplates = $title->getTemplateLinksFrom();
267
268 if ( count( $listItems ) > 0
269 || count( $hiddenCategories ) > 0
270 || count( $transcludedTemplates ) > 0 ) {
271 // Page properties
272 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-properties' )->text() );
273 $table = '';
274
275 // Magic words
276 if ( count( $listItems ) > 0 ) {
277 $table = $this->addRow( $table,
278 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) )->escaped(),
279 $localizedList
280 );
281 }
282
283 // Hide "This page is a member of # hidden categories explanation
284 $content .= Html::element( 'style', array(),
285 '.mw-hiddenCategoriesExplanation { display: none; }' );
286
287 // Hidden categories
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 )
293 );
294 }
295
296 // Hide "Templates used on this page:" explanation
297 $content .= Html::element( 'style', array(),
298 '.mw-templatesUsedExplanation { display: none; }' );
299
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 )
306 );
307 }
308
309 $content = $this->addTable( $content, $table );
310 }
311
312 // Footer
313 if ( !$this->msg( 'pageinfo-footer' )->isDisabled() ) {
314 $content .= $this->msg( 'pageinfo-footer' )->parse();
315 }
316
317 return $content;
318 }
319
320 /**
321 * Returns page information that would be too "expensive" to retrieve by normal means.
322 *
323 * @param $title Title object
324 * @param $canViewUnwatched bool
325 * @param $disableCounter bool
326 * @return array
327 */
328 public static function pageCountInfo( $title, $canViewUnwatched, $disableCounter ) {
329 global $wgRCMaxAge;
330
331 wfProfileIn( __METHOD__ );
332 $id = $title->getArticleID();
333
334 $dbr = wfGetDB( DB_SLAVE );
335 $result = array();
336
337 if ( !$disableCounter ) {
338 // Number of views
339 $views = (int) $dbr->selectField(
340 'page',
341 'page_counter',
342 array( 'page_id' => $id ),
343 __METHOD__
344 );
345 $result['views'] = $views;
346 }
347
348 if ( $canViewUnwatched ) {
349 // Number of page watchers
350 $watchers = (int) $dbr->selectField(
351 'watchlist',
352 'COUNT(*)',
353 array(
354 'wl_namespace' => $title->getNamespace(),
355 'wl_title' => $title->getDBkey(),
356 ),
357 __METHOD__
358 );
359 $result['watchers'] = $watchers;
360 }
361
362 // Total number of edits
363 $edits = (int) $dbr->selectField(
364 'revision',
365 'COUNT(rev_page)',
366 array( 'rev_page' => $id ),
367 __METHOD__
368 );
369 $result['edits'] = $edits;
370
371 // Total number of distinct authors
372 $authors = (int) $dbr->selectField(
373 'revision',
374 'COUNT(DISTINCT rev_user_text)',
375 array( 'rev_page' => $id ),
376 __METHOD__
377 );
378 $result['authors'] = $authors;
379
380 // "Recent" threshold defined by $wgRCMaxAge
381 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
382
383 // Recent number of edits
384 $edits = (int) $dbr->selectField(
385 'revision',
386 'COUNT(rev_page)',
387 array(
388 'rev_page' => $id ,
389 "rev_timestamp >= $threshold"
390 ),
391 __METHOD__
392 );
393 $result['recent_edits'] = $edits;
394
395 // Recent number of distinct authors
396 $authors = (int) $dbr->selectField(
397 'revision',
398 'COUNT(DISTINCT rev_user_text)',
399 array(
400 'rev_page' => $id,
401 "rev_timestamp >= $threshold"
402 ),
403 __METHOD__
404 );
405 $result['recent_authors'] = $authors;
406
407 $conds = array( 'page_namespace' => $title->getNamespace(), 'page_is_redirect' => 1 );
408 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
409
410 // Subpages of this page (redirects)
411 $result['subpages']['redirects'] = (int) $dbr->selectField(
412 'page',
413 'COUNT(page_id)',
414 $conds,
415 __METHOD__ );
416
417 // Subpages of this page (non-redirects)
418 $conds['page_is_redirect'] = 0;
419 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
420 'page',
421 'COUNT(page_id)',
422 $conds,
423 __METHOD__
424 );
425
426 // Subpages of this page (total)
427 $result['subpages']['total'] = $result['subpages']['redirects']
428 + $result['subpages']['nonredirects'];
429
430 // Latest editor + date of latest edit
431 $options = array( 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 1 );
432 $row = $dbr->fetchRow( $dbr->select(
433 'revision',
434 array( 'rev_user_text', 'rev_timestamp' ),
435 array( 'rev_page' => $id ),
436 __METHOD__,
437 $options
438 ) );
439
440 $result['firstuser'] = $row['rev_user_text'];
441 $result['firsttime'] = $row['rev_timestamp'];
442
443 // Latest editor + date of latest edit
444 $options['ORDER BY'] = 'rev_timestamp DESC';
445 $row = $dbr->fetchRow( $dbr->select(
446 'revision',
447 array( 'rev_user_text', 'rev_timestamp' ),
448 array( 'rev_page' => $id ),
449 __METHOD__,
450 $options
451 ) );
452
453 $result['lastuser'] = $row['rev_user_text'];
454 $result['lasttime'] = $row['rev_timestamp'];
455
456 wfProfileOut( __METHOD__ );
457 return $result;
458 }
459
460 /**
461 * Adds a header to the content that will be added to the output.
462 *
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
466 */
467 protected function addHeader( $content, $header ) {
468 return $content . Html::element( 'h2', array(), $header );
469 }
470
471 /**
472 * Adds a row to a table that will be added to the content.
473 *
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
478 */
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 )
483 );
484 }
485
486 /**
487 * Adds a table to the content that will be added to the output.
488 *
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
492 */
493 protected function addTable( $content, $table ) {
494 return $content . Html::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
495 $table );
496 }
497
498 /**
499 * Returns the description that goes below the <h1> tag.
500 *
501 * @return string
502 */
503 protected function getDescription() {
504 return '';
505 }
506
507 /**
508 * Returns the name that goes in the <h1> page title.
509 *
510 * @return string
511 */
512 protected function getPageTitle() {
513 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
514 }
515 }