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