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