Skip to content

Commit

Permalink
Tidy (PropertySpecificationLookup) (#2067)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwjames committed Dec 3, 2016
1 parent a814e8c commit 9f92c8b
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 131 deletions.
4 changes: 2 additions & 2 deletions src/DataValues/ValueFormatters/PropertyValueFormatter.php
Expand Up @@ -217,8 +217,8 @@ private function canHighlight( &$propertyDescription, $linker ) {

$propertyDescription = ApplicationFactory::getInstance()->getPropertySpecificationLookup()->getPropertyDescriptionBy(
$dataItem,
$linker,
$this->dataValue->getOptionBy( PropertyValue::OPT_USER_LANGUAGE )
$this->dataValue->getOptionBy( PropertyValue::OPT_USER_LANGUAGE ),
$linker
);

return !$dataItem->isUserDefined() || $propertyDescription !== '';
Expand Down
60 changes: 7 additions & 53 deletions src/InMemoryPoolCache.php
Expand Up @@ -16,17 +16,17 @@ class InMemoryPoolCache {
/**
* Stats as plain string
*/
const FORMAT_PLAIN = 'plain';
const FORMAT_PLAIN = StatsFormatter::FORMAT_PLAIN;

/**
* Stats as JSON output
*/
const FORMAT_JSON = 'json';
const FORMAT_JSON = StatsFormatter::FORMAT_JSON;

/**
* Stats as HTML list output
*/
const FORMAT_HTML = 'html';
const FORMAT_HTML = StatsFormatter::FORMAT_HTML;

/**
* @var InMemoryPoolCache
Expand Down Expand Up @@ -86,61 +86,15 @@ public function resetPoolCacheFor( $poolCacheName = '' ) {
}
}

/**
* @since 2.3
*
* @return array
*/
public function getStats() {

$stats = array();

foreach ( $this->poolCacheList as $key => $value ) {
$stats[$key] = $value->getStats();
}

return $stats;
}

/**
* @since 2.4
*
* @param string $format
* @param string|null $format
*
* @return string
* @return string|array
*/
public function getFormattedStats( $format = self::FORMAT_PLAIN ) {

$stats = $this->computeStats();
$output = '';

if ( $format === self::FORMAT_PLAIN ) {
foreach ( $stats as $key => $value ) {
$output .= '- ' . $key . "\n";

foreach ( $value as $k => $v ) {
$output .= ' - ' . $k . ': ' . $v . "\n";
}
}
}

if ( $format === self::FORMAT_HTML ) {
$output .= '<ul>';
foreach ( $stats as $key => $value ) {
$output .= '<li>' . $key . '<ul>';
foreach ( $value as $k => $v ) {
$output .= '<li>' . $k . ': ' . $v . "</li>";
}
$output .= '</ul></li>';
}
$output .= '</ul>';
}

if ( $format === self::FORMAT_JSON ) {
$output .= json_encode( $stats, JSON_PRETTY_PRINT );
}

return $output;
public function getStats( $format = null ) {
return StatsFormatter::format( $this->computeStats(), $format );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/PropertyLabelFinder.php
Expand Up @@ -136,7 +136,7 @@ public function findPreferredPropertyLabelByLanguageCode( $id, $languageCode = '
$propertySpecificationLookup = ApplicationFactory::getInstance()->getPropertySpecificationLookup();

$preferredPropertyLabel = $propertySpecificationLookup->getPreferredPropertyLabelBy(
$id,
new DIProperty( str_replace( ' ', '_', $id ) ),
$languageCode
);

Expand Down
43 changes: 17 additions & 26 deletions src/PropertySpecificationLookup.php
Expand Up @@ -12,13 +12,19 @@
* This class should be accessed via ApplicationFactory::getPropertySpecificationLookup
* to ensure a singleton instance.
*
* Changes to a property should trigger a PropertySpecificationLookup::resetCacheBy to
* evict all cached item store to that property.
*
* @license GNU GPL v2+
* @since 2.4
*
* @author mwjames
*/
class PropertySpecificationLookup {

/**
* Reference used in InMemoryPoolCache
*/
const POOLCACHE_ID = 'property.specification.lookup';

/**
Expand All @@ -45,33 +51,18 @@ class PropertySpecificationLookup {
public function __construct( CachedPropertyValuesPrefetcher $cachedPropertyValuesPrefetcher, Cache $intermediaryMemoryCache ) {
$this->cachedPropertyValuesPrefetcher = $cachedPropertyValuesPrefetcher;
$this->intermediaryMemoryCache = $intermediaryMemoryCache;
$this->languageCode = Localizer::getInstance()->getContentLanguage()->getCode();
}

/**
* @since 2.4
*
* @param DIWikiPage $subject
*/
public function resetCacheBy( DIWikiPage $subject ) {
$this->cachedPropertyValuesPrefetcher->resetCacheBy( $subject );
}

/**
* @since 2.4
*
* @param string
*/
public function getLanguageCode() {
return $this->languageCode;
}

/**
* @since 2.4
*
* @param string $languageCode
*/
public function setLanguageCode( $languageCode ) {
$this->languageCode = Localizer::asBCP47FormattedLanguageCode( $languageCode );
}

/**
* @since 2.5
*
Expand All @@ -85,8 +76,8 @@ public function getFieldListBy( DIProperty $property ) {
$key = 'list:'. $property->getKey();

// Guard against high frequency lookup
if ( $this->intermediaryMemoryCache->contains( $key ) ) {
return $this->intermediaryMemoryCache->fetch( $key );
if ( ( $fieldList = $this->intermediaryMemoryCache->fetch( $key ) ) !== false ) {
return $fieldList;
}

$dataItems = $this->cachedPropertyValuesPrefetcher->getPropertyValues(
Expand All @@ -106,23 +97,23 @@ public function getFieldListBy( DIProperty $property ) {
/**
* @since 2.5
*
* @param string $id
* @param DIProperty $property
* @param string $languageCode
*
* @return string
*/
public function getPreferredPropertyLabelBy( $id, $languageCode = '' ) {
public function getPreferredPropertyLabelBy( DIProperty $property, $languageCode = '' ) {

$languageCode = $languageCode === '' ? $this->languageCode : $languageCode;
$key = 'ppl:' . $languageCode . ':'. $id;
$key = 'ppl:' . $languageCode . ':'. $property->getKey();

// Guard against high frequency lookup
if ( ( $preferredPropertyLabel = $this->intermediaryMemoryCache->fetch( $key ) ) !== false ) {
return $preferredPropertyLabel;
}

$preferredPropertyLabel = $this->findPreferredPropertyLabel(
new DIProperty( str_replace( ' ', '_', $id ) ),
$property,
$languageCode
);

Expand Down Expand Up @@ -341,12 +332,12 @@ public function getDisplayUnitsBy( DIProperty $property ) {
* @since 2.4
*
* @param DIProperty $property
* @param mixed|null $linker
* @param string $languageCode
* @param mixed|null $linker
*
* @return string
*/
public function getPropertyDescriptionBy( DIProperty $property, $linker = null, $languageCode = '' ) {
public function getPropertyDescriptionBy( DIProperty $property, $languageCode = '', $linker = null ) {

// Take the linker into account (Special vs. in page rendering etc.)
$languageCode = $languageCode === '' ? $this->languageCode : $languageCode;
Expand Down
6 changes: 0 additions & 6 deletions src/SharedCallbackContainer.php
Expand Up @@ -340,12 +340,6 @@ private function registerCallbackHandlersByConstructedInstance( $callbackLoader
$callbackLoader->singleton( 'InMemoryPoolCache' )->getPoolCacheById( PropertySpecificationLookup::POOLCACHE_ID )
);

// Uses the language object selected in user preferences. It is one
// of two global language objects
$propertySpecificationLookup->setLanguageCode(
Localizer::getInstance()->getUserLanguage()->getCode()
);

return $propertySpecificationLookup;
} );

Expand Down
125 changes: 125 additions & 0 deletions src/StatsFormatter.php
@@ -0,0 +1,125 @@
<?php

namespace SMW;

/**
* @license GNU GPL v2+
* @since 2.5
*
* @author mwjames
*/
class StatsFormatter {

/**
* Stats as plain string
*/
const FORMAT_PLAIN = 'plain';

/**
* Stats as JSON output
*/
const FORMAT_JSON = 'json';

/**
* Stats as HTML list output
*/
const FORMAT_HTML = 'html';

/**
* @since 2.5
*
* @param array $stats
* @param string|null $format
*
* @return string|array
*/
public static function format( array $stats, $format = null ) {

$output = '';

if ( $format === self::FORMAT_PLAIN ) {
foreach ( $stats as $key => $value ) {
$output .= '- ' . $key . "\n";

if ( !is_array( $value ) ) {
continue;
}

foreach ( $value as $k => $v ) {
$output .= ' - ' . $k . ': ' . $v . "\n";
}
}
}

if ( $format === self::FORMAT_HTML ) {
$output .= '<ul>';
foreach ( $stats as $key => $value ) {
$output .= '<li>' . $key . '<ul>';

if ( !is_array( $value ) ) {
continue;
}

foreach ( $value as $k => $v ) {
$output .= '<li>' . $k . ': ' . $v . "</li>";
}
$output .= '</ul></li>';
}
$output .= '</ul>';
}

if ( $format === self::FORMAT_JSON ) {
$output .= json_encode( $stats, JSON_PRETTY_PRINT );
}

if ( $format === null ) {
$output = $stats;
}

return $output;
}

/**
* @since 2.5
*
* @param array $stats
* @param string $separator
*
* @return array
*/
public static function getStatsFromFlatKey( array $stats, $separator = '.' ) {

$data = $stats;
$stats = array();

foreach ( $data as $key => $value ) {
if ( strpos( $key, $separator ) !== false ) {
$stats = array_merge_recursive( $stats, self::stringToArray( $separator, $key, $value ) );
} else {
$stats[$key] = $value;
}
}

return $stats;
}

// http://stackoverflow.com/questions/10123604/multstatsdIdimensional-array-from-string
private static function stringToArray( $separator, $path, $value ) {

$pos = strpos( $path, $separator );

if ( $pos === false ) {
return array( $path => $value );
}

$key = substr( $path, 0, $pos );
$path = substr( $path, $pos + 1 );

$result = array(
$key => self::stringToArray( $separator, $path, $value )
);

return $result;
}

}

0 comments on commit 9f92c8b

Please sign in to comment.