00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if (!defined('MEDIAWIKI')) {
00027
00028 require_once ("ApiBase.php");
00029 }
00030
00038 class ApiEditPage extends ApiBase {
00039
00040 public function __construct($query, $moduleName) {
00041 parent :: __construct($query, $moduleName);
00042 }
00043
00044 public function execute() {
00045 global $wgUser;
00046 $params = $this->extractRequestParams();
00047 if(is_null($params['title']))
00048 $this->dieUsageMsg(array('missingparam', 'title'));
00049 if(is_null($params['text']) && is_null($params['appendtext']) &&
00050 is_null($params['prependtext']) &&
00051 $params['undo'] == 0)
00052 $this->dieUsageMsg(array('missingtext'));
00053 if(is_null($params['token']))
00054 $this->dieUsageMsg(array('missingparam', 'token'));
00055 if(!$wgUser->matchEditToken($params['token']))
00056 $this->dieUsageMsg(array('sessionfailure'));
00057
00058 $titleObj = Title::newFromText($params['title']);
00059 if(!$titleObj)
00060 $this->dieUsageMsg(array('invalidtitle', $params['title']));
00061
00062 global $wgTitle;
00063 $wgTitle = $titleObj;
00064
00065 if($params['createonly'] && $titleObj->exists())
00066 $this->dieUsageMsg(array('createonly-exists'));
00067 if($params['nocreate'] && !$titleObj->exists())
00068 $this->dieUsageMsg(array('nocreate-missing'));
00069
00070
00071 $errors = $titleObj->getUserPermissionsErrors('edit', $wgUser);
00072 if(!$titleObj->exists())
00073 $errors = array_merge($errors, $titleObj->getUserPermissionsErrors('create', $wgUser));
00074 if(count($errors))
00075 $this->dieUsageMsg($errors[0]);
00076
00077 $articleObj = new Article($titleObj);
00078 $toMD5 = $params['text'];
00079 if(!is_null($params['appendtext']) || !is_null($params['prependtext']))
00080 {
00081
00082
00083
00084
00085 if($articleObj->getID() == 0 && $titleObj->getNamespace() != NS_MEDIAWIKI)
00086 $content = '';
00087 else
00088 $content = $articleObj->getContent();
00089 $params['text'] = $params['prependtext'] . $content . $params['appendtext'];
00090 $toMD5 = $params['prependtext'] . $params['appendtext'];
00091 }
00092
00093 if($params['undo'] > 0)
00094 {
00095 if($params['undoafter'] > 0)
00096 {
00097 if($params['undo'] < $params['undoafter'])
00098 list($params['undo'], $params['undoafter']) =
00099 array($params['undoafter'], $params['undo']);
00100 $undoafterRev = Revision::newFromID($params['undoafter']);
00101 }
00102 $undoRev = Revision::newFromID($params['undo']);
00103 if(is_null($undoRev) || $undoRev->isDeleted(Revision::DELETED_TEXT))
00104 $this->dieUsageMsg(array('nosuchrevid', $params['undo']));
00105 if($params['undoafter'] == 0)
00106 $undoafterRev = $undoRev->getPrevious();
00107 if(is_null($undoafterRev) || $undoafterRev->isDeleted(Revision::DELETED_TEXT))
00108 $this->dieUsageMsg(array('nosuchrevid', $params['undoafter']));
00109 if($undoRev->getPage() != $articleObj->getID())
00110 $this->dieUsageMsg(array('revwrongpage', $undoRev->getID(), $titleObj->getPrefixedText()));
00111 if($undoafterRev->getPage() != $articleObj->getID())
00112 $this->dieUsageMsg(array('revwrongpage', $undoafterRev->getID(), $titleObj->getPrefixedText()));
00113 $newtext = $articleObj->getUndoText($undoRev, $undoafterRev);
00114 if($newtext === false)
00115 $this->dieUsageMsg(array('undo-failure'));
00116 $params['text'] = $newtext;
00117
00118
00119 if(is_null($params['summary']) && $titleObj->getNextRevisionID($undoafterRev->getID()) == $params['undo'])
00120 $params['summary'] = wfMsgForContent('undo-summary', $params['undo'], $undoRev->getUserText());
00121 }
00122
00123 # See if the MD5 hash checks out
00124 if(!is_null($params['md5']))
00125 if(md5($toMD5) !== $params['md5'])
00126 $this->dieUsageMsg(array('hashcheckfailed'));
00127
00128 $ep = new EditPage($articleObj);
00129
00130
00131 $reqArr = array('wpTextbox1' => $params['text'],
00132 'wpEdittoken' => $params['token'],
00133 'wpIgnoreBlankSummary' => ''
00134 );
00135 if(!is_null($params['summary']))
00136 $reqArr['wpSummary'] = $params['summary'];
00137 # Watch out for basetimestamp == ''
00138 # wfTimestamp() treats it as NOW, almost certainly causing an edit conflict
00139 if(!is_null($params['basetimestamp']) && $params['basetimestamp'] != '')
00140 $reqArr['wpEdittime'] = wfTimestamp(TS_MW, $params['basetimestamp']);
00141 else
00142 $reqArr['wpEdittime'] = $articleObj->getTimestamp();
00143 if(!is_null($params['starttimestamp']) && $params['starttimestamp'] != '')
00144 $reqArr['wpStarttime'] = wfTimestamp(TS_MW, $params['starttimestamp']);
00145 else
00146 # Fake wpStartime
00147 $reqArr['wpStarttime'] = $reqArr['wpEdittime'];
00148 if($params['minor'] || (!$params['notminor'] && $wgUser->getOption('minordefault')))
00149 $reqArr['wpMinoredit'] = '';
00150 if($params['recreate'])
00151 $reqArr['wpRecreate'] = '';
00152 if(!is_null($params['section']))
00153 {
00154 $section = intval($params['section']);
00155 if($section == 0 && $params['section'] != '0' && $params['section'] != 'new')
00156 $this->dieUsage("The section parameter must be set to an integer or 'new'", "invalidsection");
00157 $reqArr['wpSection'] = $params['section'];
00158 }
00159 else
00160 $reqArr['wpSection'] = '';
00161
00162 if($params['watch'])
00163 $watch = true;
00164 else if($params['unwatch'])
00165 $watch = false;
00166 else if($titleObj->userIsWatching())
00167 $watch = true;
00168 else if($wgUser->getOption('watchdefault'))
00169 $watch = true;
00170 else if($wgUser->getOption('watchcreations') && !$titleObj->exists())
00171 $watch = true;
00172 else
00173 $watch = false;
00174 if($watch)
00175 $reqArr['wpWatchthis'] = '';
00176
00177 $req = new FauxRequest($reqArr, true);
00178 $ep->importFormData($req);
00179
00180 # Run hooks
00181 # Handle CAPTCHA parameters
00182 global $wgRequest;
00183 if(!is_null($params['captchaid']))
00184 $wgRequest->setVal( 'wpCaptchaId', $params['captchaid'] );
00185 if(!is_null($params['captchaword']))
00186 $wgRequest->setVal( 'wpCaptchaWord', $params['captchaword'] );
00187 $r = array();
00188 if(!wfRunHooks('APIEditBeforeSave', array(&$ep, $ep->textbox1, &$r)))
00189 {
00190 if(count($r))
00191 {
00192 $r['result'] = "Failure";
00193 $this->getResult()->addValue(null, $this->getModuleName(), $r);
00194 return;
00195 }
00196 else
00197 $this->dieUsageMsg(array('hookaborted'));
00198 }
00199
00200 # Do the actual save
00201 $oldRevId = $articleObj->getRevIdFetched();
00202 $result = null;
00203 # Fake $wgRequest for some hooks inside EditPage
00204 # FIXME: This interface SUCKS
00205 $oldRequest = $wgRequest;
00206 $wgRequest = $req;
00207
00208 $retval = $ep->internalAttemptSave($result, $wgUser->isAllowed('bot') && $params['bot']);
00209 $wgRequest = $oldRequest;
00210 switch($retval)
00211 {
00212 case EditPage::AS_HOOK_ERROR:
00213 case EditPage::AS_HOOK_ERROR_EXPECTED:
00214 $this->dieUsageMsg(array('hookaborted'));
00215 case EditPage::AS_IMAGE_REDIRECT_ANON:
00216 $this->dieUsageMsg(array('noimageredirect-anon'));
00217 case EditPage::AS_IMAGE_REDIRECT_LOGGED:
00218 $this->dieUsageMsg(array('noimageredirect-logged'));
00219 case EditPage::AS_SPAM_ERROR:
00220 $this->dieUsageMsg(array('spamdetected', $result['spam']));
00221 case EditPage::AS_FILTERING:
00222 $this->dieUsageMsg(array('filtered'));
00223 case EditPage::AS_BLOCKED_PAGE_FOR_USER:
00224 $this->dieUsageMsg(array('blockedtext'));
00225 case EditPage::AS_MAX_ARTICLE_SIZE_EXCEEDED:
00226 case EditPage::AS_CONTENT_TOO_BIG:
00227 global $wgMaxArticleSize;
00228 $this->dieUsageMsg(array('contenttoobig', $wgMaxArticleSize));
00229 case EditPage::AS_READ_ONLY_PAGE_ANON:
00230 $this->dieUsageMsg(array('noedit-anon'));
00231 case EditPage::AS_READ_ONLY_PAGE_LOGGED:
00232 $this->dieUsageMsg(array('noedit'));
00233 case EditPage::AS_READ_ONLY_PAGE:
00234 $this->dieUsageMsg(array('readonlytext'));
00235 case EditPage::AS_RATE_LIMITED:
00236 $this->dieUsageMsg(array('actionthrottledtext'));
00237 case EditPage::AS_ARTICLE_WAS_DELETED:
00238 $this->dieUsageMsg(array('wasdeleted'));
00239 case EditPage::AS_NO_CREATE_PERMISSION:
00240 $this->dieUsageMsg(array('nocreate-loggedin'));
00241 case EditPage::AS_BLANK_ARTICLE:
00242 $this->dieUsageMsg(array('blankpage'));
00243 case EditPage::AS_CONFLICT_DETECTED:
00244 $this->dieUsageMsg(array('editconflict'));
00245 #case EditPage::AS_SUMMARY_NEEDED: Can't happen since we set wpIgnoreBlankSummary
00246 case EditPage::AS_TEXTBOX_EMPTY:
00247 $this->dieUsageMsg(array('emptynewsection'));
00248 case EditPage::AS_END:
00249 # This usually means some kind of race condition
00250 # or DB weirdness occurred. Throw an unknown error here.
00251 $this->dieUsageMsg(array('unknownerror'));
00252 case EditPage::AS_SUCCESS_NEW_ARTICLE:
00253 $r['new'] = '';
00254 case EditPage::AS_SUCCESS_UPDATE:
00255 $r['result'] = "Success";
00256 $r['pageid'] = intval($titleObj->getArticleID());
00257 $r['title'] = $titleObj->getPrefixedText();
00258 # HACK: We create a new Article object here because getRevIdFetched()
00259 # refuses to be run twice, and because Title::getLatestRevId()
00260 # won't fetch from the master unless we select for update, which we
00261 # don't want to do.
00262 $newArticle = new Article($titleObj);
00263 $newRevId = $newArticle->getRevIdFetched();
00264 if($newRevId == $oldRevId)
00265 $r['nochange'] = '';
00266 else
00267 {
00268 $r['oldrevid'] = intval($oldRevId);
00269 $r['newrevid'] = intval($newRevId);
00270 }
00271 break;
00272 default:
00273 $this->dieUsageMsg(array('unknownerror', $retval));
00274 }
00275 $this->getResult()->addValue(null, $this->getModuleName(), $r);
00276 }
00277
00278 public function mustBePosted() {
00279 return true;
00280 }
00281
00282 public function isWriteMode() {
00283 return true;
00284 }
00285
00286 protected function getDescription() {
00287 return 'Create and edit pages.';
00288 }
00289
00290 protected function getAllowedParams() {
00291 return array (
00292 'title' => null,
00293 'section' => null,
00294 'text' => null,
00295 'token' => null,
00296 'summary' => null,
00297 'minor' => false,
00298 'notminor' => false,
00299 'bot' => false,
00300 'basetimestamp' => null,
00301 'starttimestamp' => null,
00302 'recreate' => false,
00303 'createonly' => false,
00304 'nocreate' => false,
00305 'captchaword' => null,
00306 'captchaid' => null,
00307 'watch' => false,
00308 'unwatch' => false,
00309 'md5' => null,
00310 'prependtext' => null,
00311 'appendtext' => null,
00312 'undo' => array(
00313 ApiBase :: PARAM_TYPE => 'integer'
00314 ),
00315 'undoafter' => array(
00316 ApiBase :: PARAM_TYPE => 'integer'
00317 ),
00318 );
00319 }
00320
00321 protected function getParamDescription() {
00322 return array (
00323 'title' => 'Page title',
00324 'section' => 'Section number. 0 for the top section, \'new\' for a new section',
00325 'text' => 'Page content',
00326 'token' => 'Edit token. You can get one of these through prop=info',
00327 'summary' => 'Edit summary. Also section title when section=new',
00328 'minor' => 'Minor edit',
00329 'notminor' => 'Non-minor edit',
00330 'bot' => 'Mark this edit as bot',
00331 'basetimestamp' => array('Timestamp of the base revision (gotten through prop=revisions&rvprop=timestamp).',
00332 'Used to detect edit conflicts; leave unset to ignore conflicts.'
00333 ),
00334 'starttimestamp' => array('Timestamp when you obtained the edit token.',
00335 'Used to detect edit conflicts; leave unset to ignore conflicts.'
00336 ),
00337 'recreate' => 'Override any errors about the article having been deleted in the meantime',
00338 'createonly' => 'Don\'t edit the page if it exists already',
00339 'nocreate' => 'Throw an error if the page doesn\'t exist',
00340 'watch' => 'Add the page to your watchlist',
00341 'unwatch' => 'Remove the page from your watchlist',
00342 'captchaid' => 'CAPTCHA ID from previous request',
00343 'captchaword' => 'Answer to the CAPTCHA',
00344 'md5' => array( 'The MD5 hash of the text parameter, or the prependtext and appendtext parameters concatenated.',
00345 'If set, the edit won\'t be done unless the hash is correct'),
00346 'prependtext' => array( 'Add this text to the beginning of the page. Overrides text.',
00347 'Don\'t use together with section: that won\'t do what you expect.'),
00348 'appendtext' => 'Add this text to the end of the page. Overrides text',
00349 'undo' => 'Undo this revision. Overrides text, prependtext and appendtext',
00350 'undoafter' => 'Undo all revisions from undo to this one. If not set, just undo one revision',
00351 );
00352 }
00353
00354 protected function getExamples() {
00355 return array (
00356 "Edit a page (anonymous user):",
00357 " api.php?action=edit&title=Test&summary=test%20summary&text=article%20content&basetimestamp=20070824123454&token=%2B\\",
00358 "Prepend __NOTOC__ to a page (anonymous user):",
00359 " api.php?action=edit&title=Test&summary=NOTOC&minor&prependtext=__NOTOC__%0A&basetimestamp=20070824123454&token=%2B\\",
00360 "Undo r13579 through r13585 with autosummary(anonymous user):",
00361 " api.php?action=edit&title=Test&undo=13585&undoafter=13579&basetimestamp=20070824123454&token=%2B\\",
00362 );
00363 }
00364
00365 public function getVersion() {
00366 return __CLASS__ . ': $Id: ApiEditPage.php 50220 2009-05-05 14:07:59Z tstarling $';
00367 }
00368 }