4 * Created on August 16, 2007
6 * API for MediaWiki 1.8+
8 * Copyright © 2007 Iker Labarga <Firstname><Lastname>@gmail.com
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.
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.
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
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( "ApiBase.php" );
32 * A module that allows for editing and creating pages.
34 * Currently, this wraps around the EditPage class in an ugly way,
35 * EditPage.php should be rewritten to provide a cleaner interface
38 class ApiEditPage
extends ApiBase
{
40 public function __construct( $query, $moduleName ) {
41 parent
::__construct( $query, $moduleName );
44 public function execute() {
46 $params = $this->extractRequestParams();
48 if ( is_null( $params['title'] ) ) {
49 $this->dieUsageMsg( array( 'missingparam', 'title' ) );
52 if ( is_null( $params['text'] ) && is_null( $params['appendtext'] ) &&
53 is_null( $params['prependtext'] ) &&
54 $params['undo'] == 0 )
56 $this->dieUsageMsg( array( 'missingtext' ) );
59 $titleObj = Title
::newFromText( $params['title'] );
60 if ( !$titleObj ||
$titleObj->isExternal() ) {
61 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
64 // Some functions depend on $wgTitle == $ep->mTitle
68 if ( $params['createonly'] && $titleObj->exists() ) {
69 $this->dieUsageMsg( array( 'createonly-exists' ) );
71 if ( $params['nocreate'] && !$titleObj->exists() ) {
72 $this->dieUsageMsg( array( 'nocreate-missing' ) );
75 // Now let's check whether we're even allowed to do this
76 $errors = $titleObj->getUserPermissionsErrors( 'edit', $wgUser );
77 if ( !$titleObj->exists() ) {
78 $errors = array_merge( $errors, $titleObj->getUserPermissionsErrors( 'create', $wgUser ) );
80 if ( count( $errors ) ) {
81 $this->dieUsageMsg( $errors[0] );
84 $articleObj = new Article( $titleObj );
85 $toMD5 = $params['text'];
86 if ( !is_null( $params['appendtext'] ) ||
!is_null( $params['prependtext'] ) )
88 // For non-existent pages, Article::getContent()
89 // returns an interface message rather than ''
90 // We do want getContent()'s behavior for non-existent
91 // MediaWiki: pages, though
92 if ( $articleObj->getID() == 0 && $titleObj->getNamespace() != NS_MEDIAWIKI
) {
95 $content = $articleObj->getContent();
98 if ( !is_null( $params['section'] ) ) {
99 // Process the content for section edits
101 $section = intval( $params['section'] );
102 $content = $wgParser->getSection( $content, $section, false );
103 if ( $content === false ) {
104 $this->dieUsage( "There is no section {$section}.", 'nosuchsection' );
107 $params['text'] = $params['prependtext'] . $content . $params['appendtext'];
108 $toMD5 = $params['prependtext'] . $params['appendtext'];
111 if ( $params['undo'] > 0 ) {
112 if ( $params['undoafter'] > 0 ) {
113 if ( $params['undo'] < $params['undoafter'] ) {
114 list( $params['undo'], $params['undoafter'] ) =
115 array( $params['undoafter'], $params['undo'] );
117 $undoafterRev = Revision
::newFromID( $params['undoafter'] );
119 $undoRev = Revision
::newFromID( $params['undo'] );
120 if ( is_null( $undoRev ) ||
$undoRev->isDeleted( Revision
::DELETED_TEXT
) )
122 $this->dieUsageMsg( array( 'nosuchrevid', $params['undo'] ) );
125 if ( $params['undoafter'] == 0 ) {
126 $undoafterRev = $undoRev->getPrevious();
128 if ( is_null( $undoafterRev ) ||
$undoafterRev->isDeleted( Revision
::DELETED_TEXT
) )
130 $this->dieUsageMsg( array( 'nosuchrevid', $params['undoafter'] ) );
133 if ( $undoRev->getPage() != $articleObj->getID() ) {
134 $this->dieUsageMsg( array( 'revwrongpage', $undoRev->getID(), $titleObj->getPrefixedText() ) );
136 if ( $undoafterRev->getPage() != $articleObj->getID() ) {
137 $this->dieUsageMsg( array( 'revwrongpage', $undoafterRev->getID(), $titleObj->getPrefixedText() ) );
140 $newtext = $articleObj->getUndoText( $undoRev, $undoafterRev );
141 if ( $newtext === false ) {
142 $this->dieUsageMsg( array( 'undo-failure' ) );
144 $params['text'] = $newtext;
145 // If no summary was given and we only undid one rev,
146 // use an autosummary
147 if ( is_null( $params['summary'] ) && $titleObj->getNextRevisionID( $undoafterRev->getID() ) == $params['undo'] )
149 $params['summary'] = wfMsgForContent( 'undo-summary', $params['undo'], $undoRev->getUserText() );
153 // See if the MD5 hash checks out
154 if ( !is_null( $params['md5'] ) && md5( $toMD5 ) !== $params['md5'] ) {
155 $this->dieUsageMsg( array( 'hashcheckfailed' ) );
158 $ep = new EditPage( $articleObj );
159 // EditPage wants to parse its stuff from a WebRequest
160 // That interface kind of sucks, but it's workable
162 'wpTextbox1' => $params['text'],
163 'wpEditToken' => $params['token'],
164 'wpIgnoreBlankSummary' => ''
167 if ( !is_null( $params['summary'] ) ) {
168 $reqArr['wpSummary'] = $params['summary'];
171 // Watch out for basetimestamp == ''
172 // wfTimestamp() treats it as NOW, almost certainly causing an edit conflict
173 if ( !is_null( $params['basetimestamp'] ) && $params['basetimestamp'] != '' )
175 $reqArr['wpEdittime'] = wfTimestamp( TS_MW
, $params['basetimestamp'] );
177 $reqArr['wpEdittime'] = $articleObj->getTimestamp();
180 if ( !is_null( $params['starttimestamp'] ) && $params['starttimestamp'] != '' ) {
181 $reqArr['wpStarttime'] = wfTimestamp( TS_MW
, $params['starttimestamp'] );
183 $reqArr['wpStarttime'] = $reqArr['wpEdittime']; // Fake wpStartime
186 if ( $params['minor'] ||
( !$params['notminor'] && $wgUser->getOption( 'minordefault' ) ) ) {
187 $reqArr['wpMinoredit'] = '';
190 if ( $params['recreate'] ) {
191 $reqArr['wpRecreate'] = '';
194 if ( !is_null( $params['section'] ) ) {
195 $section = intval( $params['section'] );
196 if ( $section == 0 && $params['section'] != '0' && $params['section'] != 'new' )
198 $this->dieUsage( "The section parameter must be set to an integer or 'new'", "invalidsection" );
200 $reqArr['wpSection'] = $params['section'];
202 $reqArr['wpSection'] = '';
205 $watch = $this->getWatchlistValue( $params['watchlist'], $titleObj ) ||
$wgUser->getOption( 'watchcreations' );
207 // Deprecated parameters
208 if ( $params['watch'] ) {
210 } elseif ( $params['unwatch'] ) {
214 if ( $watch ||
$titleObj->userIsWatching() ) {
215 $reqArr['wpWatchthis'] = '';
218 $req = new FauxRequest( $reqArr, true );
219 $ep->importFormData( $req );
222 // Handle CAPTCHA parameters
224 if ( !is_null( $params['captchaid'] ) ) {
225 $wgRequest->setVal( 'wpCaptchaId', $params['captchaid'] );
227 if ( !is_null( $params['captchaword'] ) ) {
228 $wgRequest->setVal( 'wpCaptchaWord', $params['captchaword'] );
232 if ( !wfRunHooks( 'APIEditBeforeSave', array( $ep, $ep->textbox1
, &$r ) ) )
235 $r['result'] = 'Failure';
236 $this->getResult()->addValue( null, $this->getModuleName(), $r );
239 $this->dieUsageMsg( array( 'hookaborted' ) );
243 // Do the actual save
244 $oldRevId = $articleObj->getRevIdFetched();
246 // Fake $wgRequest for some hooks inside EditPage
247 // FIXME: This interface SUCKS
248 $oldRequest = $wgRequest;
251 $retval = $ep->internalAttemptSave( $result, $wgUser->isAllowed( 'bot' ) && $params['bot'] );
252 $wgRequest = $oldRequest;
254 case EditPage
::AS_HOOK_ERROR
:
255 case EditPage
::AS_HOOK_ERROR_EXPECTED
:
256 $this->dieUsageMsg( array( 'hookaborted' ) );
258 case EditPage
::AS_IMAGE_REDIRECT_ANON
:
259 $this->dieUsageMsg( array( 'noimageredirect-anon' ) );
261 case EditPage
::AS_IMAGE_REDIRECT_LOGGED
:
262 $this->dieUsageMsg( array( 'noimageredirect-logged' ) );
264 case EditPage
::AS_SPAM_ERROR
:
265 $this->dieUsageMsg( array( 'spamdetected', $result['spam'] ) );
267 case EditPage
::AS_FILTERING
:
268 $this->dieUsageMsg( array( 'filtered' ) );
270 case EditPage
::AS_BLOCKED_PAGE_FOR_USER
:
271 $this->dieUsageMsg( array( 'blockedtext' ) );
273 case EditPage
::AS_MAX_ARTICLE_SIZE_EXCEEDED
:
274 case EditPage
::AS_CONTENT_TOO_BIG
:
275 global $wgMaxArticleSize;
276 $this->dieUsageMsg( array( 'contenttoobig', $wgMaxArticleSize ) );
278 case EditPage
::AS_READ_ONLY_PAGE_ANON
:
279 $this->dieUsageMsg( array( 'noedit-anon' ) );
281 case EditPage
::AS_READ_ONLY_PAGE_LOGGED
:
282 $this->dieUsageMsg( array( 'noedit' ) );
284 case EditPage
::AS_READ_ONLY_PAGE
:
285 $this->dieReadOnly();
287 case EditPage
::AS_RATE_LIMITED
:
288 $this->dieUsageMsg( array( 'actionthrottledtext' ) );
290 case EditPage
::AS_ARTICLE_WAS_DELETED
:
291 $this->dieUsageMsg( array( 'wasdeleted' ) );
293 case EditPage
::AS_NO_CREATE_PERMISSION
:
294 $this->dieUsageMsg( array( 'nocreate-loggedin' ) );
296 case EditPage
::AS_BLANK_ARTICLE
:
297 $this->dieUsageMsg( array( 'blankpage' ) );
299 case EditPage
::AS_CONFLICT_DETECTED
:
300 $this->dieUsageMsg( array( 'editconflict' ) );
302 // case EditPage::AS_SUMMARY_NEEDED: Can't happen since we set wpIgnoreBlankSummary
303 case EditPage
::AS_TEXTBOX_EMPTY
:
304 $this->dieUsageMsg( array( 'emptynewsection' ) );
306 case EditPage
::AS_SUCCESS_NEW_ARTICLE
:
308 case EditPage
::AS_SUCCESS_UPDATE
:
309 $r['result'] = 'Success';
310 $r['pageid'] = intval( $titleObj->getArticleID() );
311 $r['title'] = $titleObj->getPrefixedText();
312 // HACK: We create a new Article object here because getRevIdFetched()
313 // refuses to be run twice, and because Title::getLatestRevId()
314 // won't fetch from the master unless we select for update, which we
316 $newArticle = new Article( $titleObj );
317 $newRevId = $newArticle->getRevIdFetched();
318 if ( $newRevId == $oldRevId ) {
321 $r['oldrevid'] = intval( $oldRevId );
322 $r['newrevid'] = intval( $newRevId );
323 $r['newtimestamp'] = wfTimestamp( TS_ISO_8601
,
324 $newArticle->getTimestamp() );
328 case EditPage
::AS_END
:
329 // This usually means some kind of race condition
330 // or DB weirdness occurred. Fall through to throw an unknown
333 // This needs fixing higher up, as Article::doEdit should be
334 // used rather than Article::updateArticle, so that specific
335 // error conditions can be returned
337 $this->dieUsageMsg( array( 'unknownerror', $retval ) );
339 $this->getResult()->addValue( null, $this->getModuleName(), $r );
342 public function mustBePosted() {
346 public function isWriteMode() {
350 protected function getDescription() {
351 return 'Create and edit pages.';
354 public function getPossibleErrors() {
355 global $wgMaxArticleSize;
357 return array_merge( parent
::getPossibleErrors(), array(
358 array( 'missingparam', 'title' ),
359 array( 'missingtext' ),
360 array( 'invalidtitle', 'title' ),
361 array( 'createonly-exists' ),
362 array( 'nocreate-missing' ),
363 array( 'nosuchrevid', 'undo' ),
364 array( 'nosuchrevid', 'undoafter' ),
365 array( 'revwrongpage', 'id', 'text' ),
366 array( 'undo-failure' ),
367 array( 'hashcheckfailed' ),
368 array( 'hookaborted' ),
369 array( 'noimageredirect-anon' ),
370 array( 'noimageredirect-logged' ),
371 array( 'spamdetected', 'spam' ),
373 array( 'blockedtext' ),
374 array( 'contenttoobig', $wgMaxArticleSize ),
375 array( 'noedit-anon' ),
377 array( 'actionthrottledtext' ),
378 array( 'wasdeleted' ),
379 array( 'nocreate-loggedin' ),
380 array( 'blankpage' ),
381 array( 'editconflict' ),
382 array( 'emptynewsection' ),
383 array( 'unknownerror', 'retval' ),
384 array( 'code' => 'nosuchsection', 'info' => 'There is no section section.' ),
385 array( 'code' => 'invalidsection', 'info' => 'The section parameter must be set to an integer or \'new\'' ),
389 protected function getAllowedParams() {
399 'basetimestamp' => null,
400 'starttimestamp' => null,
402 'createonly' => false,
404 'captchaword' => null,
407 ApiBase
::PARAM_DFLT
=> false,
408 ApiBase
::PARAM_DEPRECATED
=> true,
411 ApiBase
::PARAM_DFLT
=> false,
412 ApiBase
::PARAM_DEPRECATED
=> true,
414 'watchlist' => array(
415 ApiBase
::PARAM_DFLT
=> 'preferences',
416 ApiBase
::PARAM_TYPE
=> array(
424 'prependtext' => null,
425 'appendtext' => null,
427 ApiBase
::PARAM_TYPE
=> 'integer'
429 'undoafter' => array(
430 ApiBase
::PARAM_TYPE
=> 'integer'
435 protected function getParamDescription() {
437 'title' => 'Page title',
438 'section' => 'Section number. 0 for the top section, \'new\' for a new section',
439 'text' => 'Page content',
440 'token' => 'Edit token. You can get one of these through prop=info',
441 'summary' => 'Edit summary. Also section title when section=new',
442 'minor' => 'Minor edit',
443 'notminor' => 'Non-minor edit',
444 'bot' => 'Mark this edit as bot',
445 'basetimestamp' => array( 'Timestamp of the base revision (gotten through prop=revisions&rvprop=timestamp).',
446 'Used to detect edit conflicts; leave unset to ignore conflicts.'
448 'starttimestamp' => array( 'Timestamp when you obtained the edit token.',
449 'Used to detect edit conflicts; leave unset to ignore conflicts.'
451 'recreate' => 'Override any errors about the article having been deleted in the meantime',
452 'createonly' => 'Don\'t edit the page if it exists already',
453 'nocreate' => 'Throw an error if the page doesn\'t exist',
454 'watch' => 'Add the page to your watchlist',
455 'unwatch' => 'Remove the page from your watchlist',
456 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
457 'captchaid' => 'CAPTCHA ID from previous request',
458 'captchaword' => 'Answer to the CAPTCHA',
459 'md5' => array( 'The MD5 hash of the text parameter, or the prependtext and appendtext parameters concatenated.',
460 'If set, the edit won\'t be done unless the hash is correct' ),
461 'prependtext' => 'Add this text to the beginning of the page. Overrides text.',
462 'appendtext' => 'Add this text to the end of the page. Overrides text',
463 'undo' => 'Undo this revision. Overrides text, prependtext and appendtext',
464 'undoafter' => 'Undo all revisions from undo to this one. If not set, just undo one revision',
468 public function getTokenSalt() {
472 protected function getExamples() {
474 'Edit a page (anonymous user):',
475 ' api.php?action=edit&title=Test&summary=test%20summary&text=article%20content&basetimestamp=20070824123454&token=%2B\\',
476 'Prepend __NOTOC__ to a page (anonymous user):',
477 ' api.php?action=edit&title=Test&summary=NOTOC&minor&prependtext=__NOTOC__%0A&basetimestamp=20070824123454&token=%2B\\',
478 'Undo r13579 through r13585 with autosummary (anonymous user):',
479 ' api.php?action=edit&title=Test&undo=13585&undoafter=13579&basetimestamp=20070824123454&token=%2B\\',
483 public function getVersion() {
484 return __CLASS__
. ': $Id$';