merge latest master into Wikidata branch
[lhc/web/wiklou.git] / includes / api / ApiEditPage.php
index 2b9e849..87302e6 100644 (file)
@@ -54,17 +54,37 @@ class ApiEditPage extends ApiBase {
                        $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
                }
 
+               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
+
+               if ( !isset( $params['contentformat'] ) || $params['contentformat'] == '' ) {
+                       $params['contentformat'] = $contentHandler->getDefaultFormat();
+               }
+
+               $contentFormat = $params['contentformat'];
+
+               if ( !$contentHandler->isSupportedFormat( $contentFormat ) ) {
+                       $name = $titleObj->getPrefixedDBkey();
+                       $model = $contentHandler->getModelID();
+
+                       $this->dieUsage( "The requested format $contentFormat is not supported for content model ".
+                                                       " $model used by $name", 'badformat' );
+               }
+
                $apiResult = $this->getResult();
 
                if ( $params['redirect'] ) {
                        if ( $titleObj->isRedirect() ) {
                                $oldTitle = $titleObj;
 
-                               $titles = Title::newFromRedirectArray(
-                                       Revision::newFromTitle(
-                                               $oldTitle, false, Revision::READ_LATEST
-                                       )->getText( Revision::FOR_THIS_USER, $user )
-                               );
+                               $titles = Revision::newFromTitle( $oldTitle, false, Revision::READ_LATEST )
+                                                       ->getContent( Revision::FOR_THIS_USER, $user )
+                                                       ->getRedirectChain();
                                // array_shift( $titles );
 
                                $redirValues = array();
@@ -103,31 +123,58 @@ class ApiEditPage extends ApiBase {
                        $this->dieUsageMsg( $errors[0] );
                }
 
-               $articleObj = Article::newFromTitle( $titleObj, $this->getContext() );
-
                $toMD5 = $params['text'];
                if ( !is_null( $params['appendtext'] ) || !is_null( $params['prependtext'] ) )
                {
-                       // For non-existent pages, Article::getContent()
-                       // returns an interface message rather than ''
-                       // We do want getContent()'s behavior for non-existent
-                       // MediaWiki: pages, though
-                       if ( $articleObj->getID() == 0 && $titleObj->getNamespace() != NS_MEDIAWIKI ) {
-                               $content = '';
-                       } else {
-                               $content = $articleObj->getContent();
+                       $content = $pageObj->getContent();
+
+                       // @todo: Add support for appending/prepending to the Content interface
+
+                       if ( !( $content instanceof TextContent ) ) {
+                               $mode = $contentHandler->getModelID();
+                               $this->dieUsage( "Can't append to pages using content model $mode", 'appendnotsupported' );
+                       }
+
+                       if ( !$content ) {
+                               # If this is a MediaWiki:x message, then load the messages
+                               # and return the message value for x.
+                               if ( $titleObj->getNamespace() == NS_MEDIAWIKI ) {
+                                       $text = $titleObj->getDefaultMessageText();
+                                       if ( $text === false ) {
+                                               $text = '';
+                                       }
+
+                                       try {
+                                               $content = ContentHandler::makeContent( $text, $this->getTitle() );
+                                       } catch ( MWContentSerializationException $ex ) {
+                                               $this->dieUsage( $ex->getMessage(), 'parseerror' );
+                                               return;
+                                       }
+                               }
                        }
 
                        if ( !is_null( $params['section'] ) ) {
+                               if ( !$contentHandler->supportsSections() ) {
+                                       $modelName = $contentHandler->getModelID();
+                                       $this->dieUsage( "Sections are not supported for this content model: $modelName.", 'sectionsnotsupported' );
+                               }
+
                                // Process the content for section edits
-                               global $wgParser;
                                $section = intval( $params['section'] );
-                               $content = $wgParser->getSection( $content, $section, false );
-                               if ( $content === false ) {
+                               $content = $content->getSection( $section );
+
+                               if ( !$content ) {
                                        $this->dieUsage( "There is no section {$section}.", 'nosuchsection' );
                                }
                        }
-                       $params['text'] = $params['prependtext'] . $content . $params['appendtext'];
+
+                       if ( !$content ) {
+                               $text = '';
+                       } else {
+                               $text = $content->serialize( $contentFormat );
+                       }
+
+                       $params['text'] = $params['prependtext'] . $text . $params['appendtext'];
                        $toMD5 = $params['prependtext'] . $params['appendtext'];
                }
 
@@ -151,18 +198,21 @@ class ApiEditPage extends ApiBase {
                                $this->dieUsageMsg( array( 'nosuchrevid', $params['undoafter'] ) );
                        }
 
-                       if ( $undoRev->getPage() != $articleObj->getID() ) {
+                       if ( $undoRev->getPage() != $pageObj->getID() ) {
                                $this->dieUsageMsg( array( 'revwrongpage', $undoRev->getID(), $titleObj->getPrefixedText() ) );
                        }
-                       if ( $undoafterRev->getPage() != $articleObj->getID() ) {
+                       if ( $undoafterRev->getPage() != $pageObj->getID() ) {
                                $this->dieUsageMsg( array( 'revwrongpage', $undoafterRev->getID(), $titleObj->getPrefixedText() ) );
                        }
 
-                       $newtext = $articleObj->getUndoText( $undoRev, $undoafterRev );
-                       if ( $newtext === false ) {
+                       $newContent = $contentHandler->getUndoContent( $pageObj->getRevision(), $undoRev, $undoafterRev );
+
+                       if ( !$newContent ) {
                                $this->dieUsageMsg( 'undo-failure' );
                        }
-                       $params['text'] = $newtext;
+
+                       $params['text'] = $newContent->serialize( $params['contentformat'] );
+
                        // If no summary was given and we only undid one rev,
                        // use an autosummary
                        if ( is_null( $params['summary'] ) && $titleObj->getNextRevisionID( $undoafterRev->getID() ) == $params['undo'] ) {
@@ -179,6 +229,8 @@ class ApiEditPage extends ApiBase {
                // That interface kind of sucks, but it's workable
                $requestArray = array(
                        'wpTextbox1' => $params['text'],
+                       'format' => $contentFormat,
+                       'model' => $contentHandler->getModelID(),
                        'wpEditToken' => $params['token'],
                        'wpIgnoreBlankSummary' => ''
                );
@@ -196,7 +248,7 @@ class ApiEditPage extends ApiBase {
                if ( !is_null( $params['basetimestamp'] ) && $params['basetimestamp'] != '' ) {
                        $requestArray['wpEdittime'] = wfTimestamp( TS_MW, $params['basetimestamp'] );
                } else {
-                       $requestArray['wpEdittime'] = $articleObj->getTimestamp();
+                       $requestArray['wpEdittime'] = $pageObj->getTimestamp();
                }
 
                if ( !is_null( $params['starttimestamp'] ) && $params['starttimestamp'] != '' ) {
@@ -244,7 +296,12 @@ class ApiEditPage extends ApiBase {
                // TODO: Make them not or check if they still do
                $wgTitle = $titleObj;
 
-               $ep = new EditPage( $articleObj );
+               $articleObject = new Article( $titleObj );
+               $ep = new EditPage( $articleObject );
+
+               // allow editing of non-textual content.
+               $ep->allowNonTextContent = true;
+
                $ep->setContextTitle( $titleObj );
                $ep->importFormData( $req );
 
@@ -262,7 +319,7 @@ class ApiEditPage extends ApiBase {
                }
 
                // Do the actual save
-               $oldRevId = $articleObj->getRevIdFetched();
+               $oldRevId = $articleObject->getRevIdFetched();
                $result = null;
                // Fake $wgRequest for some hooks inside EditPage
                // @todo FIXME: This interface SUCKS
@@ -278,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' );
 
@@ -329,14 +389,15 @@ class ApiEditPage extends ApiBase {
                                $r['result'] = 'Success';
                                $r['pageid'] = intval( $titleObj->getArticleID() );
                                $r['title'] = $titleObj->getPrefixedText();
-                               $newRevId = $articleObj->getLatest();
+                               $r['contentmodel'] = $titleObj->getContentModel();
+                               $newRevId = $articleObject->getLatest();
                                if ( $newRevId == $oldRevId ) {
                                        $r['nochange'] = '';
                                } else {
                                        $r['oldrevid'] = intval( $oldRevId );
                                        $r['newrevid'] = intval( $newRevId );
                                        $r['newtimestamp'] = wfTimestamp( TS_ISO_8601,
-                                               $articleObj->getTimestamp() );
+                                               $pageObj->getTimestamp() );
                                }
                                break;
 
@@ -380,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' ),
@@ -397,6 +459,13 @@ class ApiEditPage extends ApiBase {
                                array( 'unknownerror', 'retval' ),
                                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( 'customcssprotected' ),
                                array( 'customjsprotected' ),
                        )
@@ -460,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(),
+                       )
                );
        }
 
@@ -498,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',
                );
        }