ApiParse: Support previewing section=new
authorDerk-Jan Hartman <hartman.wiki@gmail.com>
Tue, 10 Mar 2015 21:27:01 +0000 (22:27 +0100)
committerAnomie <bjorsch@wikimedia.org>
Mon, 16 Mar 2015 14:56:45 +0000 (14:56 +0000)
When live previewing a 'new' section, we want to automatically add the
edit summary/sectiontitle as an H2 element, as the Edit Page and API
do as well.

Bug: T84877
Change-Id: I40925c16284bb97a4d491e12a8e7878b9d1b4810

includes/api/ApiParse.php
includes/api/i18n/en.json
includes/api/i18n/qqq.json

index 83e105b..20592ca 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;
                }
@@ -203,7 +206,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'] ) {
@@ -706,6 +716,9 @@ class ApiParse extends ApiBase {
                        'onlypst' => false,
                        'effectivelanglinks' => false,
                        'section' => null,
+                       'sectiontitle' => array(
+                               ApiBase::PARAM_TYPE => 'string',
+                       ),
                        'disablepp' => false,
                        'disableeditsection' => false,
                        'generatexml' => array(
index 4237ff8..1c6af66 100644 (file)
        "apihelp-parse-param-pst": "Do a pre-save transform on the input before parsing it. Only valid when used with text.",
        "apihelp-parse-param-onlypst": "Do a pre-save transform (PST) on the input, but don't parse it. Returns the same wikitext, after a PST has been applied. Only valid when used with <var>$1text</var>.",
        "apihelp-parse-param-effectivelanglinks": "Includes language links supplied by extensions (for use with <kbd>$1prop=langlinks</kbd>).",
-       "apihelp-parse-param-section": "Only retrieve the content of this section number.",
+       "apihelp-parse-param-section": "Only retrieve the content of this section number or when <kbd>new</kbd> generate a new section.\n\n<kbd>new</kbd> section is only honored when specifying <var>text</var>.",
+       "apihelp-parse-param-sectiontitle": "New section title when <var>section</var> is <kbd>new</kbd>.\n\nUnlike page editing, this does not fall back to <var>summary</var> when omitted or empty.",
        "apihelp-parse-param-disablepp": "Disable the PP Report from the parser output.",
        "apihelp-parse-param-disableeditsection": "Disable edit section links from the parser output.",
        "apihelp-parse-param-generatexml": "Generate XML parse tree (requires content model <code>$1</code>).",
index 3f5084d..3ecb7a5 100644 (file)
        "apihelp-parse-param-onlypst": "{{doc-apihelp-param|parse|onlypst}}",
        "apihelp-parse-param-effectivelanglinks": "{{doc-apihelp-param|parse|effectivelanglinks}}",
        "apihelp-parse-param-section": "{{doc-apihelp-param|parse|section}}",
+       "apihelp-parse-param-sectiontitle": "{{doc-apihelp-param|parse|sectiontitle}}",
        "apihelp-parse-param-disablepp": "{{doc-apihelp-param|parse|disablepp}}",
        "apihelp-parse-param-disableeditsection": "{{doc-apihelp-param|parse|disableeditsection}}",
        "apihelp-parse-param-generatexml": "{{doc-apihelp-param|parse|generatexml|params=* $1 - Value of the constant CONTENT_MODEL_WIKITEXT|paramstart=2}}",