From: daniel Date: Tue, 21 Aug 2012 14:14:18 +0000 (+0200) Subject: Improved content model handling in ApiEditPage X-Git-Tag: 1.31.0-rc.0~22097^2^2~53^2 X-Git-Url: https://git.cyclocoop.org/?a=commitdiff_plain;h=daf415aa884b70cc675b83c6152916e1fa6404e6;p=lhc%2Fweb%2Fwiklou.git Improved content model handling in ApiEditPage Change-Id: Ifd9e344864f7663de7263cb536e83dbdc03cf2ed --- diff --git a/includes/ContentHandler.php b/includes/ContentHandler.php index e460eeef21..732e14ec15 100644 --- a/includes/ContentHandler.php +++ b/includes/ContentHandler.php @@ -331,6 +331,12 @@ abstract class ContentHandler { else return wfMsg( $key ); } + public static function getContentModels() { + global $wgContentHandlers; + + return array_keys( $wgContentHandlers ); + } + public static function getAllContentFormats() { global $wgContentHandlers; diff --git a/includes/api/ApiEditPage.php b/includes/api/ApiEditPage.php index 7574e6ce81..a611c9f146 100644 --- a/includes/api/ApiEditPage.php +++ b/includes/api/ApiEditPage.php @@ -54,7 +54,11 @@ class ApiEditPage extends ApiBase { $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) ); } - $contentHandler = $pageObj->getContentHandler(); + if ( !isset( $params['contentmodel'] ) || $params['contentmodel'] == '' ) { + $contentHandler = $pageObj->getContentHandler(); + } else { + $contentHandler = ContentHandler::getForModelID( $params['contentmodel'] ); + } // @todo ask handler whether direct editing is supported at all! make allowFlatEdit() method or some such @@ -139,7 +143,12 @@ class ApiEditPage extends ApiBase { $text = ''; } - $content = ContentHandler::makeContent( $text, $this->getTitle() ); + try { + $content = ContentHandler::makeContent( $text, $this->getTitle() ); + } catch ( MWContentSerializationException $ex ) { + $this->dieUsage( $ex->getMessage(), 'parseerror' ); + return; + } } } @@ -195,12 +204,12 @@ class ApiEditPage extends ApiBase { $this->dieUsageMsg( array( 'revwrongpage', $undoafterRev->getID(), $titleObj->getPrefixedText() ) ); } - $newContent = $contentHandler->getUndoContent( $undoRev, $undoafterRev ); + $newContent = $contentHandler->getUndoContent( $pageObj->getRevision(), $undoRev, $undoafterRev ); + if ( !$newContent ) { $this->dieUsageMsg( 'undo-failure' ); } - $params['contentformat'] = $contentHandler->getDefaultFormat(); $params['text'] = $newContent->serialize( $params['contentformat'] ); // If no summary was given and we only undid one rev, @@ -322,6 +331,9 @@ class ApiEditPage extends ApiBase { case EditPage::AS_HOOK_ERROR_EXPECTED: $this->dieUsageMsg( 'hookaborted' ); + case EditPage::AS_PARSE_ERROR: + $this->dieUsage( $status->getMessage(), 'parseerror' ); + case EditPage::AS_IMAGE_REDIRECT_ANON: $this->dieUsageMsg( 'noimageredirect-anon' ); @@ -425,6 +437,7 @@ class ApiEditPage extends ApiBase { array( 'undo-failure' ), array( 'hashcheckfailed' ), array( 'hookaborted' ), + array( 'code' => 'parseerror', 'info' => 'Failed to parse the given text.' ), array( 'noimageredirect-anon' ), array( 'noimageredirect-logged' ), array( 'spamdetected', 'spam' ), @@ -512,6 +525,12 @@ class ApiEditPage extends ApiBase { ApiBase::PARAM_TYPE => 'boolean', ApiBase::PARAM_DFLT => false, ), + 'contentformat' => array( + ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(), + ), + 'contentmodel' => array( + ApiBase::PARAM_TYPE => ContentHandler::getContentModels(), + ) ); } @@ -550,6 +569,8 @@ class ApiEditPage extends ApiBase { 'undo' => "Undo this revision. Overrides {$p}text, {$p}prependtext and {$p}appendtext", 'undoafter' => 'Undo all revisions from undo to this one. If not set, just undo one revision', 'redirect' => 'Automatically resolve redirects', + 'contentformat' => 'Content serialization format used for the input text', + 'contentmodel' => 'Content model of the new content', ); }