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