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