Merge "deferred: make DeferredUpdates::attemptUpdate() use callback owners for $fname...
[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\Revision\RevisionAccessException;
25 use MediaWiki\Revision\RevisionRecord;
26 use MediaWiki\Revision\RevisionStore;
27 use MediaWiki\Revision\SlotRecord;
28
29 /**
30 * @ingroup API
31 */
32 class ApiFeedContributions extends ApiBase {
33
34 /** @var RevisionStore */
35 private $revisionStore;
36
37 /** @var TitleParser */
38 private $titleParser;
39
40 /**
41 * This module uses a custom feed wrapper printer.
42 *
43 * @return ApiFormatFeedWrapper
44 */
45 public function getCustomPrinter() {
46 return new ApiFormatFeedWrapper( $this->getMain() );
47 }
48
49 public function execute() {
50 $this->revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
51 $this->titleParser = MediaWikiServices::getInstance()->getTitleParser();
52
53 $params = $this->extractRequestParams();
54
55 $config = $this->getConfig();
56 if ( !$config->get( 'Feed' ) ) {
57 $this->dieWithError( 'feed-unavailable' );
58 }
59
60 $feedClasses = $config->get( 'FeedClasses' );
61 if ( !isset( $feedClasses[$params['feedformat']] ) ) {
62 $this->dieWithError( 'feed-invalid' );
63 }
64
65 if ( $params['showsizediff'] && $this->getConfig()->get( 'MiserMode' ) ) {
66 $this->dieWithError( 'apierror-sizediffdisabled' );
67 }
68
69 $msg = wfMessage( 'Contributions' )->inContentLanguage()->text();
70 $feedTitle = $config->get( 'Sitename' ) . ' - ' . $msg .
71 ' [' . $config->get( 'LanguageCode' ) . ']';
72 $feedUrl = SpecialPage::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
73
74 $target = 'newbies';
75 if ( $params['user'] != 'newbies' ) {
76 try {
77 $target = $this->titleParser
78 ->parseTitle( $params['user'], NS_USER )
79 ->getText();
80 } catch ( MalformedTitleException $e ) {
81 $this->dieWithError(
82 [ 'apierror-baduser', 'user', wfEscapeWikiText( $params['user'] ) ],
83 'baduser_' . $this->encodeParamName( 'user' )
84 );
85 }
86 }
87
88 $feed = new $feedClasses[$params['feedformat']] (
89 $feedTitle,
90 htmlspecialchars( $msg ),
91 $feedUrl
92 );
93
94 // Convert year/month parameters to end parameter
95 $params['start'] = '';
96 $params['end'] = '';
97 $params = ContribsPager::processDateFilter( $params );
98
99 $pager = new ContribsPager( $this->getContext(), [
100 'target' => $target,
101 'namespace' => $params['namespace'],
102 'start' => $params['start'],
103 'end' => $params['end'],
104 'tagFilter' => $params['tagfilter'],
105 'deletedOnly' => $params['deletedonly'],
106 'topOnly' => $params['toponly'],
107 'newOnly' => $params['newonly'],
108 'hideMinor' => $params['hideminor'],
109 'showSizeDiff' => $params['showsizediff'],
110 ] );
111
112 $feedLimit = $this->getConfig()->get( 'FeedLimit' );
113 if ( $pager->getLimit() > $feedLimit ) {
114 $pager->setLimit( $feedLimit );
115 }
116
117 $feedItems = [];
118 if ( $pager->getNumRows() > 0 ) {
119 $count = 0;
120 $limit = $pager->getLimit();
121 foreach ( $pager->mResult as $row ) {
122 // ContribsPager selects one more row for navigation, skip that row
123 if ( ++$count > $limit ) {
124 break;
125 }
126 $item = $this->feedItem( $row );
127 if ( $item !== null ) {
128 $feedItems[] = $item;
129 }
130 }
131 }
132
133 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
134 }
135
136 protected function feedItem( $row ) {
137 // This hook is the api contributions equivalent to the
138 // ContributionsLineEnding hook. Hook implementers may cancel
139 // the hook to signal the user is not allowed to read this item.
140 $feedItem = null;
141 $hookResult = Hooks::run(
142 'ApiFeedContributions::feedItem',
143 [ $row, $this->getContext(), &$feedItem ]
144 );
145 // Hook returned a valid feed item
146 if ( $feedItem instanceof FeedItem ) {
147 return $feedItem;
148 // Hook was canceled and did not return a valid feed item
149 } elseif ( !$hookResult ) {
150 return null;
151 }
152
153 // Hook completed and did not return a valid feed item
154 $title = Title::makeTitle( (int)$row->page_namespace, $row->page_title );
155 $user = $this->getUser();
156
157 if ( $title && $this->getPermissionManager()->userCan( 'read', $user, $title ) ) {
158 $date = $row->rev_timestamp;
159 $comments = $title->getTalkPage()->getFullURL();
160 $revision = $this->revisionStore->newRevisionFromRow( $row );
161
162 return new FeedItem(
163 $title->getPrefixedText(),
164 $this->feedItemDesc( $revision ),
165 $title->getFullURL( [ 'diff' => $revision->getId() ] ),
166 $date,
167 $this->feedItemAuthor( $revision ),
168 $comments
169 );
170 }
171
172 return null;
173 }
174
175 /**
176 * @since 1.32, takes a RevisionRecord instead of a Revision
177 * @param RevisionRecord $revision
178 * @return string
179 */
180 protected function feedItemAuthor( RevisionRecord $revision ) {
181 $user = $revision->getUser();
182 return $user ? $user->getName() : '';
183 }
184
185 /**
186 * @since 1.32, takes a RevisionRecord instead of a Revision
187 * @param RevisionRecord $revision
188 * @return string
189 */
190 protected function feedItemDesc( RevisionRecord $revision ) {
191 $msg = wfMessage( 'colon-separator' )->inContentLanguage()->text();
192 try {
193 $content = $revision->getContent( SlotRecord::MAIN );
194 } catch ( RevisionAccessException $e ) {
195 $content = null;
196 }
197
198 if ( $content instanceof TextContent ) {
199 // only textual content has a "source view".
200 $html = nl2br( htmlspecialchars( $content->getText() ) );
201 } else {
202 // XXX: we could get an HTML representation of the content via getParserOutput, but that may
203 // contain JS magic and generally may not be suitable for inclusion in a feed.
204 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
205 // Compare also FeedUtils::formatDiffRow.
206 $html = '';
207 }
208
209 $comment = $revision->getComment();
210
211 return '<p>' . htmlspecialchars( $this->feedItemAuthor( $revision ) ) . $msg .
212 htmlspecialchars( FeedItem::stripComment( $comment ? $comment->text : '' ) ) .
213 "</p>\n<hr />\n<div>" . $html . '</div>';
214 }
215
216 public function getAllowedParams() {
217 $feedFormatNames = array_keys( $this->getConfig()->get( 'FeedClasses' ) );
218
219 $ret = [
220 'feedformat' => [
221 ApiBase::PARAM_DFLT => 'rss',
222 ApiBase::PARAM_TYPE => $feedFormatNames
223 ],
224 'user' => [
225 ApiBase::PARAM_TYPE => 'user',
226 ApiBase::PARAM_REQUIRED => true,
227 ],
228 'namespace' => [
229 ApiBase::PARAM_TYPE => 'namespace'
230 ],
231 'year' => [
232 ApiBase::PARAM_TYPE => 'integer'
233 ],
234 'month' => [
235 ApiBase::PARAM_TYPE => 'integer'
236 ],
237 'tagfilter' => [
238 ApiBase::PARAM_ISMULTI => true,
239 ApiBase::PARAM_TYPE => array_values( ChangeTags::listDefinedTags() ),
240 ApiBase::PARAM_DFLT => '',
241 ],
242 'deletedonly' => false,
243 'toponly' => false,
244 'newonly' => false,
245 'hideminor' => false,
246 'showsizediff' => [
247 ApiBase::PARAM_DFLT => false,
248 ],
249 ];
250
251 if ( $this->getConfig()->get( 'MiserMode' ) ) {
252 $ret['showsizediff'][ApiBase::PARAM_HELP_MSG] = 'api-help-param-disabled-in-miser-mode';
253 }
254
255 return $ret;
256 }
257
258 protected function getExamplesMessages() {
259 return [
260 'action=feedcontributions&user=Example'
261 => 'apihelp-feedcontributions-example-simple',
262 ];
263 }
264 }