Use local context to get messages instead of relying on global variables
[lhc/web/wiklou.git] / includes / actions / InfoAction.php
1 <?php
2 /**
3 * Display informations about a page.
4 * Very inefficient for the moment.
5 *
6 * Copyright © 2011 Alexandre Emsenhuber
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 *
22 * @file
23 * @ingroup Actions
24 */
25
26 class InfoAction extends FormlessAction {
27
28 public function getName() {
29 return 'info';
30 }
31
32 protected function getDescription() {
33 return '';
34 }
35
36 public function requiresWrite() {
37 return false;
38 }
39
40 public function requiresUnblock() {
41 return false;
42 }
43
44 protected function getPageTitle() {
45 return $this->msg( 'pageinfo-title', $this->getTitle()->getSubjectPage()->getPrefixedText() )->text();
46 }
47
48 public function onView() {
49 global $wgDisableCounters;
50
51 $title = $this->getTitle()->getSubjectPage();
52
53 $pageInfo = self::pageCountInfo( $title );
54 $talkInfo = self::pageCountInfo( $title->getTalkPage() );
55
56 return Html::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
57 Html::rawElement( 'tr', array(),
58 Html::element( 'th', array(), '' ) .
59 Html::element( 'th', array(), $this->msg( 'pageinfo-subjectpage' )->text() ) .
60 Html::element( 'th', array(), $this->msg( 'pageinfo-talkpage' )->text() )
61 ) .
62 Html::rawElement( 'tr', array(),
63 Html::element( 'th', array( 'colspan' => 3 ), $this->msg( 'pageinfo-header-edits' )->text() )
64 ) .
65 Html::rawElement( 'tr', array(),
66 Html::element( 'td', array(), $this->msg( 'pageinfo-edits' )->text() ) .
67 Html::element( 'td', array(), $this->getLanguage()->formatNum( $pageInfo['edits'] ) ) .
68 Html::element( 'td', array(), $this->getLanguage()->formatNum( $talkInfo['edits'] ) )
69 ) .
70 Html::rawElement( 'tr', array(),
71 Html::element( 'td', array(), $this->msg( 'pageinfo-authors' )->text() ) .
72 Html::element( 'td', array(), $this->getLanguage()->formatNum( $pageInfo['authors'] ) ) .
73 Html::element( 'td', array(), $this->getLanguage()->formatNum( $talkInfo['authors'] ) )
74 ) .
75 ( !$this->getUser()->isAllowed( 'unwatchedpages' ) ? '' :
76 Html::rawElement( 'tr', array(),
77 Html::element( 'th', array( 'colspan' => 3 ), $this->msg( 'pageinfo-header-watchlist' )->text() )
78 ) .
79 Html::rawElement( 'tr', array(),
80 Html::element( 'td', array(), $this->msg( 'pageinfo-watchers' )->text() ) .
81 Html::element( 'td', array( 'colspan' => 2 ), $this->getLanguage()->formatNum( $pageInfo['watchers'] ) )
82 )
83 ).
84 ( $wgDisableCounters ? '' :
85 Html::rawElement( 'tr', array(),
86 Html::element( 'th', array( 'colspan' => 3 ), $this->msg( 'pageinfo-header-views' )->text() )
87 ) .
88 Html::rawElement( 'tr', array(),
89 Html::element( 'td', array(), $this->msg( 'pageinfo-views' )->text() ) .
90 Html::element( 'td', array(), $this->getLanguage()->formatNum( $pageInfo['views'] ) ) .
91 Html::element( 'td', array(), $this->getLanguage()->formatNum( $talkInfo['views'] ) )
92 ) .
93 Html::rawElement( 'tr', array(),
94 Html::element( 'td', array(), $this->msg( 'pageinfo-viewsperedit' )->text() ) .
95 Html::element( 'td', array(), $this->getLanguage()->formatNum( sprintf( '%.2f', $pageInfo['edits'] ? $pageInfo['views'] / $pageInfo['edits'] : 0 ) ) ) .
96 Html::element( 'td', array(), $this->getLanguage()->formatNum( sprintf( '%.2f', $talkInfo['edits'] ? $talkInfo['views'] / $talkInfo['edits'] : 0 ) ) )
97 )
98 )
99 );
100 }
101
102 /**
103 * Return the total number of edits and number of unique editors
104 * on a given page. If page does not exist, returns false.
105 *
106 * @param $title Title object
107 * @return mixed array or boolean false
108 */
109 public static function pageCountInfo( $title ) {
110 $id = $title->getArticleId();
111 $dbr = wfGetDB( DB_SLAVE );
112
113 $watchers = (int)$dbr->selectField(
114 'watchlist',
115 'COUNT(*)',
116 array(
117 'wl_title' => $title->getDBkey(),
118 'wl_namespace' => $title->getNamespace()
119 ),
120 __METHOD__
121 );
122
123 $edits = (int)$dbr->selectField(
124 'revision',
125 'COUNT(rev_page)',
126 array( 'rev_page' => $id ),
127 __METHOD__
128 );
129
130 $authors = (int)$dbr->selectField(
131 'revision',
132 'COUNT(DISTINCT rev_user_text)',
133 array( 'rev_page' => $id ),
134 __METHOD__
135 );
136
137 $views = (int)$dbr->selectField(
138 'page',
139 'page_counter',
140 array( 'page_id' => $id ),
141 __METHOD__
142 );
143
144 return array( 'watchers' => $watchers, 'edits' => $edits,
145 'authors' => $authors, 'views' => $views );
146 }
147 }