Exemple #1
0
 /**
  * @see StringValueParser::stringParse
  *
  * @since 3.0
  *
  * @param string $value
  *
  * @return float
  * @throws ParseException
  */
 public function stringParse($value)
 {
     $distance = \MapsDistanceParser::parseDistance($value);
     if ($distance === false) {
         throw new ParseException('Not a distance');
     }
     return $distance;
 }
 /**
  * @param SMWDataItem $areaCenter
  * @param string $comparator
  * @param string $radius
  * @param SMWDIProperty $property
  *
  * @throws InvalidArgumentException
  */
 public function __construct(SMWDataItem $areaCenter, $comparator, $radius, SMWDIProperty $property = null)
 {
     if (!$areaCenter instanceof SMWDIGeoCoord) {
         throw new InvalidArgumentException('$areaCenter needs to be a SMWDIGeoCoord');
     }
     parent::__construct($areaCenter, $property, $comparator);
     $this->radius = MapsDistanceParser::parseDistance($radius);
     $this->center = $areaCenter;
     $this->bounds = $this->getBoundingBox();
 }
 /**
  * Parses the value into the coordinates and any meta data provided, such as distance.
  * 
  * @since 0.6
  * 
  * @param $value String
  * @param $asQuery Boolean
  */
 protected function parseUserValueOrQuery($value, $asQuery = false)
 {
     $this->wikiValue = $value;
     $comparator = SMW_CMP_EQ;
     if ($value === '') {
         $this->addError(wfMsg('smw_novalues'));
     } else {
         SMWDataValue::prepareValue($value, $comparator);
         $parts = explode('(', $value);
         $coordinates = trim(array_shift($parts));
         $distance = count($parts) > 0 ? trim(array_shift($parts)) : false;
         if ($distance !== false) {
             $distance = substr(trim($distance), 0, -1);
             if (!MapsDistanceParser::isDistance($distance)) {
                 $this->addError(wfMsgExt('semanticmaps-unrecognizeddistance', array('parsemag'), $distance));
                 $distance = false;
             }
         }
         $parsedCoords = MapsCoordinateParser::parseCoordinates($coordinates);
         if ($parsedCoords) {
             $this->m_dataitem = new SMWDIGeoCoord($parsedCoords);
         } else {
             $this->addError(wfMsgExt('maps_unrecognized_coords', array('parsemag'), $coordinates, 1));
             // Make sure this is always set
             // TODO: Why is this needed?!
             $this->m_dataitem = new SMWDIGeoCoord(array('lat' => 0, 'lon' => 0));
         }
     }
     if ($asQuery) {
         $this->setUserValue($value);
         switch (true) {
             case !$this->isValid():
                 return new SMWThingDescription();
             case $distance !== false:
                 return new SMAreaValueDescription($this->getDataItem(), $comparator, $distance);
             default:
                 return new SMGeoCoordsValueDescription($this->getDataItem(), $comparator);
         }
     }
 }
 protected function parserDistance($distance)
 {
     if ($distance !== false) {
         $distance = substr(trim($distance), 0, -1);
         if (!MapsDistanceParser::isDistance($distance)) {
             $this->addError(wfMessage('semanticmaps-unrecognizeddistance', $distance)->text());
             $distance = false;
         }
     }
     return $distance;
 }
 /**
  * Renders and returns the output.
  * @see ParserHook::render
  * 
  * @since 0.7
  * 
  * @param array $parameters
  * 
  * @return string
  */
 public function render(array $parameters)
 {
     $distanceInMeters = MapsDistanceParser::parseDistance($parameters['distance']);
     return MapsDistanceParser::formatDistance($distanceInMeters, $parameters['unit'], $parameters['decimals']);
 }
 /**
  * Renders and returns the output.
  * @see ParserHook::render
  * 
  * @since 0.7
  * 
  * @param array $parameters
  * 
  * @return string
  */
 public function render(array $parameters)
 {
     $canGeocode = MapsGeocoders::canGeocode();
     if ($canGeocode) {
         $location = MapsGeocoders::attemptToGeocode($parameters['location'], $parameters['geoservice'], $parameters['mappingservice']);
     } else {
         $location = MapsCoordinateParser::parseCoordinates($parameters['location']);
     }
     // TODO
     if ($location) {
         $destination = MapsGeoFunctions::findDestination($location, $parameters['bearing'], MapsDistanceParser::parseDistance($parameters['distance']));
         $output = MapsCoordinateParser::formatCoordinates($destination, $parameters['format'], $parameters['directional']);
     } else {
         // The location should be valid when this method gets called.
         throw new MWException('Attempt to find a destination from an invalid location');
     }
     return $output;
 }
 /**
  * Renders and returns the output.
  * @see ParserHook::render
  * 
  * @since 0.7
  * 
  * @param array $parameters
  * 
  * @return string
  * @throws MWException
  */
 public function render(array $parameters)
 {
     /**
      * @var \DataValues\LatLongValue $coordinates1
      * @var \DataValues\LatLongValue $coordinates2
      */
     $coordinates1 = $parameters['location1']->getCoordinates();
     $coordinates2 = $parameters['location2']->getCoordinates();
     $distance = MapsGeoFunctions::calculateDistance($coordinates1, $coordinates2);
     $output = MapsDistanceParser::formatDistance($distance, $parameters['unit'], $parameters['decimals']);
     return $output;
 }
 /**
  * Sets the bounds fields to an array returned by SMAreaValueDescription::getBoundingBox.
  * 
  * @since 0.6
  * 
  * @param SMWDIGeoCoord $dataItem
  * @param string $radius
  */
 protected function calculateBounds(SMWDIGeoCoord $dataItem, $radius)
 {
     $this->bounds = self::getBoundingBox(array('lat' => $dataItem->getLatitude(), 'lon' => $dataItem->getLongitude()), MapsDistanceParser::parseDistance($radius));
 }
 public function __construct(&$results, SMWPrintRequest $printRequest, SRFFiltered &$queryPrinter)
 {
     global $wgParser;
     parent::__construct($results, $printRequest, $queryPrinter);
     if (!defined('Maps_VERSION') || version_compare(Maps_VERSION, '1.0', '<')) {
         throw new FatalError('You need to have the <a href="http://www.mediawiki.org/wiki/Extension:Maps">Maps</a> extension version 1.0 or higher installed in order to use the distance filter.<br />');
     }
     MapsGeocoders::init();
     $params = $this->getActualParameters();
     if (array_key_exists('distance filter origin', $params)) {
         $origin = MapsGeocoders::attemptToGeocode($wgParser->recursiveTagParse($params['distance filter origin']));
     } else {
         $origin = array('lat' => '0', 'lon' => '0');
     }
     if (array_key_exists('distance filter unit', $params)) {
         $this->mUnit = MapsDistanceParser::getValidUnit($wgParser->recursiveTagParse($params['distance filter unit']));
     } else {
         $this->mUnit = MapsDistanceParser::getValidUnit();
     }
     // Is the real position stored in a property?
     if (array_key_exists('distance filter property', $params)) {
         $property = trim($wgParser->recursiveTagParse($params['distance filter property']));
         $locations = array();
     } else {
         $property = null;
         $locations = null;
     }
     $targetLabel = $printRequest->getLabel();
     foreach ($this->getQueryResults() as $id => $filteredItem) {
         $row = $filteredItem->getValue();
         // $filteredItem is of class SRF_Filtered_Item
         // $row is an array of SMWResultArray
         foreach ($row as $field) {
             // $field is an SMWResultArray
             $label = $field->getPrintRequest()->getLabel();
             if ($label === $targetLabel) {
                 $field->reset();
                 $dataValue = $field->getNextDataValue();
                 // only use first value
                 if ($dataValue !== false) {
                     $posText = $dataValue->getShortText(SMW_OUTPUT_WIKI, false);
                     if ($property === null) {
                         // position is directly given
                         $pos = MapsGeocoders::attemptToGeocode($posText);
                     } else {
                         // position is given in a property of a page
                         // if we used this page before, just look up the coordinates
                         if (array_key_exists($posText, $locations)) {
                             $pos = $locations[$posText];
                         } else {
                             // query the position's page for the coordinates or address
                             $posText = SMWQueryProcessor::getResultFromFunctionParams(array($posText, '?' . $property), SMW_OUTPUT_WIKI, SMWQueryProcessor::INLINE_QUERY, true);
                             //
                             if ($posText !== '') {
                                 // geocode
                                 $pos = MapsGeocoders::attemptToGeocode($posText);
                             } else {
                                 $pos = array('lat' => '0', 'lon' => '0');
                             }
                             // store coordinates in case we need them again
                             $locations[$posText] = $pos;
                         }
                     }
                     if (is_array($pos)) {
                         $distance = round(MapsGeoFunctions::calculateDistance($origin, $pos) / MapsDistanceParser::getUnitRatio($this->mUnit));
                         if ($distance > $this->mMaxDistance) {
                             $this->mMaxDistance = $distance;
                         }
                     } else {
                         $distance = -1;
                     }
                 } else {
                     $distance = -1;
                     // no location given
                 }
                 $filteredItem->setData('distance-filter', $distance);
                 break;
             }
         }
     }
     if (array_key_exists('distance filter max distance', $params) && is_numeric($maxDist = trim($wgParser->recursiveTagParse($params['distance filter max distance'])))) {
         // this assignation ^^^ is ugly, but intentional
         $this->mMaxDistance = $maxDist;
     } else {
         if ($this->mMaxDistance > 1) {
             $base = pow(10, floor(log10($this->mMaxDistance)));
             $this->mMaxDistance = ceil($this->mMaxDistance / $base) * $base;
         }
     }
 }
 /**
  * Renders and returns the output.
  * @see ParserHook::render
  * 
  * @since 0.7
  * 
  * @param array $parameters
  * 
  * @return string
  */
 public function render(array $parameters)
 {
     if (MapsGeocoders::canGeocode()) {
         $start = MapsGeocoders::attemptToGeocode($parameters['location1'], $parameters['geoservice'], $parameters['mappingservice']);
         $end = MapsGeocoders::attemptToGeocode($parameters['location2'], $parameters['geoservice'], $parameters['mappingservice']);
     } else {
         $start = MapsCoordinateParser::parseCoordinates($parameters['location1']);
         $end = MapsCoordinateParser::parseCoordinates($parameters['location2']);
     }
     if ($start && $end) {
         $output = MapsDistanceParser::formatDistance(MapsGeoFunctions::calculateDistance($start, $end), $parameters['unit'], $parameters['decimals']);
     } else {
         // The locations should be valid when this method gets called.
         throw new Exception('Attempt to find the distance between locations of at least one is invalid');
     }
     return $output;
 }
 private static function initUnitRegex()
 {
     if (self::$unitRegex === false) {
         global $egMapsDistanceUnits;
         self::$unitRegex = implode('|', array_keys($egMapsDistanceUnits)) . '|';
     }
 }
 /**
  * @see ItemParameterCriterion::validate
  */
 protected function doValidation($value, Parameter $parameter, array $parameters)
 {
     return MapsDistanceParser::isDistance($value);
 }
 /**
  * Tests MapsDistanceParser::getUnits()
  */
 public function testGetUnits()
 {
     $this->assertEquals(array_keys($GLOBALS['egMapsDistanceUnits']), MapsDistanceParser::getUnits());
 }