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