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