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