37 private const CACHE_TTL = 10;
39 private const CACHE_SIZE = 100;
49 $this->linkBatchFactory = $linkBatchFactory;
50 $this->dbProvider = $dbProvider;
59 if ( $this->cache->getMaxSize() < $size ) {
60 $this->cache->setMaxSize( $size );
88 if ( is_array( $propertyNames ) ) {
91 $propertyNames = [ $propertyNames ];
96 $goodIDs = $this->getGoodIDs( $titles );
98 foreach ( $goodIDs as $pageID ) {
99 foreach ( $propertyNames as $propertyName ) {
100 $propertyValue = $this->getCachedProperty( $pageID, $propertyName );
101 if ( $propertyValue ===
false ) {
102 $queryIDs[] = $pageID;
104 } elseif ( $gotArray ) {
105 $values[$pageID][$propertyName] = $propertyValue;
107 $values[$pageID] = $propertyValue;
113 $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder();
114 $queryBuilder->select( [
'pp_page',
'pp_propname',
'pp_value' ] )
115 ->from(
'page_props' )
116 ->where( [
'pp_page' => $queryIDs,
'pp_propname' => $propertyNames ] )
117 ->caller( __METHOD__ );
118 $result = $queryBuilder->fetchResultSet();
120 foreach ( $result as $row ) {
121 $pageID = $row->pp_page;
122 $propertyName = $row->pp_propname;
123 $propertyValue = $row->pp_value;
124 $this->cache->setField( $pageID, $propertyName, $propertyValue );
126 $values[$pageID][$propertyName] = $propertyValue;
128 $values[$pageID] = $propertyValue;
155 $goodIDs = $this->getGoodIDs( $titles );
157 foreach ( $goodIDs as $pageID ) {
158 $pageProperties = $this->getCachedProperties( $pageID );
159 if ( $pageProperties ===
false ) {
160 $queryIDs[] = $pageID;
162 $values[$pageID] = $pageProperties;
166 if ( $queryIDs != [] ) {
167 $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder();
168 $queryBuilder->select( [
'pp_page',
'pp_propname',
'pp_value' ] )
169 ->from(
'page_props' )
170 ->where( [
'pp_page' => $queryIDs ] )
171 ->caller( __METHOD__ );
172 $result = $queryBuilder->fetchResultSet();
175 $pageProperties = [];
176 foreach ( $result as $row ) {
177 $pageID = $row->pp_page;
178 if ( $currentPageID != $pageID ) {
179 if ( $pageProperties ) {
181 $this->cacheProperties( $currentPageID, $pageProperties );
182 $values[$currentPageID] = $pageProperties;
184 $currentPageID = $pageID;
185 $pageProperties = [];
187 $pageProperties[$row->pp_propname] = $row->pp_value;
189 if ( $pageProperties != [] ) {
192 $this->cacheProperties( $pageID, $pageProperties );
195 $values[$pageID] = $pageProperties;
206 private function getGoodIDs( $titles ) {
208 if ( is_iterable( $titles ) ) {
210 ( is_array( $titles ) && reset( $titles ) instanceof
Title
216 $this->linkBatchFactory->newLinkBatch( $titles )->execute();
219 foreach ( $titles as $title ) {
222 if ( $title->canExist() ) {
223 $pageID = $title->getId();
232 if ( $titles->canExist() ) {
233 $pageID = $titles->getId();
249 private function getCachedProperty( $pageID, $propertyName ) {
250 if ( $this->cache->hasField( $pageID, $propertyName, self::CACHE_TTL ) ) {
251 return $this->cache->getField( $pageID, $propertyName );
253 if ( $this->cache->hasField( 0, $pageID, self::CACHE_TTL ) ) {
254 $pageProperties = $this->cache->getField( 0, $pageID );
255 if ( isset( $pageProperties[$propertyName] ) ) {
256 return $pageProperties[$propertyName];
268 private function getCachedProperties( $pageID ) {
269 if ( $this->cache->hasField( 0, $pageID, self::CACHE_TTL ) ) {
270 return $this->cache->getField( 0, $pageID );
281 private function cacheProperties( $pageID, $pageProperties ) {
282 $this->cache->clear( $pageID );
283 $this->cache->setField( 0, $pageID, $pageProperties );