X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fapi%2FApiEditPage.php;h=357b4662369e5e7b0bc5caef1dfbc49729333129;hb=d87135d706004373b2cfdc4c588ce6d80358631f;hp=e75c4ad7c505991123a46b7928402b12fd2ccdda;hpb=85f3f3d7b679a5bc2bfc8f7f0210ebd8b49af0a3;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/api/ApiEditPage.php b/includes/api/ApiEditPage.php index e75c4ad7c5..357b466236 100644 --- a/includes/api/ApiEditPage.php +++ b/includes/api/ApiEditPage.php @@ -54,13 +54,30 @@ class ApiEditPage extends ApiBase { $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) ); } + $contentHandler = $pageObj->getContentHandler(); + + // @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 )->getText( Revision::FOR_THIS_USER ) ); + $titles = Revision::newFromTitle( $oldTitle )->getContent( Revision::FOR_THIS_USER )->getRedirectChain(); // array_shift( $titles ); $redirValues = array(); @@ -99,31 +116,53 @@ 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(); + + 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' ); + } + + 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 = ''; + } + + $content = ContentHandler::makeContent( $text, $this->getTitle() ); + } } 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']; } @@ -147,18 +186,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( $undoRev, $undoafterRev ); + if ( !$newContent ) { $this->dieUsageMsg( 'undo-failure' ); } - $params['text'] = $newtext; + + $params['contentformat'] = $contentHandler->getDefaultFormat(); + $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'] ) { @@ -175,6 +217,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' => '' ); @@ -192,7 +236,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'] != '' ) { @@ -240,7 +284,9 @@ 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 ); + $ep->setContextTitle( $titleObj ); $ep->importFormData( $req ); @@ -258,7 +304,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 @@ -325,14 +371,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; @@ -393,6 +440,10 @@ 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' ), )