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