4 * Created on August 16, 2007
6 * API for MediaWiki 1.8+
8 * Copyright (C) 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' ) );
51 if ( is_null( $params['text'] ) && is_null( $params['appendtext'] ) &&
52 is_null( $params['prependtext'] ) &&
53 $params['undo'] == 0 )
54 $this->dieUsageMsg( array( 'missingtext' ) );
56 if ( is_null( $params['token'] ) )
57 $this->dieUsageMsg( array( 'missingparam', 'token' ) );
59 if ( !$wgUser->matchEditToken( $params['token'] ) )
60 $this->dieUsageMsg( array( 'sessionfailure' ) );
62 $titleObj = Title
::newFromText( $params['title'] );
63 if ( !$titleObj ||
$titleObj->isExternal() )
64 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
66 // Some functions depend on $wgTitle == $ep->mTitle
70 if ( $params['createonly'] && $titleObj->exists() )
71 $this->dieUsageMsg( array( 'createonly-exists' ) );
72 if ( $params['nocreate'] && !$titleObj->exists() )
73 $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 ) );
79 if ( count( $errors ) )
80 $this->dieUsageMsg( $errors[0] );
82 $articleObj = new Article( $titleObj );
83 $toMD5 = $params['text'];
84 if ( !is_null( $params['appendtext'] ) ||
!is_null( $params['prependtext'] ) )
86 // For non-existent pages, Article::getContent()
87 // returns an interface message rather than ''
88 // We do want getContent()'s behavior for non-existent
89 // MediaWiki: pages, though
90 if ( $articleObj->getID() == 0 && $titleObj->getNamespace() != NS_MEDIAWIKI
)
93 $content = $articleObj->getContent();
95 if ( !is_null( $params['section'] ) )
97 // Process the content for section edits
99 $section = intval( $params['section'] );
100 $content = $wgParser->getSection( $content, $section, false );
101 if ( $content === false )
102 $this->dieUsage( "There is no section {$section}.", 'nosuchsection' );
104 $params['text'] = $params['prependtext'] . $content . $params['appendtext'];
105 $toMD5 = $params['prependtext'] . $params['appendtext'];
108 if ( $params['undo'] > 0 )
110 if ( $params['undoafter'] > 0 )
112 if ( $params['undo'] < $params['undoafter'] )
113 list( $params['undo'], $params['undoafter'] ) =
114 array( $params['undoafter'], $params['undo'] );
115 $undoafterRev = Revision
::newFromID( $params['undoafter'] );
117 $undoRev = Revision
::newFromID( $params['undo'] );
118 if ( is_null( $undoRev ) ||
$undoRev->isDeleted( Revision
::DELETED_TEXT
) )
119 $this->dieUsageMsg( array( 'nosuchrevid', $params['undo'] ) );
121 if ( $params['undoafter'] == 0 )
122 $undoafterRev = $undoRev->getPrevious();
123 if ( is_null( $undoafterRev ) ||
$undoafterRev->isDeleted( Revision
::DELETED_TEXT
) )
124 $this->dieUsageMsg( array( 'nosuchrevid', $params['undoafter'] ) );
126 if ( $undoRev->getPage() != $articleObj->getID() )
127 $this->dieUsageMsg( array( 'revwrongpage', $undoRev->getID(), $titleObj->getPrefixedText() ) );
128 if ( $undoafterRev->getPage() != $articleObj->getID() )
129 $this->dieUsageMsg( array( 'revwrongpage', $undoafterRev->getID(), $titleObj->getPrefixedText() ) );
131 $newtext = $articleObj->getUndoText( $undoRev, $undoafterRev );
132 if ( $newtext === false )
133 $this->dieUsageMsg( array( 'undo-failure' ) );
134 $params['text'] = $newtext;
135 // If no summary was given and we only undid one rev,
136 // use an autosummary
137 if ( is_null( $params['summary'] ) && $titleObj->getNextRevisionID( $undoafterRev->getID() ) == $params['undo'] )
138 $params['summary'] = wfMsgForContent( 'undo-summary', $params['undo'], $undoRev->getUserText() );
141 // See if the MD5 hash checks out
142 if ( !is_null( $params['md5'] ) && md5( $toMD5 ) !== $params['md5'] )
143 $this->dieUsageMsg( array( 'hashcheckfailed' ) );
145 $ep = new EditPage( $articleObj );
146 // EditPage wants to parse its stuff from a WebRequest
147 // That interface kind of sucks, but it's workable
148 $reqArr = array( 'wpTextbox1' => $params['text'],
149 'wpEditToken' => $params['token'],
150 'wpIgnoreBlankSummary' => ''
153 if ( !is_null( $params['summary'] ) )
154 $reqArr['wpSummary'] = $params['summary'];
156 // Watch out for basetimestamp == ''
157 // wfTimestamp() treats it as NOW, almost certainly causing an edit conflict
158 if ( !is_null( $params['basetimestamp'] ) && $params['basetimestamp'] != '' )
159 $reqArr['wpEdittime'] = wfTimestamp( TS_MW
, $params['basetimestamp'] );
161 $reqArr['wpEdittime'] = $articleObj->getTimestamp();
163 if ( !is_null( $params['starttimestamp'] ) && $params['starttimestamp'] != '' )
164 $reqArr['wpStarttime'] = wfTimestamp( TS_MW
, $params['starttimestamp'] );
166 $reqArr['wpStarttime'] = $reqArr['wpEdittime']; // Fake wpStartime
168 if ( $params['minor'] ||
( !$params['notminor'] && $wgUser->getOption( 'minordefault' ) ) )
169 $reqArr['wpMinoredit'] = '';
171 if ( $params['recreate'] )
172 $reqArr['wpRecreate'] = '';
174 if ( !is_null( $params['section'] ) )
176 $section = intval( $params['section'] );
177 if ( $section == 0 && $params['section'] != '0' && $params['section'] != 'new' )
178 $this->dieUsage( "The section parameter must be set to an integer or 'new'", "invalidsection" );
179 $reqArr['wpSection'] = $params['section'];
182 $reqArr['wpSection'] = '';
184 // Handle watchlist settings
185 switch ( $params['watchlist'] )
194 if ( $titleObj->exists() )
195 $watch = $wgUser->getOption( 'watchdefault' ) ||
$titleObj->userIsWatching();
197 $watch = $wgUser->getOption( 'watchcreations' );
201 $watch = $titleObj->userIsWatching();
203 // Deprecated parameters
204 if ( $params['watch'] )
206 elseif ( $params['unwatch'] )
210 $reqArr['wpWatchthis'] = '';
212 $req = new FauxRequest( $reqArr, true );
213 $ep->importFormData( $req );
216 // Handle CAPTCHA parameters
218 if ( !is_null( $params['captchaid'] ) )
219 $wgRequest->setVal( 'wpCaptchaId', $params['captchaid'] );
220 if ( !is_null( $params['captchaword'] ) )
221 $wgRequest->setVal( 'wpCaptchaWord', $params['captchaword'] );
224 if ( !wfRunHooks( 'APIEditBeforeSave', array( $ep, $ep->textbox1
, &$r ) ) )
228 $r['result'] = "Failure";
229 $this->getResult()->addValue( null, $this->getModuleName(), $r );
233 $this->dieUsageMsg( array( 'hookaborted' ) );
236 // Do the actual save
237 $oldRevId = $articleObj->getRevIdFetched();
239 // Fake $wgRequest for some hooks inside EditPage
240 // FIXME: This interface SUCKS
241 $oldRequest = $wgRequest;
244 $retval = $ep->internalAttemptSave( $result, $wgUser->isAllowed( 'bot' ) && $params['bot'] );
245 $wgRequest = $oldRequest;
248 case EditPage
::AS_HOOK_ERROR
:
249 case EditPage
::AS_HOOK_ERROR_EXPECTED
:
250 $this->dieUsageMsg( array( 'hookaborted' ) );
252 case EditPage
::AS_IMAGE_REDIRECT_ANON
:
253 $this->dieUsageMsg( array( 'noimageredirect-anon' ) );
255 case EditPage
::AS_IMAGE_REDIRECT_LOGGED
:
256 $this->dieUsageMsg( array( 'noimageredirect-logged' ) );
258 case EditPage
::AS_SPAM_ERROR
:
259 $this->dieUsageMsg( array( 'spamdetected', $result['spam'] ) );
261 case EditPage
::AS_FILTERING
:
262 $this->dieUsageMsg( array( 'filtered' ) );
264 case EditPage
::AS_BLOCKED_PAGE_FOR_USER
:
265 $this->dieUsageMsg( array( 'blockedtext' ) );
267 case EditPage
::AS_MAX_ARTICLE_SIZE_EXCEEDED
:
268 case EditPage
::AS_CONTENT_TOO_BIG
:
269 global $wgMaxArticleSize;
270 $this->dieUsageMsg( array( 'contenttoobig', $wgMaxArticleSize ) );
272 case EditPage
::AS_READ_ONLY_PAGE_ANON
:
273 $this->dieUsageMsg( array( 'noedit-anon' ) );
275 case EditPage
::AS_READ_ONLY_PAGE_LOGGED
:
276 $this->dieUsageMsg( array( 'noedit' ) );
278 case EditPage
::AS_READ_ONLY_PAGE
:
279 $this->dieReadOnly();
281 case EditPage
::AS_RATE_LIMITED
:
282 $this->dieUsageMsg( array( 'actionthrottledtext' ) );
284 case EditPage
::AS_ARTICLE_WAS_DELETED
:
285 $this->dieUsageMsg( array( 'wasdeleted' ) );
287 case EditPage
::AS_NO_CREATE_PERMISSION
:
288 $this->dieUsageMsg( array( 'nocreate-loggedin' ) );
290 case EditPage
::AS_BLANK_ARTICLE
:
291 $this->dieUsageMsg( array( 'blankpage' ) );
293 case EditPage
::AS_CONFLICT_DETECTED
:
294 $this->dieUsageMsg( array( 'editconflict' ) );
296 // case EditPage::AS_SUMMARY_NEEDED: Can't happen since we set wpIgnoreBlankSummary
297 case EditPage
::AS_TEXTBOX_EMPTY
:
298 $this->dieUsageMsg( array( 'emptynewsection' ) );
300 case EditPage
::AS_SUCCESS_NEW_ARTICLE
:
302 case EditPage
::AS_SUCCESS_UPDATE
:
303 $r['result'] = "Success";
304 $r['pageid'] = intval( $titleObj->getArticleID() );
305 $r['title'] = $titleObj->getPrefixedText();
306 // HACK: We create a new Article object here because getRevIdFetched()
307 // refuses to be run twice, and because Title::getLatestRevId()
308 // won't fetch from the master unless we select for update, which we
310 $newArticle = new Article( $titleObj );
311 $newRevId = $newArticle->getRevIdFetched();
312 if ( $newRevId == $oldRevId )
316 $r['oldrevid'] = intval( $oldRevId );
317 $r['newrevid'] = intval( $newRevId );
318 $r['newtimestamp'] = wfTimestamp( TS_ISO_8601
,
319 $newArticle->getTimestamp() );
323 case EditPage
::AS_END
:
324 // This usually means some kind of race condition
325 // or DB weirdness occurred. Fall through to throw an unknown
328 // This needs fixing higher up, as Article::doEdit should be
329 // used rather than Article::updateArticle, so that specific
330 // error conditions can be returned
332 $this->dieUsageMsg( array( 'unknownerror', $retval ) );
334 $this->getResult()->addValue( null, $this->getModuleName(), $r );
337 public function mustBePosted() {
341 public function isWriteMode() {
345 protected function getDescription() {
346 return 'Create and edit pages.';
349 protected function getAllowedParams() {
359 'basetimestamp' => null,
360 'starttimestamp' => null,
362 'createonly' => false,
364 'captchaword' => null,
367 ApiBase
:: PARAM_DFLT
=> false,
368 ApiBase
:: PARAM_DEPRECATED
=> true,
371 ApiBase
:: PARAM_DFLT
=> false,
372 ApiBase
:: PARAM_DEPRECATED
=> true,
374 'watchlist' => array(
375 ApiBase
:: PARAM_DFLT
=> 'preferences',
376 ApiBase
:: PARAM_TYPE
=> array(
384 'prependtext' => null,
385 'appendtext' => null,
387 ApiBase
:: PARAM_TYPE
=> 'integer'
389 'undoafter' => array(
390 ApiBase
:: PARAM_TYPE
=> 'integer'
395 protected function getParamDescription() {
397 'title' => 'Page title',
398 'section' => 'Section number. 0 for the top section, \'new\' for a new section',
399 'text' => 'Page content',
400 'token' => 'Edit token. You can get one of these through prop=info',
401 'summary' => 'Edit summary. Also section title when section=new',
402 'minor' => 'Minor edit',
403 'notminor' => 'Non-minor edit',
404 'bot' => 'Mark this edit as bot',
405 'basetimestamp' => array( 'Timestamp of the base revision (gotten through prop=revisions&rvprop=timestamp).',
406 'Used to detect edit conflicts; leave unset to ignore conflicts.'
408 'starttimestamp' => array( 'Timestamp when you obtained the edit token.',
409 'Used to detect edit conflicts; leave unset to ignore conflicts.'
411 'recreate' => 'Override any errors about the article having been deleted in the meantime',
412 'createonly' => 'Don\'t edit the page if it exists already',
413 'nocreate' => 'Throw an error if the page doesn\'t exist',
414 'watch' => 'Add the page to your watchlist',
415 'unwatch' => 'Remove the page from your watchlist',
416 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
417 'captchaid' => 'CAPTCHA ID from previous request',
418 'captchaword' => 'Answer to the CAPTCHA',
419 'md5' => array( 'The MD5 hash of the text parameter, or the prependtext and appendtext parameters concatenated.',
420 'If set, the edit won\'t be done unless the hash is correct' ),
421 'prependtext' => 'Add this text to the beginning of the page. Overrides text.',
422 'appendtext' => 'Add this text to the end of the page. Overrides text',
423 'undo' => 'Undo this revision. Overrides text, prependtext and appendtext',
424 'undoafter' => 'Undo all revisions from undo to this one. If not set, just undo one revision',
428 protected function getExamples() {
430 "Edit a page (anonymous user):",
431 " api.php?action=edit&title=Test&summary=test%20summary&text=article%20content&basetimestamp=20070824123454&token=%2B\\",
432 "Prepend __NOTOC__ to a page (anonymous user):",
433 " api.php?action=edit&title=Test&summary=NOTOC&minor&prependtext=__NOTOC__%0A&basetimestamp=20070824123454&token=%2B\\",
434 "Undo r13579 through r13585 with autosummary(anonymous user):",
435 " api.php?action=edit&title=Test&undo=13585&undoafter=13579&basetimestamp=20070824123454&token=%2B\\",
439 public function getVersion() {
440 return __CLASS__
. ': $Id$';