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