/**
  * @since 2.4
  *
  * @return FixedInMemoryLruCache
  */
 public static function getCache()
 {
     if (self::$messageCache === null) {
         self::$messageCache = InMemoryPoolCache::getInstance()->getPoolCacheFor(self::POOLCACHE_ID, 1000);
     }
     return self::$messageCache;
 }
 private function doLookupResourceUriTargetFor(ExpNsResource $expNsResource)
 {
     $poolCache = InMemoryPoolCache::getInstance()->getPoolCacheFor('sparql.store.redirectlookup');
     if (!$poolCache->contains($expNsResource->getUri())) {
         $poolCache->save($expNsResource->getUri(), $this->lookupResourceUriTargetFromDatabase($expNsResource));
     }
     return $poolCache->fetch($expNsResource->getUri());
 }
 public function testPoolCache()
 {
     $instance = InMemoryPoolCache::getInstance();
     $this->assertInstanceOf('\\Onoi\\Cache\\Cache', $instance->getPoolCacheFor('Foo'));
     $instance->getPoolCacheFor('Foo')->save('Bar', 42);
     $this->assertEquals(42, $instance->getPoolCacheFor('Foo')->fetch('Bar'));
     $this->assertNotEmpty($instance->getStats('Foo'));
     $instance->resetPoolCacheFor('Foo');
     $this->assertEmpty($instance->getStats('Foo'));
 }
 public function testGetFormattedStats()
 {
     $instance = InMemoryPoolCache::getInstance();
     $instance->getPoolCacheFor('Foo')->save('Bar', 42);
     $this->assertNotEmpty($instance->getStats());
     $this->assertInternalType('string', $instance->getFormattedStats(InMemoryPoolCache::FORMAT_PLAIN));
     $this->assertContains('ul', $instance->getFormattedStats(InMemoryPoolCache::FORMAT_HTML));
     $this->assertInternalType('string', $instance->getFormattedStats(InMemoryPoolCache::FORMAT_JSON));
     $instance->resetPoolCacheFor('Foo');
 }
 public function testGetRedirectTargetFromInMemoryCache()
 {
     $inMemoryPoolCache = InMemoryPoolCache::getInstance();
     $instance = $this->getMockBuilder('\\SMW\\Store')->disableOriginalConstructor()->getMockForAbstractClass();
     $wikipage = new DIWikiPage('Foo', NS_MAIN);
     $expected = new DIWikiPage('Bar', NS_MAIN);
     $inMemoryPoolCache->getPoolCacheFor('store.redirectTarget.lookup')->save($wikipage->getHash(), $expected);
     $this->assertEquals($expected, $instance->getRedirectTarget($wikipage));
     $inMemoryPoolCache->resetPoolCacheFor('store.redirectTarget.lookup');
 }
 public function testGetDataItemForCachedId()
 {
     $connection = $this->getMockBuilder('\\SMW\\MediaWiki\\Database')->disableOriginalConstructor()->getMock();
     $connection->expects($this->never())->method('selectRow');
     InMemoryPoolCache::getInstance()->getPoolCacheFor(IdToDataItemMatchFinder::POOLCACHE_ID)->save(42, 'Foo#0##');
     $instance = new IdToDataItemMatchFinder($connection, $this->iteratorFactory);
     $this->assertInstanceOf('\\SMW\\DIWikiPage', $instance->getDataItemById(42));
     $stats = InMemoryPoolCache::getInstance()->getStats();
     $this->assertEquals(0, $stats[IdToDataItemMatchFinder::POOLCACHE_ID]['misses']);
     $this->assertEquals(1, $stats[IdToDataItemMatchFinder::POOLCACHE_ID]['hits']);
 }
 public function testGetDataItemForCachedId()
 {
     $connection = $this->getMockBuilder('\\SMW\\MediaWiki\\Database')->disableOriginalConstructor()->getMock();
     $connection->expects($this->never())->method('selectRow');
     InMemoryPoolCache::getInstance()->getPoolCacheFor('sql.store.dataitem.finder')->save(42, 'Foo#0##');
     $instance = new ByIdDataItemFinder($connection);
     $this->assertInstanceOf('\\SMW\\DIWikiPage', $instance->getDataItemForId(42));
     $stats = InMemoryPoolCache::getInstance()->getStats();
     $this->assertEquals(0, $stats['sql.store.dataitem.finder']['misses']);
     $this->assertEquals(1, $stats['sql.store.dataitem.finder']['hits']);
 }
 /**
  * @since 2.1
  *
  * @param integer $id
  *
  * @return DIWikiPage|null
  */
 public function getDataItemForId($id)
 {
     $poolCache = $this->inMemoryPoolCache->getPoolCacheFor('sql.store.dataitem.finder');
     if (!$poolCache->contains($id)) {
         $row = $this->connection->selectRow(\SMWSQLStore3::ID_TABLE, array('smw_title', 'smw_namespace', 'smw_iw', 'smw_subobject'), array('smw_id' => $id), __METHOD__);
         if ($row === false) {
             return null;
         }
         $hash = HashBuilder::createHashIdFromSegments($row->smw_title, $row->smw_namespace, $row->smw_iw, $row->smw_subobject);
         $this->saveToCache($id, $hash);
     }
     return HashBuilder::newDiWikiPageFromHash($poolCache->fetch($id));
 }
 public function testFindRedirectIdForNonCachedRedirect()
 {
     $row = new \stdClass();
     $row->o_id = 42;
     $connection = $this->getMockBuilder('\\SMW\\MediaWiki\\Database')->disableOriginalConstructor()->getMock();
     $connection->expects($this->once())->method('selectRow')->with($this->anything(), $this->anything(), $this->equalTo(array('s_title' => 'Foo', 's_namespace' => 0)))->will($this->returnValue($row));
     $instance = new RedirectInfoStore($connection);
     $this->assertEquals(42, $instance->findRedirectIdFor('Foo', 0));
     $stats = InMemoryPoolCache::getInstance()->getStats();
     $this->assertEquals(0, $stats['sql.store.redirect.infostore']['hits']);
     $instance->findRedirectIdFor('Foo', 0);
     $stats = InMemoryPoolCache::getInstance()->getStats();
     $this->assertEquals(1, $stats['sql.store.redirect.infostore']['hits']);
 }
 /**
  * Create an ExpElement for some internal resource, given by an
  * DIWikiPage object. This is the one place in the code where URIs
  * of wiki pages and user-defined properties are determined. A modifier
  * can be given to make variants of a URI, typically done for
  * auxiliary properties. In this case, the URI is modiied by appending
  * "-23$modifier" where "-23" is the URI encoding of "#" (a symbol not
  * occuring in MW titles).
  *
  * @param DIWikiPage $diWikiPage
  * @param boolean $useAuxiliaryModifier
  *
  * @return ExpResource
  */
 public function mapWikiPageToResourceElement(DIWikiPage $diWikiPage, $useAuxiliaryModifier = false)
 {
     $modifier = $useAuxiliaryModifier ? self::AUX_MARKER : '';
     $hash = $diWikiPage->getHash() . $modifier;
     $poolCache = $this->inMemoryPoolCache->getPoolCacheFor('exporter.dataitem.resource.encoder');
     if ($poolCache->contains($hash)) {
         return $poolCache->fetch($hash);
     }
     if ($diWikiPage->getSubobjectName() !== '') {
         $modifier = $diWikiPage->getSubobjectName();
     }
     $resource = $this->newExpNsResource($diWikiPage, $modifier);
     $poolCache->save($hash, $resource);
     return $resource;
 }
 /**
  * Create an ExpElement for some internal resource, given by an
  * DIWikiPage object. This is the one place in the code where URIs
  * of wiki pages and user-defined properties are determined. A modifier
  * can be given to make variants of a URI, typically done for
  * auxiliary properties. In this case, the URI is modiied by appending
  * "-23$modifier" where "-23" is the URI encoding of "#" (a symbol not
  * occuring in MW titles).
  *
  * @param DIWikiPage $diWikiPage
  * @param boolean $useAuxiliaryModifier
  *
  * @return ExpResource
  */
 public function mapWikiPageToResourceElement(DIWikiPage $diWikiPage, $useAuxiliaryModifier = false)
 {
     $modifier = $useAuxiliaryModifier ? self::AUX_MARKER : '';
     $hash = $diWikiPage->getHash() . $modifier;
     $poolCache = $this->inMemoryPoolCache->getPoolCacheFor('exporter.dataitem.resource.encoder');
     if ($poolCache->contains($hash)) {
         return $poolCache->fetch($hash);
     }
     if ($diWikiPage->getSubobjectName() !== '') {
         $modifier = $diWikiPage->getSubobjectName();
     }
     $importDataItem = $this->tryToFindImportDataItem($diWikiPage, $modifier);
     if ($importDataItem instanceof DataItem) {
         list($localName, $namespace, $namespaceId) = $this->defineElementsForImportDataItem($importDataItem);
     } else {
         list($localName, $namespace, $namespaceId) = $this->defineElementsForDiWikiPage($diWikiPage, $modifier);
     }
     $resource = new ExpNsResource($localName, $namespace, $namespaceId, $diWikiPage);
     $poolCache->save($hash, $resource);
     return $resource;
 }
 /**
  * @since 2.0
  */
 public function clear()
 {
     $this->connectionManager->releaseConnections();
     InMemoryPoolCache::getInstance()->resetPoolCacheFor('store.redirectTarget.lookup');
 }
 /**
  * Serialize the given SMWExpData object, possibly recursively with
  * increased indentation.
  *
  * @param $data SMWExpData containing the data to be serialised.
  * @param $indent string specifying a prefix for indentation (usually a sequence of tabs)
  */
 protected function serializeNestedExpData(SMWExpData $data, $indent)
 {
     if (count($data->getProperties()) == 0) {
         return;
         // nothing to export
     }
     // Avoid posting turtle property declarations already known for the
     // subject more than once
     if ($data->getSubject()->getDataItem() !== null && $data->getSubject()->getDataItem()->getNamespace() === SMW_NS_PROPERTY) {
         $hash = $data->getHash();
         $poolCache = InMemoryPoolCache::getInstance()->getPoolCacheFor('turtle.serializer');
         if ($poolCache->contains($hash) && $poolCache->fetch($hash)) {
             return;
         }
         $poolCache->save($hash, true);
     }
     $this->recordDeclarationTypes($data);
     $bnode = false;
     $this->post_ns_buffer .= $indent;
     if (!$data->getSubject()->isBlankNode()) {
         $this->serializeExpResource($data->getSubject());
     } else {
         // blank node
         $bnode = true;
         $this->post_ns_buffer .= "[";
     }
     if ($indent !== '' && !$bnode) {
         // called to generate a nested descripion; but Turtle cannot nest non-bnode descriptions, do this later
         $this->subexpdata[] = $data;
         return;
     } elseif (!$bnode) {
         $this->post_ns_buffer .= "\n ";
     }
     $firstproperty = true;
     foreach ($data->getProperties() as $property) {
         $this->post_ns_buffer .= $firstproperty ? "\t" : " ;\n {$indent}\t";
         $firstproperty = false;
         $prop_decl_queued = false;
         $class_type_prop = $this->isOWLClassTypeProperty($property);
         $this->serializeExpResource($property);
         $firstvalue = true;
         foreach ($data->getValues($property) as $value) {
             $this->post_ns_buffer .= $firstvalue ? '  ' : ' ,  ';
             $firstvalue = false;
             if ($value instanceof SMWExpLiteral) {
                 $prop_decl_type = SMW_SERIALIZER_DECL_APROP;
                 $this->serializeExpLiteral($value);
             } elseif ($value instanceof SMWExpResource) {
                 $prop_decl_type = SMW_SERIALIZER_DECL_OPROP;
                 $this->serializeExpResource($value);
             } elseif ($value instanceof SMWExpData) {
                 // resource (maybe blank node), could have subdescriptions
                 $prop_decl_type = SMW_SERIALIZER_DECL_OPROP;
                 $collection = $value->getCollection();
                 if ($collection !== false) {
                     // RDF-style collection (list)
                     $this->post_ns_buffer .= "( ";
                     foreach ($collection as $subvalue) {
                         $this->serializeNestedExpData($subvalue, $indent . "\t\t");
                         if ($class_type_prop) {
                             $this->requireDeclaration($subvalue->getSubject(), SMW_SERIALIZER_DECL_CLASS);
                         }
                     }
                     $this->post_ns_buffer .= " )";
                 } else {
                     if ($class_type_prop) {
                         $this->requireDeclaration($value->getSubject(), SMW_SERIALIZER_DECL_CLASS);
                     }
                     if (count($value->getProperties()) > 0) {
                         // resource with data: serialise
                         $this->post_ns_buffer .= "\n";
                         $this->serializeNestedExpData($value, $indent . "\t\t");
                     } else {
                         // resource without data: may need to be queued
                         $this->serializeExpResource($value->getSubject());
                     }
                 }
             }
             if (!$prop_decl_queued) {
                 $this->requireDeclaration($property, $prop_decl_type);
                 $prop_decl_queued = true;
             }
         }
     }
     $this->post_ns_buffer .= ($bnode ? " ]" : " .") . ($indent === '' ? "\n\n" : '');
 }
 private function registerCallbackHandlers($callbackLoader)
 {
     $callbackLoader->registerExpectedReturnType('Settings', '\\SMW\\Settings');
     $callbackLoader->registerCallback('Settings', function () use($callbackLoader) {
         return Settings::newFromGlobals();
     });
     $callbackLoader->registerExpectedReturnType('Store', '\\SMW\\Store');
     $callbackLoader->registerCallback('Store', function ($store = null) use($callbackLoader) {
         return StoreFactory::getStore($store !== null ? $store : $callbackLoader->singleton('Settings')->get('smwgDefaultStore'));
     });
     $callbackLoader->registerExpectedReturnType('Cache', '\\Onoi\\Cache\\Cache');
     $callbackLoader->registerCallback('Cache', function ($cacheType = null) use($callbackLoader) {
         return $callbackLoader->load('CacheFactory')->newMediaWikiCompositeCache($cacheType);
     });
     $callbackLoader->registerCallback('NamespaceExaminer', function () use($callbackLoader) {
         return NamespaceExaminer::newFromArray($callbackLoader->singleton('Settings')->get('smwgNamespacesWithSemanticLinks'));
     });
     $callbackLoader->registerExpectedReturnType('ParserData', '\\SMW\\ParserData');
     $callbackLoader->registerCallback('ParserData', function (\Title $title, \ParserOutput $parserOutput) {
         return new ParserData($title, $parserOutput);
     });
     $callbackLoader->registerCallback('MessageFormatter', function (\Language $language) {
         return new MessageFormatter($language);
     });
     $callbackLoader->registerCallback('MediaWikiNsContentReader', function () use($callbackLoader) {
         $callbackLoader->registerExpectedReturnType('MediaWikiNsContentReader', '\\SMW\\MediaWiki\\MediaWikiNsContentReader');
         return new MediaWikiNsContentReader();
     });
     $callbackLoader->registerExpectedReturnType('PageCreator', '\\SMW\\MediaWiki\\PageCreator');
     $callbackLoader->registerCallback('PageCreator', function () {
         return new PageCreator();
     });
     $callbackLoader->registerCallback('JobQueueLookup', function (Database $connection) use($callbackLoader) {
         $callbackLoader->registerExpectedReturnType('JobQueueLookup', '\\SMW\\MediaWiki\\JobQueueLookup');
         return new JobQueueLookup($connection);
     });
     $callbackLoader->registerCallback('ManualEntryLogger', function () use($callbackLoader) {
         $callbackLoader->registerExpectedReturnType('ManualEntryLogger', '\\SMW\\MediaWiki\\ManualEntryLogger');
         return new ManualEntryLogger();
     });
     $callbackLoader->registerExpectedReturnType('TitleCreator', '\\SMW\\MediaWiki\\TitleCreator');
     $callbackLoader->registerCallback('TitleCreator', function () {
         return new TitleCreator();
     });
     $callbackLoader->registerExpectedReturnType('ContentParser', '\\SMW\\ContentParser');
     $callbackLoader->registerCallback('ContentParser', function (\Title $title) {
         return new ContentParser($title);
     });
     $callbackLoader->registerExpectedReturnType('DeferredCallableUpdate', '\\SMW\\DeferredCallableUpdate');
     $callbackLoader->registerCallback('DeferredCallableUpdate', function (\Closure $callback) {
         return new DeferredCallableUpdate($callback);
     });
     /**
      * @var InMemoryPoolCache
      */
     $callbackLoader->registerCallback('InMemoryPoolCache', function () use($callbackLoader) {
         $callbackLoader->registerExpectedReturnType('InMemoryPoolCache', '\\SMW\\InMemoryPoolCache');
         return InMemoryPoolCache::getInstance();
     });
     /**
      * @var PropertyAnnotatorFactory
      */
     $callbackLoader->registerCallback('PropertyAnnotatorFactory', function () use($callbackLoader) {
         $callbackLoader->registerExpectedReturnType('PropertyAnnotatorFactory', '\\SMW\\PropertyAnnotatorFactory');
         return new PropertyAnnotatorFactory();
     });
 }
 public function testRedirectTargetForCachedLookup()
 {
     $dataItem = new DIWikiPage('Foo', NS_MAIN);
     $expNsResource = new ExpNsResource('Foo', 'Bar', '', $dataItem);
     $poolCache = InMemoryPoolCache::getInstance()->getPoolCacheFor('sparql.store.redirectlookup');
     $poolCache->save($expNsResource->getUri(), $expNsResource);
     $repositoryConnection = $this->getMockBuilder('\\SMW\\SPARQLStore\\RepositoryConnection')->disableOriginalConstructor()->getMock();
     $instance = new RedirectLookup($repositoryConnection);
     $exists = null;
     $instance->findRedirectTargetResource($expNsResource, $exists);
     $this->assertTrue($exists);
     $instance->reset();
 }
 protected function setUp()
 {
     $this->inMemoryPoolCache = InMemoryPoolCache::getInstance();
 }
 /**
  * @since 2.1
  *
  * @param string $title
  * @param integer $namespace
  */
 public function deleteRedirectEntry($title, $namespace)
 {
     $this->delete($title, $namespace);
     $hash = HashBuilder::createHashIdFromSegments($title, $namespace);
     $this->inMemoryPoolCache->getPoolCacheFor('sql.store.redirect.infostore')->delete($hash);
 }