Merge "Remove unused 'XMPGetInfo' and 'XMPGetResults' hooks"
[lhc/web/wiklou.git] / includes / api / ApiParse.php
index 267bced..cc8ca97 100644 (file)
@@ -138,6 +138,7 @@ class ApiParse extends ApiBase {
                                        $main = new ApiMain( $req );
                                        $pageSet = new ApiPageSet( $main );
                                        $pageSet->execute();
+                                       $redirValues = $pageSet->getRedirectTitlesAsResult( $this->getResult() );
 
                                        $to = $page;
                                        foreach ( $pageSet->getRedirectTitles() as $title ) {
@@ -225,21 +226,17 @@ class ApiParse extends ApiBase {
                        if ( $params['onlypst'] ) {
                                // Build a result and bail out
                                $result_array = array();
-                               $result_array['text'] = array();
-                               ApiResult::setContentValue( $result_array['text'], 'text', $this->pstContent->serialize( $format ) );
+                               $result_array['text'] = $this->pstContent->serialize( $format );
+                               $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'text';
                                if ( isset( $prop['wikitext'] ) ) {
-                                       $result_array['wikitext'] = array();
-                                       ApiResult::setContentValue( $result_array['wikitext'], 'wikitext', $this->content->serialize( $format ) );
+                                       $result_array['wikitext'] = $this->content->serialize( $format );
+                                       $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'wikitext';
                                }
                                if ( !is_null( $params['summary'] ) ||
                                        ( !is_null( $params['sectiontitle'] ) && $this->section === 'new' )
                                ) {
-                                       $result_array['parsedsummary'] = array();
-                                       ApiResult::setContentValue(
-                                               $result_array['parsedsummary'],
-                                               'parsedsummary',
-                                               $this->formatSummary( $titleObj, $params )
-                                       );
+                                       $result_array['parsedsummary'] = $this->formatSummary( $titleObj, $params );
+                                       $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'parsedsummary';
                                }
 
                                $result->addValue( null, $this->getModuleName(), $result_array );
@@ -272,19 +269,15 @@ class ApiParse extends ApiBase {
                }
 
                if ( isset( $prop['text'] ) ) {
-                       $result_array['text'] = array();
-                       ApiResult::setContentValue( $result_array['text'], 'text', $p_result->getText() );
+                       $result_array['text'] = $p_result->getText();
+                       $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'text';
                }
 
                if ( !is_null( $params['summary'] ) ||
                        ( !is_null( $params['sectiontitle'] ) && $this->section === 'new' )
                ) {
-                       $result_array['parsedsummary'] = array();
-                       ApiResult::setContentValue(
-                               $result_array['parsedsummary'],
-                               'parsedsummary',
-                               $this->formatSummary( $titleObj, $params )
-                       );
+                       $result_array['parsedsummary'] = $this->formatSummary( $titleObj, $params );
+                       $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'parsedsummary';
                }
 
                if ( isset( $prop['langlinks'] ) ) {
@@ -307,9 +300,8 @@ class ApiParse extends ApiBase {
                        $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
                }
                if ( isset( $prop['categorieshtml'] ) ) {
-                       $categoriesHtml = $this->categoriesHtml( $p_result->getCategories() );
-                       $result_array['categorieshtml'] = array();
-                       ApiResult::setContentValue( $result_array['categorieshtml'], 'categorieshtml', $categoriesHtml );
+                       $result_array['categorieshtml'] = $this->categoriesHtml( $p_result->getCategories() );
+                       $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'categorieshtml';
                }
                if ( isset( $prop['links'] ) ) {
                        $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
@@ -349,12 +341,8 @@ class ApiParse extends ApiBase {
                        }
 
                        if ( isset( $prop['headhtml'] ) ) {
-                               $result_array['headhtml'] = array();
-                               ApiResult::setContentValue(
-                                       $result_array['headhtml'],
-                                       'headhtml',
-                                       $context->getOutput()->headElement( $context->getSkin() )
-                               );
+                               $result_array['headhtml'] = $context->getOutput()->headElement( $context->getSkin() );
+                               $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'headhtml';
                        }
                }
 
@@ -362,15 +350,25 @@ class ApiParse extends ApiBase {
                        $result_array['modules'] = array_values( array_unique( $p_result->getModules() ) );
                        $result_array['modulescripts'] = array_values( array_unique( $p_result->getModuleScripts() ) );
                        $result_array['modulestyles'] = array_values( array_unique( $p_result->getModuleStyles() ) );
-                       $result_array['modulemessages'] = array_values( array_unique( $p_result->getModuleMessages() ) );
+                       // To be removed in 1.27
+                       $result_array['modulemessages'] = array();
+                       $this->setWarning( 'modulemessages is deprecated since MediaWiki 1.26' );
+               }
+
+               if ( isset( $prop['jsconfigvars'] ) ) {
+                       $result_array['jsconfigvars'] = $this->formatJsConfigVars( $p_result->getJsConfigVars() );
+               }
+
+               if ( isset( $prop['encodedjsconfigvars'] ) ) {
+                       $result_array['encodedjsconfigvars'] = FormatJson::encode(
+                               $p_result->getJsConfigVars(), false, FormatJson::ALL_OK
+                       );
+                       $result_array[ApiResult::META_SUBELEMENTS][] = 'encodedjsconfigvars';
                }
 
                if ( isset( $prop['indicators'] ) ) {
-                       foreach ( $p_result->getIndicators() as $name => $content ) {
-                               $indicator = array( 'name' => $name );
-                               ApiResult::setContentValue( $indicator, 'content', $content );
-                               $result_array['indicators'][] = $indicator;
-                       }
+                       $result_array['indicators'] = (array)$p_result->getIndicators();
+                       ApiResult::setArrayType( $result_array['indicators'], 'BCkvp', 'name' );
                }
 
                if ( isset( $prop['iwlinks'] ) ) {
@@ -378,15 +376,16 @@ class ApiParse extends ApiBase {
                }
 
                if ( isset( $prop['wikitext'] ) ) {
-                       $result_array['wikitext'] = array();
-                       ApiResult::setContentValue( $result_array['wikitext'], 'wikitext', $this->content->serialize( $format ) );
+                       $result_array['wikitext'] = $this->content->serialize( $format );
+                       $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'wikitext';
                        if ( !is_null( $this->pstContent ) ) {
-                               $result_array['psttext'] = array();
-                               ApiResult::setContentValue( $result_array['psttext'], 'psttext', $this->pstContent->serialize( $format ) );
+                               $result_array['psttext'] = $this->pstContent->serialize( $format );
+                               $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'psttext';
                        }
                }
                if ( isset( $prop['properties'] ) ) {
-                       $result_array['properties'] = $this->formatProperties( $p_result->getProperties() );
+                       $result_array['properties'] = (array)$p_result->getProperties();
+                       ApiResult::setArrayType( $result_array['properties'], 'BCkvp', 'name' );
                }
 
                if ( isset( $prop['limitreportdata'] ) ) {
@@ -394,9 +393,8 @@ class ApiParse extends ApiBase {
                                $this->formatLimitReportData( $p_result->getLimitReportData() );
                }
                if ( isset( $prop['limitreporthtml'] ) ) {
-                       $limitreportHtml = EditPage::getPreviewLimitReport( $p_result );
-                       $result_array['limitreporthtml'] = array();
-                       ApiResult::setContentValue( $result_array['limitreporthtml'], 'limitreporthtml', $limitreportHtml );
+                       $result_array['limitreporthtml'] = EditPage::getPreviewLimitReport( $p_result );
+                       $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'limitreporthtml';
                }
 
                if ( $params['generatexml'] ) {
@@ -411,8 +409,8 @@ class ApiParse extends ApiBase {
                        } else {
                                $xml = $dom->__toString();
                        }
-                       $result_array['parsetree'] = array();
-                       ApiResult::setContentValue( $result_array['parsetree'], 'parsetree', $xml );
+                       $result_array['parsetree'] = $xml;
+                       $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'parsetree';
                }
 
                $result_mapping = array(
@@ -589,9 +587,9 @@ class ApiParse extends ApiBase {
                        $entry['sortkey'] = $sortkey;
                        ApiResult::setContentValue( $entry, 'category', $link );
                        if ( !isset( $hiddencats[$link] ) ) {
-                               $entry['missing'] = '';
+                               $entry['missing'] = true;
                        } elseif ( $hiddencats[$link] ) {
-                               $entry['hidden'] = '';
+                               $entry['hidden'] = true;
                        }
                        $result[] = $entry;
                }
@@ -613,9 +611,7 @@ class ApiParse extends ApiBase {
                                $entry = array();
                                $entry['ns'] = $ns;
                                ApiResult::setContentValue( $entry, 'title', Title::makeTitle( $ns, $title )->getFullText() );
-                               if ( $id != 0 ) {
-                                       $entry['exists'] = '';
-                               }
+                               $entry['exists'] = $id != 0;
                                $result[] = $entry;
                        }
                }
@@ -655,18 +651,6 @@ class ApiParse extends ApiBase {
                return $result;
        }
 
-       private function formatProperties( $properties ) {
-               $result = array();
-               foreach ( $properties as $name => $value ) {
-                       $entry = array();
-                       $entry['name'] = $name;
-                       ApiResult::setContentValue( $entry, 'value', $value );
-                       $result[] = $entry;
-               }
-
-               return $result;
-       }
-
        private function formatCss( $css ) {
                $result = array();
                foreach ( $css as $file => $link ) {
@@ -689,8 +673,7 @@ class ApiParse extends ApiBase {
                        if ( !is_array( $value ) ) {
                                $value = array( $value );
                        }
-                       ApiResult::setIndexedTagName( $value, 'param' );
-                       ApiResult::setIndexedTagNameOnSubarrays( $value, 'param' );
+                       ApiResult::setIndexedTagNameRecursive( $value, 'param' );
                        $entry = array_merge( $entry, $value );
                        $result[] = $entry;
                }
@@ -698,6 +681,53 @@ class ApiParse extends ApiBase {
                return $result;
        }
 
+       private function formatJsConfigVars( $vars, $forceHash = true ) {
+               // Process subarrays and determine if this is a JS [] or {}
+               $hash = $forceHash;
+               $maxKey = -1;
+               $bools = array();
+               foreach ( $vars as $k => $v ) {
+                       if ( is_array( $v ) || is_object( $v ) ) {
+                               $vars[$k] = $this->formatJsConfigVars( (array)$v, false );
+                       } elseif ( is_bool( $v ) ) {
+                               // Better here to use real bools even in BC formats
+                               $bools[] = $k;
+                       }
+                       if ( is_string( $k ) ) {
+                               $hash = true;
+                       } elseif ( $k > $maxKey ) {
+                               $maxKey = $k;
+                       }
+               }
+               if ( !$hash && $maxKey !== count( $vars ) - 1 ) {
+                       $hash = true;
+               }
+
+               // Get the list of keys we actually care about. Unfortunately, we can't support
+               // certain keys that conflict with ApiResult metadata.
+               $keys = array_diff( array_keys( $vars ), array(
+                       ApiResult::META_TYPE, ApiResult::META_PRESERVE_KEYS, ApiResult::META_KVP_KEY_NAME,
+                       ApiResult::META_INDEXED_TAG_NAME, ApiResult::META_BC_BOOLS
+               ) );
+
+               // Set metadata appropriately
+               if ( $hash ) {
+                       return array(
+                               ApiResult::META_TYPE => 'kvp',
+                               ApiResult::META_KVP_KEY_NAME => 'key',
+                               ApiResult::META_PRESERVE_KEYS => $keys,
+                               ApiResult::META_BC_BOOLS => $bools,
+                               ApiResult::META_INDEXED_TAG_NAME => 'var',
+                       ) + $vars;
+               } else {
+                       return array(
+                               ApiResult::META_TYPE => 'array',
+                               ApiResult::META_BC_BOOLS => $bools,
+                               ApiResult::META_INDEXED_TAG_NAME => 'value',
+                       ) + $vars;
+               }
+       }
+
        private function setIndexedTagNames( &$array, $mapping ) {
                foreach ( $mapping as $key => $name ) {
                        if ( isset( $array[$key] ) ) {
@@ -738,13 +768,16 @@ class ApiParse extends ApiBase {
                                        'headitems',
                                        'headhtml',
                                        'modules',
+                                       'jsconfigvars',
+                                       'encodedjsconfigvars',
                                        'indicators',
                                        'iwlinks',
                                        'wikitext',
                                        'properties',
                                        'limitreportdata',
                                        'limitreporthtml',
-                               )
+                               ),
+                               ApiBase::PARAM_HELP_MSG_PER_VALUE => array(),
                        ),
                        'pst' => false,
                        'onlypst' => false,