Remove the AlternateEdit hook from the API: it is a hook meant to show a different...
[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 # See if the MD5 hash checks out
71 if(isset($params['md5']))
72 if(md5($params['text']) !== $params['md5'])
73 $this->dieUsageMsg(array('hashcheckfailed'));
74
75 $articleObj = new Article($titleObj);
76 $ep = new EditPage($articleObj);
77
78 // EditPage wants to parse its stuff from a WebRequest
79 // That interface kind of sucks, but it's workable
80 $reqArr = array('wpTextbox1' => $params['text'],
81 'wpEdittoken' => $params['token'],
82 'wpIgnoreBlankSummary' => ''
83 );
84 if(!is_null($params['summary']))
85 $reqArr['wpSummary'] = $params['summary'];
86 # Watch out for basetimestamp == ''
87 # wfTimestamp() treats it as NOW, almost certainly causing an edit conflict
88 if(!is_null($params['basetimestamp']) && $params['basetimestamp'] != '')
89 $reqArr['wpEdittime'] = wfTimestamp(TS_MW, $params['basetimestamp']);
90 else
91 $reqArr['wpEdittime'] = $articleObj->getTimestamp();
92 # Fake wpStartime
93 $reqArr['wpStarttime'] = $reqArr['wpEdittime'];
94 if($params['minor'] || (!$params['notminor'] && $wgUser->getOption('minordefault')))
95 $reqArr['wpMinoredit'] = '';
96 if($params['recreate'])
97 $reqArr['wpRecreate'] = '';
98 if(!is_null($params['section']))
99 {
100 $section = intval($params['section']);
101 if($section == 0 && $params['section'] != '0' && $params['section'] != 'new')
102 $this->dieUsage("The section parameter must be set to an integer or 'new'", "invalidsection");
103 $reqArr['wpSection'] = $params['section'];
104 }
105
106 if($params['watch'])
107 $watch = true;
108 else if($params['unwatch'])
109 $watch = false;
110 else if($titleObj->userIsWatching())
111 $watch = true;
112 else if($wgUser->getOption('watchdefault'))
113 $watch = true;
114 else if($wgUser->getOption('watchcreations') && !$titleObj->exists())
115 $watch = true;
116 else
117 $watch = false;
118 if($watch)
119 $reqArr['wpWatchthis'] = '';
120
121 $req = new FauxRequest($reqArr, true);
122 $ep->importFormData($req);
123
124 # Run hooks
125 # Handle CAPTCHA parameters
126 global $wgRequest;
127 if(isset($params['captchaid']))
128 $wgRequest->data['wpCaptchaId'] = $params['captchaid'];
129 if(isset($params['captchaword']))
130 $wgRequest->data['wpCaptchaWord'] = $params['captchaword'];
131 $r = array();
132 if(!wfRunHooks('APIEditBeforeSave', array(&$ep, $ep->textbox1, &$r)))
133 {
134 if(!empty($r))
135 {
136 $r['result'] = "Failure";
137 $this->getResult()->addValue(null, $this->getModuleName(), $r);
138 return;
139 }
140 else
141 $this->dieUsageMsg(array('hookaborted'));
142 }
143
144 # Do the actual save
145 $oldRevId = $articleObj->getRevIdFetched();
146 $result = null;
147 # *Something* is setting $wgTitle to a title corresponding to "Msg",
148 # but that breaks API mode detection through is_null($wgTitle)
149 global $wgTitle;
150 $wgTitle = null;
151 # Fake $wgRequest for some hooks inside EditPage
152 # FIXME: This interface SUCKS
153 $oldRequest = $wgRequest;
154 $wgRequest = $req;
155
156 $retval = $ep->internalAttemptSave($result, $wgUser->isAllowed('bot') && $params['bot']);
157 $wgRequest = $oldRequest;
158 switch($retval)
159 {
160 case EditPage::AS_HOOK_ERROR:
161 case EditPage::AS_HOOK_ERROR_EXPECTED:
162 $this->dieUsageMsg(array('hookaborted'));
163 case EditPage::AS_IMAGE_REDIRECT_ANON:
164 $this->dieUsageMsg(array('noimageredirect-anon'));
165 case EditPage::AS_IMAGE_REDIRECT_LOGGED:
166 $this->dieUsageMsg(array('noimageredirect-logged'));
167 case EditPage::AS_SPAM_ERROR:
168 $this->dieUsageMsg(array('spamdetected', $result['spam']));
169 case EditPage::AS_FILTERING:
170 $this->dieUsageMsg(array('filtered'));
171 case EditPage::AS_BLOCKED_PAGE_FOR_USER:
172 $this->dieUsageMsg(array('blockedtext'));
173 case EditPage::AS_MAX_ARTICLE_SIZE_EXCEEDED:
174 case EditPage::AS_CONTENT_TOO_BIG:
175 global $wgMaxArticleSize;
176 $this->dieUsageMsg(array('contenttoobig', $wgMaxArticleSize));
177 case EditPage::AS_READ_ONLY_PAGE_ANON:
178 $this->dieUsageMsg(array('noedit-anon'));
179 case EditPage::AS_READ_ONLY_PAGE_LOGGED:
180 $this->dieUsageMsg(array('noedit'));
181 case EditPage::AS_READ_ONLY_PAGE:
182 $this->dieUsageMsg(array('readonlytext'));
183 case EditPage::AS_RATE_LIMITED:
184 $this->dieUsageMsg(array('actionthrottledtext'));
185 case EditPage::AS_ARTICLE_WAS_DELETED:
186 $this->dieUsageMsg(array('wasdeleted'));
187 case EditPage::AS_NO_CREATE_PERMISSION:
188 $this->dieUsageMsg(array('nocreate-loggedin'));
189 case EditPage::AS_BLANK_ARTICLE:
190 $this->dieUsageMsg(array('blankpage'));
191 case EditPage::AS_CONFLICT_DETECTED:
192 $this->dieUsageMsg(array('editconflict'));
193 #case EditPage::AS_SUMMARY_NEEDED: Can't happen since we set wpIgnoreBlankSummary
194 #case EditPage::AS_TEXTBOX_EMPTY: Can't happen since we don't do sections
195 case EditPage::AS_END:
196 # This usually means some kind of race condition
197 # or DB weirdness occurred. Throw an unknown error here.
198 $this->dieUsageMsg(array('unknownerror', 'AS_END'));
199 case EditPage::AS_SUCCESS_NEW_ARTICLE:
200 $r['new'] = '';
201 case EditPage::AS_SUCCESS_UPDATE:
202 $r['result'] = "Success";
203 $r['pageid'] = $titleObj->getArticleID();
204 $r['title'] = $titleObj->getPrefixedText();
205 $newRevId = $titleObj->getLatestRevId();
206 if($newRevId == $oldRevId)
207 $r['nochange'] = '';
208 else
209 {
210 $r['oldrevid'] = $oldRevId;
211 $r['newrevid'] = $newRevId;
212 }
213 break;
214 default:
215 $this->dieUsageMsg(array('unknownerror', $retval));
216 }
217 $this->getResult()->addValue(null, $this->getModuleName(), $r);
218 }
219
220 public function mustBePosted() {
221 return true;
222 }
223
224 protected function getDescription() {
225 return 'Create and edit pages.';
226 }
227
228 protected function getAllowedParams() {
229 return array (
230 'title' => null,
231 'section' => null,
232 'text' => null,
233 'token' => null,
234 'summary' => null,
235 'minor' => false,
236 'notminor' => false,
237 'bot' => false,
238 'basetimestamp' => null,
239 'recreate' => false,
240 'createonly' => false,
241 'captchaword' => null,
242 'captchaid' => null,
243 'watch' => false,
244 'unwatch' => false,
245 'md5' => null,
246 );
247 }
248
249 protected function getParamDescription() {
250 return array (
251 'title' => 'Page title',
252 'section' => 'Section number. 0 for the top section, \'new\' for a new section',
253 'text' => 'Page content',
254 'token' => 'Edit token. You can get one of these through prop=info',
255 'summary' => 'Edit summary. Also section title when section=new',
256 'minor' => 'Minor edit',
257 'notminor' => 'Non-minor edit',
258 'bot' => 'Mark this edit as bot',
259 'basetimestamp' => array('Timestamp of the base revision (gotten through prop=revisions&rvprop=timestamp).',
260 'Used to detect edit conflicts; leave unset to ignore conflicts.'
261 ),
262 'recreate' => 'Override any errors about the article having been deleted in the meantime',
263 'createonly' => 'Don\'t create the page if it exists already',
264 'watch' => 'Add the page to your watchlist',
265 'unwatch' => 'Remove the page from your watchlist',
266 'captchaid' => 'CAPTCHA ID from previous request',
267 'captchaword' => 'Answer to the CAPTCHA',
268 'md5' => 'The MD5 hash of the new article text. If set, the edit won\'t be done unless the hash is correct',
269 );
270 }
271
272 protected function getExamples() {
273 return array (
274 "Edit a page (anonymous user):",
275 " api.php?action=edit&title=Test&summary=test%20summary&text=article%20content&basetimestamp=20070824123454&token=%2B\\"
276 );
277 }
278
279 public function getVersion() {
280 return __CLASS__ . ': $Id$';
281 }
282 }