Merge "(bug 40257) action=info no longer shows subpages where disabled"
[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 // Page creator
208 $table = $this->addRow( $table,
209 $this->msg( 'pageinfo-firstuser' )->escaped(), $pageInfo['firstuser']
210 );
211
212 // Date of page creation
213 $table = $this->addRow( $table,
214 $this->msg( 'pageinfo-firsttime' )->escaped(), $lang->userTimeAndDate( $pageInfo['firsttime'], $user )
215 );
216
217 // Latest editor
218 $table = $this->addRow( $table,
219 $this->msg( 'pageinfo-lastuser' )->escaped(), $pageInfo['lastuser']
220 );
221
222 // Date of latest edit
223 $table = $this->addRow( $table,
224 $this->msg( 'pageinfo-lasttime' )->escaped(), $lang->userTimeAndDate( $pageInfo['lasttime'], $user )
225 );
226
227 // Total number of edits
228 $table = $this->addRow( $table,
229 $this->msg( 'pageinfo-edits' )->escaped(), $lang->formatNum( $pageInfo['edits'] )
230 );
231
232 // Total number of distinct authors
233 $table = $this->addRow( $table,
234 $this->msg( 'pageinfo-authors' )->escaped(), $lang->formatNum( $pageInfo['authors'] )
235 );
236
237 // Recent number of edits (within past 30 days)
238 $table = $this->addRow( $table,
239 $this->msg( 'pageinfo-recent-edits', $lang->formatDuration( $wgRCMaxAge ) )->escaped(),
240 $lang->formatNum( $pageInfo['recent_edits'] )
241 );
242
243 // Recent number of distinct authors
244 $table = $this->addRow( $table,
245 $this->msg( 'pageinfo-recent-authors' )->escaped(), $lang->formatNum( $pageInfo['recent_authors'] )
246 );
247
248 $content = $this->addTable( $content, $table );
249
250 // Array of MagicWord objects
251 $magicWords = MagicWord::getDoubleUnderscoreArray();
252
253 // Array of magic word IDs
254 $wordIDs = $magicWords->names;
255
256 // Array of IDs => localized magic words
257 $localizedWords = $wgContLang->getMagicWords();
258
259 $listItems = array();
260 foreach ( $pageProperties as $property => $value ) {
261 if ( in_array( $property, $wordIDs ) ) {
262 $listItems[] = Html::element( 'li', array(), $localizedWords[$property][1] );
263 }
264 }
265
266 $localizedList = Html::rawElement( 'ul', array(), implode( '', $listItems ) );
267 $hiddenCategories = $this->page->getHiddenCategories();
268 $transcludedTemplates = $title->getTemplateLinksFrom();
269
270 if ( count( $listItems ) > 0
271 || count( $hiddenCategories ) > 0
272 || count( $transcludedTemplates ) > 0 ) {
273 // Page properties
274 $content = $this->addHeader( $content, $this->msg( 'pageinfo-header-properties' )->text() );
275 $table = '';
276
277 // Magic words
278 if ( count( $listItems ) > 0 ) {
279 $table = $this->addRow( $table,
280 $this->msg( 'pageinfo-magic-words' )->numParams( count( $listItems ) )->escaped(),
281 $localizedList
282 );
283 }
284
285 // Hide "This page is a member of # hidden categories explanation
286 $content .= Html::element( 'style', array(),
287 '.mw-hiddenCategoriesExplanation { display: none; }' );
288
289 // Hidden categories
290 if ( count( $hiddenCategories ) > 0 ) {
291 $table = $this->addRow( $table,
292 $this->msg( 'pageinfo-hidden-categories' )
293 ->numParams( count( $hiddenCategories ) )->escaped(),
294 Linker::formatHiddenCategories( $hiddenCategories )
295 );
296 }
297
298 // Hide "Templates used on this page:" explanation
299 $content .= Html::element( 'style', array(),
300 '.mw-templatesUsedExplanation { display: none; }' );
301
302 // Transcluded templates
303 if ( count( $transcludedTemplates ) > 0 ) {
304 $table = $this->addRow( $table,
305 $this->msg( 'pageinfo-templates' )
306 ->numParams( count( $transcludedTemplates ) )->escaped(),
307 Linker::formatTemplates( $transcludedTemplates )
308 );
309 }
310
311 $content = $this->addTable( $content, $table );
312 }
313
314 // Footer
315 if ( !$this->msg( 'pageinfo-footer' )->isDisabled() ) {
316 $content .= $this->msg( 'pageinfo-footer' )->parse();
317 }
318
319 return $content;
320 }
321
322 /**
323 * Returns page information that would be too "expensive" to retrieve by normal means.
324 *
325 * @param $title Title object
326 * @param $canViewUnwatched bool
327 * @param $disableCounter bool
328 * @return array
329 */
330 public static function pageCountInfo( $title, $canViewUnwatched, $disableCounter ) {
331 global $wgRCMaxAge;
332
333 wfProfileIn( __METHOD__ );
334 $id = $title->getArticleID();
335
336 $dbr = wfGetDB( DB_SLAVE );
337 $result = array();
338
339 if ( !$disableCounter ) {
340 // Number of views
341 $views = (int) $dbr->selectField(
342 'page',
343 'page_counter',
344 array( 'page_id' => $id ),
345 __METHOD__
346 );
347 $result['views'] = $views;
348 }
349
350 if ( $canViewUnwatched ) {
351 // Number of page watchers
352 $watchers = (int) $dbr->selectField(
353 'watchlist',
354 'COUNT(*)',
355 array(
356 'wl_namespace' => $title->getNamespace(),
357 'wl_title' => $title->getDBkey(),
358 ),
359 __METHOD__
360 );
361 $result['watchers'] = $watchers;
362 }
363
364 // Total number of edits
365 $edits = (int) $dbr->selectField(
366 'revision',
367 'COUNT(rev_page)',
368 array( 'rev_page' => $id ),
369 __METHOD__
370 );
371 $result['edits'] = $edits;
372
373 // Total number of distinct authors
374 $authors = (int) $dbr->selectField(
375 'revision',
376 'COUNT(DISTINCT rev_user_text)',
377 array( 'rev_page' => $id ),
378 __METHOD__
379 );
380 $result['authors'] = $authors;
381
382 // "Recent" threshold defined by $wgRCMaxAge
383 $threshold = $dbr->timestamp( time() - $wgRCMaxAge );
384
385 // Recent number of edits
386 $edits = (int) $dbr->selectField(
387 'revision',
388 'COUNT(rev_page)',
389 array(
390 'rev_page' => $id ,
391 "rev_timestamp >= $threshold"
392 ),
393 __METHOD__
394 );
395 $result['recent_edits'] = $edits;
396
397 // Recent number of distinct authors
398 $authors = (int) $dbr->selectField(
399 'revision',
400 'COUNT(DISTINCT rev_user_text)',
401 array(
402 'rev_page' => $id,
403 "rev_timestamp >= $threshold"
404 ),
405 __METHOD__
406 );
407 $result['recent_authors'] = $authors;
408
409 // Subpages (if enabled)
410 if ( MWNamespace::hasSubpages( $title->getNamespace() ) ) {
411 $conds = array( 'page_namespace' => $title->getNamespace() );
412 $conds[] = 'page_title ' . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() );
413
414 // Subpages of this page (redirects)
415 $conds['page_is_redirect'] = 1;
416 $result['subpages']['redirects'] = (int) $dbr->selectField(
417 'page',
418 'COUNT(page_id)',
419 $conds,
420 __METHOD__ );
421
422 // Subpages of this page (non-redirects)
423 $conds['page_is_redirect'] = 0;
424 $result['subpages']['nonredirects'] = (int) $dbr->selectField(
425 'page',
426 'COUNT(page_id)',
427 $conds,
428 __METHOD__
429 );
430
431 // Subpages of this page (total)
432 $result['subpages']['total'] = $result['subpages']['redirects']
433 + $result['subpages']['nonredirects'];
434 }
435
436 // Latest editor + date of latest edit
437 $options = array( 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 1 );
438 $row = $dbr->fetchRow( $dbr->select(
439 'revision',
440 array( 'rev_user_text', 'rev_timestamp' ),
441 array( 'rev_page' => $id ),
442 __METHOD__,
443 $options
444 ) );
445
446 $result['firstuser'] = $row['rev_user_text'];
447 $result['firsttime'] = $row['rev_timestamp'];
448
449 // Latest editor + date of latest edit
450 $options['ORDER BY'] = 'rev_timestamp DESC';
451 $row = $dbr->fetchRow( $dbr->select(
452 'revision',
453 array( 'rev_user_text', 'rev_timestamp' ),
454 array( 'rev_page' => $id ),
455 __METHOD__,
456 $options
457 ) );
458
459 $result['lastuser'] = $row['rev_user_text'];
460 $result['lasttime'] = $row['rev_timestamp'];
461
462 wfProfileOut( __METHOD__ );
463 return $result;
464 }
465
466 /**
467 * Adds a header to the content that will be added to the output.
468 *
469 * @param $content string The content that will be added to the output
470 * @param $header string The value of the header
471 * @return string The content with the header added
472 */
473 protected function addHeader( $content, $header ) {
474 return $content . Html::element( 'h2', array(), $header );
475 }
476
477 /**
478 * Adds a row to a table that will be added to the content.
479 *
480 * @param $table string The table that will be added to the content
481 * @param $name string The name of the row
482 * @param $value string The value of the row
483 * @return string The table with the row added
484 */
485 protected function addRow( $table, $name, $value ) {
486 return $table . Html::rawElement( 'tr', array(),
487 Html::rawElement( 'td', array( 'valign' => 'top' ), $name ) .
488 Html::rawElement( 'td', array(), $value )
489 );
490 }
491
492 /**
493 * Adds a table to the content that will be added to the output.
494 *
495 * @param $content string The content that will be added to the output
496 * @param $table string The table
497 * @return string The content with the table added
498 */
499 protected function addTable( $content, $table ) {
500 return $content . Html::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
501 $table );
502 }
503
504 /**
505 * Returns the description that goes below the <h1> tag.
506 *
507 * @return string
508 */
509 protected function getDescription() {
510 return '';
511 }
512
513 /**
514 * Returns the name that goes in the <h1> page title.
515 *
516 * @return string
517 */
518 protected function getPageTitle() {
519 return $this->msg( 'pageinfo-title', $this->getTitle()->getPrefixedText() )->text();
520 }
521 }