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