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