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