APIEditPage:
[lhc/web/wiklou.git] / includes / api / ApiEditPage.php
1 <?php
2
3 /*
4 * Created on August 16, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2007 Iker Labarga <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ("ApiBase.php");
29 }
30
31 /**
32 * A query module to list all external URLs found on a given set of pages.
33 *
34 * @ingroup API
35 */
36 class ApiEditPage extends ApiBase {
37
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName);
40 }
41
42 public function execute() {
43 global $wgUser;
44 $this->getMain()->requestWriteMode();
45
46 $params = $this->extractRequestParams();
47 if(is_null($params['title']))
48 $this->dieUsageMsg(array('missingparam', 'title'));
49 if(is_null($params['text']))
50 $this->dieUsageMsg(array('missingparam', 'text'));
51 if(is_null($params['token']))
52 $this->dieUsageMsg(array('missingparam', 'token'));
53 if(!$wgUser->matchEditToken($params['token']))
54 $this->dieUsageMsg(array('sessionfailure'));
55
56 $titleObj = Title::newFromText($params['title']);
57 if(!$titleObj)
58 $this->dieUsageMsg(array('invalidtitle', $params['title']));
59
60 if($params['createonly'] && $titleObj->exists())
61 $this->dieUsageMsg(array('createonly-exists'));
62
63 // Now let's check whether we're even allowed to do this
64 $errors = $titleObj->getUserPermissionsErrors('edit', $wgUser);
65 if(!$titleObj->exists())
66 $errors = array_merge($errors, $titleObj->getUserPermissionsErrors('create', $wgUser));
67 if(!empty($errors))
68 $this->dieUsageMsg($errors[0]);
69
70 $articleObj = new Article($titleObj);
71 $ep = new EditPage($articleObj);
72
73 if ( !wfRunHooks( 'AlternateEdit', array( &$ep ) ) )
74 $this->dieUsageMsg(array('hookaborted'));
75
76 // EditPage wants to parse its stuff from a WebRequest
77 // That interface kind of sucks, but it's workable
78 $reqArr = array('wpTextbox1' => $params['text'],
79 'wpEdittoken' => $params['token'],
80 'wpIgnoreBlankSummary' => ''
81 );
82 if(!is_null($params['summary']))
83 $reqArr['wpSummary'] = $params['summary'];
84 # Watch out for basetimestamp == ''
85 # wfTimestamp() treats it as NOW, almost certainly causing an edit conflict
86 if(!is_null($params['basetimestamp']) && $params['basetimestamp'] != '')
87 $reqArr['wpEdittime'] = wfTimestamp(TS_MW, $params['basetimestamp']);
88 else
89 $reqArr['wpEdittime'] = $articleObj->getTimestamp();
90 # Fake wpStartime
91 $reqArr['wpStarttime'] = $reqArr['wpEdittime'];
92 if($params['minor'] || (!$params['notminor'] && $wgUser->getOption('minordefault')))
93 $reqArr['wpMinoredit'] = '';
94 if($params['recreate'])
95 $reqArr['wpRecreate'] = '';
96 if(!is_null($params['section']))
97 {
98 $section = intval($params['section']);
99 if($section == 0 && $params['section'] != '0' && $params['section'] != 'new')
100 $this->dieUsage("The section parameter must be set to an integer or 'new'", "invalidsection");
101 $reqArr['wpSection'] = $params['section'];
102 }
103
104 if($params['watch'])
105 $watch = true;
106 else if($params['unwatch'])
107 $watch = false;
108 else if($titleObj->userIsWatching())
109 $watch = true;
110 else if($wgUser->getOption('watchdefault'))
111 $watch = true;
112 else if($wgUser->getOption('watchcreations') && !$titleObj->exists())
113 $watch = true;
114 else
115 $watch = false;
116 if($watch)
117 $reqArr['wpWatchthis'] = '';
118
119 $req = new FauxRequest($reqArr, true);
120 $ep->importFormData($req);
121
122 # Run hooks
123 # Handle CAPTCHA parameters
124 global $wgRequest;
125 if(isset($params['captchaid']))
126 $wgRequest->data['wpCaptchaId'] = $params['captchaid'];
127 if(isset($params['captchaword']))
128 $wgRequest->data['wpCaptchaWord'] = $params['captchaword'];
129 $r = array();
130 if(!wfRunHooks('APIEditBeforeSave', array(&$ep, $ep->textbox1, &$r)))
131 {
132 if(!empty($r))
133 {
134 $r['result'] = "Failure";
135 $this->getResult()->addValue(null, $this->getModuleName(), $r);
136 return;
137 }
138 else
139 $this->dieUsageMsg(array('hookaborted'));
140 }
141
142 # Do the actual save
143 $oldRevId = $articleObj->getRevIdFetched();
144 $result = null;
145 # *Something* is setting $wgTitle to a title corresponding to "Msg",
146 # but that breaks API mode detection through is_null($wgTitle)
147 global $wgTitle;
148 $wgTitle = null;
149 # Fake $wgRequest for some hooks inside EditPage
150 # FIXME: This interface SUCKS
151 $oldRequest = $wgRequest;
152 $wgRequest = $req;
153
154 $retval = $ep->internalAttemptSave($result, $wgUser->isAllowed('bot') && $params['bot']);
155 $wgRequest = $oldRequest;
156 switch($retval)
157 {
158 case EditPage::AS_HOOK_ERROR:
159 case EditPage::AS_HOOK_ERROR_EXPECTED:
160 $this->dieUsageMsg(array('hookaborted'));
161 case EditPage::AS_IMAGE_REDIRECT_ANON:
162 $this->dieUsageMsg(array('noimageredirect-anon'));
163 case EditPage::AS_IMAGE_REDIRECT_LOGGED:
164 $this->dieUsageMsg(array('noimageredirect-logged'));
165 case EditPage::AS_SPAM_ERROR:
166 $this->dieUsageMsg(array('spamdetected', $result['spam']));
167 case EditPage::AS_FILTERING:
168 $this->dieUsageMsg(array('filtered'));
169 case EditPage::AS_BLOCKED_PAGE_FOR_USER:
170 $this->dieUsageMsg(array('blockedtext'));
171 case EditPage::AS_MAX_ARTICLE_SIZE_EXCEEDED:
172 case EditPage::AS_CONTENT_TOO_BIG:
173 global $wgMaxArticleSize;
174 $this->dieUsageMsg(array('contenttoobig', $wgMaxArticleSize));
175 case EditPage::AS_READ_ONLY_PAGE_ANON:
176 $this->dieUsageMsg(array('noedit-anon'));
177 case EditPage::AS_READ_ONLY_PAGE_LOGGED:
178 $this->dieUsageMsg(array('noedit'));
179 case EditPage::AS_READ_ONLY_PAGE:
180 $this->dieUsageMsg(array('readonlytext'));
181 case EditPage::AS_RATE_LIMITED:
182 $this->dieUsageMsg(array('actionthrottledtext'));
183 case EditPage::AS_ARTICLE_WAS_DELETED:
184 $this->dieUsageMsg(array('wasdeleted'));
185 case EditPage::AS_NO_CREATE_PERMISSION:
186 $this->dieUsageMsg(array('nocreate-loggedin'));
187 case EditPage::AS_BLANK_ARTICLE:
188 $this->dieUsageMsg(array('blankpage'));
189 case EditPage::AS_CONFLICT_DETECTED:
190 $this->dieUsageMsg(array('editconflict'));
191 #case EditPage::AS_SUMMARY_NEEDED: Can't happen since we set wpIgnoreBlankSummary
192 #case EditPage::AS_TEXTBOX_EMPTY: Can't happen since we don't do sections
193 case EditPage::AS_END:
194 # This usually means some kind of race condition
195 # or DB weirdness occurred. Throw an unknown error here.
196 $this->dieUsageMsg(array('unknownerror', 'AS_END'));
197 case EditPage::AS_SUCCESS_NEW_ARTICLE:
198 $r['new'] = '';
199 case EditPage::AS_SUCCESS_UPDATE:
200 $r['result'] = "Success";
201 $r['pageid'] = $titleObj->getArticleID();
202 $r['title'] = $titleObj->getPrefixedText();
203 $newRevId = $titleObj->getLatestRevId();
204 if($newRevId == $oldRevId)
205 $r['nochange'] = '';
206 else
207 {
208 $r['oldrevid'] = $oldRevId;
209 $r['newrevid'] = $newRevId;
210 }
211 break;
212 default:
213 $this->dieUsageMsg(array('unknownerror', $retval));
214 }
215 $this->getResult()->addValue(null, $this->getModuleName(), $r);
216 }
217
218 public function mustBePosted() {
219 return true;
220 }
221
222 protected function getDescription() {
223 return 'Create and edit pages.';
224 }
225
226 protected function getAllowedParams() {
227 return array (
228 'title' => null,
229 'section' => null,
230 'text' => null,
231 'token' => null,
232 'summary' => null,
233 'minor' => false,
234 'notminor' => false,
235 'bot' => false,
236 'basetimestamp' => null,
237 'recreate' => false,
238 'createonly' => false,
239 'captchaword' => null,
240 'captchaid' => null,
241 'watch' => false,
242 'unwatch' => false,
243 );
244 }
245
246 protected function getParamDescription() {
247 return array (
248 'title' => 'Page title',
249 'section' => 'Section number. 0 for the top section, \'new\' for a new section',
250 'text' => 'Page content',
251 'token' => 'Edit token. You can get one of these through prop=info',
252 'summary' => 'Edit summary. Also section title when section=new',
253 'minor' => 'Minor edit',
254 'notminor' => 'Non-minor edit',
255 'bot' => 'Mark this edit as bot',
256 'basetimestamp' => array('Timestamp of the base revision (gotten through prop=revisions&rvprop=timestamp).',
257 'Used to detect edit conflicts; leave unset to ignore conflicts.'
258 ),
259 'recreate' => 'Override any errors about the article having been deleted in the meantime',
260 'createonly' => 'Don\'t create the page if it exists already',
261 'watch' => 'Add the page to your watchlist',
262 'unwatch' => 'Remove the page from your watchlist',
263 'captchaid' => 'CAPTCHA ID from previous request',
264 'captchaword' => 'Answer to the CAPTCHA',
265 );
266 }
267
268 protected function getExamples() {
269 return array (
270 "Edit a page (anonymous user):",
271 " api.php?action=edit&title=Test&summary=test%20summary&text=article%20content&basetimestamp=20070824123454&token=%2B\\"
272 );
273 }
274
275 public function getVersion() {
276 return __CLASS__ . ': $Id$';
277 }
278 }