Merge "(bug 38559) Add hook to info action to allow extensions to add extra informati...
[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::userLink(
295 $firstRev->getUser( Revision::FOR_THIS_USER, $user ),
296 $firstRev->getUserText( Revision::FOR_THIS_USER, $user )
297 )
298 );
299
300 // Date of page creation
301 $pageInfo['header-edits'][] = array(
302 $this->msg( 'pageinfo-firsttime' ),
303 Linker::linkKnown(
304 $title,
305 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
306 array(),
307 array( 'oldid' => $firstRev->getId() )
308 )
309 );
310
311 // Latest editor
312 $pageInfo['header-edits'][] = array(
313 $this->msg( 'pageinfo-lastuser' ),
314 Linker::userLink(
315 $this->page->getUser( Revision::FOR_THIS_USER, $user ),
316 $this->page->getUserText( Revision::FOR_THIS_USER, $user )
317 )
318 );
319
320 // Date of latest edit
321 $pageInfo['header-edits'][] = array(
322 $this->msg( 'pageinfo-lasttime' ),
323 Linker::linkKnown(
324 $title,
325 $lang->userTimeAndDate( $this->page->getTimestamp(), $user ),
326 array(),
327 array( 'oldid' => $this->page->getLatest() )
328 )
329 );
330
331 // Total number of edits
332 $pageInfo['header-edits'][] = array(
333 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
334 );
335
336 // Total number of distinct authors
337 $pageInfo['header-edits'][] = array(
338 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
339 );
340
341 // Recent number of edits (within past 30 days)
342 $pageInfo['header-edits'][] = array(
343 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
344 $lang->formatNum( $pageCounts['recent_edits'] )
345 );
346
347 // Recent number of distinct authors
348 $pageInfo['header-edits'][] = array(
349 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
350 );
351
352 // Array of MagicWord objects
353 $magicWords = MagicWord::getDoubleUnderscoreArray();
354
355 // Array of magic word IDs
356 $wordIDs = $magicWords->names;
357
358 // Array of IDs => localized magic words
359 $localizedWords = $wgContLang->getMagicWords();
360
361 $listItems = array();
362 foreach ( $pageProperties as $property => $value ) {
363 if ( in_array( $property, $wordIDs ) ) {
364 $listItems[] = Html::element( 'li', array(), $localizedWords[$property][1] );
365 }
366 }
367
368 $localizedList = Html::rawElement( 'ul', array(), implode( '', $listItems ) );
369 $hiddenCategories = $this->page->getHiddenCategories();
370 $transcludedTemplates = $title->getTemplateLinksFrom();
371
372 if ( count( $listItems ) > 0
373 || count( $hiddenCategories ) > 0
374 || count( $transcludedTemplates ) > 0 ) {
375 // Page properties
376 $pageInfo['header-properties'] = array();
377
378 // Magic words
379 if ( count( $listItems ) > 0 ) {
380 $pageInfo['header-properties'][] = array(
381 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
382 $localizedList
383 );
384 }
385
386 // Hidden categories
387 if ( count( $hiddenCategories ) > 0 ) {
388 $pageInfo['header-properties'][] = array(
389 $this->msg( 'pageinfo-hidden-categories' )
390 ->numParams( count( $hiddenCategories ) ),
391 Linker::formatHiddenCategories( $hiddenCategories )
392 );
393 }
394
395 // Transcluded templates
396 if ( count( $transcludedTemplates ) > 0 ) {
397 $pageInfo['header-properties'][] = array(
398 $this->msg( 'pageinfo-templates' )
399 ->numParams( count( $transcludedTemplates ) ),
400 Linker::formatTemplates( $transcludedTemplates )
401 );
402 }
403 }
404
405 return $pageInfo;
406 }
407
408 /**
409 * Returns page counts that would be too "expensive" to retrieve by normal means.
410 *
411 * @param $title Title object
412 * @param $canViewUnwatched bool
413 * @param $disableCounter bool
414 * @return array
415 */
416 protected static function pageCounts( $title, $canViewUnwatched, $disableCounter ) {
417 global $wgRCMaxAge;
418
419 wfProfileIn( __METHOD__ );
420 $id = $title->getArticleID();
421
422 $dbr = wfGetDB( DB_SLAVE );
423 $result = array();
424
425 if ( !$disableCounter ) {
426 // Number of views
427 $views = (int) $dbr->selectField(
428 'page',
429 'page_counter',
430 array( 'page_id' => $id ),
431 __METHOD__
432 );
433 $result['views'] = $views;
434 }
435
436 if ( $canViewUnwatched ) {
437 // Number of page watchers
438 $watchers = (int) $dbr->selectField(
439 'watchlist',
440 'COUNT(*)',
441 array(
442 'wl_namespace' => $title->getNamespace(),
443 'wl_title' => $title->getDBkey(),
444 ),
445 __METHOD__
446 );
447 $result['watchers'] = $watchers;
448 }
449
450 // Total number of edits
451 $edits = (int) $dbr->selectField(
452 'revision',
453 'COUNT(rev_page)',
454 array( 'rev_page' => $id ),
455 __METHOD__
456 );
457 $result['edits'] = $edits;
458
459 // Total number of distinct authors
460 $authors = (int) $dbr->selectField(
461 'revision',
462 'COUNT(DISTINCT rev_user_text)',
463 array( 'rev_page' => $id ),
464 __METHOD__
465 );
466 $result['authors'] = $authors;
467
468 // "Recent" threshold defined by $wgRCMaxAge
469 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
470
471 // Recent number of edits
472 $edits = (int) $dbr->selectField(
473 'revision',
474 'COUNT(rev_page)',
475 array(
476 'rev_page' => $id ,
477 "rev_timestamp >= $threshold"
478 ),
479 __METHOD__
480 );
481 $result['recent_edits'] = $edits;
482
483 // Recent number of distinct authors
484 $authors = (int) $dbr->selectField(
485 'revision',
486 'COUNT(DISTINCT rev_user_text)',
487 array(
488 'rev_page' => $id,
489 "rev_timestamp >= $threshold"
490 ),
491 __METHOD__
492 );
493 $result['recent_authors'] = $authors;
494
495 // Subpages (if enabled)
496 if ( MWNamespace::hasSubpages( $title->getNamespace() ) ) {
497 $conds = array( 'page_namespace' => $title->getNamespace() );
498 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
499
500 // Subpages of this page (redirects)
501 $conds['page_is_redirect'] = 1;
502 $result['subpages']['redirects'] = (int) $dbr->selectField(
503 'page',
504 'COUNT(page_id)',
505 $conds,
506 __METHOD__ );
507
508 // Subpages of this page (non-redirects)
509 $conds['page_is_redirect'] = 0;
510 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
511 'page',
512 'COUNT(page_id)',
513 $conds,
514 __METHOD__
515 );
516
517 // Subpages of this page (total)
518 $result['subpages']['total'] = $result['subpages']['redirects']
519 + $result['subpages']['nonredirects'];
520 }
521
522 wfProfileOut( __METHOD__ );
523 return $result;
524 }
525
526 /**
527 * Returns the name that goes in the <h1> page title.
528 *
529 * @return string
530 */
531 protected function getPageTitle() {
532 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
533 }
534
535 /**
536 * Get a list of contributors of $article
537 * @return string: html
538 */
539 protected function getContributors() {
540 global $wgHiddenPrefs;
541
542 $contributors = $this->page->getContributors();
543 $real_names = array();
544 $user_names = array();
545 $anon_ips = array();
546
547 # Sift for real versus user names
548 foreach ( $contributors as $user ) {
549 $page = $user->isAnon()
550 ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
551 : $user->getUserPage();
552
553 if ( $user->getID() == 0 ) {
554 $anon_ips[] = Linker::link( $page, htmlspecialchars( $user->getName() ) );
555 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
556 $real_names[] = Linker::link( $page, htmlspecialchars( $user->getRealName() ) );
557 } else {
558 $user_names[] = Linker::link( $page, htmlspecialchars( $user->getName() ) );
559 }
560 }
561
562 $lang = $this->getLanguage();
563
564 $real = $lang->listToText( $real_names );
565
566 # "ThisSite user(s) A, B and C"
567 if ( count( $user_names ) ) {
568 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
569 count( $user_names ) )->escaped();
570 } else {
571 $user = false;
572 }
573
574 if ( count( $anon_ips ) ) {
575 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
576 count( $anon_ips ) )->escaped();
577 } else {
578 $anon = false;
579 }
580
581 # This is the big list, all mooshed together. We sift for blank strings
582 $fulllist = array();
583 foreach ( array( $real, $user, $anon ) as $s ) {
584 if ( $s !== '' ) {
585 array_push( $fulllist, $s );
586 }
587 }
588
589 $count = count( $fulllist );
590 # "Based on work by ..."
591 return $count
592 ? $this->msg( 'othercontribs' )->rawParams(
593 $lang->listToText( $fulllist ) )->params( $count )->escaped()
594 : '';
595 }
596
597 /**
598 * Returns the description that goes below the <h1> tag.
599 *
600 * @return string
601 */
602 protected function getDescription() {
603 return '';
604 }
605 }