(bug 40820) Revert my changes to includes/actions/CreditsAction.php
[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 $content = '';
60
61 // Page header
62 if ( !$this->msg( 'pageinfo-header' )->isDisabled() ) {
63 $content .= $this->msg( 'pageinfo-header' )->parse();
64 }
65
66 // Hide "This page is a member of # hidden categories" explanation
67 $content .= Html::element( 'style', array(),
68 '.mw-hiddenCategoriesExplanation { display: none; }' );
69
70 // Hide "Templates used on this page" explanation
71 $content .= Html::element( 'style', array(),
72 '.mw-templatesUsedExplanation { display: none; }' );
73
74 // Get page information
75 $title = $this->getTitle();
76 $pageInfo = $this->pageInfo( $title );
77
78 // Allow extensions to add additional information
79 wfRunHooks( 'InfoAction', array( &$pageInfo ) );
80
81 // Render page information
82 foreach ( $pageInfo as $header => $infoTable ) {
83 $content .= $this->makeHeader( $this->msg( "pageinfo-${header}" )->escaped() );
84 $table = '';
85 foreach ( $infoTable as $infoRow ) {
86 $name = ( $infoRow[0] instanceof Message ) ? $infoRow[0]->escaped() : $infoRow[0];
87 $value = ( $infoRow[1] instanceof Message ) ? $infoRow[1]->escaped() : $infoRow[1];
88 $table = $this->addRow( $table, $name, $value );
89 }
90 $content = $this->addTable( $content, $table );
91 }
92
93 // Page footer
94 if ( !$this->msg( 'pageinfo-footer' )->isDisabled() ) {
95 $content .= $this->msg( 'pageinfo-footer' )->parse();
96 }
97
98 // Page credits
99 /*if ( $title->exists() ) {
100 $content .= Html::rawElement( 'div', array( 'id' => 'mw-credits' ), $this->getContributors() );
101 }*/
102
103 return $content;
104 }
105
106 /**
107 * Creates a header that can be added to the output.
108 *
109 * @param $header The header text.
110 * @return string The HTML.
111 */
112 protected function makeHeader( $header ) {
113 global $wgParser;
114 $spanAttribs = array( 'class' => 'mw-headline', 'id' => $wgParser->guessSectionNameFromWikiText( $header ) );
115 return Html::rawElement( 'h2', array(), Html::element( 'span', $spanAttribs, $header ) );
116 }
117
118 /**
119 * Adds a row to a table that will be added to the content.
120 *
121 * @param $table string The table that will be added to the content
122 * @param $name string The name of the row
123 * @param $value string The value of the row
124 * @return string The table with the row added
125 */
126 protected function addRow( $table, $name, $value ) {
127 return $table . Html::rawElement( 'tr', array(),
128 Html::rawElement( 'td', array( 'style' => 'vertical-align: top;' ), $name ) .
129 Html::rawElement( 'td', array(), $value )
130 );
131 }
132
133 /**
134 * Adds a table to the content that will be added to the output.
135 *
136 * @param $content string The content that will be added to the output
137 * @param $table string The table
138 * @return string The content with the table added
139 */
140 protected function addTable( $content, $table ) {
141 return $content . Html::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
142 $table );
143 }
144
145 /**
146 * Returns page information in an easily-manipulated format. Array keys are used so extensions
147 * may add additional information in arbitrary positions. Array values are arrays with one
148 * element to be rendered as a header, arrays with two elements to be rendered as a table row.
149 *
150 * @param $title Title object
151 */
152 protected function pageInfo( $title ) {
153 global $wgContLang, $wgDisableCounters, $wgRCMaxAge;
154
155 $user = $this->getUser();
156 $lang = $this->getLanguage();
157 $title = $this->getTitle();
158 $id = $title->getArticleID();
159
160 // Get page information that would be too "expensive" to retrieve by normal means
161 $userCanViewUnwatchedPages = $user->isAllowed( 'unwatchedpages' );
162 $pageCounts = self::pageCounts( $title, $userCanViewUnwatchedPages, $wgDisableCounters );
163
164 // Get page properties
165 $dbr = wfGetDB( DB_SLAVE );
166 $result = $dbr->select(
167 'page_props',
168 array( 'pp_propname', 'pp_value' ),
169 array( 'pp_page' => $id ),
170 __METHOD__
171 );
172
173 $pageProperties = array();
174 foreach ( $result as $row ) {
175 $pageProperties[$row->pp_propname] = $row->pp_value;
176 }
177
178 // Basic information
179 $pageInfo = array();
180 $pageInfo['header-basic'] = array();
181
182 // Display title
183 $displayTitle = $title->getPrefixedText();
184 if ( !empty( $pageProperties['displaytitle'] ) ) {
185 $displayTitle = $pageProperties['displaytitle'];
186 }
187
188 $pageInfo['header-basic'][] = array(
189 $this->msg( 'pageinfo-display-title' ), $displayTitle
190 );
191
192 // Default sort key
193 $sortKey = $title->getCategorySortKey();
194 if ( !empty( $pageProperties['defaultsort'] ) ) {
195 $sortKey = $pageProperties['defaultsort'];
196 }
197
198 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-default-sort' ), $sortKey );
199
200 // Page length (in bytes)
201 $pageInfo['header-basic'][] = array(
202 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
203 );
204
205 // Page ID (number not localised, as it's a database ID)
206 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-article-id' ), $id );
207
208 // Search engine status
209 $pOutput = new ParserOutput();
210 if ( isset( $pageProperties['noindex'] ) ) {
211 $pOutput->setIndexPolicy( 'noindex' );
212 }
213
214 // Use robot policy logic
215 $policy = $this->page->getRobotPolicy( 'view', $pOutput );
216 $pageInfo['header-basic'][] = array(
217 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
218 );
219
220 if ( !$wgDisableCounters ) {
221 // Number of views
222 $pageInfo['header-basic'][] = array(
223 $this->msg( 'pageinfo-views' ), $lang->formatNum( $pageCounts['views'] )
224 );
225 }
226
227 if ( $userCanViewUnwatchedPages ) {
228 // Number of page watchers
229 $pageInfo['header-basic'][] = array(
230 $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
231 );
232 }
233
234 // Redirects to this page
235 $whatLinksHere = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
236 $pageInfo['header-basic'][] = array(
237 Linker::link(
238 $whatLinksHere,
239 $this->msg( 'pageinfo-redirects-name' )->escaped(),
240 array(),
241 array( 'hidelinks' => 1, 'hidetrans' => 1 )
242 ),
243 $this->msg( 'pageinfo-redirects-value' )
244 ->numParams( count( $title->getRedirectsHere() ) )
245 );
246
247 // Subpages of this page, if subpages are enabled for the current NS
248 if ( MWNamespace::hasSubpages( $title->getNamespace() ) ) {
249 $prefixIndex = SpecialPage::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
250 $pageInfo['header-basic'][] = array(
251 Linker::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
252 $this->msg( 'pageinfo-subpages-value' )
253 ->numParams(
254 $pageCounts['subpages']['total'],
255 $pageCounts['subpages']['redirects'],
256 $pageCounts['subpages']['nonredirects'] )
257 );
258 }
259
260 // Page protection
261 $pageInfo['header-restrictions'] = array();
262
263 // Page protection
264 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
265 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
266
267 if ( $protectionLevel == '' ) {
268 // Allow all users
269 $message = $this->msg( 'protect-default' )->escaped();
270 } else {
271 // Administrators only
272 $message = $this->msg( "protect-level-$protectionLevel" );
273 if ( $message->isDisabled() ) {
274 // Require "$1" permission
275 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
276 } else {
277 $message = $message->escaped();
278 }
279 }
280
281 $pageInfo['header-restrictions'][] = array(
282 $this->msg( "restriction-$restrictionType" ), $message
283 );
284 }
285
286 // Edit history
287 $pageInfo['header-edits'] = array();
288
289 $firstRev = $this->page->getOldestRevision();
290
291 // Page creator
292 $pageInfo['header-edits'][] = array(
293 $this->msg( 'pageinfo-firstuser' ),
294 Linker::revUserTools( $firstRev )
295 );
296
297 // Date of page creation
298 $pageInfo['header-edits'][] = array(
299 $this->msg( 'pageinfo-firsttime' ),
300 Linker::linkKnown(
301 $title,
302 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
303 array(),
304 array( 'oldid' => $firstRev->getId() )
305 )
306 );
307
308 // Latest editor
309 $pageInfo['header-edits'][] = array(
310 $this->msg( 'pageinfo-lastuser' ),
311 Linker::revUserTools( $this->page->getRevision() )
312 );
313
314 // Date of latest edit
315 $pageInfo['header-edits'][] = array(
316 $this->msg( 'pageinfo-lasttime' ),
317 Linker::linkKnown(
318 $title,
319 $lang->userTimeAndDate( $this->page->getTimestamp(), $user ),
320 array(),
321 array( 'oldid' => $this->page->getLatest() )
322 )
323 );
324
325 // Total number of edits
326 $pageInfo['header-edits'][] = array(
327 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
328 );
329
330 // Total number of distinct authors
331 $pageInfo['header-edits'][] = array(
332 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
333 );
334
335 // Recent number of edits (within past 30 days)
336 $pageInfo['header-edits'][] = array(
337 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
338 $lang->formatNum( $pageCounts['recent_edits'] )
339 );
340
341 // Recent number of distinct authors
342 $pageInfo['header-edits'][] = array(
343 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
344 );
345
346 // Array of MagicWord objects
347 $magicWords = MagicWord::getDoubleUnderscoreArray();
348
349 // Array of magic word IDs
350 $wordIDs = $magicWords->names;
351
352 // Array of IDs => localized magic words
353 $localizedWords = $wgContLang->getMagicWords();
354
355 $listItems = array();
356 foreach ( $pageProperties as $property => $value ) {
357 if ( in_array( $property, $wordIDs ) ) {
358 $listItems[] = Html::element( 'li', array(), $localizedWords[$property][1] );
359 }
360 }
361
362 $localizedList = Html::rawElement( 'ul', array(), implode( '', $listItems ) );
363 $hiddenCategories = $this->page->getHiddenCategories();
364 $transcludedTemplates = $title->getTemplateLinksFrom();
365
366 if ( count( $listItems ) > 0
367 || count( $hiddenCategories ) > 0
368 || count( $transcludedTemplates ) > 0 ) {
369 // Page properties
370 $pageInfo['header-properties'] = array();
371
372 // Magic words
373 if ( count( $listItems ) > 0 ) {
374 $pageInfo['header-properties'][] = array(
375 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
376 $localizedList
377 );
378 }
379
380 // Hidden categories
381 if ( count( $hiddenCategories ) > 0 ) {
382 $pageInfo['header-properties'][] = array(
383 $this->msg( 'pageinfo-hidden-categories' )
384 ->numParams( count( $hiddenCategories ) ),
385 Linker::formatHiddenCategories( $hiddenCategories )
386 );
387 }
388
389 // Transcluded templates
390 if ( count( $transcludedTemplates ) > 0 ) {
391 $pageInfo['header-properties'][] = array(
392 $this->msg( 'pageinfo-templates' )
393 ->numParams( count( $transcludedTemplates ) ),
394 Linker::formatTemplates( $transcludedTemplates )
395 );
396 }
397 }
398
399 return $pageInfo;
400 }
401
402 /**
403 * Returns page counts that would be too "expensive" to retrieve by normal means.
404 *
405 * @param $title Title object
406 * @param $canViewUnwatched bool
407 * @param $disableCounter bool
408 * @return array
409 */
410 protected static function pageCounts( $title, $canViewUnwatched, $disableCounter ) {
411 global $wgRCMaxAge;
412
413 wfProfileIn( __METHOD__ );
414 $id = $title->getArticleID();
415
416 $dbr = wfGetDB( DB_SLAVE );
417 $result = array();
418
419 if ( !$disableCounter ) {
420 // Number of views
421 $views = (int) $dbr->selectField(
422 'page',
423 'page_counter',
424 array( 'page_id' => $id ),
425 __METHOD__
426 );
427 $result['views'] = $views;
428 }
429
430 if ( $canViewUnwatched ) {
431 // Number of page watchers
432 $watchers = (int) $dbr->selectField(
433 'watchlist',
434 'COUNT(*)',
435 array(
436 'wl_namespace' => $title->getNamespace(),
437 'wl_title' => $title->getDBkey(),
438 ),
439 __METHOD__
440 );
441 $result['watchers'] = $watchers;
442 }
443
444 // Total number of edits
445 $edits = (int) $dbr->selectField(
446 'revision',
447 'COUNT(rev_page)',
448 array( 'rev_page' => $id ),
449 __METHOD__
450 );
451 $result['edits'] = $edits;
452
453 // Total number of distinct authors
454 $authors = (int) $dbr->selectField(
455 'revision',
456 'COUNT(DISTINCT rev_user_text)',
457 array( 'rev_page' => $id ),
458 __METHOD__
459 );
460 $result['authors'] = $authors;
461
462 // "Recent" threshold defined by $wgRCMaxAge
463 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
464
465 // Recent number of edits
466 $edits = (int) $dbr->selectField(
467 'revision',
468 'COUNT(rev_page)',
469 array(
470 'rev_page' => $id ,
471 "rev_timestamp >= $threshold"
472 ),
473 __METHOD__
474 );
475 $result['recent_edits'] = $edits;
476
477 // Recent number of distinct authors
478 $authors = (int) $dbr->selectField(
479 'revision',
480 'COUNT(DISTINCT rev_user_text)',
481 array(
482 'rev_page' => $id,
483 "rev_timestamp >= $threshold"
484 ),
485 __METHOD__
486 );
487 $result['recent_authors'] = $authors;
488
489 // Subpages (if enabled)
490 if ( MWNamespace::hasSubpages( $title->getNamespace() ) ) {
491 $conds = array( 'page_namespace' => $title->getNamespace() );
492 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
493
494 // Subpages of this page (redirects)
495 $conds['page_is_redirect'] = 1;
496 $result['subpages']['redirects'] = (int) $dbr->selectField(
497 'page',
498 'COUNT(page_id)',
499 $conds,
500 __METHOD__ );
501
502 // Subpages of this page (non-redirects)
503 $conds['page_is_redirect'] = 0;
504 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
505 'page',
506 'COUNT(page_id)',
507 $conds,
508 __METHOD__
509 );
510
511 // Subpages of this page (total)
512 $result['subpages']['total'] = $result['subpages']['redirects']
513 + $result['subpages']['nonredirects'];
514 }
515
516 wfProfileOut( __METHOD__ );
517 return $result;
518 }
519
520 /**
521 * Returns the name that goes in the <h1> page title.
522 *
523 * @return string
524 */
525 protected function getPageTitle() {
526 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
527 }
528
529 /**
530 * Get a list of contributors of $article
531 * @return string: html
532 */
533 protected function getContributors() {
534 global $wgHiddenPrefs;
535
536 $contributors = $this->page->getContributors();
537 $real_names = array();
538 $user_names = array();
539 $anon_ips = array();
540
541 # Sift for real versus user names
542 foreach ( $contributors as $user ) {
543 $page = $user->isAnon()
544 ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
545 : $user->getUserPage();
546
547 if ( $user->getID() == 0 ) {
548 $anon_ips[] = Linker::link( $page, htmlspecialchars( $user->getName() ) );
549 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
550 $real_names[] = Linker::link( $page, htmlspecialchars( $user->getRealName() ) );
551 } else {
552 $user_names[] = Linker::link( $page, htmlspecialchars( $user->getName() ) );
553 }
554 }
555
556 $lang = $this->getLanguage();
557
558 $real = $lang->listToText( $real_names );
559
560 # "ThisSite user(s) A, B and C"
561 if ( count( $user_names ) ) {
562 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
563 count( $user_names ) )->escaped();
564 } else {
565 $user = false;
566 }
567
568 if ( count( $anon_ips ) ) {
569 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
570 count( $anon_ips ) )->escaped();
571 } else {
572 $anon = false;
573 }
574
575 # This is the big list, all mooshed together. We sift for blank strings
576 $fulllist = array();
577 foreach ( array( $real, $user, $anon ) as $s ) {
578 if ( $s !== '' ) {
579 array_push( $fulllist, $s );
580 }
581 }
582
583 $count = count( $fulllist );
584 # "Based on work by ..."
585 return $count
586 ? $this->msg( 'othercontribs' )->rawParams(
587 $lang->listToText( $fulllist ) )->params( $count )->escaped()
588 : '';
589 }
590
591 /**
592 * Returns the description that goes below the <h1> tag.
593 *
594 * @return string
595 */
596 protected function getDescription() {
597 return '';
598 }
599 }