protected function createDataItemByPropertyId($propertyId)
 {
     $dataItem = null;
     switch ($propertyId) {
         case DIProperty::TYPE_MODIFICATION_DATE:
             $dataItem = DITime::newFromTimestamp($this->pageInfo->getModificationDate());
             break;
         case DIProperty::TYPE_CREATION_DATE:
             $dataItem = DITime::newFromTimestamp($this->pageInfo->getCreationDate());
             break;
         case DIProperty::TYPE_NEW_PAGE:
             $dataItem = new DIBoolean($this->pageInfo->isNewPage());
             break;
         case DIProperty::TYPE_LAST_EDITOR:
             $dataItem = $this->pageInfo->getLastEditor() ? DIWikiPage::newFromTitle($this->pageInfo->getLastEditor()) : null;
             break;
         case DIProperty::TYPE_MEDIA:
             // @codingStandardsIgnoreStart phpcs, ignore --sniffs=Generic.Files.LineLength
             $dataItem = $this->pageInfo->isFilePage() && $this->pageInfo->getMediaType() !== '' && $this->pageInfo->getMediaType() !== null ? new DIBlob($this->pageInfo->getMediaType()) : null;
             // @codingStandardsIgnoreEnd
             break;
         case DIProperty::TYPE_MIME:
             // @codingStandardsIgnoreStart phpcs, ignore --sniffs=Generic.Files.LineLength
             $dataItem = $this->pageInfo->isFilePage() && $this->pageInfo->getMimeType() !== '' && $this->pageInfo->getMimeType() !== null ? new DIBlob($this->pageInfo->getMimeType()) : null;
             // @codingStandardsIgnoreEnd
             break;
     }
     return $dataItem;
 }
 private function parseToTimeValueForGregorianCalendarModel(DITime $dataItem)
 {
     if ($dataItem->getYear() > 0) {
         $xsdvalue = str_pad($dataItem->getYear(), 4, "0", STR_PAD_LEFT);
     } else {
         $xsdvalue = '-' . str_pad(1 - $dataItem->getYear(), 4, "0", STR_PAD_LEFT);
     }
     $xsdtype = 'http://www.w3.org/2001/XMLSchema#gYear';
     if ($dataItem->getPrecision() >= DITime::PREC_YM) {
         $xsdtype = 'http://www.w3.org/2001/XMLSchema#gYearMonth';
         $xsdvalue .= '-' . str_pad($dataItem->getMonth(), 2, "0", STR_PAD_LEFT);
         if ($dataItem->getPrecision() >= DITime::PREC_YMD) {
             $xsdtype = 'http://www.w3.org/2001/XMLSchema#date';
             $xsdvalue .= '-' . str_pad($dataItem->getDay(), 2, "0", STR_PAD_LEFT);
             if ($dataItem->getPrecision() == DITime::PREC_YMDT) {
                 $xsdtype = 'http://www.w3.org/2001/XMLSchema#dateTime';
                 $xsdvalue .= 'T' . sprintf("%02d", $dataItem->getHour()) . ':' . sprintf("%02d", $dataItem->getMinute()) . ':' . sprintf("%02d", $dataItem->getSecond());
             }
             // https://www.w3.org/TR/2005/NOTE-timezone-20051013/
             // "Time zone identification in the date and time types relies
             // entirely on time zone offset from UTC."
             // Zone offset Z indicates UTC
             $xsdvalue .= 'Z';
         }
     }
     $this->xsdValue = $xsdvalue;
     $this->xsdType = $xsdtype;
 }
 public function propertyObjectProvider()
 {
     $provider = array();
     // #0
     $provider[] = array(new DIProperty('_MDAT'), DITime::newFromTimestamp(1272508903));
     return $provider;
 }
 protected function createDataItemByPropertyId($propertyId)
 {
     $dataItem = null;
     switch ($propertyId) {
         case DIProperty::TYPE_MODIFICATION_DATE:
             $dataItem = DITime::newFromTimestamp($this->pageInfo->getModificationDate());
             break;
         case DIProperty::TYPE_CREATION_DATE:
             $dataItem = DITime::newFromTimestamp($this->pageInfo->getCreationDate());
             break;
         case DIProperty::TYPE_NEW_PAGE:
             $dataItem = new DIBoolean($this->pageInfo->isNewPage());
             break;
         case DIProperty::TYPE_LAST_EDITOR:
             $dataItem = $this->pageInfo->getLastEditor() ? DIWikiPage::newFromTitle($this->pageInfo->getLastEditor()) : null;
             break;
         case DIProperty::TYPE_MEDIA:
             $dataItem = $this->pageInfo->isFilePage() && $this->pageInfo->getMediaType() !== '' && $this->pageInfo->getMediaType() !== null ? new DIBlob($this->pageInfo->getMediaType()) : null;
             break;
         case DIProperty::TYPE_MIME:
             $dataItem = $this->pageInfo->isFilePage() && $this->pageInfo->getMimeType() !== '' && $this->pageInfo->getMimeType() !== null ? new DIBlob($this->pageInfo->getMimeType()) : null;
             break;
     }
     return $dataItem;
 }
 /**
  * Helper function - creates an object of type SMWTimeValue based
  * on a "Julian day" integer
  */
 protected static function jdToTimeValue($jd)
 {
     $timeDataItem = SMWDITime::newFromJD($jd, SMWDITime::CM_GREGORIAN, SMWDITime::PREC_YMDT);
     $timeValue = new SMWTimeValue('_dat');
     $timeValue->setDataItem($timeDataItem);
     return $timeValue;
 }
 /**
  * @since 1.8
  *
  * {@inheritDoc}
  */
 public function dataItemFromDBKeys($dbkeys)
 {
     if (is_string($dbkeys)) {
         return DITime::doUnserialize($dbkeys);
     }
     throw new DataItemHandlerException('Failed to create data item from DB keys.');
 }
 private function getTimezoneLiteralWithModifiedDateTime(&$dateTime)
 {
     $timezone = 0;
     // If the date/time is in the UTC form then it is assumed that no other
     // TZ was selected to modify a value output hence it would be possible
     // to match a user preference with the `timecorrection` setting and use
     // it as an input for those dates to make the display take into account
     // a user ontext.
     //
     // - Inject/use a setter for the `timecorrection` preference as it depends
     // on a User object.
     // - The ParserCache doesn't recognizes changes for users with different
     // `timecorrection` settings therefore this needs to be dealt with before
     // otherwise a change by one user will remain active in the ParserCache
     // even though a different user has a different `timecorrection` setting.
     // Changes to the output is only triggered when the ParserCache is evicted or
     // purged manually.
     if ($this->dataItem->getTimezone() === '0') {
         //	$parts = explode( '|', $GLOBALS['wgUser']->getOption( 'timecorrection' ) );
         //	$timezone = count( $parts ) == 3 ? $parts[2] : false;
     } else {
         $timezone = $this->dataItem->getTimezone();
     }
     return Timezone::getTimezoneLiteralWithModifiedDateTime($dateTime, $timezone);
 }
 /**
  * @return array
  */
 public function removePropertyObjectProvider()
 {
     $provider = array();
     $title = Title::newFromText('Foo');
     // #0
     $provider[] = array($title, new DIProperty('_MDAT'), DITime::newFromTimestamp(1272508903));
     return $provider;
 }
 public function testSerializeFormatForTimeValue()
 {
     $property = \SMW\DIProperty::newFromUserLabel('Foo');
     $property->setPropertyTypeId('_dat');
     $printRequestFactory = new \SMW\Query\PrintRequestFactory();
     $serialization = QueryResultSerializer::getSerialization(\SMWDITime::doUnserialize('2/1393/1/1'), $printRequestFactory->newPropertyPrintRequest($property));
     $expected = array('timestamp' => '-18208281600', 'raw' => '2/1393/1/1');
     $this->assertEquals($expected, $serialization);
 }
 public function testFormatWithLocalizedMonthReplacement()
 {
     // F - A full textual representation of a month, such as January or March
     $formatOption = 'F Y/m/d H:i:s';
     $language = $this->getMockBuilder('\\Language')->disableOriginalConstructor()->getMock();
     $language->expects($this->once())->method('getMonthName')->with($this->equalTo('12'))->will($this->returnValue('Foo'));
     $instance = new IntlTimeFormatter(DITime::doUnserialize('1/2000/12/12/1/1/20.200'), $language);
     $this->assertEquals('Foo 2000/12/12 01:01:20', $instance->format($formatOption));
 }
 public function testPredefinedModificationDatePropertyAndChangedDefaultsortForNewPage()
 {
     $this->applicationFactory->getSettings()->set('smwgPageSpecialProperties', array('_MDAT'));
     $title = Title::newFromText(__METHOD__);
     $subject = DIWikiPage::newFromTitle($title);
     $this->pageCreator->createPage($title, '{{DEFAULTSORT:SortForFoo}}');
     $dvPageModificationTime = $this->dataValueFactory->newDataItemValue(DITime::newFromTimestamp($this->pageCreator->getPage()->getTimestamp()));
     $expected = array('propertyCount' => 2, 'propertyKeys' => array('_MDAT', '_SKEY'), 'propertyValues' => array($dvPageModificationTime->getISO8601Date(), 'SortForFoo'));
     $this->semanticDataValidator->assertThatPropertiesAreSet($expected, $this->getStore()->getSemanticData($subject));
 }
 private function parseToTimeValueForGregorianCalendarModel(DITime $dataItem)
 {
     if ($dataItem->getYear() > 0) {
         $xsdvalue = str_pad($dataItem->getYear(), 4, "0", STR_PAD_LEFT);
     } else {
         $xsdvalue = '-' . str_pad(1 - $dataItem->getYear(), 4, "0", STR_PAD_LEFT);
     }
     $xsdtype = 'http://www.w3.org/2001/XMLSchema#gYear';
     if ($dataItem->getPrecision() >= DITime::PREC_YM) {
         $xsdtype = 'http://www.w3.org/2001/XMLSchema#gYearMonth';
         $xsdvalue .= '-' . str_pad($dataItem->getMonth(), 2, "0", STR_PAD_LEFT);
         if ($dataItem->getPrecision() >= DITime::PREC_YMD) {
             $xsdtype = 'http://www.w3.org/2001/XMLSchema#date';
             $xsdvalue .= '-' . str_pad($dataItem->getDay(), 2, "0", STR_PAD_LEFT);
             if ($dataItem->getPrecision() == DITime::PREC_YMDT) {
                 $xsdtype = 'http://www.w3.org/2001/XMLSchema#dateTime';
                 $xsdvalue .= 'T' . sprintf("%02d", $dataItem->getHour()) . ':' . sprintf("%02d", $dataItem->getMinute()) . ':' . sprintf("%02d", $dataItem->getSecond());
             }
         }
     }
     $this->xsdValue = $xsdvalue .= 'Z';
     $this->xsdType = $xsdtype;
 }
 private function getUpperLimit($dataItem)
 {
     $prec = $dataItem->getPrecision();
     $dateTime = new DateTime();
     $dateTime->setDate($dataItem->getYear(), $dataItem->getMonth(), $dataItem->getDay());
     $dateTime->setTime($dataItem->getHour(), $dataItem->getMinute(), $dataItem->getSecond());
     if ($dateTime === false) {
         return $this->addError('Cannot compute interval for ' . $dataItem->getSerialization());
     }
     if ($prec === DITime::PREC_Y) {
         $dateTime->add(new DateInterval('P1Y'));
     } elseif ($prec === DITime::PREC_YM) {
         $dateTime->add(new DateInterval('P1M'));
     } elseif ($prec === DITime::PREC_YMD) {
         $dateTime->add(new DateInterval('P1D'));
     } elseif ($prec === DITime::PREC_YMDT) {
         if ($dataItem->getSecond() > 0) {
             $dateTime->add(new DateInterval('PT1S'));
         } elseif ($dataItem->getMinute() > 0) {
             $dateTime->add(new DateInterval('PT1M'));
         } elseif ($dataItem->getHour() > 0) {
             $dateTime->add(new DateInterval('PT1H'));
         } else {
             $dateTime->add(new DateInterval('PT24H'));
         }
     }
     return DITime::doUnserialize($dataItem->getCalendarModel() . '/' . $dateTime->format('Y/m/d/H/i/s'));
 }
 /**
  * Compute a suitable string to display the given date item.
  * @note MediaWiki's date functions are not applicable for the range of historic dates we support.
  *
  * @since 1.6
  *
  * @param SMWDITime $dataitem
  *
  * @return string
  * @todo Internationalize the CE and BCE strings.
  */
 public function getCaptionFromDataitem(SMWDITime $dataitem)
 {
     /**
      * @var SMWLanguage $smwgContLang
      */
     global $smwgContLang;
     if ($dataitem->getYear() > 0) {
         $cestring = '';
         $result = number_format($dataitem->getYear(), 0, '.', '') . ($cestring ? ' ' . $cestring : '');
     } else {
         $bcestring = 'BC';
         $result = number_format(-$dataitem->getYear(), 0, '.', '') . ($bcestring ? ' ' . $bcestring : '');
     }
     if ($dataitem->getPrecision() >= SMWDITime::PREC_YM) {
         $result = $smwgContLang->getMonthLabel($dataitem->getMonth()) . " " . $result;
     }
     if ($dataitem->getPrecision() >= SMWDITime::PREC_YMD) {
         $result = $dataitem->getDay() . " " . $result;
     }
     if ($dataitem->getPrecision() >= SMWDITime::PREC_YMDT) {
         $result .= " " . $this->getTimeString();
     }
     return $result;
 }
 /**
  * Initialise data from the provided intermediate results after
  * parsing, assuming that a conventional date notation is used.
  * If errors occur, error messages are added to the objects list of
  * errors, and false is returned. Otherwise, true is returned.
  * @param $datecomponents array of strings that might belong to the specification of a date
  * @param $calendarmodesl string if model was set in input, otherwise false
  * @param $era string '+' or '-' if provided, otherwise false
  * @param $hours integer value between 0 and 24
  * @param $minutes integer value between 0 and 59
  * @param $seconds integer value between 0 and 59, or false if not given
  * @param $timeoffset double value for time offset (e.g. 3.5), or false if not given
  * @return boolean stating if successful
  */
 protected function setDateFromParsedValues($datecomponents, $calendarmodel, $era, $hours, $minutes, $seconds, $microseconds, $timeoffset, $timezone)
 {
     $date = false;
     if (!$this->interpretDateComponents($datecomponents, $date)) {
         return false;
     }
     // Handle BC: the year is negative.
     if ($era == '-' && $date['y'] > 0) {
         // see class documentation on BC, "year 0", and ISO conformance ...
         $date['y'] = -$date['y'];
     }
     // Keep information about the era
     if ($era == '+' && $date['y'] > 0) {
         $date['y'] = $era . $date['y'];
     }
     // Old Style is a special case of Julian calendar model where the change of the year was 25 March:
     if ($calendarmodel == 'OS' && ($date['m'] < 3 || $date['m'] == 3 && $date['d'] < 25)) {
         $date['y']++;
     }
     $calmod = $this->getCalendarModel($calendarmodel, $date['y'], $date['m'], $date['d']);
     try {
         $this->m_dataitem = new SMWDITime($calmod, $date['y'], $date['m'], $date['d'], $hours, $minutes, $seconds . '.' . $microseconds, $timezone);
     } catch (SMWDataItemException $e) {
         $this->addErrorMsg(array('smw-datavalue-time-invalid', $this->m_wikivalue, $e->getMessage()));
         return false;
     }
     // Having more than years or specifying a calendar model does
     // not make sense for prehistoric dates, and our calendar
     // conversion would not be reliable if JD numbers get too huge:
     if ($date['y'] <= self::PREHISTORY && ($this->m_dataitem->getPrecision() > SMWDITime::PREC_Y || $calendarmodel !== false)) {
         $this->addErrorMsg(array('smw-datavalue-time-invalid-prehistoric', $this->m_wikivalue));
         return false;
     }
     if ($timeoffset != 0) {
         $newjd = $this->m_dataitem->getJD() - $timeoffset / 24;
         try {
             $this->m_dataitem = SMWDITime::newFromJD($newjd, $calmod, $this->m_dataitem->getPrecision(), $timezone);
         } catch (SMWDataItemException $e) {
             $this->addErrorMsg(array('smw-datavalue-time-invalid-jd', $this->m_wikivalue, $e->getMessage()));
             return false;
         }
     }
     return true;
 }
 public function testTryToDeserializeOnInvalidCountOfElementsThrowsException()
 {
     $this->setExpectedException('\\SMW\\DataItemException');
     DITime::doUnserialize('1');
 }
