f4813a4c0ea8791915cc890d1cb50c2e3d6b10c9
[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 $userCanViewUnwatchedPages = $this->getUser()->isAllowed( 'unwatchedpages' );
54
55 $pageInfo = self::pageCountInfo( $title, $userCanViewUnwatchedPages, $wgDisableCounters );
56 $talkInfo = self::pageCountInfo( $title->getTalkPage(), $userCanViewUnwatchedPages, $wgDisableCounters );
57
58 $lang = $this->getLanguage();
59
60 $content =
61 Html::rawElement( 'tr', array(),
62 Html::element( 'th', array(), '' ) .
63 Html::element( 'th', array(), $this->msg( 'pageinfo-subjectpage' )->text() ) .
64 Html::element( 'th', array(), $this->msg( 'pageinfo-talkpage' )->text() )
65 ) .
66 Html::rawElement( 'tr', array(),
67 Html::element( 'th', array( 'colspan' => 3 ), $this->msg( 'pageinfo-header-edits' )->text() )
68 ) .
69 Html::rawElement( 'tr', array(),
70 Html::element( 'td', array(), $this->msg( 'pageinfo-edits' )->text() ) .
71 Html::element( 'td', array(), $lang->formatNum( $pageInfo['edits'] ) ) .
72 Html::element( 'td', array(), $lang->formatNum( $talkInfo['edits'] ) )
73 ) .
74 Html::rawElement( 'tr', array(),
75 Html::element( 'td', array(), $this->msg( 'pageinfo-authors' )->text() ) .
76 Html::element( 'td', array(), $lang->formatNum( $pageInfo['authors'] ) ) .
77 Html::element( 'td', array(), $lang->formatNum( $talkInfo['authors'] ) )
78 );
79
80 if ( $userCanViewUnwatchedPages ) {
81 $content .= Html::rawElement( 'tr', array(),
82 Html::element( 'th', array( 'colspan' => 3 ), $this->msg( 'pageinfo-header-watchlist' )->text() )
83 ) .
84 Html::rawElement( 'tr', array(),
85 Html::element( 'td', array(), $this->msg( 'pageinfo-watchers' )->text() ) .
86 Html::element( 'td', array( 'colspan' => 2 ), $lang->formatNum( $pageInfo['watchers'] ) )
87 );
88 }
89
90 if ( !$wgDisableCounters ) {
91 $content .= Html::rawElement( 'tr', array(),
92 Html::element( 'th', array( 'colspan' => 3 ), $this->msg( 'pageinfo-header-views' )->text() )
93 ) .
94 Html::rawElement( 'tr', array(),
95 Html::element( 'td', array(), $this->msg( 'pageinfo-views' )->text() ) .
96 Html::element( 'td', array(), $lang->formatNum( $pageInfo['views'] ) ) .
97 Html::element( 'td', array(), $lang->formatNum( $talkInfo['views'] ) )
98 ) .
99 Html::rawElement( 'tr', array(),
100 Html::element( 'td', array(), $this->msg( 'pageinfo-viewsperedit' )->text() ) .
101 Html::element( 'td', array(), $lang->formatNum( sprintf( '%.2f', $pageInfo['edits'] ? $pageInfo['views'] / $pageInfo['edits'] : 0 ) ) ) .
102 Html::element( 'td', array(), $lang->formatNum( sprintf( '%.2f', $talkInfo['edits'] ? $talkInfo['views'] / $talkInfo['edits'] : 0 ) ) )
103 );
104 }
105 return Html::rawElement( 'table', array( 'class' => 'wikitable mw-page-info' ), $content );
106 }
107
108 /**
109 * Return the total number of edits and number of unique editors
110 * on a given page. If page does not exist, returns false.
111 *
112 * @param $title Title object
113 * @param $canViewUnwatched bool
114 * @param $disableCounter bool
115 * @return array
116 */
117 public static function pageCountInfo( $title, $canViewUnwatched, $disableCounter ) {
118 wfProfileIn( __METHOD__ );
119 $id = $title->getArticleID();
120 $dbr = wfGetDB( DB_SLAVE );
121
122 $result = array();
123 if ( $canViewUnwatched ) {
124 $watchers = (int)$dbr->selectField(
125 'watchlist',
126 'COUNT(*)',
127 array(
128 'wl_namespace' => $title->getNamespace(),
129 'wl_title' => $title->getDBkey(),
130 ),
131 __METHOD__
132 );
133 $result['watchers'] = $watchers;
134 }
135
136 $edits = (int)$dbr->selectField(
137 'revision',
138 'COUNT(rev_page)',
139 array( 'rev_page' => $id ),
140 __METHOD__
141 );
142 $result['edits'] = $edits;
143
144 $authors = (int)$dbr->selectField(
145 'revision',
146 'COUNT(DISTINCT rev_user_text)',
147 array( 'rev_page' => $id ),
148 __METHOD__
149 );
150 $result['authors'] = $authors;
151
152 if ( !$disableCounter ) {
153 $views = (int)$dbr->selectField(
154 'page',
155 'page_counter',
156 array( 'page_id' => $id ),
157 __METHOD__
158 );
159 $result['views'] = $views;
160 }
161
162 wfProfileOut( __METHOD__ );
163 return $result;
164 }
165 }