Merge "Merge branch 'Wikidata' into master."
[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 $pageInfo = $this->pageInfo();
76
77 // Allow extensions to add additional information
78 wfRunHooks( 'InfoAction', array( &$pageInfo ) );
79
80 // Render page information
81 foreach ( $pageInfo as $header => $infoTable ) {
82 $content .= $this->makeHeader( $this->msg( "pageinfo-${header}" )->escaped() );
83 $table = '';
84 foreach ( $infoTable as $infoRow ) {
85 $name = ( $infoRow[0] instanceof Message ) ? $infoRow[0]->escaped() : $infoRow[0];
86 $value = ( $infoRow[1] instanceof Message ) ? $infoRow[1]->escaped() : $infoRow[1];
87 $table = $this->addRow( $table, $name, $value );
88 }
89 $content = $this->addTable( $content, $table );
90 }
91
92 // Page footer
93 if ( !$this->msg( 'pageinfo-footer' )->isDisabled() ) {
94 $content .= $this->msg( 'pageinfo-footer' )->parse();
95 }
96
97 // Page credits
98 /*if ( $this->page->exists() ) {
99 $content .= Html::rawElement( 'div', array( 'id' => 'mw-credits' ), $this->getContributors() );
100 }*/
101
102 return $content;
103 }
104
105 /**
106 * Creates a header that can be added to the output.
107 *
108 * @param $header The header text.
109 * @return string The HTML.
110 */
111 protected function makeHeader( $header ) {
112 global $wgParser;
113 $spanAttribs = array( 'class' => 'mw-headline', 'id' => $wgParser->guessSectionNameFromWikiText( $header ) );
114 return Html::rawElement( 'h2', array(), Html::element( 'span', $spanAttribs, $header ) );
115 }
116
117 /**
118 * Adds a row to a table that will be added to the content.
119 *
120 * @param $table string The table that will be added to the content
121 * @param $name string The name of the row
122 * @param $value string The value of the row
123 * @return string The table with the row added
124 */
125 protected function addRow( $table, $name, $value ) {
126 return $table . Html::rawElement( 'tr', array(),
127 Html::rawElement( 'td', array( 'style' => 'vertical-align: top;' ), $name ) .
128 Html::rawElement( 'td', array(), $value )
129 );
130 }
131
132 /**
133 * Adds a table to the content that will be added to the output.
134 *
135 * @param $content string The content that will be added to the output
136 * @param $table string The table
137 * @return string The content with the table added
138 */
139 protected function addTable( $content, $table ) {
140 return $content . Html::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
141 $table );
142 }
143
144 /**
145 * Returns page information in an easily-manipulated format. Array keys are used so extensions
146 * may add additional information in arbitrary positions. Array values are arrays with one
147 * element to be rendered as a header, arrays with two elements to be rendered as a table row.
148 */
149 protected function pageInfo() {
150 global $wgContLang, $wgDisableCounters, $wgRCMaxAge;
151
152 $user = $this->getUser();
153 $lang = $this->getLanguage();
154 $title = $this->getTitle();
155 $id = $title->getArticleID();
156
157 // Get page information that would be too "expensive" to retrieve by normal means
158 $userCanViewUnwatchedPages = $user->isAllowed( 'unwatchedpages' );
159 $pageCounts = self::pageCounts( $title, $userCanViewUnwatchedPages, $wgDisableCounters );
160
161 // Get page properties
162 $dbr = wfGetDB( DB_SLAVE );
163 $result = $dbr->select(
164 'page_props',
165 array( 'pp_propname', 'pp_value' ),
166 array( 'pp_page' => $id ),
167 __METHOD__
168 );
169
170 $pageProperties = array();
171 foreach ( $result as $row ) {
172 $pageProperties[$row->pp_propname] = $row->pp_value;
173 }
174
175 // Basic information
176 $pageInfo = array();
177 $pageInfo['header-basic'] = array();
178
179 // Display title
180 $displayTitle = $title->getPrefixedText();
181 if ( !empty( $pageProperties['displaytitle'] ) ) {
182 $displayTitle = $pageProperties['displaytitle'];
183 }
184
185 $pageInfo['header-basic'][] = array(
186 $this->msg( 'pageinfo-display-title' ), $displayTitle
187 );
188
189 // Default sort key
190 $sortKey = $title->getCategorySortKey();
191 if ( !empty( $pageProperties['defaultsort'] ) ) {
192 $sortKey = $pageProperties['defaultsort'];
193 }
194
195 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-default-sort' ), $sortKey );
196
197 // Page length (in bytes)
198 $pageInfo['header-basic'][] = array(
199 $this->msg( 'pageinfo-length' ), $lang->formatNum( $title->getLength() )
200 );
201
202 // Page ID (number not localised, as it's a database ID)
203 $pageInfo['header-basic'][] = array( $this->msg( 'pageinfo-article-id' ), $id );
204
205 // Search engine status
206 $pOutput = new ParserOutput();
207 if ( isset( $pageProperties['noindex'] ) ) {
208 $pOutput->setIndexPolicy( 'noindex' );
209 }
210
211 // Use robot policy logic
212 $policy = $this->page->getRobotPolicy( 'view', $pOutput );
213 $pageInfo['header-basic'][] = array(
214 $this->msg( 'pageinfo-robot-policy' ), $this->msg( "pageinfo-robot-${policy['index']}" )
215 );
216
217 if ( !$wgDisableCounters ) {
218 // Number of views
219 $pageInfo['header-basic'][] = array(
220 $this->msg( 'pageinfo-views' ), $lang->formatNum( $pageCounts['views'] )
221 );
222 }
223
224 if ( $userCanViewUnwatchedPages ) {
225 // Number of page watchers
226 $pageInfo['header-basic'][] = array(
227 $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
228 );
229 }
230
231 // Redirects to this page
232 $whatLinksHere = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
233 $pageInfo['header-basic'][] = array(
234 Linker::link(
235 $whatLinksHere,
236 $this->msg( 'pageinfo-redirects-name' )->escaped(),
237 array(),
238 array( 'hidelinks' => 1, 'hidetrans' => 1 )
239 ),
240 $this->msg( 'pageinfo-redirects-value' )
241 ->numParams( count( $title->getRedirectsHere() ) )
242 );
243
244 // Subpages of this page, if subpages are enabled for the current NS
245 if ( MWNamespace::hasSubpages( $title->getNamespace() ) ) {
246 $prefixIndex = SpecialPage::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
247 $pageInfo['header-basic'][] = array(
248 Linker::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
249 $this->msg( 'pageinfo-subpages-value' )
250 ->numParams(
251 $pageCounts['subpages']['total'],
252 $pageCounts['subpages']['redirects'],
253 $pageCounts['subpages']['nonredirects'] )
254 );
255 }
256
257 // Page protection
258 $pageInfo['header-restrictions'] = array();
259
260 // Page protection
261 foreach ( $title->getRestrictionTypes() as $restrictionType ) {
262 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
263
264 if ( $protectionLevel == '' ) {
265 // Allow all users
266 $message = $this->msg( 'protect-default' )->escaped();
267 } else {
268 // Administrators only
269 $message = $this->msg( "protect-level-$protectionLevel" );
270 if ( $message->isDisabled() ) {
271 // Require "$1" permission
272 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
273 } else {
274 $message = $message->escaped();
275 }
276 }
277
278 $pageInfo['header-restrictions'][] = array(
279 $this->msg( "restriction-$restrictionType" ), $message
280 );
281 }
282
283 // Edit history
284 $pageInfo['header-edits'] = array();
285
286 $firstRev = $this->page->getOldestRevision();
287
288 // Page creator
289 $pageInfo['header-edits'][] = array(
290 $this->msg( 'pageinfo-firstuser' ),
291 Linker::revUserTools( $firstRev )
292 );
293
294 // Date of page creation
295 $pageInfo['header-edits'][] = array(
296 $this->msg( 'pageinfo-firsttime' ),
297 Linker::linkKnown(
298 $title,
299 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
300 array(),
301 array( 'oldid' => $firstRev->getId() )
302 )
303 );
304
305 // Latest editor
306 $pageInfo['header-edits'][] = array(
307 $this->msg( 'pageinfo-lastuser' ),
308 Linker::revUserTools( $this->page->getRevision() )
309 );
310
311 // Date of latest edit
312 $pageInfo['header-edits'][] = array(
313 $this->msg( 'pageinfo-lasttime' ),
314 Linker::linkKnown(
315 $title,
316 $lang->userTimeAndDate( $this->page->getTimestamp(), $user ),
317 array(),
318 array( 'oldid' => $this->page->getLatest() )
319 )
320 );
321
322 // Total number of edits
323 $pageInfo['header-edits'][] = array(
324 $this->msg( 'pageinfo-edits' ), $lang->formatNum( $pageCounts['edits'] )
325 );
326
327 // Total number of distinct authors
328 $pageInfo['header-edits'][] = array(
329 $this->msg( 'pageinfo-authors' ), $lang->formatNum( $pageCounts['authors'] )
330 );
331
332 // Recent number of edits (within past 30 days)
333 $pageInfo['header-edits'][] = array(
334 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) ),
335 $lang->formatNum( $pageCounts['recent_edits'] )
336 );
337
338 // Recent number of distinct authors
339 $pageInfo['header-edits'][] = array(
340 $this->msg( 'pageinfo-recent-authors' ), $lang->formatNum( $pageCounts['recent_authors'] )
341 );
342
343 // Array of MagicWord objects
344 $magicWords = MagicWord::getDoubleUnderscoreArray();
345
346 // Array of magic word IDs
347 $wordIDs = $magicWords->names;
348
349 // Array of IDs => localized magic words
350 $localizedWords = $wgContLang->getMagicWords();
351
352 $listItems = array();
353 foreach ( $pageProperties as $property => $value ) {
354 if ( in_array( $property, $wordIDs ) ) {
355 $listItems[] = Html::element( 'li', array(), $localizedWords[$property][1] );
356 }
357 }
358
359 $localizedList = Html::rawElement( 'ul', array(), implode( '', $listItems ) );
360 $hiddenCategories = $this->page->getHiddenCategories();
361 $transcludedTemplates = $title->getTemplateLinksFrom();
362
363 if ( count( $listItems ) > 0
364 || count( $hiddenCategories ) > 0
365 || count( $transcludedTemplates ) > 0 ) {
366 // Page properties
367 $pageInfo['header-properties'] = array();
368
369 // Magic words
370 if ( count( $listItems ) > 0 ) {
371 $pageInfo['header-properties'][] = array(
372 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) ),
373 $localizedList
374 );
375 }
376
377 // Hidden categories
378 if ( count( $hiddenCategories ) > 0 ) {
379 $pageInfo['header-properties'][] = array(
380 $this->msg( 'pageinfo-hidden-categories' )
381 ->numParams( count( $hiddenCategories ) ),
382 Linker::formatHiddenCategories( $hiddenCategories )
383 );
384 }
385
386 // Transcluded templates
387 if ( count( $transcludedTemplates ) > 0 ) {
388 $pageInfo['header-properties'][] = array(
389 $this->msg( 'pageinfo-templates' )
390 ->numParams( count( $transcludedTemplates ) ),
391 Linker::formatTemplates( $transcludedTemplates )
392 );
393 }
394 }
395
396 return $pageInfo;
397 }
398
399 /**
400 * Returns page counts that would be too "expensive" to retrieve by normal means.
401 *
402 * @param $title Title object
403 * @param $canViewUnwatched bool
404 * @param $disableCounter bool
405 * @return array
406 */
407 protected static function pageCounts( $title, $canViewUnwatched, $disableCounter ) {
408 global $wgRCMaxAge;
409
410 wfProfileIn( __METHOD__ );
411 $id = $title->getArticleID();
412
413 $dbr = wfGetDB( DB_SLAVE );
414 $result = array();
415
416 if ( !$disableCounter ) {
417 // Number of views
418 $views = (int) $dbr->selectField(
419 'page',
420 'page_counter',
421 array( 'page_id' => $id ),
422 __METHOD__
423 );
424 $result['views'] = $views;
425 }
426
427 if ( $canViewUnwatched ) {
428 // Number of page watchers
429 $watchers = (int) $dbr->selectField(
430 'watchlist',
431 'COUNT(*)',
432 array(
433 'wl_namespace' => $title->getNamespace(),
434 'wl_title' => $title->getDBkey(),
435 ),
436 __METHOD__
437 );
438 $result['watchers'] = $watchers;
439 }
440
441 // Total number of edits
442 $edits = (int) $dbr->selectField(
443 'revision',
444 'COUNT(rev_page)',
445 array( 'rev_page' => $id ),
446 __METHOD__
447 );
448 $result['edits'] = $edits;
449
450 // Total number of distinct authors
451 $authors = (int) $dbr->selectField(
452 'revision',
453 'COUNT(DISTINCT rev_user_text)',
454 array( 'rev_page' => $id ),
455 __METHOD__
456 );
457 $result['authors'] = $authors;
458
459 // "Recent" threshold defined by $wgRCMaxAge
460 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
461
462 // Recent number of edits
463 $edits = (int) $dbr->selectField(
464 'revision',
465 'COUNT(rev_page)',
466 array(
467 'rev_page' => $id ,
468 "rev_timestamp >= $threshold"
469 ),
470 __METHOD__
471 );
472 $result['recent_edits'] = $edits;
473
474 // Recent number of distinct authors
475 $authors = (int) $dbr->selectField(
476 'revision',
477 'COUNT(DISTINCT rev_user_text)',
478 array(
479 'rev_page' => $id,
480 "rev_timestamp >= $threshold"
481 ),
482 __METHOD__
483 );
484 $result['recent_authors'] = $authors;
485
486 // Subpages (if enabled)
487 if ( MWNamespace::hasSubpages( $title->getNamespace() ) ) {
488 $conds = array( 'page_namespace' => $title->getNamespace() );
489 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
490
491 // Subpages of this page (redirects)
492 $conds['page_is_redirect'] = 1;
493 $result['subpages']['redirects'] = (int) $dbr->selectField(
494 'page',
495 'COUNT(page_id)',
496 $conds,
497 __METHOD__ );
498
499 // Subpages of this page (non-redirects)
500 $conds['page_is_redirect'] = 0;
501 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
502 'page',
503 'COUNT(page_id)',
504 $conds,
505 __METHOD__
506 );
507
508 // Subpages of this page (total)
509 $result['subpages']['total'] = $result['subpages']['redirects']
510 + $result['subpages']['nonredirects'];
511 }
512
513 wfProfileOut( __METHOD__ );
514 return $result;
515 }
516
517 /**
518 * Returns the name that goes in the <h1> page title.
519 *
520 * @return string
521 */
522 protected function getPageTitle() {
523 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
524 }
525
526 /**
527 * Get a list of contributors of $article
528 * @return string: html
529 */
530 protected function getContributors() {
531 global $wgHiddenPrefs;
532
533 $contributors = $this->page->getContributors();
534 $real_names = array();
535 $user_names = array();
536 $anon_ips = array();
537
538 # Sift for real versus user names
539 foreach ( $contributors as $user ) {
540 $page = $user->isAnon()
541 ? SpecialPage::getTitleFor( 'Contributions', $user->getName() )
542 : $user->getUserPage();
543
544 if ( $user->getID() == 0 ) {
545 $anon_ips[] = Linker::link( $page, htmlspecialchars( $user->getName() ) );
546 } elseif ( !in_array( 'realname', $wgHiddenPrefs ) && $user->getRealName() ) {
547 $real_names[] = Linker::link( $page, htmlspecialchars( $user->getRealName() ) );
548 } else {
549 $user_names[] = Linker::link( $page, htmlspecialchars( $user->getName() ) );
550 }
551 }
552
553 $lang = $this->getLanguage();
554
555 $real = $lang->listToText( $real_names );
556
557 # "ThisSite user(s) A, B and C"
558 if ( count( $user_names ) ) {
559 $user = $this->msg( 'siteusers' )->rawParams( $lang->listToText( $user_names ) )->params(
560 count( $user_names ) )->escaped();
561 } else {
562 $user = false;
563 }
564
565 if ( count( $anon_ips ) ) {
566 $anon = $this->msg( 'anonusers' )->rawParams( $lang->listToText( $anon_ips ) )->params(
567 count( $anon_ips ) )->escaped();
568 } else {
569 $anon = false;
570 }
571
572 # This is the big list, all mooshed together. We sift for blank strings
573 $fulllist = array();
574 foreach ( array( $real, $user, $anon ) as $s ) {
575 if ( $s !== '' ) {
576 array_push( $fulllist, $s );
577 }
578 }
579
580 $count = count( $fulllist );
581 # "Based on work by ..."
582 return $count
583 ? $this->msg( 'othercontribs' )->rawParams(
584 $lang->listToText( $fulllist ) )->params( $count )->escaped()
585 : '';
586 }
587
588 /**
589 * Returns the description that goes below the <h1> tag.
590 *
591 * @return string
592 */
593 protected function getDescription() {
594 return '';
595 }
596 }