Use getPageTitle() to set the page title instead of doing it manually
[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 public function getRestriction() {
33 return 'read';
34 }
35
36 protected function getDescription() {
37 return '';
38 }
39
40 public function requiresWrite() {
41 return false;
42 }
43
44 public function requiresUnblock() {
45 return false;
46 }
47
48 protected function getPageTitle() {
49 return wfMsg( 'pageinfo-title', $this->getTitle()->getSubjectPage()->getPrefixedText() );
50 }
51
52 public function onView() {
53 global $wgDisableCounters;
54
55 $title = $this->getTitle()->getSubjectPage();
56
57 $pageInfo = self::pageCountInfo( $title );
58 $talkInfo = self::pageCountInfo( $title->getTalkPage() );
59
60 return Html::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ),
61 Html::rawElement( 'tr', array(),
62 Html::element( 'th', array(), '' ) .
63 Html::element( 'th', array(), wfMsg( 'pageinfo-subjectpage' ) ) .
64 Html::element( 'th', array(), wfMsg( 'pageinfo-talkpage' ) )
65 ) .
66 Html::rawElement( 'tr', array(),
67 Html::element( 'th', array( 'colspan' => 3 ), wfMsg( 'pageinfo-header-edits' ) )
68 ) .
69 Html::rawElement( 'tr', array(),
70 Html::element( 'td', array(), wfMsg( 'pageinfo-edits' ) ) .
71 Html::element( 'td', array(), $this->getLang()->formatNum( $pageInfo['edits'] ) ) .
72 Html::element( 'td', array(), $this->getLang()->formatNum( $talkInfo['edits'] ) )
73 ) .
74 Html::rawElement( 'tr', array(),
75 Html::element( 'td', array(), wfMsg( 'pageinfo-authors' ) ) .
76 Html::element( 'td', array(), $this->getLang()->formatNum( $pageInfo['authors'] ) ) .
77 Html::element( 'td', array(), $this->getLang()->formatNum( $talkInfo['authors'] ) )
78 ) .
79 ( !$this->getUser()->isAllowed( 'unwatchedpages' ) ? '' :
80 Html::rawElement( 'tr', array(),
81 Html::element( 'th', array( 'colspan' => 3 ), wfMsg( 'pageinfo-header-watchlist' ) )
82 ) .
83 Html::rawElement( 'tr', array(),
84 Html::element( 'td', array(), wfMsg( 'pageinfo-watchers' ) ) .
85 Html::element( 'td', array( 'colspan' => 2 ), $this->getLang()->formatNum( $pageInfo['watchers'] ) )
86 )
87 ).
88 ( $wgDisableCounters ? '' :
89 Html::rawElement( 'tr', array(),
90 Html::element( 'th', array( 'colspan' => 3 ), wfMsg( 'pageinfo-header-views' ) )
91 ) .
92 Html::rawElement( 'tr', array(),
93 Html::element( 'td', array(), wfMsg( 'pageinfo-views' ) ) .
94 Html::element( 'td', array(), $this->getLang()->formatNum( $pageInfo['views'] ) ) .
95 Html::element( 'td', array(), $this->getLang()->formatNum( $talkInfo['views'] ) )
96 ) .
97 Html::rawElement( 'tr', array(),
98 Html::element( 'td', array(), wfMsg( 'pageinfo-viewsperedit' ) ) .
99 Html::element( 'td', array(), $this->getLang()->formatNum( sprintf( '%.2f', $pageInfo['edits'] ? $pageInfo['views'] / $pageInfo['edits'] : 0 ) ) ) .
100 Html::element( 'td', array(), $this->getLang()->formatNum( sprintf( '%.2f', $talkInfo['edits'] ? $talkInfo['views'] / $talkInfo['edits'] : 0 ) ) )
101 )
102 )
103 );
104 }
105
106 /**
107 * Return the total number of edits and number of unique editors
108 * on a given page. If page does not exist, returns false.
109 *
110 * @param $title Title object
111 * @return mixed array or boolean false
112 */
113 public static function pageCountInfo( $title ) {
114 $id = $title->getArticleId();
115 $dbr = wfGetDB( DB_SLAVE );
116
117 $watchers = (int)$dbr->selectField(
118 'watchlist',
119 'COUNT(*)',
120 array(
121 'wl_title' => $title->getDBkey(),
122 'wl_namespace' => $title->getNamespace()
123 ),
124 __METHOD__
125 );
126
127 $edits = (int)$dbr->selectField(
128 'revision',
129 'COUNT(rev_page)',
130 array( 'rev_page' => $id ),
131 __METHOD__
132 );
133
134 $authors = (int)$dbr->selectField(
135 'revision',
136 'COUNT(DISTINCT rev_user_text)',
137 array( 'rev_page' => $id ),
138 __METHOD__
139 );
140
141 $views = (int)$dbr->selectField(
142 'page',
143 'page_counter',
144 array( 'page_id' => $id ),
145 __METHOD__
146 );
147
148 return array( 'watchers' => $watchers, 'edits' => $edits,
149 'authors' => $authors, 'views' => $views );
150 }
151 }