Merge "(bug 40380) inexistent language files are loaded"
[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, $wgRestrictionTypes;
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 // Basic information
93 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-basic' )->text() );
94
95 // Display title
96 $displayTitle = $title->getPrefixedText();
97 if ( !empty( $pageProperties['displaytitle'] ) ) {
98 $displayTitle = $pageProperties['displaytitle'];
99 }
100
101 $table = $this->addRow( $table,
102 $this->msg( 'pageinfo-display-title' )->escaped(), $displayTitle );
103
104 // Default sort key
105 $sortKey = $title->getCategorySortKey();
106 if ( !empty( $pageProperties['defaultsort'] ) ) {
107 $sortKey = $pageProperties['defaultsort'];
108 }
109
110 $table = $this->addRow( $table,
111 $this->msg( 'pageinfo-default-sort' )->escaped(), $sortKey );
112
113 // Page length (in bytes)
114 $table = $this->addRow( $table,
115 $this->msg( 'pageinfo-length' )->escaped(), $lang->formatNum( $title->getLength() ) );
116
117 // Page ID (number not localised, as it's a database ID.)
118 $table = $this->addRow( $table,
119 $this->msg( 'pageinfo-article-id' )->escaped(), $id );
120
121 // Search engine status
122 $pOutput = new ParserOutput();
123 if ( isset( $pageProperties['noindex'] ) ) {
124 $pOutput->setIndexPolicy( 'noindex' );
125 }
126
127 // Use robot policy logic
128 $policy = $this->page->getRobotPolicy( 'view', $pOutput );
129 $table = $this->addRow( $table,
130 $this->msg( 'pageinfo-robot-policy' )->escaped(),
131 $this->msg( "pageinfo-robot-${policy['index']}" )->escaped()
132 );
133
134 if ( !$wgDisableCounters ) {
135 // Number of views
136 $table = $this->addRow( $table,
137 $this->msg( 'pageinfo-views' )->escaped(), $lang->formatNum( $pageInfo['views'] )
138 );
139 }
140
141 if ( $userCanViewUnwatchedPages ) {
142 // Number of page watchers
143 $table = $this->addRow( $table,
144 $this->msg( 'pageinfo-watchers' )->escaped(), $lang->formatNum( $pageInfo['watchers'] ) );
145 }
146
147 // Redirects to this page
148 $whatLinksHere = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
149 $table = $this->addRow( $table,
150 Linker::link(
151 $whatLinksHere,
152 $this->msg( 'pageinfo-redirects-name' )->escaped(),
153 array(),
154 array( 'hidelinks' => 1, 'hidetrans' => 1 )
155 ),
156 $this->msg( 'pageinfo-redirects-value' )
157 ->numParams( count( $title->getRedirectsHere() ) )->escaped()
158 );
159
160 // Subpages of this page, if subpages are enabled for the current NS
161 if ( MWNamespace::hasSubpages( $title->getNamespace() ) ) {
162 $prefixIndex = SpecialPage::getTitleFor( 'Prefixindex', $title->getPrefixedText() . '/' );
163 $table = $this->addRow( $table,
164 Linker::link( $prefixIndex, $this->msg( 'pageinfo-subpages-name' )->escaped() ),
165 $this->msg( 'pageinfo-subpages-value' )
166 ->numParams(
167 $pageInfo['subpages']['total'],
168 $pageInfo['subpages']['redirects'],
169 $pageInfo['subpages']['nonredirects'] )->escaped()
170 );
171 }
172
173 // Page protection
174 $content = $this->addTable( $content, $table );
175 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-restrictions' )->text() );
176 $table = '';
177
178 // Page protection
179 foreach ( $wgRestrictionTypes as $restrictionType ) {
180 $protectionLevel = implode( ', ', $title->getRestrictions( $restrictionType ) );
181 if ( $protectionLevel == '' ) {
182 // Allow all users
183 $message = $this->msg( 'protect-default' )->escaped();
184 } else {
185 // Administrators only
186 $message = $this->msg( "protect-level-$protectionLevel" );
187 if ( $message->isDisabled() ) {
188 // Require "$1" permission
189 $message = $this->msg( "protect-fallback", $protectionLevel )->parse();
190 } else {
191 $message = $message->escaped();
192 }
193 }
194
195 $table = $this->addRow( $table,
196 $this->msg( 'pageinfo-restriction',
197 $this->msg( "restriction-$restrictionType" )->plain()
198 )->parse(), $message
199 );
200 }
201
202 // Edit history
203 $content = $this->addTable( $content, $table );
204 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-edits' )->text() );
205 $table = '';
206
207 $firstRev = $this->page->getOldestRevision();
208
209 // Page creator
210 $table = $this->addRow( $table,
211 $this->msg( 'pageinfo-firstuser' )->escaped(),
212 $firstRev->getUserText( Revision::FOR_THIS_USER, $user )
213 );
214
215 // Date of page creation
216 $table = $this->addRow( $table,
217 $this->msg( 'pageinfo-firsttime' )->escaped(),
218 Linker::linkKnown(
219 $title,
220 $lang->userTimeAndDate( $firstRev->getTimestamp(), $user ),
221 array(),
222 array( 'oldid' => $firstRev->getId() )
223 )
224 );
225
226 // Latest editor
227 $table = $this->addRow( $table,
228 $this->msg( 'pageinfo-lastuser' )->escaped(),
229 $this->page->getUserText( Revision::FOR_THIS_USER, $user )
230 );
231
232 // Date of latest edit
233 $table = $this->addRow( $table,
234 $this->msg( 'pageinfo-lasttime' )->escaped(),
235 Linker::linkKnown(
236 $title,
237 $lang->userTimeAndDate( $this->page->getTimestamp(), $user ),
238 array(),
239 array( 'oldid' => $this->page->getLatest() )
240 )
241 );
242
243 // Total number of edits
244 $table = $this->addRow( $table,
245 $this->msg( 'pageinfo-edits' )->escaped(), $lang->formatNum( $pageInfo['edits'] )
246 );
247
248 // Total number of distinct authors
249 $table = $this->addRow( $table,
250 $this->msg( 'pageinfo-authors' )->escaped(), $lang->formatNum( $pageInfo['authors'] )
251 );
252
253 // Recent number of edits (within past 30 days)
254 $table = $this->addRow( $table,
255 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) )->escaped(),
256 $lang->formatNum( $pageInfo['recent_edits'] )
257 );
258
259 // Recent number of distinct authors
260 $table = $this->addRow( $table,
261 $this->msg( 'pageinfo-recent-authors' )->escaped(), $lang->formatNum( $pageInfo['recent_authors'] )
262 );
263
264 $content = $this->addTable( $content, $table );
265
266 // Array of MagicWord objects
267 $magicWords = MagicWord::getDoubleUnderscoreArray();
268
269 // Array of magic word IDs
270 $wordIDs = $magicWords->names;
271
272 // Array of IDs => localized magic words
273 $localizedWords = $wgContLang->getMagicWords();
274
275 $listItems = array();
276 foreach ( $pageProperties as $property => $value ) {
277 if ( in_array( $property, $wordIDs ) ) {
278 $listItems[] = Html::element( 'li', array(), $localizedWords[$property][1] );
279 }
280 }
281
282 $localizedList = Html::rawElement( 'ul', array(), implode( '', $listItems ) );
283 $hiddenCategories = $this->page->getHiddenCategories();
284 $transcludedTemplates = $title->getTemplateLinksFrom();
285
286 if ( count( $listItems ) > 0
287 || count( $hiddenCategories ) > 0
288 || count( $transcludedTemplates ) > 0 ) {
289 // Page properties
290 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-properties' )->text() );
291 $table = '';
292
293 // Magic words
294 if ( count( $listItems ) > 0 ) {
295 $table = $this->addRow( $table,
296 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) )->escaped(),
297 $localizedList
298 );
299 }
300
301 // Hide "This page is a member of # hidden categories explanation
302 $content .= Html::element( 'style', array(),
303 '.mw-hiddenCategoriesExplanation { display: none; }' );
304
305 // Hidden categories
306 if ( count( $hiddenCategories ) > 0 ) {
307 $table = $this->addRow( $table,
308 $this->msg( 'pageinfo-hidden-categories' )
309 ->numParams( count( $hiddenCategories ) )->escaped(),
310 Linker::formatHiddenCategories( $hiddenCategories )
311 );
312 }
313
314 // Hide "Templates used on this page:" explanation
315 $content .= Html::element( 'style', array(),
316 '.mw-templatesUsedExplanation { display: none; }' );
317
318 // Transcluded templates
319 if ( count( $transcludedTemplates ) > 0 ) {
320 $table = $this->addRow( $table,
321 $this->msg( 'pageinfo-templates' )
322 ->numParams( count( $transcludedTemplates ) )->escaped(),
323 Linker::formatTemplates( $transcludedTemplates )
324 );
325 }
326
327 $content = $this->addTable( $content, $table );
328 }
329
330 // Footer
331 if ( !$this->msg( 'pageinfo-footer' )->isDisabled() ) {
332 $content .= $this->msg( 'pageinfo-footer' )->parse();
333 }
334
335 return $content;
336 }
337
338 /**
339 * Returns page information that would be too "expensive" to retrieve by normal means.
340 *
341 * @param $title Title object
342 * @param $canViewUnwatched bool
343 * @param $disableCounter bool
344 * @return array
345 */
346 public static function pageCountInfo( $title, $canViewUnwatched, $disableCounter ) {
347 global $wgRCMaxAge;
348
349 wfProfileIn( __METHOD__ );
350 $id = $title->getArticleID();
351
352 $dbr = wfGetDB( DB_SLAVE );
353 $result = array();
354
355 if ( !$disableCounter ) {
356 // Number of views
357 $views = (int) $dbr->selectField(
358 'page',
359 'page_counter',
360 array( 'page_id' => $id ),
361 __METHOD__
362 );
363 $result['views'] = $views;
364 }
365
366 if ( $canViewUnwatched ) {
367 // Number of page watchers
368 $watchers = (int) $dbr->selectField(
369 'watchlist',
370 'COUNT(*)',
371 array(
372 'wl_namespace' => $title->getNamespace(),
373 'wl_title' => $title->getDBkey(),
374 ),
375 __METHOD__
376 );
377 $result['watchers'] = $watchers;
378 }
379
380 // Total number of edits
381 $edits = (int) $dbr->selectField(
382 'revision',
383 'COUNT(rev_page)',
384 array( 'rev_page' => $id ),
385 __METHOD__
386 );
387 $result['edits'] = $edits;
388
389 // Total number of distinct authors
390 $authors = (int) $dbr->selectField(
391 'revision',
392 'COUNT(DISTINCT rev_user_text)',
393 array( 'rev_page' => $id ),
394 __METHOD__
395 );
396 $result['authors'] = $authors;
397
398 // "Recent" threshold defined by $wgRCMaxAge
399 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
400
401 // Recent number of edits
402 $edits = (int) $dbr->selectField(
403 'revision',
404 'COUNT(rev_page)',
405 array(
406 'rev_page' => $id ,
407 "rev_timestamp >= $threshold"
408 ),
409 __METHOD__
410 );
411 $result['recent_edits'] = $edits;
412
413 // Recent number of distinct authors
414 $authors = (int) $dbr->selectField(
415 'revision',
416 'COUNT(DISTINCT rev_user_text)',
417 array(
418 'rev_page' => $id,
419 "rev_timestamp >= $threshold"
420 ),
421 __METHOD__
422 );
423 $result['recent_authors'] = $authors;
424
425 // Subpages (if enabled)
426 if ( MWNamespace::hasSubpages( $title->getNamespace() ) ) {
427 $conds = array( 'page_namespace' => $title->getNamespace() );
428 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
429
430 // Subpages of this page (redirects)
431 $conds['page_is_redirect'] = 1;
432 $result['subpages']['redirects'] = (int) $dbr->selectField(
433 'page',
434 'COUNT(page_id)',
435 $conds,
436 __METHOD__ );
437
438 // Subpages of this page (non-redirects)
439 $conds['page_is_redirect'] = 0;
440 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
441 'page',
442 'COUNT(page_id)',
443 $conds,
444 __METHOD__
445 );
446
447 // Subpages of this page (total)
448 $result['subpages']['total'] = $result['subpages']['redirects']
449 + $result['subpages']['nonredirects'];
450 }
451
452 wfProfileOut( __METHOD__ );
453 return $result;
454 }
455
456 /**
457 * Adds a header to the content that will be added to the output.
458 *
459 * @param $content string The content that will be added to the output
460 * @param $header string The value of the header
461 * @return string The content with the header added
462 */
463 protected function addHeader( $content, $header ) {
464 return $content . Html::element( 'h2', array(), $header );
465 }
466
467 /**
468 * Adds a row to a table that will be added to the content.
469 *
470 * @param $table string The table that will be added to the content
471 * @param $name string The name of the row
472 * @param $value string The value of the row
473 * @return string The table with the row added
474 */
475 protected function addRow( $table, $name, $value ) {
476 return $table . Html::rawElement( 'tr', array(),
477 Html::rawElement( 'td', array( 'valign' => 'top' ), $name ) .
478 Html::rawElement( 'td', array(), $value )
479 );
480 }
481
482 /**
483 * Adds a table to the content that will be added to the output.
484 *
485 * @param $content string The content that will be added to the output
486 * @param $table string The table
487 * @return string The content with the table added
488 */
489 protected function addTable( $content, $table ) {
490 return $content . Html::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
491 $table );
492 }
493
494 /**
495 * Returns the description that goes below the <h1> tag.
496 *
497 * @return string
498 */
499 protected function getDescription() {
500 return '';
501 }
502
503 /**
504 * Returns the name that goes in the <h1> page title.
505 *
506 * @return string
507 */
508 protected function getPageTitle() {
509 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
510 }
511 }