} else {
$contentHandler = ContentHandler::getForModelID( $params['contentmodel'] );
}
+ $contentModel = $contentHandler->getModelID();
$name = $titleObj->getPrefixedDBkey();
$model = $contentHandler->getModelID();
}
if ( !isset( $params['contentformat'] ) || $params['contentformat'] == '' ) {
- $params['contentformat'] = $contentHandler->getDefaultFormat();
+ $contentFormat = $contentHandler->getDefaultFormat();
+ } else {
+ $contentFormat = $params['contentformat'];
}
- $contentFormat = $params['contentformat'];
-
if ( !$contentHandler->isSupportedFormat( $contentFormat ) ) {
$this->dieUsage( "The requested format $contentFormat is not supported for content model " .
if ( !$newContent ) {
$this->dieUsageMsg( 'undo-failure' );
}
-
- $params['text'] = $newContent->serialize( $params['contentformat'] );
-
+ if ( empty( $params['contentmodel'] )
+ && empty( $params['contentformat'] )
+ ) {
+ // If we are reverting content model, the new content model
+ // might not support the current serialization format, in
+ // which case go back to the old serialization format,
+ // but only if the user hasn't specified a format/model
+ // parameter.
+ if ( !$newContent->isSupportedFormat( $contentFormat ) ) {
+ $contentFormat = $undoafterRev->getContentFormat();
+ }
+ // Override content model with model of undid revision.
+ $contentModel = $newContent->getModel();
+ }
+ $params['text'] = $newContent->serialize( $contentFormat );
// If no summary was given and we only undid one rev,
// use an autosummary
if ( is_null( $params['summary'] ) &&
$requestArray = [
'wpTextbox1' => $params['text'],
'format' => $contentFormat,
- 'model' => $contentHandler->getModelID(),
+ 'model' => $contentModel,
'wpEditToken' => $params['token'],
'wpIgnoreBlankSummary' => true,
'wpIgnoreBlankArticle' => true,
$this->assertEquals( "testing-nontext", $page->getContentModel() );
$this->assertEquals( $data, $page->getContent()->serialize() );
}
+
+ /**
+ * This test verifies that after changing the content model
+ * of a page, undoing that edit via the API will also
+ * undo the content model change.
+ */
+ public function testUndoAfterContentModelChange() {
+ $name = 'Help:' . __FUNCTION__;
+ $uploader = self::$users['uploader']->getUser();
+ $sysop = self::$users['sysop']->getUser();
+ $apiResult = $this->doApiRequestWithToken( [
+ 'action' => 'edit',
+ 'title' => $name,
+ 'text' => 'some text',
+ ], null, $sysop )[0];
+
+ // Check success
+ $this->assertArrayHasKey( 'edit', $apiResult );
+ $this->assertArrayHasKey( 'result', $apiResult['edit'] );
+ $this->assertEquals( 'Success', $apiResult['edit']['result'] );
+ $this->assertArrayHasKey( 'contentmodel', $apiResult['edit'] );
+ // Content model is wikitext
+ $this->assertEquals( 'wikitext', $apiResult['edit']['contentmodel'] );
+
+ // Convert the page to JSON
+ $apiResult = $this->doApiRequestWithToken( [
+ 'action' => 'edit',
+ 'title' => $name,
+ 'text' => '{}',
+ 'contentmodel' => 'json',
+ ], null, $uploader )[0];
+
+ // Check success
+ $this->assertArrayHasKey( 'edit', $apiResult );
+ $this->assertArrayHasKey( 'result', $apiResult['edit'] );
+ $this->assertEquals( 'Success', $apiResult['edit']['result'] );
+ $this->assertArrayHasKey( 'contentmodel', $apiResult['edit'] );
+ $this->assertEquals( 'json', $apiResult['edit']['contentmodel'] );
+
+ $apiResult = $this->doApiRequestWithToken( [
+ 'action' => 'edit',
+ 'title' => $name,
+ 'undo' => $apiResult['edit']['newrevid']
+ ], null, $sysop )[0];
+
+ // Check success
+ $this->assertArrayHasKey( 'edit', $apiResult );
+ $this->assertArrayHasKey( 'result', $apiResult['edit'] );
+ $this->assertEquals( 'Success', $apiResult['edit']['result'] );
+ $this->assertArrayHasKey( 'contentmodel', $apiResult['edit'] );
+ // Check that the contentmodel is back to wikitext now.
+ $this->assertEquals( 'wikitext', $apiResult['edit']['contentmodel'] );
+ }
}