Merge "Http::getProxy() method to get proxy configuration"
[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 .
60 ' [' . $config->get( 'LanguageCode' ) . ']';
61 $feedUrl = SpecialPage::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
62
63 $target = $params['user'] == 'newbies'
64 ? 'newbies'
65 : Title::makeTitleSafe( NS_USER, $params['user'] )->getText();
66
67 $feed = new $feedClasses[$params['feedformat']] (
68 $feedTitle,
69 htmlspecialchars( $msg ),
70 $feedUrl
71 );
72
73 $pager = new ContribsPager( $this->getContext(), [
74 'target' => $target,
75 'namespace' => $params['namespace'],
76 'year' => $params['year'],
77 'month' => $params['month'],
78 'tagFilter' => $params['tagfilter'],
79 'deletedOnly' => $params['deletedonly'],
80 'topOnly' => $params['toponly'],
81 'newOnly' => $params['newonly'],
82 'showSizeDiff' => $params['showsizediff'],
83 ] );
84
85 $feedLimit = $this->getConfig()->get( 'FeedLimit' );
86 if ( $pager->getLimit() > $feedLimit ) {
87 $pager->setLimit( $feedLimit );
88 }
89
90 $feedItems = [];
91 if ( $pager->getNumRows() > 0 ) {
92 $count = 0;
93 $limit = $pager->getLimit();
94 foreach ( $pager->mResult as $row ) {
95 // ContribsPager selects one more row for navigation, skip that row
96 if ( ++$count > $limit ) {
97 break;
98 }
99 $item = $this->feedItem( $row );
100 if ( $item !== null ) {
101 $feedItems[] = $item;
102 }
103 }
104 }
105
106 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
107 }
108
109 protected function feedItem( $row ) {
110 // This hook is the api contributions equivalent to the
111 // ContributionsLineEnding hook. Hook implementers may cancel
112 // the hook to signal the user is not allowed to read this item.
113 $feedItem = null;
114 $hookResult = Hooks::run(
115 'ApiFeedContributions::feedItem',
116 [ $row, $this->getContext(), &$feedItem ]
117 );
118 // Hook returned a valid feed item
119 if ( $feedItem instanceof FeedItem ) {
120 return $feedItem;
121 // Hook was canceled and did not return a valid feed item
122 } elseif ( !$hookResult ) {
123 return null;
124 }
125
126 // Hook completed and did not return a valid feed item
127 $title = Title::makeTitle( intval( $row->page_namespace ), $row->page_title );
128 if ( $title && $title->userCan( 'read', $this->getUser() ) ) {
129 $date = $row->rev_timestamp;
130 $comments = $title->getTalkPage()->getFullURL();
131 $revision = Revision::newFromRow( $row );
132
133 return new FeedItem(
134 $title->getPrefixedText(),
135 $this->feedItemDesc( $revision ),
136 $title->getFullURL( [ 'diff' => $revision->getId() ] ),
137 $date,
138 $this->feedItemAuthor( $revision ),
139 $comments
140 );
141 }
142
143 return null;
144 }
145
146 /**
147 * @param Revision $revision
148 * @return string
149 */
150 protected function feedItemAuthor( $revision ) {
151 return $revision->getUserText();
152 }
153
154 /**
155 * @param Revision $revision
156 * @return string
157 */
158 protected function feedItemDesc( $revision ) {
159 if ( $revision ) {
160 $msg = wfMessage( 'colon-separator' )->inContentLanguage()->text();
161 $content = $revision->getContent();
162
163 if ( $content instanceof TextContent ) {
164 // only textual content has a "source view".
165 $html = nl2br( htmlspecialchars( $content->getNativeData() ) );
166 } else {
167 // XXX: we could get an HTML representation of the content via getParserOutput, but that may
168 // contain JS magic and generally may not be suitable for inclusion in a feed.
169 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
170 // Compare also FeedUtils::formatDiffRow.
171 $html = '';
172 }
173
174 return '<p>' . htmlspecialchars( $revision->getUserText() ) . $msg .
175 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
176 "</p>\n<hr />\n<div>" . $html . '</div>';
177 }
178
179 return '';
180 }
181
182 public function getAllowedParams() {
183 $feedFormatNames = array_keys( $this->getConfig()->get( 'FeedClasses' ) );
184
185 $ret = [
186 'feedformat' => [
187 ApiBase::PARAM_DFLT => 'rss',
188 ApiBase::PARAM_TYPE => $feedFormatNames
189 ],
190 'user' => [
191 ApiBase::PARAM_TYPE => 'user',
192 ApiBase::PARAM_REQUIRED => true,
193 ],
194 'namespace' => [
195 ApiBase::PARAM_TYPE => 'namespace'
196 ],
197 'year' => [
198 ApiBase::PARAM_TYPE => 'integer'
199 ],
200 'month' => [
201 ApiBase::PARAM_TYPE => 'integer'
202 ],
203 'tagfilter' => [
204 ApiBase::PARAM_ISMULTI => true,
205 ApiBase::PARAM_TYPE => array_values( ChangeTags::listDefinedTags() ),
206 ApiBase::PARAM_DFLT => '',
207 ],
208 'deletedonly' => false,
209 'toponly' => false,
210 'newonly' => false,
211 'showsizediff' => [
212 ApiBase::PARAM_DFLT => false,
213 ],
214 ];
215
216 if ( $this->getConfig()->get( 'MiserMode' ) ) {
217 $ret['showsizediff'][ApiBase::PARAM_HELP_MSG] = 'api-help-param-disabled-in-miser-mode';
218 }
219
220 return $ret;
221 }
222
223 protected function getExamplesMessages() {
224 return [
225 'action=feedcontributions&user=Example'
226 => 'apihelp-feedcontributions-example-simple',
227 ];
228 }
229 }