39 private $linkBatchFactory;
42 private $loadBalancer;
45 private const CACHE_TTL = 10;
46 private const CACHE_SIZE = 100;
59 $this->linkBatchFactory = $linkBatchFactory;
60 $this->loadBalancer = $loadBalancer;
69 if ( $this->cache->getMaxSize() < $size ) {
70 $this->cache->setMaxSize( $size );
93 if ( is_array( $propertyNames ) ) {
96 $propertyNames = [ $propertyNames ];
101 $goodIDs = $this->getGoodIDs( $titles );
103 foreach ( $goodIDs as $pageID ) {
104 foreach ( $propertyNames as $propertyName ) {
105 $propertyValue = $this->getCachedProperty( $pageID, $propertyName );
106 if ( $propertyValue ===
false ) {
107 $queryIDs[] = $pageID;
109 } elseif ( $gotArray ) {
110 $values[$pageID][$propertyName] = $propertyValue;
112 $values[$pageID] = $propertyValue;
118 $queryBuilder = $this->loadBalancer->getConnectionRef(
DB_REPLICA )->newSelectQueryBuilder();
119 $queryBuilder->select( [
'pp_page',
'pp_propname',
'pp_value' ] )
120 ->from(
'page_props' )
121 ->where( [
'pp_page' => $queryIDs,
'pp_propname' => $propertyNames ] )
122 ->caller( __METHOD__ );
123 $result = $queryBuilder->fetchResultSet();
125 foreach ( $result as $row ) {
126 $pageID = $row->pp_page;
127 $propertyName = $row->pp_propname;
128 $propertyValue = $row->pp_value;
129 $this->cache->setField( $pageID, $propertyName, $propertyValue );
131 $values[$pageID][$propertyName] = $propertyValue;
133 $values[$pageID] = $propertyValue;
156 $goodIDs = $this->getGoodIDs( $titles );
158 foreach ( $goodIDs as $pageID ) {
159 $pageProperties = $this->getCachedProperties( $pageID );
160 if ( $pageProperties ===
false ) {
161 $queryIDs[] = $pageID;
163 $values[$pageID] = $pageProperties;
167 if ( $queryIDs != [] ) {
168 $queryBuilder = $this->loadBalancer->getConnectionRef(
DB_REPLICA )->newSelectQueryBuilder();
169 $queryBuilder->select( [
'pp_page',
'pp_propname',
'pp_value' ] )
170 ->from(
'page_props' )
171 ->where( [
'pp_page' => $queryIDs ] )
172 ->caller( __METHOD__ );
173 $result = $queryBuilder->fetchResultSet();
176 $pageProperties = [];
177 foreach ( $result as $row ) {
178 $pageID = $row->pp_page;
179 if ( $currentPageID != $pageID ) {
180 if ( $pageProperties ) {
182 $this->cacheProperties( $currentPageID, $pageProperties );
183 $values[$currentPageID] = $pageProperties;
185 $currentPageID = $pageID;
186 $pageProperties = [];
188 $pageProperties[$row->pp_propname] = $row->pp_value;
190 if ( $pageProperties != [] ) {
193 $this->cacheProperties( $pageID, $pageProperties );
196 $values[$pageID] = $pageProperties;
207 private function getGoodIDs( $titles ) {
209 if ( is_iterable( $titles ) ) {
211 ( is_array( $titles ) && reset( $titles ) instanceof
Title
217 $this->linkBatchFactory->newLinkBatch( $titles )->execute();
220 foreach ( $titles as
$title ) {
223 if (
$title->canExist() ) {
224 $pageID =
$title->getId();
233 if ( $titles->canExist() ) {
234 $pageID = $titles->getId();
250 private function getCachedProperty( $pageID, $propertyName ) {
251 if ( $this->cache->hasField( $pageID, $propertyName, self::CACHE_TTL ) ) {
252 return $this->cache->getField( $pageID, $propertyName );
254 if ( $this->cache->hasField( 0, $pageID, self::CACHE_TTL ) ) {
255 $pageProperties = $this->cache->getField( 0, $pageID );
256 if ( isset( $pageProperties[$propertyName] ) ) {
257 return $pageProperties[$propertyName];
269 private function getCachedProperties( $pageID ) {
270 if ( $this->cache->hasField( 0, $pageID, self::CACHE_TTL ) ) {
271 return $this->cache->getField( 0, $pageID );
282 private function cacheProperties( $pageID, $pageProperties ) {
283 $this->cache->clear( $pageID );
284 $this->cache->setField( 0, $pageID, $pageProperties );