22 private const CACHE_TTL = 10;
24 private const CACHE_SIZE = 100;
34 $this->linkBatchFactory = $linkBatchFactory;
35 $this->dbProvider = $dbProvider;
44 if ( $this->cache->getMaxSize() < $size ) {
45 $this->cache->setMaxSize( $size );
73 if ( is_array( $propertyNames ) ) {
76 $propertyNames = [ $propertyNames ];
81 $goodIDs = $this->getGoodIDs( $titles );
83 foreach ( $goodIDs as $pageID ) {
84 foreach ( $propertyNames as $propertyName ) {
85 $propertyValue = $this->getCachedProperty( $pageID, $propertyName );
86 if ( $propertyValue ===
false ) {
87 $queryIDs[] = $pageID;
89 } elseif ( $gotArray ) {
90 $values[$pageID][$propertyName] = $propertyValue;
92 $values[$pageID] = $propertyValue;
98 $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder();
99 $queryBuilder->select( [
'pp_page',
'pp_propname',
'pp_value' ] )
100 ->from(
'page_props' )
101 ->where( [
'pp_page' => $queryIDs,
'pp_propname' => $propertyNames ] )
102 ->caller( __METHOD__ );
103 $result = $queryBuilder->fetchResultSet();
105 foreach ( $result as $row ) {
106 $pageID = $row->pp_page;
107 $propertyName = $row->pp_propname;
108 $propertyValue = $row->pp_value;
109 $this->cache->setField( $pageID, $propertyName, $propertyValue );
111 $values[$pageID][$propertyName] = $propertyValue;
113 $values[$pageID] = $propertyValue;
140 $goodIDs = $this->getGoodIDs( $titles );
142 foreach ( $goodIDs as $pageID ) {
143 $pageProperties = $this->getCachedProperties( $pageID );
144 if ( $pageProperties ===
false ) {
145 $queryIDs[] = $pageID;
147 $values[$pageID] = $pageProperties;
151 if ( $queryIDs != [] ) {
152 $queryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder();
153 $queryBuilder->select( [
'pp_page',
'pp_propname',
'pp_value' ] )
154 ->from(
'page_props' )
155 ->where( [
'pp_page' => $queryIDs ] )
156 ->caller( __METHOD__ );
157 $result = $queryBuilder->fetchResultSet();
160 $pageProperties = [];
161 foreach ( $result as $row ) {
162 $pageID = $row->pp_page;
163 if ( $currentPageID != $pageID ) {
164 if ( $pageProperties ) {
166 $this->cacheProperties( $currentPageID, $pageProperties );
167 $values[$currentPageID] = $pageProperties;
169 $currentPageID = $pageID;
170 $pageProperties = [];
172 $pageProperties[$row->pp_propname] = $row->pp_value;
174 if ( $pageProperties != [] ) {
177 $this->cacheProperties( $pageID, $pageProperties );
180 $values[$pageID] = $pageProperties;
191 private function getGoodIDs( $titles ) {
193 if ( is_iterable( $titles ) ) {
195 ( is_array( $titles ) && reset( $titles ) instanceof
Title
201 $this->linkBatchFactory->newLinkBatch( $titles )->execute();
204 foreach ( $titles as $title ) {
207 if ( $title->canExist() ) {
208 $pageID = $title->getId();
217 if ( $titles->canExist() ) {
218 $pageID = $titles->getId();
234 private function getCachedProperty( $pageID, $propertyName ) {
235 if ( $this->cache->hasField( $pageID, $propertyName, self::CACHE_TTL ) ) {
236 return $this->cache->getField( $pageID, $propertyName );
238 if ( $this->cache->hasField( 0, $pageID, self::CACHE_TTL ) ) {
239 $pageProperties = $this->cache->getField( 0, $pageID );
240 if ( isset( $pageProperties[$propertyName] ) ) {
241 return $pageProperties[$propertyName];
253 private function getCachedProperties( $pageID ) {
254 if ( $this->cache->hasField( 0, $pageID, self::CACHE_TTL ) ) {
255 return $this->cache->getField( 0, $pageID );
266 private function cacheProperties( $pageID, $pageProperties ) {
267 $this->cache->clear( $pageID );
268 $this->cache->setField( 0, $pageID, $pageProperties );