Exemple #17
0
 /**
  * @see SMWDataItemHandler::dataItemFromDBKeys()
  * @since 1.8
  * @param array|string $dbkeys expecting string here
  *
  * @return SMWDataItem
  */
 public function dataItemFromDBKeys($dbkeys)
 {
     if (is_string($dbkeys)) {
         return SMWDITime::doUnserialize($dbkeys);
     } else {
         throw new SMWDataItemException('Failed to create data item from DB keys.');
     }
 }
 /**
  * @private
  *
  * @since 2.4
  *
  * @param  DITime|null $dataItem
  *
  * @return string
  */
 public function getLocalizedFormat(DITime $dataItem = null)
 {
     if ($dataItem === null) {
         return '';
     }
     if ($dataItem->getYear() < DITime::PREHISTORY) {
         return $this->getISO8601Date();
     }
     $outputFormat = $this->dataValue->getOutputFormat();
     $formatFlag = IntlTimeFormatter::LOCL_DEFAULT;
     if (strpos($outputFormat, 'TZ') !== false) {
         $formatFlag = IntlTimeFormatter::LOCL_TIMEZONE;
         $outputFormat = str_replace('#TZ', '', $outputFormat);
     }
     if (($language = Localizer::getInstance()->getLanguageCodeFrom($outputFormat)) === false) {
         $language = $this->dataValue->getOptionBy(DataValue::OPT_USER_LANGUAGE);
     }
     $language = Localizer::getInstance()->getLanguage($language);
     $intlTimeFormatter = new IntlTimeFormatter($dataItem, $language);
     // Avoid an exception on "DateTime::__construct(): Failed to parse time
     // string (2147483647-01-01 00:00:0.0000000) at position 17 (0): Double
     // time specification" for an annotation like [[Date::Jan 10000000000]]
     try {
         $localizedFormat = $intlTimeFormatter->getLocalizedFormat($formatFlag) . $this->hintCalendarModel($dataItem);
     } catch (\Exception $e) {
         $localizedFormat = $this->getISO8601Date();
     }
     return $localizedFormat;
 }
Exemple #19
0
 /**
  * @return array
  */
 public function removePropertyObjectProvider()
 {
     $provider = array();
     $title = Title::newFromText(__METHOD__);
     $subobject = $this->newSubobject($title, __METHOD__, '999');
     // #0
     $provider[] = array($title, new DIProperty('_MDAT'), DITime::newFromTimestamp(1272508903));
     // #1
     $provider[] = array($title, $subobject->getProperty(), $subobject->getContainer());
     return $provider;
 }
 /**
  * Method to create a dataitem from an array of DB keys.
  *
  * @since 1.8
  * @param $dbkeys array of mixed
  *
  * @return SMWDataItem
  */
 public function dataItemFromDBKeys($dbkeys)
 {
     return SMWDITime::doUnserialize($dbkeys[0]);
 }