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