37 private const CACHE_TTL = 10;
39 private const CACHE_SIZE = 100;
53 $this->linkBatchFactory = $linkBatchFactory;
54 $this->dbProvider = $dbProvider;
63 if ( $this->cache->getMaxSize() < $size ) {
64 $this->cache->setMaxSize( $size );
92 if ( is_array( $propertyNames ) ) {
95 $propertyNames = [ $propertyNames ];
100 $goodIDs = $this->getGoodIDs( $titles );
102 foreach ( $goodIDs as $pageID ) {
103 foreach ( $propertyNames as $propertyName ) {
104 $propertyValue = $this->getCachedProperty( $pageID, $propertyName );
105 if ( $propertyValue ===
false ) {
106 $queryIDs[] = $pageID;
108 } elseif ( $gotArray ) {
109 $values[$pageID][$propertyName] = $propertyValue;
111 $values[$pageID] = $propertyValue;
117 $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder();
118 $queryBuilder->select( [
'pp_page',
'pp_propname',
'pp_value' ] )
119 ->from(
'page_props' )
120 ->where( [
'pp_page' => $queryIDs,
'pp_propname' => $propertyNames ] )
121 ->caller( __METHOD__ );
122 $result = $queryBuilder->fetchResultSet();
124 foreach ( $result as $row ) {
125 $pageID = $row->pp_page;
126 $propertyName = $row->pp_propname;
127 $propertyValue = $row->pp_value;
128 $this->cache->setField( $pageID, $propertyName, $propertyValue );
130 $values[$pageID][$propertyName] = $propertyValue;
132 $values[$pageID] = $propertyValue;
159 $goodIDs = $this->getGoodIDs( $titles );
161 foreach ( $goodIDs as $pageID ) {
162 $pageProperties = $this->getCachedProperties( $pageID );
163 if ( $pageProperties ===
false ) {
164 $queryIDs[] = $pageID;
166 $values[$pageID] = $pageProperties;
170 if ( $queryIDs != [] ) {
171 $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder();
172 $queryBuilder->select( [
'pp_page',
'pp_propname',
'pp_value' ] )
173 ->from(
'page_props' )
174 ->where( [
'pp_page' => $queryIDs ] )
175 ->caller( __METHOD__ );
176 $result = $queryBuilder->fetchResultSet();
179 $pageProperties = [];
180 foreach ( $result as $row ) {
181 $pageID = $row->pp_page;
182 if ( $currentPageID != $pageID ) {
183 if ( $pageProperties ) {
185 $this->cacheProperties( $currentPageID, $pageProperties );
186 $values[$currentPageID] = $pageProperties;
188 $currentPageID = $pageID;
189 $pageProperties = [];
191 $pageProperties[$row->pp_propname] = $row->pp_value;
193 if ( $pageProperties != [] ) {
196 $this->cacheProperties( $pageID, $pageProperties );
199 $values[$pageID] = $pageProperties;
210 private function getGoodIDs( $titles ) {
212 if ( is_iterable( $titles ) ) {
214 ( is_array( $titles ) && reset( $titles ) instanceof
Title
220 $this->linkBatchFactory->newLinkBatch( $titles )->execute();
223 foreach ( $titles as $title ) {
226 if ( $title->canExist() ) {
227 $pageID = $title->getId();
236 if ( $titles->canExist() ) {
237 $pageID = $titles->getId();
253 private function getCachedProperty( $pageID, $propertyName ) {
254 if ( $this->cache->hasField( $pageID, $propertyName, self::CACHE_TTL ) ) {
255 return $this->cache->getField( $pageID, $propertyName );
257 if ( $this->cache->hasField( 0, $pageID, self::CACHE_TTL ) ) {
258 $pageProperties = $this->cache->getField( 0, $pageID );
259 if ( isset( $pageProperties[$propertyName] ) ) {
260 return $pageProperties[$propertyName];
272 private function getCachedProperties( $pageID ) {
273 if ( $this->cache->hasField( 0, $pageID, self::CACHE_TTL ) ) {
274 return $this->cache->getField( 0, $pageID );
285 private function cacheProperties( $pageID, $pageProperties ) {
286 $this->cache->clear( $pageID );
287 $this->cache->setField( 0, $pageID, $pageProperties );