(bug 13587) Execute deferred updates in api.php
[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 $retval = $ep->internalAttemptSave($result, $wgUser->isAllowed('bot') && $params['bot']);
151 switch($retval)
152 {
153 case EditPage::AS_HOOK_ERROR:
154 case EditPage::AS_HOOK_ERROR_EXPECTED:
155 $this->dieUsageMsg(array('hookaborted'));
156 case EditPage::AS_IMAGE_REDIRECT_ANON:
157 $this->dieUsageMsg(array('noimageredirect-anon'));
158 case EditPage::AS_IMAGE_REDIRECT_LOGGED:
159 $this->dieUsageMsg(array('noimageredirect-logged'));
160 case EditPage::AS_SPAM_ERROR:
161 $this->dieUsageMsg(array('spamdetected', $result['spam']));
162 case EditPage::AS_FILTERING:
163 $this->dieUsageMsg(array('filtered'));
164 case EditPage::AS_BLOCKED_PAGE_FOR_USER:
165 $this->dieUsageMsg(array('blockedtext'));
166 case EditPage::AS_MAX_ARTICLE_SIZE_EXCEEDED:
167 case EditPage::AS_CONTENT_TOO_BIG:
168 global $wgMaxArticleSize;
169 $this->dieUsageMsg(array('contenttoobig', $wgMaxArticleSize));
170 case EditPage::AS_READ_ONLY_PAGE_ANON:
171 $this->dieUsageMsg(array('noedit-anon'));
172 case EditPage::AS_READ_ONLY_PAGE_LOGGED:
173 $this->dieUsageMsg(array('noedit'));
174 case EditPage::AS_READ_ONLY_PAGE:
175 $this->dieUsageMsg(array('readonlytext'));
176 case EditPage::AS_RATE_LIMITED:
177 $this->dieUsageMsg(array('actionthrottledtext'));
178 case EditPage::AS_ARTICLE_WAS_DELETED:
179 $this->dieUsageMsg(array('wasdeleted'));
180 case EditPage::AS_NO_CREATE_PERMISSION:
181 $this->dieUsageMsg(array('nocreate-loggedin'));
182 case EditPage::AS_BLANK_ARTICLE:
183 $this->dieUsageMsg(array('blankpage'));
184 case EditPage::AS_CONFLICT_DETECTED:
185 $this->dieUsageMsg(array('editconflict'));
186 #case EditPage::AS_SUMMARY_NEEDED: Can't happen since we set wpIgnoreBlankSummary
187 #case EditPage::AS_TEXTBOX_EMPTY: Can't happen since we don't do sections
188 case EditPage::AS_END:
189 # This usually means some kind of race condition
190 # or DB weirdness occurred. Throw an unknown error here.
191 $this->dieUsageMsg(array('unknownerror', 'AS_END'));
192 case EditPage::AS_SUCCESS_NEW_ARTICLE:
193 $r['new'] = '';
194 case EditPage::AS_SUCCESS_UPDATE:
195 $r['result'] = "Success";
196 $r['pageid'] = $titleObj->getArticleID();
197 $r['title'] = $titleObj->getPrefixedText();
198 $newRevId = $titleObj->getLatestRevId();
199 if($newRevId == $oldRevId)
200 $r['nochange'] = '';
201 else
202 {
203 $r['oldrevid'] = $oldRevId;
204 $r['newrevid'] = $newRevId;
205 }
206 break;
207 default:
208 $this->dieUsageMsg(array('unknownerror', $retval));
209 }
210 $this->getResult()->addValue(null, $this->getModuleName(), $r);
211 }
212
213 protected function getDescription() {
214 return 'Create and edit pages.';
215 }
216
217 protected function getAllowedParams() {
218 return array (
219 'title' => null,
220 'section' => null,
221 'text' => null,
222 'token' => null,
223 'summary' => null,
224 'minor' => false,
225 'notminor' => false,
226 'bot' => false,
227 'basetimestamp' => null,
228 'recreate' => false,
229 'createonly' => false,
230 'captchaword' => null,
231 'captchaid' => null,
232 'watch' => false,
233 'unwatch' => false,
234 );
235 }
236
237 protected function getParamDescription() {
238 return array (
239 'title' => 'Page title',
240 'section' => 'Section number. 0 for the top section, \'new\' for a new section',
241 'text' => 'Page content',
242 'token' => 'Edit token. You can get one of these through prop=info',
243 'summary' => 'Edit summary',
244 'minor' => 'Minor edit',
245 'notminor' => 'Non-minor edit',
246 'bot' => 'Mark this edit as bot',
247 'basetimestamp' => array('Timestamp of the base revision (gotten through prop=revisions&rvprop=timestamp).',
248 'Used to detect edit conflicts; leave unset to ignore conflicts.'
249 ),
250 'recreate' => 'Override any errors about the article having been deleted in the meantime',
251 'createonly' => 'Don\'t create the page if it exists already',
252 'watch' => 'Add the page to your watchlist',
253 'unwatch' => 'Remove the page from your watchlist',
254 'captchaid' => 'CAPTCHA ID from previous request',
255 'captchaword' => 'Answer to the CAPTCHA',
256 );
257 }
258
259 protected function getExamples() {
260 return array (
261 "Edit a page (anonymous user):",
262 " api.php?action=edit&title=Test&summary=test%20summary&text=article%20content&basetimestamp=20070824123454&token=%2B\\"
263 );
264 }
265
266 public function getVersion() {
267 return __CLASS__ . ': $Id$';
268 }
269 }