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