https://www.mediawiki.org/wiki/Special:Code/MediaWiki/114699

Revision: 114699
Author:   ning
Date:     2012-04-04 03:54:34 +0000 (Wed, 04 Apr 2012)
Log Message:
-----------
Apply api format for WOM get wiki result

Modified Paths:
--------------
    trunk/extensions/WikiObjectModel/includes/apis/SRF_Xml.php
    trunk/extensions/WikiObjectModel/includes/apis/WOM_GetWikiResult.php

Modified: trunk/extensions/WikiObjectModel/includes/apis/SRF_Xml.php
===================================================================
--- trunk/extensions/WikiObjectModel/includes/apis/SRF_Xml.php  2012-04-04 
01:53:26 UTC (rev 114698)
+++ trunk/extensions/WikiObjectModel/includes/apis/SRF_Xml.php  2012-04-04 
03:54:34 UTC (rev 114699)
@@ -11,29 +11,35 @@
 
 class SRFXml extends SMWResultPrinter {
        protected function getResultText( $res, $outputmode ) {
-               $xml = '';
-               if ( $this->mShowHeaders != SMW_HEADERS_HIDE ) {
-                       $xml .= '<head>' . "\n";
-                       foreach ( $res->getPrintRequests() as $pr ) {
-                               $xml .= "<item>{$pr->getText( $outputmode, null 
)}</item>\n";
+               $heads = array();
+               $first = true;
+               foreach ( $res->getPrintRequests() as $pr ) {
+                       $head = $pr->getText( $outputmode, null );
+                       if ( $first ) {
+                               if ( !$head ) $head = 'mainlabel';
+                               $first = false;
                        }
-                       $xml .= '</head>' . "\n";
+
+                       $heads[] = str_replace( ' ', '_', str_replace( '/', 
'_', $head ) );
                }
 
-               $xml .= '<res>' . "\n";
+               $xml = '<res>' . "\n";
                // print all result rows
                while ( $row = $res->getNext() ) {
                        $xml .= '<row>' . "\n";
                        $firstcol = true;
+                       $idx = 0;
                        foreach ( $row as $field ) {
-                               $xml .= '<item>' . "\n";
-                               $growing = array();
+                               $xml .= "<{$heads[$idx]}>\n";
+//                             $growing = array();
                                while ( ( $object = $field->getNextObject() ) 
!== false ) {
                                        $text = 
Sanitizer::decodeCharReferences( $object->getWikiValue() );
-                                       $growing[] = $text;
+                                       $xml .= "<val>{$text}</val>";
+//                                     $growing[] = $text;
                                } // while...
-                               $xml .= implode( ',', $growing );
-                               $xml .= '</item>' . "\n";
+//                             $xml .= implode( ',', $growing );
+                               $xml .= "</{$heads[$idx]}>\n";
+                               ++ $idx;
                        } // foreach...
                        $xml .= '</row>' . "\n";
                }

Modified: trunk/extensions/WikiObjectModel/includes/apis/WOM_GetWikiResult.php
===================================================================
--- trunk/extensions/WikiObjectModel/includes/apis/WOM_GetWikiResult.php        
2012-04-04 01:53:26 UTC (rev 114698)
+++ trunk/extensions/WikiObjectModel/includes/apis/WOM_GetWikiResult.php        
2012-04-04 03:54:34 UTC (rev 114699)
@@ -18,18 +18,25 @@
                $wiki = $params['wiki'];
                $type = $params['type'];
 
+               $result = array(
+                       'wiki' => array(),
+                       'message' => array(),
+                       'return' => array(),
+               );
+               $this->getResult()->setContent( $result['wiki'], $wiki );
+
                global $wgParser;
                $popt = new ParserOptions();
                $popt->setEditSection( false );
                $title = Title::newFromText( '__TEMPWIKITITLE__' );
                if ( strtolower( $type ) == 'ask' ) {
-                       $wiki = "{$wiki}|format=xml";
+                       $_wiki = "{$wiki}|format=xml";
 
                        global $wgOMIP, $smwgResultFormats, $wgAutoloadClasses;
                        $smwgResultFormats['xml'] = 'SRFXml';
                        $wgAutoloadClasses['SRFXml'] = $wgOMIP . 
'/includes/apis/SRF_Xml.php';
 
-                       $s = $wgParser->preprocess( $wiki, $title, $popt );
+                       $s = $wgParser->preprocess( $_wiki, $title, $popt );
                        $b = 0;
                        for ( $i = 0; $i < strlen( $s ); ++$i ) {
                                if ( $s { $i } == '[' ) {
@@ -42,22 +49,42 @@
                        }
                        $rawparams = array( substr( $s, 0, $i ) );
                        if ( $i < strlen( $s ) ) $rawparams = array_merge( 
$rawparams, explode( '|', substr( $s, $i + 1 ) ) );
-                       $result = 
SMWQueryProcessor::getResultFromFunctionParams( $rawparams, SMW_OUTPUT_WIKI );
+                       $xml = SMWQueryProcessor::getResultFromFunctionParams( 
$rawparams, SMW_OUTPUT_WIKI );
+
+                       $xObj = simplexml_load_string( $xml );
+                       try {
+                               $rows = array();
+                               foreach ( $xObj->xpath( '/res/row' ) as $objs ) 
{
+                                       $row = array();
+                                       foreach ( $objs as $label => $vals ) {
+                                               $vs = array();
+                                               foreach ( $vals as $v ) {
+                                                       $vs[] = strval( $v );
+                                               }
+                               $this->getResult()->setIndexedTagName( $vs, 
'value' );
+                                               $row[$label] = $vs;
+                                       }
+//                                     
$this->getResult()->setIndexedTagName($rows, 'list-item');
+                                       $rows[] = $row;
+                               }
+                   $this->getResult()->setIndexedTagName( $rows, 'item' );
+                   $result['return'] = $rows;
+//                 $this->getResult()->addValue(array($this->getModuleName(), 
'result'), 'items', $rows);
+                       } catch ( Exception $e ) {
+                               $err = $e->getMessage();
+                       }
                } else {
                        $pout = $wgParser->parse( $wiki, $title, $popt );
-                       $result = "<![CDATA[{$pout->getText()}]]>";
+                       $this->getResult()->setContent( $result['return'], 
$pout->getText() );
                }
-
-               header ( "Content-Type: application/rdf+xml" );
-               echo <<<OUTPUT
-<?xml version="1.0" encoding="UTF-8" ?>
-<api><womwiki result="Success">
-<wiki><![CDATA[{$wiki}]]></wiki>
-<return>
-{$result}
-</return></womwiki></api>
-OUTPUT;
-               exit( 1 );
+               if ( isset( $err ) ) {
+                       $result['result'] = 'Failure';
+                       $this->getResult()->setContent( $result['message'], 
$err );
+               } else {
+                       $result['result'] = 'Success';
+                       $this->getResult()->setContent( $result['message'], 'no 
error' );
+               }
+               $this->getResult()->addValue( null, $this->getModuleName(), 
$result );
        }
 
        protected function getAllowedParams() {


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to