Change API result data structure to be cleaner in new formats
[lhc/web/wiklou.git] / includes / api / ApiParse.php
index 267bced..fc004cf 100644 (file)
@@ -225,21 +225,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 +268,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 +299,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 +340,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';
                        }
                }
 
@@ -366,11 +353,8 @@ class ApiParse extends ApiBase {
                }
 
                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 +362,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 +379,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 +395,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 +573,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 +597,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 +637,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 +659,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;
                }