37e658821db7bce14412a01b6e24e3a7fb1b00d6
[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;
169
170 $user = $this->getUser();
171 $lang = $this->getLanguage();
172 $title = $this->getTitle();
173 $id = $title->getArticleID();
174
175 // Get page information that would be too "expensive" to retrieve by normal means
176 $pageCounts = self::pageCounts( $title, $user );
177
178 // Get page properties
179 $dbr = wfGetDB( DB_SLAVE );
180 $result = $dbr->select(
181 'page_props',
182 array( 'pp_propname', 'pp_value' ),
183 array( 'pp_page' => $id ),
184 __METHOD__
185 );
186
187 $pageProperties = array();
188 foreach ( $result as $row ) {
189 $pageProperties[$row->pp_propname] = $row->pp_value;
190 }
191
192 // Basic information
193 $pageInfo = array();
194 $pageInfo['header-basic'] = array();
195
196 // Display title
197 $displayTitle = $title->getPrefixedText();
198 if ( !empty( $pageProperties['displaytitle'] ) ) {
199 $displayTitle = $pageProperties['displaytitle'];
200 }
201
202 $pageInfo['header-basic'][] = array(
203 $this->msg( 'pageinfo-display-title' ), $displayTitle
204 );
205
206 // Is it a redirect? If so, where to?
207 if ( $title->isRedirect() ) {
208 $pageInfo['header-basic'][] = array(
209 $this->msg( 'pageinfo-redirectsto' ),
210 Linker::link( $this->page->getRedirectTarget() ) . ' ' .
211 $this->msg( 'parentheses', Linker::link(
212 $this->page->getRedirectTarget(),
213 $this->msg( 'pageinfo-redirectsto-info' ),
214 array(),
215 array( 'action' => 'info' )
216 ) )->text()
217 );
218 }
219
220 // Default sort key
221 $sortKey = $title->getCategorySortKey();
222 if ( !empty( $pageProperties['defaultsort'] ) ) {
223 $sortKey = $pageProperties['defaultsort'];
224 }
225
226 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-default-sort' ), $sortKey );
227
228 // Page length (in bytes)
229 $pageInfo['header-basic'][] = array(
230 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
231 );
232
233 // Page ID (number not localised, as it's a database ID)
234 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-article-id' ), $id );
235
236 // Search engine status
237 $pOutput = new ParserOutput();
238 if ( isset( $pageProperties['noindex'] ) ) {
239 $pOutput->setIndexPolicy( 'noindex' );
240 }
241
242 // Use robot policy logic
243 $policy = $this->page->getRobotPolicy( 'view', $pOutput );
244 $pageInfo['header-basic'][] = array(
245 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
246 );
247
248 if ( isset( $pageCounts['views'] ) ) {
249 // Number of views
250 $pageInfo['header-basic'][] = array(
251 $this->msg( 'pageinfo-views' ), $lang->formatNum( $pageCounts['views'] )
252 );
253 }
254
255 if ( isset( $pageCounts['watchers'] ) ) {
256 // Number of page watchers
257 $pageInfo['header-basic'][] = array(
258 $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
259 );
260 }
261
262 // Redirects to this page
263 $whatLinksHere = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
264 $pageInfo['header-basic'][] = array(
265 Linker::link(
266 $whatLinksHere,
267 $this->msg( 'pageinfo-redirects-name' )->escaped(),
268 array(),
269 array( 'hidelinks' => 1, 'hidetrans' => 1 )
270 ),
271 $this->msg( 'pageinfo-redirects-value' )
272 ->numParams( count( $title->getRedirectsHere() ) )
273 );
274
275 // Is it counted as a content page?
276 if ( $this->page->isCountable() ) {
277 $pageInfo['header-basic'][] = array(
278 $this->msg( 'pageinfo-contentpage' ),
279 $this->msg( 'pageinfo-contentpage-yes' )
280 );
281 }
282
283 // Subpages of this page, if subpages are enabled for the current NS
284 if ( MWNamespace::hasSubpages( $title->getNamespace() ) ) {
285 $prefixIndex = SpecialPage::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
286 $pageInfo['header-basic'][] = array(
287 Linker::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
288 $this->msg( 'pageinfo-subpages-value' )
289 ->numParams(
290 $pageCounts['subpages']['total'],
291 $pageCounts['subpages']['redirects'],
292 $pageCounts['subpages']['nonredirects'] )
293 );
294 }
295
296 // Page protection
297 $pageInfo['header-restrictions'] = array();
298
299 // Page protection
300 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
301 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
302
303 if ( $protectionLevel == '' ) {
304 // Allow all users
305 $message = $this->msg( 'protect-default' )->escaped();
306 } else {
307 // Administrators only
308 $message = $this->msg( "protect-level-$protectionLevel" );
309 if ( $message->isDisabled() ) {
310 // Require "$1" permission
311 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
312 } else {
313 $message = $message->escaped();
314 }
315 }
316
317 $pageInfo['header-restrictions'][] = array(
318 $this->msg( "restriction-$restrictionType" ), $message
319 );
320 }
321
322 if ( !$this->page->exists() ) {
323 return $pageInfo;
324 }
325
326 // Edit history
327 $pageInfo['header-edits'] = array();
328
329 $firstRev = $this->page->getOldestRevision();
330
331 // Page creator
332 $pageInfo['header-edits'][] = array(
333 $this->msg( 'pageinfo-firstuser' ),
334 Linker::revUserTools( $firstRev )
335 );
336
337 // Date of page creation
338 $pageInfo['header-edits'][] = array(
339 $this->msg( 'pageinfo-firsttime' ),
340 Linker::linkKnown(
341 $title,
342 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
343 array(),
344 array( 'oldid' => $firstRev->getId() )
345 )
346 );
347
348 // Latest editor
349 $pageInfo['header-edits'][] = array(
350 $this->msg( 'pageinfo-lastuser' ),
351 Linker::revUserTools( $this->page->getRevision() )
352 );
353
354 // Date of latest edit
355 $pageInfo['header-edits'][] = array(
356 $this->msg( 'pageinfo-lasttime' ),
357 Linker::linkKnown(
358 $title,
359 $lang->userTimeAndDate( $this->page->getTimestamp(), $user ),
360 array(),
361 array( 'oldid' => $this->page->getLatest() )
362 )
363 );
364
365 // Total number of edits
366 $pageInfo['header-edits'][] = array(
367 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
368 );
369
370 // Total number of distinct authors
371 $pageInfo['header-edits'][] = array(
372 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
373 );
374
375 // Recent number of edits (within past 30 days)
376 $pageInfo['header-edits'][] = array(
377 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
378 $lang->formatNum( $pageCounts['recent_edits'] )
379 );
380
381 // Recent number of distinct authors
382 $pageInfo['header-edits'][] = array(
383 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
384 );
385
386 // Array of MagicWord objects
387 $magicWords = MagicWord::getDoubleUnderscoreArray();
388
389 // Array of magic word IDs
390 $wordIDs = $magicWords->names;
391
392 // Array of IDs => localized magic words
393 $localizedWords = $wgContLang->getMagicWords();
394
395 $listItems = array();
396 foreach ( $pageProperties as $property => $value ) {
397 if ( in_array( $property, $wordIDs ) ) {
398 $listItems[] = Html::element( 'li', array(), $localizedWords[$property][1] );
399 }
400 }
401
402 $localizedList = Html::rawElement( 'ul', array(), implode( '', $listItems ) );
403 $hiddenCategories = $this->page->getHiddenCategories();
404 $transcludedTemplates = $title->getTemplateLinksFrom();
405
406 if ( count( $listItems ) > 0
407 || count( $hiddenCategories ) > 0
408 || count( $transcludedTemplates ) > 0 ) {
409 // Page properties
410 $pageInfo['header-properties'] = array();
411
412 // Magic words
413 if ( count( $listItems ) > 0 ) {
414 $pageInfo['header-properties'][] = array(
415 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
416 $localizedList
417 );
418 }
419
420 // Hidden categories
421 if ( count( $hiddenCategories ) > 0 ) {
422 $pageInfo['header-properties'][] = array(
423 $this->msg( 'pageinfo-hidden-categories' )
424 ->numParams( count( $hiddenCategories ) ),
425 Linker::formatHiddenCategories( $hiddenCategories )
426 );
427 }
428
429 // Transcluded templates
430 if ( count( $transcludedTemplates ) > 0 ) {
431 $pageInfo['header-properties'][] = array(
432 $this->msg( 'pageinfo-templates' )
433 ->numParams( count( $transcludedTemplates ) ),
434 Linker::formatTemplates( $transcludedTemplates )
435 );
436 }
437 }
438
439 return $pageInfo;
440 }
441
442 /**
443 * Returns page counts that would be too "expensive" to retrieve by normal means.
444 *
445 * @param $title Title object
446 * @param $user User object
447 * @return array
448 */
449 protected static function pageCounts( $title, $user ) {
450 global $wgRCMaxAge, $wgDisableCounters;
451
452 wfProfileIn( __METHOD__ );
453 $id = $title->getArticleID();
454
455 $dbr = wfGetDB( DB_SLAVE );
456 $result = array();
457
458 if ( !$wgDisableCounters ) {
459 // Number of views
460 $views = (int) $dbr->selectField(
461 'page',
462 'page_counter',
463 array( 'page_id' => $id ),
464 __METHOD__
465 );
466 $result['views'] = $views;
467 }
468
469 if ( $user->isAllowed( 'unwatchedpages' ) ) {
470 // Number of page watchers
471 $watchers = (int) $dbr->selectField(
472 'watchlist',
473 'COUNT(*)',
474 array(
475 'wl_namespace' => $title->getNamespace(),
476 'wl_title' => $title->getDBkey(),
477 ),
478 __METHOD__
479 );
480 $result['watchers'] = $watchers;
481 }
482
483 // Total number of edits
484 $edits = (int) $dbr->selectField(
485 'revision',
486 'COUNT(rev_page)',
487 array( 'rev_page' => $id ),
488 __METHOD__
489 );
490 $result['edits'] = $edits;
491
492 // Total number of distinct authors
493 $authors = (int) $dbr->selectField(
494 'revision',
495 'COUNT(DISTINCT rev_user_text)',
496 array( 'rev_page' => $id ),
497 __METHOD__
498 );
499 $result['authors'] = $authors;
500
501 // "Recent" threshold defined by $wgRCMaxAge
502 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
503
504 // Recent number of edits
505 $edits = (int) $dbr->selectField(
506 'revision',
507 'COUNT(rev_page)',
508 array(
509 'rev_page' => $id ,
510 "rev_timestamp >= $threshold"
511 ),
512 __METHOD__
513 );
514 $result['recent_edits'] = $edits;
515
516 // Recent number of distinct authors
517 $authors = (int) $dbr->selectField(
518 'revision',
519 'COUNT(DISTINCT rev_user_text)',
520 array(
521 'rev_page' => $id,
522 "rev_timestamp >= $threshold"
523 ),
524 __METHOD__
525 );
526 $result['recent_authors'] = $authors;
527
528 // Subpages (if enabled)
529 if ( MWNamespace::hasSubpages( $title->getNamespace() ) ) {
530 $conds = array( 'page_namespace' => $title->getNamespace() );
531 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
532
533 // Subpages of this page (redirects)
534 $conds['page_is_redirect'] = 1;
535 $result['subpages']['redirects'] = (int) $dbr->selectField(
536 'page',
537 'COUNT(page_id)',
538 $conds,
539 __METHOD__ );
540
541 // Subpages of this page (non-redirects)
542 $conds['page_is_redirect'] = 0;
543 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
544 'page',
545 'COUNT(page_id)',
546 $conds,
547 __METHOD__
548 );
549
550 // Subpages of this page (total)
551 $result['subpages']['total'] = $result['subpages']['redirects']
552 + $result['subpages']['nonredirects'];
553 }
554
555 wfProfileOut( __METHOD__ );
556 return $result;
557 }
558
559 /**
560 * Returns the name that goes in the <h1> page title.
561 *
562 * @return string
563 */
564 protected function getPageTitle() {
565 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
566 }
567
568 /**
569 * Get a list of contributors of $article
570 * @return string: html
571 */
572 protected function getContributors() {
573 global $wgHiddenPrefs;
574
575 $contributors = $this->page->getContributors();
576 $real_names = array();
577 $user_names = array();
578 $anon_ips = array();
579
580 # Sift for real versus user names
581 foreach ( $contributors as $user ) {
582 $page = $user->isAnon()
583 ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
584 : $user->getUserPage();
585
586 if ( $user->getID() == 0 ) {
587 $anon_ips[] = Linker::link( $page, htmlspecialchars( $user->getName() ) );
588 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
589 $real_names[] = Linker::link( $page, htmlspecialchars( $user->getRealName() ) );
590 } else {
591 $user_names[] = Linker::link( $page, htmlspecialchars( $user->getName() ) );
592 }
593 }
594
595 $lang = $this->getLanguage();
596
597 $real = $lang->listToText( $real_names );
598
599 # "ThisSite user(s) A, B and C"
600 if ( count( $user_names ) ) {
601 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
602 count( $user_names ) )->escaped();
603 } else {
604 $user = false;
605 }
606
607 if ( count( $anon_ips ) ) {
608 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
609 count( $anon_ips ) )->escaped();
610 } else {
611 $anon = false;
612 }
613
614 # This is the big list, all mooshed together. We sift for blank strings
615 $fulllist = array();
616 foreach ( array( $real, $user, $anon ) as $s ) {
617 if ( $s !== '' ) {
618 array_push( $fulllist, $s );
619 }
620 }
621
622 $count = count( $fulllist );
623 # "Based on work by ..."
624 return $count
625 ? $this->msg( 'othercontribs' )->rawParams(
626 $lang->listToText( $fulllist ) )->params( $count )->escaped()
627 : '';
628 }
629
630 /**
631 * Returns the description that goes below the <h1> tag.
632 *
633 * @return string
634 */
635 protected function getDescription() {
636 return '';
637 }
638 }