MediaWiki
REL1_20
|
00001 <?php 00034 class ApiEditPage extends ApiBase { 00035 00036 public function __construct( $query, $moduleName ) { 00037 parent::__construct( $query, $moduleName ); 00038 } 00039 00040 public function execute() { 00041 $user = $this->getUser(); 00042 $params = $this->extractRequestParams(); 00043 00044 if ( is_null( $params['text'] ) && is_null( $params['appendtext'] ) && 00045 is_null( $params['prependtext'] ) && 00046 $params['undo'] == 0 ) 00047 { 00048 $this->dieUsageMsg( 'missingtext' ); 00049 } 00050 00051 $pageObj = $this->getTitleOrPageId( $params ); 00052 $titleObj = $pageObj->getTitle(); 00053 if ( $titleObj->isExternal() ) { 00054 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) ); 00055 } 00056 00057 $apiResult = $this->getResult(); 00058 00059 if ( $params['redirect'] ) { 00060 if ( $titleObj->isRedirect() ) { 00061 $oldTitle = $titleObj; 00062 00063 $titles = Title::newFromRedirectArray( 00064 Revision::newFromTitle( 00065 $oldTitle, false, Revision::READ_LATEST 00066 )->getText( Revision::FOR_THIS_USER ) 00067 ); 00068 // array_shift( $titles ); 00069 00070 $redirValues = array(); 00071 foreach ( $titles as $id => $newTitle ) { 00072 00073 if ( !isset( $titles[ $id - 1 ] ) ) { 00074 $titles[ $id - 1 ] = $oldTitle; 00075 } 00076 00077 $redirValues[] = array( 00078 'from' => $titles[ $id - 1 ]->getPrefixedText(), 00079 'to' => $newTitle->getPrefixedText() 00080 ); 00081 00082 $titleObj = $newTitle; 00083 } 00084 00085 $apiResult->setIndexedTagName( $redirValues, 'r' ); 00086 $apiResult->addValue( null, 'redirects', $redirValues ); 00087 } 00088 } 00089 00090 if ( $params['createonly'] && $titleObj->exists() ) { 00091 $this->dieUsageMsg( 'createonly-exists' ); 00092 } 00093 if ( $params['nocreate'] && !$titleObj->exists() ) { 00094 $this->dieUsageMsg( 'nocreate-missing' ); 00095 } 00096 00097 // Now let's check whether we're even allowed to do this 00098 $errors = $titleObj->getUserPermissionsErrors( 'edit', $user ); 00099 if ( !$titleObj->exists() ) { 00100 $errors = array_merge( $errors, $titleObj->getUserPermissionsErrors( 'create', $user ) ); 00101 } 00102 if ( count( $errors ) ) { 00103 $this->dieUsageMsg( $errors[0] ); 00104 } 00105 00106 $articleObj = Article::newFromTitle( $titleObj, $this->getContext() ); 00107 00108 $toMD5 = $params['text']; 00109 if ( !is_null( $params['appendtext'] ) || !is_null( $params['prependtext'] ) ) 00110 { 00111 // For non-existent pages, Article::getContent() 00112 // returns an interface message rather than '' 00113 // We do want getContent()'s behavior for non-existent 00114 // MediaWiki: pages, though 00115 if ( $articleObj->getID() == 0 && $titleObj->getNamespace() != NS_MEDIAWIKI ) { 00116 $content = ''; 00117 } else { 00118 $content = $articleObj->getContent(); 00119 } 00120 00121 if ( !is_null( $params['section'] ) ) { 00122 // Process the content for section edits 00123 global $wgParser; 00124 $section = intval( $params['section'] ); 00125 $content = $wgParser->getSection( $content, $section, false ); 00126 if ( $content === false ) { 00127 $this->dieUsage( "There is no section {$section}.", 'nosuchsection' ); 00128 } 00129 } 00130 $params['text'] = $params['prependtext'] . $content . $params['appendtext']; 00131 $toMD5 = $params['prependtext'] . $params['appendtext']; 00132 } 00133 00134 if ( $params['undo'] > 0 ) { 00135 if ( $params['undoafter'] > 0 ) { 00136 if ( $params['undo'] < $params['undoafter'] ) { 00137 list( $params['undo'], $params['undoafter'] ) = 00138 array( $params['undoafter'], $params['undo'] ); 00139 } 00140 $undoafterRev = Revision::newFromID( $params['undoafter'] ); 00141 } 00142 $undoRev = Revision::newFromID( $params['undo'] ); 00143 if ( is_null( $undoRev ) || $undoRev->isDeleted( Revision::DELETED_TEXT ) ) { 00144 $this->dieUsageMsg( array( 'nosuchrevid', $params['undo'] ) ); 00145 } 00146 00147 if ( $params['undoafter'] == 0 ) { 00148 $undoafterRev = $undoRev->getPrevious(); 00149 } 00150 if ( is_null( $undoafterRev ) || $undoafterRev->isDeleted( Revision::DELETED_TEXT ) ) { 00151 $this->dieUsageMsg( array( 'nosuchrevid', $params['undoafter'] ) ); 00152 } 00153 00154 if ( $undoRev->getPage() != $articleObj->getID() ) { 00155 $this->dieUsageMsg( array( 'revwrongpage', $undoRev->getID(), $titleObj->getPrefixedText() ) ); 00156 } 00157 if ( $undoafterRev->getPage() != $articleObj->getID() ) { 00158 $this->dieUsageMsg( array( 'revwrongpage', $undoafterRev->getID(), $titleObj->getPrefixedText() ) ); 00159 } 00160 00161 $newtext = $articleObj->getUndoText( $undoRev, $undoafterRev ); 00162 if ( $newtext === false ) { 00163 $this->dieUsageMsg( 'undo-failure' ); 00164 } 00165 $params['text'] = $newtext; 00166 // If no summary was given and we only undid one rev, 00167 // use an autosummary 00168 if ( is_null( $params['summary'] ) && $titleObj->getNextRevisionID( $undoafterRev->getID() ) == $params['undo'] ) { 00169 $params['summary'] = wfMessage( 'undo-summary', $params['undo'], $undoRev->getUserText() )->inContentLanguage()->text(); 00170 } 00171 } 00172 00173 // See if the MD5 hash checks out 00174 if ( !is_null( $params['md5'] ) && md5( $toMD5 ) !== $params['md5'] ) { 00175 $this->dieUsageMsg( 'hashcheckfailed' ); 00176 } 00177 00178 // EditPage wants to parse its stuff from a WebRequest 00179 // That interface kind of sucks, but it's workable 00180 $requestArray = array( 00181 'wpTextbox1' => $params['text'], 00182 'wpEditToken' => $params['token'], 00183 'wpIgnoreBlankSummary' => '' 00184 ); 00185 00186 if ( !is_null( $params['summary'] ) ) { 00187 $requestArray['wpSummary'] = $params['summary']; 00188 } 00189 00190 if ( !is_null( $params['sectiontitle'] ) ) { 00191 $requestArray['wpSectionTitle'] = $params['sectiontitle']; 00192 } 00193 00194 // Watch out for basetimestamp == '' 00195 // wfTimestamp() treats it as NOW, almost certainly causing an edit conflict 00196 if ( !is_null( $params['basetimestamp'] ) && $params['basetimestamp'] != '' ) { 00197 $requestArray['wpEdittime'] = wfTimestamp( TS_MW, $params['basetimestamp'] ); 00198 } else { 00199 $requestArray['wpEdittime'] = $articleObj->getTimestamp(); 00200 } 00201 00202 if ( !is_null( $params['starttimestamp'] ) && $params['starttimestamp'] != '' ) { 00203 $requestArray['wpStarttime'] = wfTimestamp( TS_MW, $params['starttimestamp'] ); 00204 } else { 00205 $requestArray['wpStarttime'] = wfTimestampNow(); // Fake wpStartime 00206 } 00207 00208 if ( $params['minor'] || ( !$params['notminor'] && $user->getOption( 'minordefault' ) ) ) { 00209 $requestArray['wpMinoredit'] = ''; 00210 } 00211 00212 if ( $params['recreate'] ) { 00213 $requestArray['wpRecreate'] = ''; 00214 } 00215 00216 if ( !is_null( $params['section'] ) ) { 00217 $section = intval( $params['section'] ); 00218 if ( $section == 0 && $params['section'] != '0' && $params['section'] != 'new' ) { 00219 $this->dieUsage( "The section parameter must be set to an integer or 'new'", "invalidsection" ); 00220 } 00221 $requestArray['wpSection'] = $params['section']; 00222 } else { 00223 $requestArray['wpSection'] = ''; 00224 } 00225 00226 $watch = $this->getWatchlistValue( $params['watchlist'], $titleObj ); 00227 00228 // Deprecated parameters 00229 if ( $params['watch'] ) { 00230 $watch = true; 00231 } elseif ( $params['unwatch'] ) { 00232 $watch = false; 00233 } 00234 00235 if ( $watch ) { 00236 $requestArray['wpWatchthis'] = ''; 00237 } 00238 00239 global $wgTitle, $wgRequest; 00240 00241 $req = new DerivativeRequest( $this->getRequest(), $requestArray, true ); 00242 00243 // Some functions depend on $wgTitle == $ep->mTitle 00244 // TODO: Make them not or check if they still do 00245 $wgTitle = $titleObj; 00246 00247 $ep = new EditPage( $articleObj ); 00248 $ep->setContextTitle( $titleObj ); 00249 $ep->importFormData( $req ); 00250 00251 // Run hooks 00252 // Handle APIEditBeforeSave parameters 00253 $r = array(); 00254 if ( !wfRunHooks( 'APIEditBeforeSave', array( $ep, $ep->textbox1, &$r ) ) ) { 00255 if ( count( $r ) ) { 00256 $r['result'] = 'Failure'; 00257 $apiResult->addValue( null, $this->getModuleName(), $r ); 00258 return; 00259 } else { 00260 $this->dieUsageMsg( 'hookaborted' ); 00261 } 00262 } 00263 00264 // Do the actual save 00265 $oldRevId = $articleObj->getRevIdFetched(); 00266 $result = null; 00267 // Fake $wgRequest for some hooks inside EditPage 00268 // @todo FIXME: This interface SUCKS 00269 $oldRequest = $wgRequest; 00270 $wgRequest = $req; 00271 00272 $status = $ep->internalAttemptSave( $result, $user->isAllowed( 'bot' ) && $params['bot'] ); 00273 $wgRequest = $oldRequest; 00274 global $wgMaxArticleSize; 00275 00276 switch( $status->value ) { 00277 case EditPage::AS_HOOK_ERROR: 00278 case EditPage::AS_HOOK_ERROR_EXPECTED: 00279 $this->dieUsageMsg( 'hookaborted' ); 00280 00281 case EditPage::AS_IMAGE_REDIRECT_ANON: 00282 $this->dieUsageMsg( 'noimageredirect-anon' ); 00283 00284 case EditPage::AS_IMAGE_REDIRECT_LOGGED: 00285 $this->dieUsageMsg( 'noimageredirect-logged' ); 00286 00287 case EditPage::AS_SPAM_ERROR: 00288 $this->dieUsageMsg( array( 'spamdetected', $result['spam'] ) ); 00289 00290 case EditPage::AS_BLOCKED_PAGE_FOR_USER: 00291 $this->dieUsageMsg( 'blockedtext' ); 00292 00293 case EditPage::AS_MAX_ARTICLE_SIZE_EXCEEDED: 00294 case EditPage::AS_CONTENT_TOO_BIG: 00295 $this->dieUsageMsg( array( 'contenttoobig', $wgMaxArticleSize ) ); 00296 00297 case EditPage::AS_READ_ONLY_PAGE_ANON: 00298 $this->dieUsageMsg( 'noedit-anon' ); 00299 00300 case EditPage::AS_READ_ONLY_PAGE_LOGGED: 00301 $this->dieUsageMsg( 'noedit' ); 00302 00303 case EditPage::AS_READ_ONLY_PAGE: 00304 $this->dieReadOnly(); 00305 00306 case EditPage::AS_RATE_LIMITED: 00307 $this->dieUsageMsg( 'actionthrottledtext' ); 00308 00309 case EditPage::AS_ARTICLE_WAS_DELETED: 00310 $this->dieUsageMsg( 'wasdeleted' ); 00311 00312 case EditPage::AS_NO_CREATE_PERMISSION: 00313 $this->dieUsageMsg( 'nocreate-loggedin' ); 00314 00315 case EditPage::AS_BLANK_ARTICLE: 00316 $this->dieUsageMsg( 'blankpage' ); 00317 00318 case EditPage::AS_CONFLICT_DETECTED: 00319 $this->dieUsageMsg( 'editconflict' ); 00320 00321 // case EditPage::AS_SUMMARY_NEEDED: Can't happen since we set wpIgnoreBlankSummary 00322 case EditPage::AS_TEXTBOX_EMPTY: 00323 $this->dieUsageMsg( 'emptynewsection' ); 00324 00325 case EditPage::AS_SUCCESS_NEW_ARTICLE: 00326 $r['new'] = ''; 00327 00328 case EditPage::AS_SUCCESS_UPDATE: 00329 $r['result'] = 'Success'; 00330 $r['pageid'] = intval( $titleObj->getArticleID() ); 00331 $r['title'] = $titleObj->getPrefixedText(); 00332 $newRevId = $articleObj->getLatest(); 00333 if ( $newRevId == $oldRevId ) { 00334 $r['nochange'] = ''; 00335 } else { 00336 $r['oldrevid'] = intval( $oldRevId ); 00337 $r['newrevid'] = intval( $newRevId ); 00338 $r['newtimestamp'] = wfTimestamp( TS_ISO_8601, 00339 $articleObj->getTimestamp() ); 00340 } 00341 break; 00342 00343 case EditPage::AS_SUMMARY_NEEDED: 00344 $this->dieUsageMsg( 'summaryrequired' ); 00345 00346 case EditPage::AS_END: 00347 default: 00348 // $status came from WikiPage::doEdit() 00349 $errors = $status->getErrorsArray(); 00350 $this->dieUsageMsg( $errors[0] ); // TODO: Add new errors to message map 00351 break; 00352 } 00353 $apiResult->addValue( null, $this->getModuleName(), $r ); 00354 } 00355 00356 public function mustBePosted() { 00357 return true; 00358 } 00359 00360 public function isWriteMode() { 00361 return true; 00362 } 00363 00364 public function getDescription() { 00365 return 'Create and edit pages.'; 00366 } 00367 00368 public function getPossibleErrors() { 00369 global $wgMaxArticleSize; 00370 00371 return array_merge( parent::getPossibleErrors(), 00372 $this->getTitleOrPageIdErrorMessage(), 00373 array( 00374 array( 'missingtext' ), 00375 array( 'createonly-exists' ), 00376 array( 'nocreate-missing' ), 00377 array( 'nosuchrevid', 'undo' ), 00378 array( 'nosuchrevid', 'undoafter' ), 00379 array( 'revwrongpage', 'id', 'text' ), 00380 array( 'undo-failure' ), 00381 array( 'hashcheckfailed' ), 00382 array( 'hookaborted' ), 00383 array( 'noimageredirect-anon' ), 00384 array( 'noimageredirect-logged' ), 00385 array( 'spamdetected', 'spam' ), 00386 array( 'summaryrequired' ), 00387 array( 'blockedtext' ), 00388 array( 'contenttoobig', $wgMaxArticleSize ), 00389 array( 'noedit-anon' ), 00390 array( 'noedit' ), 00391 array( 'actionthrottledtext' ), 00392 array( 'wasdeleted' ), 00393 array( 'nocreate-loggedin' ), 00394 array( 'blankpage' ), 00395 array( 'editconflict' ), 00396 array( 'emptynewsection' ), 00397 array( 'unknownerror', 'retval' ), 00398 array( 'code' => 'nosuchsection', 'info' => 'There is no section section.' ), 00399 array( 'code' => 'invalidsection', 'info' => 'The section parameter must be set to an integer or \'new\'' ), 00400 array( 'customcssprotected' ), 00401 array( 'customjsprotected' ), 00402 ) 00403 ); 00404 } 00405 00406 public function getAllowedParams() { 00407 return array( 00408 'title' => array( 00409 ApiBase::PARAM_TYPE => 'string', 00410 ), 00411 'pageid' => array( 00412 ApiBase::PARAM_TYPE => 'integer', 00413 ), 00414 'section' => null, 00415 'sectiontitle' => array( 00416 ApiBase::PARAM_TYPE => 'string', 00417 ApiBase::PARAM_REQUIRED => false, 00418 ), 00419 'text' => null, 00420 'token' => array( 00421 ApiBase::PARAM_TYPE => 'string', 00422 ApiBase::PARAM_REQUIRED => true 00423 ), 00424 'summary' => null, 00425 'minor' => false, 00426 'notminor' => false, 00427 'bot' => false, 00428 'basetimestamp' => null, 00429 'starttimestamp' => null, 00430 'recreate' => false, 00431 'createonly' => false, 00432 'nocreate' => false, 00433 'watch' => array( 00434 ApiBase::PARAM_DFLT => false, 00435 ApiBase::PARAM_DEPRECATED => true, 00436 ), 00437 'unwatch' => array( 00438 ApiBase::PARAM_DFLT => false, 00439 ApiBase::PARAM_DEPRECATED => true, 00440 ), 00441 'watchlist' => array( 00442 ApiBase::PARAM_DFLT => 'preferences', 00443 ApiBase::PARAM_TYPE => array( 00444 'watch', 00445 'unwatch', 00446 'preferences', 00447 'nochange' 00448 ), 00449 ), 00450 'md5' => null, 00451 'prependtext' => null, 00452 'appendtext' => null, 00453 'undo' => array( 00454 ApiBase::PARAM_TYPE => 'integer' 00455 ), 00456 'undoafter' => array( 00457 ApiBase::PARAM_TYPE => 'integer' 00458 ), 00459 'redirect' => array( 00460 ApiBase::PARAM_TYPE => 'boolean', 00461 ApiBase::PARAM_DFLT => false, 00462 ), 00463 ); 00464 } 00465 00466 public function getParamDescription() { 00467 $p = $this->getModulePrefix(); 00468 return array( 00469 'title' => "Title of the page you want to edit. Cannot be used together with {$p}pageid", 00470 'pageid' => "Page ID of the page you want to edit. Cannot be used together with {$p}title", 00471 'section' => 'Section number. 0 for the top section, \'new\' for a new section', 00472 'sectiontitle' => 'The title for a new section', 00473 'text' => 'Page content', 00474 'token' => array( 'Edit token. You can get one of these through prop=info.', 00475 "The token should always be sent as the last parameter, or at least, after the {$p}text parameter" 00476 ), 00477 'summary' => "Edit summary. Also section title when {$p}section=new and {$p}sectiontitle is not set", 00478 'minor' => 'Minor edit', 00479 'notminor' => 'Non-minor edit', 00480 'bot' => 'Mark this edit as bot', 00481 'basetimestamp' => array( 'Timestamp of the base revision (obtained through prop=revisions&rvprop=timestamp).', 00482 'Used to detect edit conflicts; leave unset to ignore conflicts' 00483 ), 00484 'starttimestamp' => array( 'Timestamp when you obtained the edit token.', 00485 'Used to detect edit conflicts; leave unset to ignore conflicts' 00486 ), 00487 'recreate' => 'Override any errors about the article having been deleted in the meantime', 00488 'createonly' => 'Don\'t edit the page if it exists already', 00489 'nocreate' => 'Throw an error if the page doesn\'t exist', 00490 'watch' => 'Add the page to your watchlist', 00491 'unwatch' => 'Remove the page from your watchlist', 00492 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch', 00493 'md5' => array( "The MD5 hash of the {$p}text parameter, or the {$p}prependtext and {$p}appendtext parameters concatenated.", 00494 'If set, the edit won\'t be done unless the hash is correct' ), 00495 'prependtext' => "Add this text to the beginning of the page. Overrides {$p}text", 00496 'appendtext' => array( "Add this text to the end of the page. Overrides {$p}text.", 00497 "Use {$p}section=new to append a new section" ), 00498 'undo' => "Undo this revision. Overrides {$p}text, {$p}prependtext and {$p}appendtext", 00499 'undoafter' => 'Undo all revisions from undo to this one. If not set, just undo one revision', 00500 'redirect' => 'Automatically resolve redirects', 00501 ); 00502 } 00503 00504 public function getResultProperties() { 00505 return array( 00506 '' => array( 00507 'new' => 'boolean', 00508 'result' => array( 00509 ApiBase::PROP_TYPE => array( 00510 'Success', 00511 'Failure' 00512 ), 00513 ), 00514 'pageid' => array( 00515 ApiBase::PROP_TYPE => 'integer', 00516 ApiBase::PROP_NULLABLE => true 00517 ), 00518 'title' => array( 00519 ApiBase::PROP_TYPE => 'string', 00520 ApiBase::PROP_NULLABLE => true 00521 ), 00522 'nochange' => 'boolean', 00523 'oldrevid' => array( 00524 ApiBase::PROP_TYPE => 'integer', 00525 ApiBase::PROP_NULLABLE => true 00526 ), 00527 'newrevid' => array( 00528 ApiBase::PROP_TYPE => 'integer', 00529 ApiBase::PROP_NULLABLE => true 00530 ), 00531 'newtimestamp' => array( 00532 ApiBase::PROP_TYPE => 'string', 00533 ApiBase::PROP_NULLABLE => true 00534 ) 00535 ) 00536 ); 00537 } 00538 00539 public function needsToken() { 00540 return true; 00541 } 00542 00543 public function getTokenSalt() { 00544 return ''; 00545 } 00546 00547 public function getExamples() { 00548 return array( 00549 00550 'api.php?action=edit&title=Test&summary=test%20summary&text=article%20content&basetimestamp=20070824123454&token=%2B\\' 00551 => 'Edit a page (anonymous user)', 00552 00553 'api.php?action=edit&title=Test&summary=NOTOC&minor=&prependtext=__NOTOC__%0A&basetimestamp=20070824123454&token=%2B\\' 00554 => 'Prepend __NOTOC__ to a page (anonymous user)', 00555 'api.php?action=edit&title=Test&undo=13585&undoafter=13579&basetimestamp=20070824123454&token=%2B\\' 00556 => 'Undo r13579 through r13585 with autosummary (anonymous user)', 00557 ); 00558 } 00559 00560 public function getHelpUrls() { 00561 return 'https://www.mediawiki.org/wiki/API:Edit'; 00562 } 00563 00564 public function getVersion() { 00565 return __CLASS__ . ': $Id$'; 00566 } 00567 }