fix merge of Iec98e472
[lhc/web/wiklou.git] / includes / api / ApiEditPage.php
index 5c4e799..506a546 100644 (file)
@@ -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
 
@@ -68,7 +72,8 @@ class ApiEditPage extends ApiBase {
                        $name = $titleObj->getPrefixedDBkey();
                        $model = $contentHandler->getModelID();
 
-                       $this->dieUsage( "The requested format $contentFormat is not supported for content model $model used by $name", 'badformat' );
+                       $this->dieUsage( "The requested format $contentFormat is not supported for content model ".
+                                                       " $model used by $name", 'badformat' );
                }
 
                $apiResult = $this->getResult();
@@ -77,7 +82,9 @@ class ApiEditPage extends ApiBase {
                        if ( $titleObj->isRedirect() ) {
                                $oldTitle = $titleObj;
 
-                               $titles = Revision::newFromTitle( $oldTitle )->getContent( Revision::FOR_THIS_USER )->getRedirectChain();
+                               $titles = Revision::newFromTitle( $oldTitle, false, Revision::READ_LATEST )
+                                                       ->getContent( Revision::FOR_THIS_USER )
+                                                       ->getRedirectChain();
                                // array_shift( $titles );
 
                                $redirValues = array();
@@ -121,9 +128,9 @@ class ApiEditPage extends ApiBase {
                {
                        $content = $pageObj->getContent();
 
+                       // @todo: Add support for appending/prepending to the Content interface
+
                        if ( !( $content instanceof TextContent ) ) {
-                               // @todo: ContentHandler should have an isFlat() method or some such
-                               // @todo: XXX: or perhaps there should be Content::append(), Content::prepend() and Content::supportsConcatenation()
                                $mode = $contentHandler->getModelID();
                                $this->dieUsage( "Can't append to pages using content model $mode", 'appendnotsupported' );
                        }
@@ -137,7 +144,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;
+                                       }
                                }
                        }
 
@@ -193,12 +205,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,
@@ -226,7 +238,7 @@ class ApiEditPage extends ApiBase {
                if ( !is_null( $params['summary'] ) ) {
                        $requestArray['wpSummary'] = $params['summary'];
                }
-               
+
                if ( !is_null( $params['sectiontitle'] ) ) {
                        $requestArray['wpSectionTitle'] = $params['sectiontitle'];
                }
@@ -287,6 +299,9 @@ class ApiEditPage extends ApiBase {
                $articleObject = new Article( $titleObj );
                $ep = new EditPage( $articleObject );
 
+               // allow editing of non-textual content.
+               $ep->allowNonTextContent = true;
+
                $ep->setContextTitle( $titleObj );
                $ep->importFormData( $req );
 
@@ -320,6 +335,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' );
 
@@ -423,6 +441,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' ),
@@ -441,9 +460,12 @@ class ApiEditPage extends ApiBase {
                                array( 'code' => 'nosuchsection', 'info' => 'There is no section section.' ),
                                array( 'code' => 'invalidsection', 'info' => 'The section parameter must be set to an integer or \'new\'' ),
                                array( 'code' => 'sectionsnotsupported', 'info' => 'Sections are not supported for this type of page.' ),
-                               array( 'code' => 'editnotsupported', 'info' => 'Editing of this type of page is not supported using the text based edit API.' ),
-                               array( 'code' => 'appendnotsupported', 'info' => 'This type of page can not be edited by appending or prepending text.' ),
-                               array( 'code' => 'badformat', 'info' => 'The requested serialization format can not be applied to the page\'s content model' ),
+                               array( 'code' => 'editnotsupported', 'info' => 'Editing of this type of page is not supported using '
+                                                                                                                               . 'the text based edit API.' ),
+                               array( 'code' => 'appendnotsupported', 'info' => 'This type of page can not be edited by appending '
+                                                                                                                               . 'or prepending text.' ),
+                               array( 'code' => 'badformat', 'info' => 'The requested serialization format can not be applied to '
+                                                                                                               . 'the page\'s content model' ),
                                array( 'customcssprotected' ),
                                array( 'customjsprotected' ),
                        )
@@ -507,6 +529,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(),
+                       )
                );
        }
 
@@ -545,6 +573,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',
                );
        }