Fix edit API adding empty section title when creating new pages

WikitextContent methods replaceSection() (used when adding a section
to an existing page) and addSectionHeader() (used when creating a new
page) behaved inconsistently - the former would omit the heading
syntax when the section title was empty, but the latter would not.
They both omit it now.

Some callers of addSectionHeader() handled this case, but others did
not, which caused T311489. (I am removing the checks now, since the
change makes them redundant.)

Bug: T311489
Change-Id: Icd59bcf2b75bf50865f19fac92bddabe0c183dcc
This commit is contained in:
Bartosz Dziewoński 2022-06-28 22:24:09 +02:00
parent b42a64f3a5
commit add6c49e01
5 changed files with 40 additions and 15 deletions

View file

@ -2301,7 +2301,7 @@ class EditPage implements IEditObject {
if ( $this->sectiontitle !== null ) {
// Insert the section title above the content.
$content = $content->addSectionHeader( $this->sectiontitle );
} elseif ( $this->summary !== '' ) {
} else {
// Insert the section title above the content.
$content = $content->addSectionHeader( $this->summary );
}
@ -4414,7 +4414,7 @@ class EditPage implements IEditObject {
# If we're adding a comment, we need to show the
# summary as the headline
if ( $this->section === "new" && $this->summary !== "" ) {
if ( $this->section === "new" ) {
$content = $content->addSectionHeader( $this->summary );
}

View file

@ -379,7 +379,7 @@ class ApiParse extends ApiBase {
if ( $this->section !== false ) {
if ( $this->section === 'new' ) {
// Insert the section title above the content.
if ( $params['sectiontitle'] !== null && $params['sectiontitle'] !== '' ) {
if ( $params['sectiontitle'] !== null ) {
$this->content = $this->content->addSectionHeader( $params['sectiontitle'] );
}
} else {

View file

@ -129,9 +129,8 @@ class WikitextContent extends TextContent {
* @return Content
*/
public function addSectionHeader( $header ) {
$text = wfMessage( 'newsectionheaderdefaultlevel' )
->rawParams( $header )->inContentLanguage()->text();
$text .= "\n\n";
$text = strval( $header ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' )
->plaintextParams( $header )->inContentLanguage()->text() . "\n\n" : '';
$text .= $this->getText();
return new static( $text );

View file

@ -260,10 +260,7 @@ class ApiEditPageTest extends ApiTestCase {
$count++;
$name = 'Help:ApiEditPageTest_testEditNewSectionSummarySectiontitle' . $count;
// Create the page first to avoid autogenerated summary for page creation
$this->editPage( $name, '' );
// Test edit
// Test edit 1 (new page)
$this->doApiRequestWithToken( [
'action' => 'edit',
'title' => $name,
@ -276,12 +273,38 @@ class ApiEditPageTest extends ApiTestCase {
$wikiPage = WikiPage::factory( Title::newFromText( $name ) );
// Check the page text is correct
$text = $wikiPage->getContent( RevisionRecord::RAW )->getText();
$this->assertSame( $expectedText, $text, 'Correct text saved' );
$savedText = $wikiPage->getContent( RevisionRecord::RAW )->getText();
$this->assertSame( $expectedText, $savedText, 'Correct text saved (new page)' );
// Check that the edit summary is correct
$summary = $wikiPage->getRevisionRecord()->getComment( RevisionRecord::RAW )->text;
$this->assertSame( $expectedSummary, $summary, 'Correct summary saved' );
// (when not provided or empty, there is an autogenerated summary for page creation)
$savedSummary = $wikiPage->getRevisionRecord()->getComment( RevisionRecord::RAW )->text;
$expectedSummaryNew = $expectedSummary ?: wfMessage( 'autosumm-new' )->rawParams( $expectedText )
->inContentLanguage()->text();
$this->assertSame( $expectedSummaryNew, $savedSummary, 'Correct summary saved (new page)' );
// Clear the page
$this->editPage( $name, '' );
// Test edit 2 (existing page)
$this->doApiRequestWithToken( [
'action' => 'edit',
'title' => $name,
'section' => 'new',
'text' => 'text',
'sectiontitle' => $sectiontitle,
'summary' => $summary,
] );
$wikiPage = WikiPage::factory( Title::newFromText( $name ) );
// Check the page text is correct
$savedText = $wikiPage->getContent( RevisionRecord::RAW )->getText();
$this->assertSame( $expectedText, $savedText, 'Correct text saved (existing page)' );
// Check that the edit summary is correct
$savedSummary = $wikiPage->getRevisionRecord()->getComment( RevisionRecord::RAW )->text;
$this->assertSame( $expectedSummary, $savedSummary, 'Correct summary saved (existing page)' );
}
public function provideEditNewSectionSummarySectiontitle() {

View file

@ -128,8 +128,11 @@ just a test"
public function testAddSectionHeader() {
$content = $this->newContent( 'hello world' );
$content = $content->addSectionHeader( 'test' );
$this->assertEquals( "== test ==\n\nhello world", $content->getText() );
$content = $this->newContent( 'hello world' );
$content = $content->addSectionHeader( '' );
$this->assertEquals( "hello world", $content->getText() );
}
public static function dataPreSaveTransform() {