Mwjames has uploaded a new change for review. https://gerrit.wikimedia.org/r/78223
Change subject: (Bug 52642) Add query duration as meta data ...................................................................... (Bug 52642) Add query duration as meta data Query duration is designed as opt-in preference (default smwgQueryDurationEnabled = false) while enabling (smwgQueryDurationEnabled = true) requires to run update.php. Change-Id: I0fea5a3f1281752632f0675a7cf8056f5b477685 --- M SemanticMediaWiki.settings.php M includes/Settings.php M includes/Setup.php A includes/Timer.php M includes/dataitems/SMW_DI_Property.php M includes/parserhooks/AskParserFunction.php M includes/parserhooks/ShowParserFunction.php M includes/query/QueryData.php M includes/storage/SQLStore/SMW_SQLStore3.php M languages/SMW_Language.php M languages/SMW_LanguageEn.php M tests/phpunit/SemanticMediaWikiTestCase.php A tests/phpunit/includes/TimerTest.php M tests/phpunit/includes/parserhooks/AskParserFunctionTest.php M tests/phpunit/includes/parserhooks/ShowParserFunctionTest.php M tests/phpunit/includes/query/QueryDataTest.php M tests/phpunit/includes/storage/sqlstore/StatisticsCollectorTest.php 17 files changed, 498 insertions(+), 276 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticMediaWiki refs/changes/23/78223/1 diff --git a/SemanticMediaWiki.settings.php b/SemanticMediaWiki.settings.php index 2fe3da4..6b30d7b 100644 --- a/SemanticMediaWiki.settings.php +++ b/SemanticMediaWiki.settings.php @@ -641,3 +641,18 @@ ## $smwgDeferredPropertyUpdate = false; ## + +### +# Settings related to enable to monitor query meta data. +# +# @note If you change these settings, please ensure to run update.php +# +# - smwgQueryDurationEnabled Enables to record query duration (the time +# between selection and output of a query result) +# +# @since 1.9 +## +$smwgQueryDataUsage = array( + 'smwgQueryDurationEnabled' => false, +); +## diff --git a/includes/Settings.php b/includes/Settings.php index f9078fc..ee5f6ec 100644 --- a/includes/Settings.php +++ b/includes/Settings.php @@ -122,6 +122,7 @@ 'smwgPropertyLowUsageThreshold' => $GLOBALS['smwgPropertyLowUsageThreshold'], 'smwgPropertyZeroCountDisplay' => $GLOBALS['smwgPropertyZeroCountDisplay'], 'smwgDeferredPropertyUpdate' => $GLOBALS['smwgDeferredPropertyUpdate'], + 'smwgQueryDataUsage' => $GLOBALS['smwgQueryDataUsage'] ); if ( self::$instance === null ) { diff --git a/includes/Setup.php b/includes/Setup.php index dd01bef..5bf9a4c 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -172,6 +172,11 @@ $wgAutoloadClasses['SMW\DispatchableSubject'] = $incDir . 'ObservableSubjectDispatcher.php'; $wgAutoloadClasses['SMW\ObservableSubjectDispatcher'] = $incDir . 'ObservableSubjectDispatcher.php'; + $wgAutoloadClasses['SMW\Settings'] = $incDir . 'Settings.php'; + $wgAutoloadClasses['SMW\NamespaceExaminer'] = $incDir . 'NamespaceExaminer.php'; + $wgAutoloadClasses['SMW\Profiler'] = $incDir . 'Profiler.php'; + $wgAutoloadClasses['SMW\Timer'] = $incDir . 'Timer.php'; + $wgAutoloadClasses['SMW\Cacheable'] = $incDir . 'Cacheable.php'; $wgAutoloadClasses['SMW\Configurable'] = $incDir . 'Configurable.php'; $wgAutoloadClasses['SMW\StoreAccess'] = $incDir . 'StoreAccess.php'; diff --git a/includes/Timer.php b/includes/Timer.php new file mode 100644 index 0000000..47c0cb7 --- /dev/null +++ b/includes/Timer.php @@ -0,0 +1,46 @@ +<?php + +namespace SMW; + +/** + * Simple timer class to keep track of start/end time + * + * @file + * + * @license GNU GPL v2+ + * @since 1.9 + * + * @author mwjames + */ + +/** + * Timer class handling + * + * @ingroup SMW + */ +class Timer { + + /** @var integer */ + private $start; + + /** + * @since 1.9 + * + * @param boolean $start whether or not to record the start time + */ + public function __construct( $start = true ) { + $this->start = $start ? microtime( true ) : 0; + } + + /** + * Returns duration + * + * @since 1.9 + * + * @return integer + */ + public function getDuration() { + return $this->start > 0 ? microtime( true ) - $this->start : 0; + } + +} diff --git a/includes/dataitems/SMW_DI_Property.php b/includes/dataitems/SMW_DI_Property.php index ca74d61..2f32907 100644 --- a/includes/dataitems/SMW_DI_Property.php +++ b/includes/dataitems/SMW_DI_Property.php @@ -404,6 +404,7 @@ '_ASKST' => array( '_cod', true ), // "has query string" '_ASKFO' => array( '_txt', true ), // "has query format" '_ASKSI' => array( '_num', true ), // "has query size" + '_ASKDU' => array( '_num', true ), // "has query duration" '_ASKDE' => array( '_num', true ), // "has query depth" ); diff --git a/includes/parserhooks/AskParserFunction.php b/includes/parserhooks/AskParserFunction.php index 15ec0d2..eb0e19c 100644 --- a/includes/parserhooks/AskParserFunction.php +++ b/includes/parserhooks/AskParserFunction.php @@ -36,6 +36,9 @@ /** @var MessageFormatter */ protected $msgFormatter; + /** @var Settings */ + protected $settings; + /** @var boolean */ protected $showMode = false; @@ -46,10 +49,11 @@ * @param QueryData $queryData * @param MessageFormatter $msgFormatter */ - public function __construct( IParserData $parserData, QueryData $queryData, MessageFormatter $msgFormatter ) { + public function __construct( IParserData $parserData, QueryData $queryData, MessageFormatter $msgFormatter, Settings $settings ) { $this->parserData = $parserData; $this->queryData = $queryData; $this->msgFormatter = $msgFormatter; + $this->settings = $settings; } /** @@ -124,13 +128,21 @@ array_shift( $rawParams ); } + // FIXME Use dependency injection for settings + $timer = new Timer( $this->settings->get( 'smwgQueryDurationEnabled' ) ); $this->initQueryProcessor( $rawParams ); // Add query data from the query // Suppose the the query returns with an error, right now we store // the query itself even though it returned with unqualified data $this->queryData->setQueryId( new HashIdGenerator( $rawParams ) ); - $this->queryData->add( $this->query, $this->params ); + $this->queryData->add( new ArrayAccessor( array( + 'queryString' => $this->query->getDescription()->getQueryString(), + 'querySize' => $this->query->getDescription()->getSize(), + 'queryDepth' => $this->query->getDescription()->getDepth(), + 'queryFormat' => $this->params['format']->getValue(), + 'queryDuration' => $timer->getDuration() + ) ) ); // Store query data to the semantic data instance $this->parserData->getData()->addPropertyObjectValue( @@ -154,11 +166,15 @@ * @return string */ public static function render( Parser &$parser ) { + $settings = Settings::newFromGlobals(); + $ask = new self( new ParserData( $parser->getTitle(), $parser->getOutput() ), new QueryData( $parser->getTitle() ), - new MessageFormatter( $parser->getTargetLanguage() ) + new MessageFormatter( $parser->getTargetLanguage() ), + $settings ); - return $GLOBALS['smwgQEnabled'] ? $ask->parse( func_get_args() ) : $ask->disabled(); + + return $settings->get( 'smwgQEnabled' ) ? $ask->parse( func_get_args() ) : $ask->disabled(); } } diff --git a/includes/parserhooks/ShowParserFunction.php b/includes/parserhooks/ShowParserFunction.php index 3da62c6..5cfc77e 100644 --- a/includes/parserhooks/ShowParserFunction.php +++ b/includes/parserhooks/ShowParserFunction.php @@ -33,6 +33,9 @@ /** @var MessageFormatter */ protected $msgFormatter; + /** @var Settings */ + protected $settings; + /** * @since 1.9 * @@ -40,10 +43,11 @@ * @param QueryData $queryData * @param MessageFormatter $messageList */ - public function __construct( IParserData $parserData, QueryData $queryData, MessageFormatter $msgFormatter ) { + public function __construct( IParserData $parserData, QueryData $queryData, MessageFormatter $msgFormatter, Settings $settings ) { $this->parserData = $parserData; $this->queryData = $queryData; $this->msgFormatter = $msgFormatter; + $this->settings = $settings; } /** @@ -73,7 +77,7 @@ * @return string|null */ public function parse( array $rawParams ) { - $ask = new AskParserFunction( $this->parserData, $this->queryData, $this->msgFormatter ); + $ask = new AskParserFunction( $this->parserData, $this->queryData, $this->msgFormatter, $this->settings ); return $ask->useShowMode()->parse( $rawParams ); } @@ -87,11 +91,15 @@ * @return string */ public static function render( Parser &$parser ) { + $settings = Settings::newFromGlobals(); + $show = new self( new ParserData( $parser->getTitle(), $parser->getOutput() ), new QueryData( $parser->getTitle() ), - new MessageFormatter( $parser->getTargetLanguage() ) + new MessageFormatter( $parser->getTargetLanguage() ), + $settings ); - return $GLOBALS['smwgQEnabled'] ? $show->parse( func_get_args() ) : $show->disabled(); + + return $settings->get( 'smwgQEnabled' ) ? $show->parse( func_get_args() ) : $show->disabled(); } } diff --git a/includes/query/QueryData.php b/includes/query/QueryData.php index 8afb800..2121f22 100644 --- a/includes/query/QueryData.php +++ b/includes/query/QueryData.php @@ -94,7 +94,7 @@ * * @return array */ - public function add( SMWQuery $query, array $params ) { + public function add( ArrayAccessor $queryData ) { if ( $this->queryId === null ) { throw new UnknownIdException( '_QUERY Id is not set' ); @@ -103,27 +103,38 @@ // Prepare subobject semantic container $this->subobject->setSemanticData( $this->queryId ); - $description = $query->getDescription(); - // Add query string - $propertyDi = new DIProperty( '_ASKST' ); - $valueDi = new SMWDIBlob( $description->getQueryString() ); - $this->subobject->getSemanticData()->addPropertyObjectValue( $propertyDi, $valueDi ); + $this->subobject->getSemanticData()->addPropertyObjectValue( + new DIProperty( '_ASKST' ), + new SMWDIBlob( $queryData->get( 'queryString' ) ) + ); // Add query size - $propertyDi = new DIProperty( '_ASKSI' ); - $valueDi = new SMWDINumber( $description->getSize() ); - $this->subobject->getSemanticData()->addPropertyObjectValue( $propertyDi, $valueDi ); + $this->subobject->getSemanticData()->addPropertyObjectValue( + new DIProperty( '_ASKSI' ), + new SMWDINumber( $queryData->get( 'querySize' ) ) + ); + + // Add query duration + if ( $queryData->has( 'queryDuration' ) && $queryData->get( 'queryDuration' ) > 0 ) { + $this->subobject->getSemanticData()->addPropertyObjectValue( + new DIProperty( '_ASKDU' ), + new SMWDINumber( $queryData->get( 'queryDuration' ) ) + ); + } // Add query depth - $propertyDi = new DIProperty( '_ASKDE' ); - $valueDi = new SMWDINumber( $description->getDepth() ); - $this->subobject->getSemanticData()->addPropertyObjectValue( $propertyDi, $valueDi ); + $this->subobject->getSemanticData()->addPropertyObjectValue( + new DIProperty( '_ASKDE' ), + new SMWDINumber( $queryData->get( 'queryDepth' ) ) + ); // Add query format - $propertyDi = new DIProperty( '_ASKFO' ); - $valueDi = new SMWDIBlob( $params['format']->getValue() ); - $this->subobject->getSemanticData()->addPropertyObjectValue( $propertyDi, $valueDi ); + $this->subobject->getSemanticData()->addPropertyObjectValue( + new DIProperty( '_ASKFO' ), + new SMWDIBlob( $queryData->get( 'queryFormat' ) ) + ); + } } \ No newline at end of file diff --git a/includes/storage/SQLStore/SMW_SQLStore3.php b/includes/storage/SQLStore/SMW_SQLStore3.php index 4320afa..44bc0df 100644 --- a/includes/storage/SQLStore/SMW_SQLStore3.php +++ b/includes/storage/SQLStore/SMW_SQLStore3.php @@ -157,7 +157,7 @@ // property declarations '_TYPE', '_UNIT', '_CONV', '_PVAL', '_LIST', '_SERV', // query statistics (very frequently used) - '_ASK', '_ASKDE', '_ASKSI', '_ASKFO', '_ASKST', + '_ASK', '_ASKDE', '_ASKSI', '_ASKFO', '_ASKST', '_ASKDU', // subproperties, classes, and instances '_SUBP', '_SUBC', '_INST', // redirects diff --git a/languages/SMW_Language.php b/languages/SMW_Language.php index b76a857..a66fc2d 100644 --- a/languages/SMW_Language.php +++ b/languages/SMW_Language.php @@ -90,6 +90,7 @@ 'Has query string' => '_ASKST', 'Has query format' => '_ASKFO', 'Has query size' => '_ASKSI', + 'Has query duration' => '_ASKDU', 'Has query depth' => '_ASKDE', ); diff --git a/languages/SMW_LanguageEn.php b/languages/SMW_LanguageEn.php index 8e08df5..2b1e7f7 100644 --- a/languages/SMW_LanguageEn.php +++ b/languages/SMW_LanguageEn.php @@ -76,6 +76,7 @@ '_ASKST'=> 'Query string', '_ASKFO'=> 'Query format', '_ASKSI'=> 'Query size', + '_ASKDU'=> 'Query duration', '_ASKDE'=> 'Query depth', ); diff --git a/tests/phpunit/SemanticMediaWikiTestCase.php b/tests/phpunit/SemanticMediaWikiTestCase.php index 842e9c3..6e2cfdb 100644 --- a/tests/phpunit/SemanticMediaWikiTestCase.php +++ b/tests/phpunit/SemanticMediaWikiTestCase.php @@ -274,20 +274,21 @@ } // Assert property values - foreach ( $semanticData->getPropertyValues( $diproperty ) as $dataItem ){ - $dataValue = DataValueFactory::newDataItemValue( $dataItem, $diproperty ); - $DItype = $dataValue->getDataItem()->getDIType(); + if ( isset( $expected['propertyValue']) ){ + foreach ( $semanticData->getPropertyValues( $diproperty ) as $dataItem ){ + $dataValue = DataValueFactory::newDataItemValue( $dataItem, $diproperty ); + $DItype = $dataValue->getDataItem()->getDIType(); - if ( $DItype === SMWDataItem::TYPE_WIKIPAGE ){ - $this->assertContains( $dataValue->getWikiValue(), $expected['propertyValue'] ); - } else if ( $DItype === SMWDataItem::TYPE_NUMBER ){ - $this->assertContains( $dataValue->getNumber(), $expected['propertyValue'] ); - } else if ( $DItype === SMWDataItem::TYPE_TIME ){ - $this->assertContains( $dataValue->getISO8601Date(), $expected['propertyValue'] ); - } else if ( $DItype === SMWDataItem::TYPE_BLOB ){ - $this->assertContains( $dataValue->getWikiValue(), $expected['propertyValue'] ); + if ( $DItype === SMWDataItem::TYPE_WIKIPAGE ){ + $this->assertContains( $dataValue->getWikiValue(), $expected['propertyValue'] ); + } else if ( $DItype === SMWDataItem::TYPE_NUMBER ){ + $this->assertContains( $dataValue->getNumber(), $expected['propertyValue'] ); + } else if ( $DItype === SMWDataItem::TYPE_TIME ){ + $this->assertContains( $dataValue->getISO8601Date(), $expected['propertyValue'] ); + } else if ( $DItype === SMWDataItem::TYPE_BLOB ){ + $this->assertContains( $dataValue->getWikiValue(), $expected['propertyValue'] ); + } } - } } } diff --git a/tests/phpunit/includes/TimerTest.php b/tests/phpunit/includes/TimerTest.php new file mode 100644 index 0000000..585cc61 --- /dev/null +++ b/tests/phpunit/includes/TimerTest.php @@ -0,0 +1,63 @@ +<?php + +namespace SMW\Test; + +use SMW\Timer; + +/** + * Tests for the Timer class + * + * @file + * + * @license GNU GPL v2+ + * @since 1.9 + * + * @author mwjames + */ + +/** + * @covers \SMW\Timer + * + * @ingroup SMW + * + * @group SMW + * @group SMWExtension + */ +class TimerTest extends SemanticMediaWikiTestCase { + + /** + * Returns the name of the class to be tested + * + * @return string|false + */ + public function getClass() { + return '\SMW\Timer'; + } + + /** + * @test Timer::__construct + * + * @since 1.9 + */ + public function testConstructor() { + $this->assertInstanceOf( $this->getClass(), new Timer() ); + } + + /** + * @test Timer::__construct + * + * @since 1.9 + */ + public function testTimer() { + + $timer = new Timer(); + + $this->assertInternalType( 'float', $timer->getDuration() ); + $this->assertGreaterThan( 0, $timer->getDuration() ); + + $timer = new Timer( false ); + $this->assertEquals( 0, $timer->getDuration() ); + + } + +} diff --git a/tests/phpunit/includes/parserhooks/AskParserFunctionTest.php b/tests/phpunit/includes/parserhooks/AskParserFunctionTest.php index c88e617..5e74c57 100644 --- a/tests/phpunit/includes/parserhooks/AskParserFunctionTest.php +++ b/tests/phpunit/includes/parserhooks/AskParserFunctionTest.php @@ -3,10 +3,10 @@ namespace SMW\Test; use SMW\AskParserFunction; - use SMW\MessageFormatter; use SMW\ParserData; use SMW\QueryData; +use SMW\Settings; use Title; use ParserOutput; @@ -43,118 +43,6 @@ } /** - * Provides sample data usually found in {{#ask}} queries - * - * @return array - */ - public function getDataProvider() { - return array( - // #0 - // {{#ask: [[Modification date::+]] - // |?Modification date - // |format=list - // }} - array( - array( - '[[Modification date::+]]', - '?Modification date', - 'format=list' - ), - array( - 'result' => false, - 'propertyCount' => 4, - 'propertyKey' => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO' ), - 'propertyValue' => array( 'list', 1, 1, '[[Modification date::+]]' ) - ) - ), - - // #1 Query string with spaces - // {{#ask: [[Modification date::+]] [[Category:Foo bar]] [[Has title::!Foo bar]] - // |?Modification date - // |?Has title - // |format=list - // }} - array( - array( - '[[Modification date::+]] [[Category:Foo bar]] [[Has title::!Foo bar]]', - '?Modification date', - '?Has title', - 'format=list' - ), - array( - 'result' => false, - 'propertyCount' => 4, - 'propertyKey' => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO' ), - 'propertyValue' => array( 'list', 4, 1, '[[Modification date::+]] [[Category:Foo bar]] [[Has title::!Foo bar]]' ) - ) - ), - - // #2 - // {{#ask: [[Modification date::+]][[Category:Foo]] - // |?Modification date - // |?Has title - // |format=list - // }} - array( - array( - '[[Modification date::+]][[Category:Foo]]', - '?Modification date', - '?Has title', - 'format=list' - ), - array( - 'result' => false, - 'propertyCount' => 4, - 'propertyKey' => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO' ), - 'propertyValue' => array( 'list', 2, 1, '[[Modification date::+]] [[Category:Foo]]' ) - ) - ), - - // #3 Known format - // {{#ask: [[File:Fooo]] - // |?Modification date - // |default=no results - // |format=feed - // }} - array( - array( - '[[File:Fooo]]', - '?Modification date', - 'default=no results', - 'format=feed' - ), - array( - 'result' => false, - 'propertyCount' => 4, - 'propertyKey' => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO' ), - 'propertyValue' => array( 'feed', 1, 1, '[[:File:Fooo]]' ) - ) - ), - - // #4 Unknown format, default table - // {{#ask: [[Modification date::+]][[Category:Foo]] - // |?Modification date - // |?Has title - // |format=bar - // }} - array( - array( - '[[Modification date::+]][[Category:Foo]]', - '?Modification date', - '?Has title', - 'format=lula' - ), - array( - 'result' => false, - 'propertyCount' => 4, - 'propertyKey' => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO' ), - 'propertyValue' => array( 'table', 2, 1, '[[Modification date::+]] [[Category:Foo]]' ) - ) - ), - ); - } - - /** * Helper method that returns a AskParserFunction object * * @since 1.9 @@ -164,11 +52,19 @@ * * @return AskParserFunction */ - private function getInstance( Title $title, ParserOutput $parserOutput = null ) { + private function getInstance( Title $title, ParserOutput $parserOutput = null, Settings $settings = null ) { + + if ( $settings === null ) { + $settings = $this->newSettings( array( + 'smwgQueryDurationEnabled' => false + ) ); + } + return new AskParserFunction( $this->getParserData( $title, $parserOutput ), new QueryData( $title ), - new MessageFormatter( $title->getPageLanguage() ) + new MessageFormatter( $title->getPageLanguage() ), + $settings ); } @@ -178,23 +74,13 @@ * @since 1.9 */ public function testConstructor() { - $instance = $this->getInstance( $this->getTitle(), $this->getParserOutput() ); + $instance = $this->getInstance( $this->newTitle(), $this->newParserOutput() ); $this->assertInstanceOf( $this->getClass(), $instance ); } /** - * @test AskParserFunction::__construct - * - * @since 1.9 - */ - public function testConstructorException() { - $this->setExpectedException( 'PHPUnit_Framework_Error' ); - $instance = new $this->getInstance( $this->getTitle() ); - } - - /** * @test AskParserFunction::parse - * @dataProvider getDataProvider + * @dataProvider queryDataProvider * * @since 1.9 * @@ -202,9 +88,11 @@ * @param array $expected */ public function testParse( array $params, array $expected ) { - $instance = $this->getInstance( $this->getTitle(), $this->getParserOutput() ); - $result = $instance->parse( $params ); + + $instance = $this->getInstance( $this->newTitle(), $this->newParserOutput() ); + $result = $instance->parse( $params ); $this->assertInternalType( 'string', $result ); + } /** @@ -213,11 +101,12 @@ * @since 1.9 */ public function testParseDisabledsmwgQEnabled() { - $title = $this->getTitle(); - $message = new MessageFormatter( $title->getPageLanguage() ); + + $title = $this->newTitle(); + $message = new MessageFormatter( $title->getPageLanguage() ); $expected = $message->addFromKey( 'smw_iq_disabled' )->getHtml(); - $instance = $this->getInstance( $title , $this->getParserOutput() ); + $instance = $this->getInstance( $title , $this->newParserOutput() ); // Make protected method accessible $reflection = new ReflectionClass( $this->getClass() ); @@ -226,23 +115,25 @@ $result = $method->invoke( $instance ); $this->assertEquals( $expected , $result ); + } /** * @test AskParserFunction::parse - * @dataProvider getDataProvider + * @dataProvider queryDataProvider * * @since 1.9 * * @param array $params * @param array $expected */ - public function testInstantiatedQueryData( array $params, array $expected ) { - $parserOutput = $this->getParserOutput(); - $title = $this->getTitle(); + public function testInstantiatedQueryData( array $params, array $expected, array $settings ) { + + $parserOutput = $this->newParserOutput(); + $title = $this->newTitle(); // Initialize and parse - $instance = $this->getInstance( $title, $parserOutput ); + $instance = $this->getInstance( $title, $parserOutput, $this->newSettings( $settings ) ); $instance->parse( $params ); // Get semantic data from the ParserOutput @@ -256,6 +147,7 @@ $this->assertInstanceOf( 'SMWContainerSemanticData', $containerSemanticData ); $this->assertSemanticData( $containerSemanticData, $expected ); } + } /** @@ -268,4 +160,147 @@ $result = AskParserFunction::render( $parser ); $this->assertInternalType( 'string', $result ); } + + /** + * Provides sample data usually found in {{#ask}} queries + * + * @return array + */ + public function queryDataProvider() { + + $provider = array(); + + // #0 + // {{#ask: [[Modification date::+]] + // |?Modification date + // |format=list + // }} + $provider[] = array( + array( + '[[Modification date::+]]', + '?Modification date', + 'format=list' + ), + array( + 'propertyCount' => 4, + 'propertyKey' => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO' ), + 'propertyValue' => array( 'list', 1, 1, '[[Modification date::+]]' ) + ), + array( + 'smwgQueryDurationEnabled' => false + ) + ); + + // #1 Query string with spaces + // {{#ask: [[Modification date::+]] [[Category:Foo bar]] [[Has title::!Foo bar]] + // |?Modification date + // |?Has title + // |format=list + // }} + $provider[] = array( + array( + '[[Modification date::+]] [[Category:Foo bar]] [[Has title::!Foo bar]]', + '?Modification date', + '?Has title', + 'format=list' + ), + array( + 'propertyCount' => 4, + 'propertyKey' => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO' ), + 'propertyValue' => array( 'list', 4, 1, '[[Modification date::+]] [[Category:Foo bar]] [[Has title::!Foo bar]]' ) + ), + array( + 'smwgQueryDurationEnabled' => false + ) + ); + + // #2 + // {{#ask: [[Modification date::+]][[Category:Foo]] + // |?Modification date + // |?Has title + // |format=list + // }} + $provider[] = array( + array( + '[[Modification date::+]][[Category:Foo]]', + '?Modification date', + '?Has title', + 'format=list' + ), + array( + 'propertyCount' => 4, + 'propertyKey' => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO' ), + 'propertyValue' => array( 'list', 2, 1, '[[Modification date::+]] [[Category:Foo]]' ) + ), + array( + 'smwgQueryDurationEnabled' => false + ) + ); + + // #3 Known format + // {{#ask: [[File:Fooo]] + // |?Modification date + // |default=no results + // |format=feed + // }} + $provider[] = array( + array( + '[[File:Fooo]]', + '?Modification date', + 'default=no results', + 'format=feed' + ), + array( + 'propertyCount' => 4, + 'propertyKey' => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO' ), + 'propertyValue' => array( 'feed', 1, 1, '[[:File:Fooo]]' ) + ), + array( + 'smwgQueryDurationEnabled' => false + ) + ); + + // #4 Unknown format, default table + // {{#ask: [[Modification date::+]][[Category:Foo]] + // |?Modification date + // |?Has title + // |format=bar + // }} + $provider[] = array( + array( + '[[Modification date::+]][[Category:Foo]]', + '?Modification date', + '?Has title', + 'format=lula' + ), + array( + 'propertyCount' => 4, + 'propertyKey' => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO' ), + 'propertyValue' => array( 'table', 2, 1, '[[Modification date::+]] [[Category:Foo]]' ) + ), + array( + 'smwgQueryDurationEnabled' => false + ) + ); + + // #5, Query duration enabled + $provider[] = array( + array( + '[[Modification date::+]][[Category:Foo]]', + '?Modification date', + '?Has title', + 'format=list' + ), + array( + 'propertyCount' => 5, + 'propertyKey' => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO', '_ASKDU' ), + ), + array( + 'smwgQueryDurationEnabled' => true + ) + ); + + return $provider; + } + } diff --git a/tests/phpunit/includes/parserhooks/ShowParserFunctionTest.php b/tests/phpunit/includes/parserhooks/ShowParserFunctionTest.php index b16d1c8..12080f0 100644 --- a/tests/phpunit/includes/parserhooks/ShowParserFunctionTest.php +++ b/tests/phpunit/includes/parserhooks/ShowParserFunctionTest.php @@ -3,8 +3,8 @@ namespace SMW\Test; use SMW\ShowParserFunction; -use SMW\QueryData; use SMW\MessageFormatter; +use SMW\QueryData; use Title; use ParserOutput; @@ -41,76 +41,6 @@ } /** - * Provides data sample normally found in connection with the {{#show}} - * parser function. The first array contains parametrized input value while - * the second array contains expected return results for the instantiated - * object. - * - * @return array - */ - public function getDataProvider() { - return array( - - // #0 - // {{#show: Foo - // |?Modification date - // }} - array( - array( - 'Foo', - '?Modification date', - ), - array( - 'output' => '', - 'propertyCount' => 4, - 'propertyKey' => array( '_ASKFO', '_ASKDE', '_ASKSI', '_ASKST' ), - 'propertyValue' => array( 'list', 0, 1, '[[:Foo]]' ) - ) - ), - - // #1 - // {{#show: Help:Bar - // |?Modification date - // |default=no results - // }} - array( - array( - 'Help:Bar', - '?Modification date', - 'default=no results' - ), - array( - 'output' => 'no results', - 'propertyCount' => 4, - 'propertyKey' => array( '_ASKFO', '_ASKDE', '_ASKSI', '_ASKST' ), - 'propertyValue' => array( 'list', 0, 1, '[[:Help:Bar]]' ) - ) - ), - - // #2 [[..]] is not acknowledged therefore displays an error message - // {{#show: [[File:Fooo]] - // |?Modification date - // |default=no results - // |format=table - // }} - array( - array( - '[[File:Fooo]]', - '?Modification date', - 'default=no results', - 'format=table' - ), - array( - 'output' => 'class="smwtticon warning"', // lazy content check for the error - 'propertyCount' => 4, - 'propertyKey' => array( '_ASKFO', '_ASKDE', '_ASKSI', '_ASKST' ), - 'propertyValue' => array( 'table', 0, 1, '[[:]]' ) - ) - ) - ); - } - - /** * Helper method that returns a ShowParserFunction object * * @since 1.9 @@ -121,10 +51,16 @@ * @return ShowParserFunction */ private function getInstance( Title $title, ParserOutput $parserOutput = null ) { + + $settings = $this->newSettings( array( + 'smwgQueryDurationEnabled' => false + ) ); + return new ShowParserFunction( $this->getParserData( $title, $parserOutput ), new QueryData( $title ), - new MessageFormatter( $title->getPageLanguage() ) + new MessageFormatter( $title->getPageLanguage() ), + $settings ); } @@ -134,7 +70,7 @@ * @since 1.9 */ public function testConstructor() { - $instance = $this->getInstance( $this->getTitle(), $this->getParserOutput() ); + $instance = $this->getInstance( $this->newTitle(), $this->newParserOutput() ); $this->assertInstanceOf( $this->getClass(), $instance ); } @@ -158,14 +94,16 @@ * @param array $expected */ public function testParse( array $params, array $expected ) { - $instance = $this->getInstance( $this->getTitle(), $this->getParserOutput() ); - $result = $instance->parse( $params, true ); + + $instance = $this->getInstance( $this->newTitle(), $this->newParserOutput() ); + $result = $instance->parse( $params, true ); if ( $expected['output'] === '' ) { $this->assertEmpty( $result ); } else { $this->assertContains( $expected['output'], $result ); } + } /** @@ -175,8 +113,9 @@ * @since 1.9 */ public function testParseDisabledsmwgQEnabled() { - $title = $this->getTitle(); - $message = new MessageFormatter( $title->getPageLanguage() ); + + $title = $this->newTitle(); + $message = new MessageFormatter( $title->getPageLanguage() ); $expected = $message->addFromKey( 'smw_iq_disabled' )->getHtml(); $instance = $this->getInstance( $title, $this->getParserOutput() ); @@ -200,8 +139,9 @@ * @param array $expected */ public function testInstantiatedQueryData( array $params, array $expected ) { - $parserOutput = $this->getParserOutput(); - $title = $this->getTitle(); + + $parserOutput = $this->newParserOutput(); + $title = $this->newTitle(); // Initialize and parse $instance = $this->getInstance( $title, $parserOutput ); @@ -218,6 +158,7 @@ $this->assertInstanceOf( 'SMWContainerSemanticData', $containerSemanticData ); $this->assertSemanticData( $containerSemanticData, $expected ); } + } /** @@ -226,8 +167,80 @@ * @since 1.9 */ public function testStaticRender() { - $parser = $this->getParser( $this->getTitle(), $this->getUser() ); + $parser = $this->getParser( $this->newTitle(), $this->getUser() ); $result = ShowParserFunction::render( $parser ); $this->assertInternalType( 'string', $result ); } + + /** + * Provides data sample normally found in connection with the {{#show}} + * parser function. The first array contains parametrized input value while + * the second array contains expected return results for the instantiated + * object. + * + * @return array + */ + public function getDataProvider() { + + $provider = array(); + + // #0 + // {{#show: Foo + // |?Modification date + // }} + $provider[] = array( + array( + 'Foo', + '?Modification date', + ), + array( + 'output' => '', + 'propertyCount' => 4, + 'propertyKey' => array( '_ASKFO', '_ASKDE', '_ASKSI', '_ASKST' ), + 'propertyValue' => array( 'list', 0, 1, '[[:Foo]]' ) + ) + ); + + // #1 + // {{#show: Help:Bar + // |?Modification date + // |default=no results + // }} + $provider[] = array( + array( + 'Help:Bar', + '?Modification date', + 'default=no results' + ), + array( + 'output' => 'no results', + 'propertyCount' => 4, + 'propertyKey' => array( '_ASKFO', '_ASKDE', '_ASKSI', '_ASKST' ), + 'propertyValue' => array( 'list', 0, 1, '[[:Help:Bar]]' ) + ) + ); + + // #2 [[..]] is not acknowledged therefore displays an error message + // {{#show: [[File:Fooo]] + // |?Modification date + // |default=no results + // |format=table + // }} + $provider[] = array( + array( + '[[File:Fooo]]', + '?Modification date', + 'default=no results', + 'format=table' + ), + array( + 'output' => 'class="smwtticon warning"', // lazy content check for the error + 'propertyCount' => 4, + 'propertyKey' => array( '_ASKFO', '_ASKDE', '_ASKSI', '_ASKST' ), + 'propertyValue' => array( 'table', 0, 1, '[[:]]' ) + ) + ); + + return $provider; + } } diff --git a/tests/phpunit/includes/query/QueryDataTest.php b/tests/phpunit/includes/query/QueryDataTest.php index fa2a69a..e7a23cc 100644 --- a/tests/phpunit/includes/query/QueryDataTest.php +++ b/tests/phpunit/includes/query/QueryDataTest.php @@ -3,6 +3,7 @@ namespace SMW\Test; use SMW\HashIdGenerator; +use SMW\ArrayAccessor; use SMW\QueryData; use SMWQueryProcessor; @@ -62,7 +63,7 @@ * @return QueryData */ private function getInstance( Title $title = null ) { - return new QueryData( $title ); + return new QueryData( $title === null ? $this->newTitle() : $title ); } /** @@ -71,8 +72,7 @@ * @since 1.9 */ public function testConstructor() { - $instance = $this->getInstance( $this->getTitle() ); - $this->assertInstanceOf( $this->getClass(), $instance ); + $this->assertInstanceOf( $this->getClass(), $this->getInstance() ); } /** @@ -81,8 +81,7 @@ * @since 1.9 */ public function testGetProperty() { - $instance = $this->getInstance( $this->getTitle() ); - $this->assertInstanceOf( '\SMWDIProperty', $instance->getProperty() ); + $this->assertInstanceOf( '\SMWDIProperty', $this->getInstance()->getProperty() ); } /** @@ -91,8 +90,7 @@ * @since 1.9 */ public function testGetErrors() { - $instance = $this->getInstance( $this->getTitle() ); - $this->assertInternalType( 'array', $instance->getErrors() ); + $this->assertInternalType( 'array', $this->getInstance()->getErrors() ); } /** @@ -110,7 +108,15 @@ list( $query, $formattedParams ) = $this->getQueryProcessor( $params ); $instance->setQueryId( new HashIdGenerator( $params ) ); - $instance->add( $query, $formattedParams ); + $queryData = new ArrayAccessor( array( + 'queryString' => $query->getDescription()->getQueryString(), + 'querySize' => $query->getDescription()->getSize(), + 'queryDepth' => $query->getDescription()->getDepth(), + 'queryFormat' => $formattedParams['format']->getValue(), + 'queryDuration' => 0.9001 + ) ); + + $instance->add( $queryData ); // Check the returned instance $this->assertInstanceOf( '\SMW\SemanticData', $instance->getContainer()->getSemanticData() ); @@ -130,11 +136,9 @@ public function testQueryIdException( array $params, array $expected ) { $this->setExpectedException( '\SMW\UnknownIdException' ); - $title = $this->getTitle(); - $instance = $this->getInstance( $title ); - list( $query, $formattedParams ) = $this->getQueryProcessor( $params ); - $instance->add( $query, $formattedParams ); + $instance = $this->getInstance(); + $instance->add( new ArrayAccessor() ); } @@ -162,9 +166,9 @@ 'format=list' ), array( - 'propertyCount' => 4, - 'propertyKey' => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO' ), - 'propertyValue' => array( 'list', 1, 1, '[[Modification date::+]]' ) + 'propertyCount' => 5, + 'propertyKey' => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO', '_ASKDU' ), + 'propertyValue' => array( 'list', 1, 1, '[[Modification date::+]]', 0.9001 ) ) ); @@ -183,9 +187,9 @@ 'format=list' ), array( - 'propertyCount' => 4, - 'propertyKey' => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO' ), - 'propertyValue' => array( 'list', 2, 1, '[[Modification date::+]] [[Category:Foo]]' ) + 'propertyCount' => 5, + 'propertyKey' => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO', '_ASKDU' ), + 'propertyValue' => array( 'list', 2, 1, '[[Modification date::+]] [[Category:Foo]]', 0.9001 ) ) ); @@ -204,9 +208,9 @@ 'format=bar' ), array( - 'propertyCount' => 4, - 'propertyKey' => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO' ), - 'propertyValue' => array( 'table', 2, 1, '[[Modification date::+]] [[Category:Foo]]' ) + 'propertyCount' => 5, + 'propertyKey' => array( '_ASKST', '_ASKSI', '_ASKDE', '_ASKFO', '_ASKDU' ), + 'propertyValue' => array( 'table', 2, 1, '[[Modification date::+]] [[Category:Foo]]', 0.9001 ) ) ); diff --git a/tests/phpunit/includes/storage/sqlstore/StatisticsCollectorTest.php b/tests/phpunit/includes/storage/sqlstore/StatisticsCollectorTest.php index a31b0f8..27b5f1d 100644 --- a/tests/phpunit/includes/storage/sqlstore/StatisticsCollectorTest.php +++ b/tests/phpunit/includes/storage/sqlstore/StatisticsCollectorTest.php @@ -140,9 +140,10 @@ * @param $segment * @param $expectedType */ - public function testGetResults( $segment, $expectedType ) { + public function testResultsOnStore( $segment, $expectedType ) { + $instance = $this->getInstance(); - $result = $instance->getResults(); + $result = $instance->getResults(); $this->assertInternalType( $expectedType, $result[$segment] ); } -- To view, visit https://gerrit.wikimedia.org/r/78223 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I0fea5a3f1281752632f0675a7cf8056f5b477685 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/SemanticMediaWiki Gerrit-Branch: master Gerrit-Owner: Mwjames <jamesin.hongkon...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits