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