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