http://www.mediawiki.org/wiki/Special:Code/MediaWiki/97479

Revision: 97479
Author:   jeroendedauw
Date:     2011-09-19 12:48:17 +0000 (Mon, 19 Sep 2011)
Log Message:
-----------
work on automatic format handling

Modified Paths:
--------------
    trunk/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php
    trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php
    trunk/extensions/SemanticMediaWiki/includes/params/SMW_ParamFormat.php
    trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Auto.php
    trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Table.php

Modified: trunk/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php
===================================================================
--- trunk/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php  
2011-09-19 12:36:58 UTC (rev 97478)
+++ trunk/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php  
2011-09-19 12:48:17 UTC (rev 97479)
@@ -31,13 +31,25 @@
         * @since 1.6.2
         * 
         * @param array $params
+        * @param array $printRequests
         * 
         * @return array
         */
-       public static function getProcessedParams( array $params ) {
+       public static function getProcessedParams( array $params, array 
$printRequests = null ) {
+               $paramDefinitions = self::getParameters();
+               
+               $formatManipulation = new SMWParamFormat();
+               
+               if ( !is_null( $printRequests ) ) {
+                       $formatManipulation->setPrintRequests( $printRequests );
+               }
+               
+               $paramDefinitions['format']->addManipulations( 
$formatManipulation );
+               
                $validator = new Validator();
-               $validator->setParameters( $params, self::getParameters(), 
false );
+               $validator->setParameters( $params, $paramDefinitions, false );
                $validator->validateParameters();
+               
                return $validator->getParameterValues();
        }
        
@@ -182,6 +194,12 @@
         * an array of additional parameters, and an array of additional 
SMWPrintRequest
         * objects, which are filled into call-by-ref parameters.
         * $showmode is true if the input should be treated as if given by #show
+        * 
+        * @param array $rawparams
+        * @param string $querystring
+        * @param array $params
+        * @param array $printouts array of SMWPrintRequest
+        * @param $showmode
         */
        static public function processFunctionParams( array $rawparams, 
&$querystring, &$params, &$printouts, $showmode = false ) {
                global $wgContLang;
@@ -285,7 +303,7 @@
         */
        static public function getResultFromFunctionParams( array $rawparams, 
$outputmode, $context = self::INLINE_QUERY, $showmode = false ) {
                self::processFunctionParams( $rawparams, $querystring, $params, 
$printouts, $showmode );
-               $params = self::getProcessedParams( $params );
+               $params = self::getProcessedParams( $params, $printouts );
                return self::getResultFromQueryString( $querystring, $params, 
$printouts, SMW_OUTPUT_WIKI, $context );
        }
 
@@ -330,8 +348,6 @@
        static public function getResultFromQuery( SMWQuery $query, array 
$params, $extraprintouts, $outputmode, $context = self::INLINE_QUERY, $format = 
'' ) {
                wfProfileIn( 'SMWQueryProcessor::getResultFromQuery (SMW)' );
 
-               //$params = self::getProcessedParams( $params );        
-
                // Query routing allows extensions to provide alternative 
stores as data sources
                // The while feature is experimental and is not properly 
integrated with most of SMW's architecture. For instance, some query printers 
just fetch their own store.
                // @todo FIXME: case-insensitive
@@ -395,7 +411,11 @@
        static public function getResultPrinter( $format, $context = 
self::SPECIAL_PAGE ) {
                global $smwgResultFormats;
 
-               $formatClass = array_key_exists( $format, $smwgResultFormats ) 
? $smwgResultFormats[$format] : 'SMWAutoResultPrinter';
+               if ( !array_key_exists( $format, $smwgResultFormats ) ) {
+                       throw new MWException( "There is no result format for 
'$format'." );
+               }
+               
+               $formatClass = $smwgResultFormats[$format];
 
                return new $formatClass( $format, ( $context != 
self::SPECIAL_PAGE ) );
        }
@@ -449,10 +469,11 @@
                        $allowedFormats += $aliases;
                }
                
+               $allowedFormats[] = 'auto';
+               
                $params['format'] = new Parameter( 'format' );
                $params['format']->setDefault( 'auto' );
                //$params['format']->addCriteria( new CriterionInArray( 
$allowedFormats ) );
-               $params['format']->addManipulations( new SMWParamFormat() );
                
                $params['limit'] = new Parameter( 'limit', 
Parameter::TYPE_INTEGER );
                $params['limit']->setMessage( 'smw_paramdesc_limit' );

Modified: trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php
===================================================================
--- trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php   2011-09-19 
12:36:58 UTC (rev 97478)
+++ trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php   2011-09-19 
12:48:17 UTC (rev 97479)
@@ -199,7 +199,6 @@
        // Printers
        $qpDir = $smwgIP . 'includes/queryprinters/';
        $wgAutoloadClasses['SMWResultPrinter']          = $qpDir . 
'SMW_QueryPrinter.php';
-       $wgAutoloadClasses['SMWAutoResultPrinter']      = $qpDir . 
'SMW_QP_Auto.php';
        $wgAutoloadClasses['SMWTableResultPrinter']     = $qpDir . 
'SMW_QP_Table.php';
        $wgAutoloadClasses['SMWListResultPrinter']      = $qpDir . 
'SMW_QP_List.php';
        $wgAutoloadClasses['SMWCategoryResultPrinter']  = $qpDir . 
'SMW_QP_Category.php';

Modified: trunk/extensions/SemanticMediaWiki/includes/params/SMW_ParamFormat.php
===================================================================
--- trunk/extensions/SemanticMediaWiki/includes/params/SMW_ParamFormat.php      
2011-09-19 12:36:58 UTC (rev 97478)
+++ trunk/extensions/SemanticMediaWiki/includes/params/SMW_ParamFormat.php      
2011-09-19 12:48:17 UTC (rev 97479)
@@ -15,6 +15,17 @@
 class SMWParamFormat extends ItemParameterManipulation {
 
        /**
+        * List of the queries print requests, used to determine the format
+        * when it's not povided. Set with setPrintRequests before passing
+        * to Validator.
+        * 
+        * @since 1.6.2
+        * 
+        * @var array|false
+        */
+       protected $printRequests = null;
+       
+       /**
         * Constructor.
         * 
         * @since 1.6.2
@@ -34,6 +45,11 @@
                
                // Add the formats parameters to the parameter list.
                $queryPrinter = SMWQueryProcessor::getResultPrinter( $value );
+               
+               if ( $queryPrinter instanceof SMWAutoResultPrinter ) {
+                       $queryPrinter->determineFormat();
+               }
+               
                $parameters = array_merge( $parameters, 
$queryPrinter->getValidatorParameters() );
        }
        
@@ -57,7 +73,7 @@
                        $isAlias = self::resolveFormatAliases( $value );
                        
                        if ( !$isAlias ) {
-                               $value = 'auto';  // If it is an unknown 
format, defaults to list/table again
+                               $value = $this->getDefaultFormat();
                        }
                }
                
@@ -89,4 +105,48 @@
                return $isAlias;
        }
        
+       /**
+        * Determines and returns the default format, based on the queries print
+        * requests, if provided.
+        * 
+        * @since 1.6.2
+        * 
+        * @return string Array key in $smwgResultFormats
+        */
+       protected function getDefaultFormat() {
+               if ( is_null( $this->printRequests ) ) {
+                       return 'table';
+               }
+               else {
+                       $format = false;
+                       
+                       /**
+                        * This hook allows extensions to override SMWs 
implementation of default result
+                        * format handling.
+                        * 
+                        * @since 1.5.2
+                        */
+                       wfRunHooks( 'SMWResultFormat', array( &$format, 
$this->printRequests, array() ) );              
+       
+                       // If no default was set by an extension, use a table 
or list, depending on the column count.
+                       if ( $format === false ) {
+                               $format = count( $this->printRequests ) == 0 ? 
'list' : 'table';
+                       }
+                       
+                       return $format;
+               }
+       }
+       
+       /**
+        * Sets the print requests of the query, used for determining
+        * the default format if none is provided.
+        * 
+        * @since 1.6.2
+        * 
+        * @param $printRequests array of SMWPrintRequest
+        */
+       public function setPrintRequests( array /* of SMWPrintRequest */ 
$printRequests ) {
+               $this->printRequests = $printRequests;
+       }
+       
 }

Modified: 
trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Auto.php
===================================================================
--- trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Auto.php   
2011-09-19 12:36:58 UTC (rev 97478)
+++ trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Auto.php   
2011-09-19 12:48:17 UTC (rev 97479)
@@ -19,6 +19,8 @@
  */
 class SMWAutoResultPrinter extends SMWResultPrinter {
 
+       protected $format = false;
+       
        /**
         * @see SMWResultPrinter::getResult
         * 
@@ -29,23 +31,10 @@
         * @return string
         */
        public function getResult( SMWQueryResult $results, array $params, 
$outputmode ) {
-               $format = false;
+               $this->determineFormat( $results, $params );
                
-               /**
-                * This hook allows extensions to override SMWs implementation 
of default result
-                * format handling.
-                * 
-                * @since 1.5.2
-                */
-               wfRunHooks( 'SMWResultFormat', array( &$format, 
$results->getPrintRequests(), $params ) );              
-
-               // If no default was set by an extension, use a table or list, 
depending on the column count.
-               if ( $format === false ) {
-                       $format = $results->getColumnCount() > 1 ? 'table' : 
'list';
-               }
-               
                $printer = SMWQueryProcessor::getResultPrinter(
-                       $format,
+                       $this->format,
                        $this->mInline ? SMWQueryProcessor::INLINE_QUERY : 
SMWQueryProcessor::SPECIAL_PAGE
                );
                
@@ -67,15 +56,42 @@
                return wfMsg( 'smw_printername_auto' );
        }
        
+       /**
+        * (non-PHPdoc)
+        * @see SMWResultPrinter::getParameters()
+        * 
+        * To work correctly as of 1.6.2, you need to call determineFormat 
first. 
+        */
        public function getParameters() {
-               // TODO: this assumes table, which is not correct when it 
should be list
-               
                $printer = SMWQueryProcessor::getResultPrinter(
-                       'table',
+                       $this->format,
                        $this->mInline ? SMWQueryProcessor::INLINE_QUERY : 
SMWQueryProcessor::SPECIAL_PAGE
                );
                
                return $printer->getParameters();
        }
+       
+       /**
+        * Determine the format, based on the result and provided parameters.
+        * 
+        * @since 1.6.2
+        * 
+        * @param SMWQueryResult $results
+        * @param array $params
+        * 
+        * @return string
+        */
+       public function determineFormat( SMWQueryResult $results = null, array 
$params = null ) {
+               if ( $this->format === false ) {
+                       if ( is_null( $results ) || is_null( $params ) ) {
+                               $this->format = 'table';
+                       }
+                       else {
 
+                       }
+               }
+               
+               return $this->format;
+       }
+
 }

Modified: 
trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Table.php
===================================================================
--- trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Table.php  
2011-09-19 12:36:58 UTC (rev 97478)
+++ trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Table.php  
2011-09-19 12:48:17 UTC (rev 97479)
@@ -33,9 +33,7 @@
         */
        protected function handleParameters( array $params, $outputmode ) {
                parent::handleParameters( $params, $outputmode );
-               if ( $params['class'] !== '' ) {
-                       $this->mHTMLClass = $params['class'];
-               }
+               $this->mHTMLClass = $params['class'];
        }
 
        protected function getResultText( SMWQueryResult $res, $outputmode ) {
@@ -92,15 +90,12 @@
                }
                
                // Put the <table> tag around the whole thing
-               // print header
-               $tableClass = "sortable wikitable";
-               if ( !empty( $this->mHTMLClass ) ) {
-                       $tableClass .= ' ' . $this->mHTMLClass;
-               }
-               $tableAttrs = array( 'class' => $tableClass );
+               $tableAttrs = array( 'class' => $this->mHTMLClass );
+               
                if ( $this->mFormat == 'broadtable' ) {
                        $tableAttrs['width'] = '100%';
                }
+               
                $result = Xml::tags( 'table', $tableAttrs, $result );
 
                $this->isHTML = ( $outputmode == SMW_OUTPUT_HTML ); // yes, our 
code can be viewed as HTML if requested, no more parsing needed
@@ -206,7 +201,7 @@
                
                $params['class'] = new Parameter( 'class', 
Parameter::TYPE_STRING );
                $params['class']->setMessage( 'smw-paramdesc-table-class' );
-               $params['class']->setDefault( '' );
+               $params['class']->setDefault( 'sortable wikitable' );
                
                return $params;
        }


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

Reply via email to