ced5f0c24932f67ca640e47ea8d996e82f99975c
[lhc/web/wiklou.git] / includes / api / ApiFeedContributions.php
1 <?php
2 /**
3 *
4 *
5 * Created on June 06, 2011
6 *
7 * Copyright © 2011 Sam Reed
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * @ingroup API
29 */
30 class ApiFeedContributions extends ApiBase {
31
32 /**
33 * This module uses a custom feed wrapper printer.
34 *
35 * @return ApiFormatFeedWrapper
36 */
37 public function getCustomPrinter() {
38 return new ApiFormatFeedWrapper( $this->getMain() );
39 }
40
41 public function execute() {
42 $params = $this->extractRequestParams();
43
44 $config = $this->getConfig();
45 if ( !$config->get( 'Feed' ) ) {
46 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
47 }
48
49 $feedClasses = $config->get( 'FeedClasses' );
50 if ( !isset( $feedClasses[$params['feedformat']] ) ) {
51 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
52 }
53
54 if ( $params['showsizediff'] && $this->getConfig()->get( 'MiserMode' ) ) {
55 $this->dieUsage( 'Size difference is disabled in Miser Mode', 'sizediffdisabled' );
56 }
57
58 $msg = wfMessage( 'Contributions' )->inContentLanguage()->text();
59 $feedTitle = $config->get( 'Sitename' ) . ' - ' . $msg . ' [' . $config->get( 'LanguageCode' ) . ']';
60 $feedUrl = SpecialPage::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
61
62 $target = $params['user'] == 'newbies'
63 ? 'newbies'
64 : Title::makeTitleSafe( NS_USER, $params['user'] )->getText();
65
66 $feed = new $feedClasses[$params['feedformat']] (
67 $feedTitle,
68 htmlspecialchars( $msg ),
69 $feedUrl
70 );
71
72 $pager = new ContribsPager( $this->getContext(), array(
73 'target' => $target,
74 'namespace' => $params['namespace'],
75 'year' => $params['year'],
76 'month' => $params['month'],
77 'tagFilter' => $params['tagfilter'],
78 'deletedOnly' => $params['deletedonly'],
79 'topOnly' => $params['toponly'],
80 'newOnly' => $params['newonly'],
81 'showSizeDiff' => $params['showsizediff'],
82 ) );
83
84 $feedLimit = $this->getConfig()->get( 'FeedLimit' );
85 if ( $pager->getLimit() > $feedLimit ) {
86 $pager->setLimit( $feedLimit );
87 }
88
89 $feedItems = array();
90 if ( $pager->getNumRows() > 0 ) {
91 $count = 0;
92 $limit = $pager->getLimit();
93 foreach ( $pager->mResult as $row ) {
94 // ContribsPager selects one more row for navigation, skip that row
95 if ( ++$count > $limit ) {
96 break;
97 }
98 $feedItems[] = $this->feedItem( $row );
99 }
100 }
101
102 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
103 }
104
105 protected function feedItem( $row ) {
106 $title = Title::makeTitle( intval( $row->page_namespace ), $row->page_title );
107 if ( $title && $title->userCan( 'read', $this->getUser() ) ) {
108 $date = $row->rev_timestamp;
109 $comments = $title->getTalkPage()->getFullURL();
110 $revision = Revision::newFromRow( $row );
111
112 return new FeedItem(
113 $title->getPrefixedText(),
114 $this->feedItemDesc( $revision ),
115 $title->getFullURL( array( 'diff' => $revision->getId() ) ),
116 $date,
117 $this->feedItemAuthor( $revision ),
118 $comments
119 );
120 }
121
122 return null;
123 }
124
125 /**
126 * @param Revision $revision
127 * @return string
128 */
129 protected function feedItemAuthor( $revision ) {
130 return $revision->getUserText();
131 }
132
133 /**
134 * @param Revision $revision
135 * @return string
136 */
137 protected function feedItemDesc( $revision ) {
138 if ( $revision ) {
139 $msg = wfMessage( 'colon-separator' )->inContentLanguage()->text();
140 $content = $revision->getContent();
141
142 if ( $content instanceof TextContent ) {
143 // only textual content has a "source view".
144 $html = nl2br( htmlspecialchars( $content->getNativeData() ) );
145 } else {
146 //XXX: we could get an HTML representation of the content via getParserOutput, but that may
147 // contain JS magic and generally may not be suitable for inclusion in a feed.
148 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
149 //Compare also FeedUtils::formatDiffRow.
150 $html = '';
151 }
152
153 return '<p>' . htmlspecialchars( $revision->getUserText() ) . $msg .
154 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
155 "</p>\n<hr />\n<div>" . $html . "</div>";
156 }
157
158 return '';
159 }
160
161 public function getAllowedParams() {
162 $feedFormatNames = array_keys( $this->getConfig()->get( 'FeedClasses' ) );
163
164 $ret = array(
165 'feedformat' => array(
166 ApiBase::PARAM_DFLT => 'rss',
167 ApiBase::PARAM_TYPE => $feedFormatNames
168 ),
169 'user' => array(
170 ApiBase::PARAM_TYPE => 'user',
171 ApiBase::PARAM_REQUIRED => true,
172 ),
173 'namespace' => array(
174 ApiBase::PARAM_TYPE => 'namespace'
175 ),
176 'year' => array(
177 ApiBase::PARAM_TYPE => 'integer'
178 ),
179 'month' => array(
180 ApiBase::PARAM_TYPE => 'integer'
181 ),
182 'tagfilter' => array(
183 ApiBase::PARAM_ISMULTI => true,
184 ApiBase::PARAM_TYPE => array_values( ChangeTags::listDefinedTags() ),
185 ApiBase::PARAM_DFLT => '',
186 ),
187 'deletedonly' => false,
188 'toponly' => false,
189 'newonly' => false,
190 'showsizediff' => array(
191 ApiBase::PARAM_DFLT => false,
192 ),
193 );
194
195 if ( $this->getConfig()->get( 'MiserMode' ) ) {
196 $ret['showsizediff'][ApiBase::PARAM_HELP_MSG] = 'api-help-param-disabled-in-miser-mode';
197 }
198
199 return $ret;
200 }
201
202 protected function getExamplesMessages() {
203 return array(
204 'action=feedcontributions&user=Example'
205 => 'apihelp-feedcontributions-example-simple',
206 );
207 }
208 }