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