Merge "ApiParse: Fix parse of new section title"
[lhc/web/wiklou.git] / includes / api / ApiParse.php
index 83d2cbc..73bea83 100644 (file)
@@ -70,6 +70,9 @@ class ApiParse extends ApiBase {
 
                if ( isset( $params['section'] ) ) {
                        $this->section = $params['section'];
+                       if ( !preg_match( '/^((T-)?\d+|new)$/', $this->section ) ) {
+                               $this->dieUsage( "The section parameter must be a valid section id or 'new'", "invalidsection" );
+                       }
                } else {
                        $this->section = false;
                }
@@ -85,6 +88,9 @@ class ApiParse extends ApiBase {
                $result = $this->getResult();
 
                if ( !is_null( $oldid ) || !is_null( $pageid ) || !is_null( $page ) ) {
+                       if ( $this->section === 'new' ) {
+                                       $this->dieUsage( 'section=new cannot be combined with oldid, pageid or page parameters. Please use text', 'params' );
+                       }
                        if ( !is_null( $oldid ) ) {
                                // Don't use the parser cache
                                $rev = Revision::newFromId( $oldid );
@@ -203,7 +209,14 @@ class ApiParse extends ApiBase {
                        }
 
                        if ( $this->section !== false ) {
-                               $this->content = $this->getSectionContent( $this->content, $titleObj->getPrefixedText() );
+                               if ( $this->section === 'new' ) {
+                                       // Insert the section title above the content.
+                                       if ( !is_null( $params['sectiontitle'] ) && $params['sectiontitle'] !== '' ) {
+                                               $this->content = $this->content->addSectionHeader( $params['sectiontitle'] );
+                                       }
+                               } else {
+                                       $this->content = $this->getSectionContent( $this->content, $titleObj->getPrefixedText() );
+                               }
                        }
 
                        if ( $params['pst'] || $params['onlypst'] ) {
@@ -218,6 +231,13 @@ class ApiParse extends ApiBase {
                                        $result_array['wikitext'] = array();
                                        ApiResult::setContent( $result_array['wikitext'], $this->content->serialize( $format ) );
                                }
+                               if ( !is_null( $params['summary'] ) ||
+                                       ( !is_null( $params['sectiontitle'] ) && $this->section === 'new' )
+                               ) {
+                                       $result_array['parsedsummary'] = array();
+                                       ApiResult::setContent( $result_array['parsedsummary'], $this->formatSummary( $titleObj, $params ) );
+                               }
+
                                $result->addValue( null, $this->getModuleName(), $result_array );
 
                                return;
@@ -252,12 +272,11 @@ class ApiParse extends ApiBase {
                        ApiResult::setContent( $result_array['text'], $p_result->getText() );
                }
 
-               if ( !is_null( $params['summary'] ) ) {
+               if ( !is_null( $params['summary'] ) ||
+                       ( !is_null( $params['sectiontitle'] ) && $this->section === 'new' )
+               ) {
                        $result_array['parsedsummary'] = array();
-                       ApiResult::setContent(
-                               $result_array['parsedsummary'],
-                               Linker::formatComment( $params['summary'], $titleObj )
-                       );
+                       ApiResult::setContent( $result_array['parsedsummary'], $this->formatSummary( $titleObj, $params ) );
                }
 
                if ( isset( $prop['langlinks'] ) ) {
@@ -481,6 +500,30 @@ class ApiParse extends ApiBase {
                return $section;
        }
 
+       /**
+        * This mimicks the behavior of EditPage in formatting a summary
+        *
+        * @param Title $title of the page being parsed
+        * @param Array $params the API parameters of the request
+        * @return Content|bool
+        */
+       private function formatSummary( $title, $params ) {
+               global $wgParser;
+               $summary = !is_null( $params['summary'] ) ? $params['summary'] : '';
+               $sectionTitle = !is_null( $params['sectiontitle'] ) ? $params['sectiontitle'] : '';
+
+               if ( $this->section === 'new' && ( $sectionTitle === '' || $summary === '' ) ) {
+                       if( $sectionTitle !== '' ) {
+                               $summary = $params['sectiontitle'];
+                       }
+                       if ( $summary !== '' ) {
+                               $summary = wfMessage( 'newsectionsummary' )->rawParams( $wgParser->stripSectionName( $summary ) )
+                                       ->inContentLanguage()->text();
+                       }
+               }
+               return Linker::formatComment( $summary, $title, $this->section === 'new' );
+       }
+
        private function formatLangLinks( $links ) {
                $result = array();
                foreach ( $links as $link ) {
@@ -698,6 +741,9 @@ class ApiParse extends ApiBase {
                        'onlypst' => false,
                        'effectivelanglinks' => false,
                        'section' => null,
+                       'sectiontitle' => array(
+                               ApiBase::PARAM_TYPE => 'string',
+                       ),
                        'disablepp' => false,
                        'disableeditsection' => false,
                        'generatexml' => array(