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