36 private $linkBatchFactory;
39 private $loadBalancer;
42 private const CACHE_TTL = 10;
43 private const CACHE_SIZE = 100;
56 return MediaWikiServices::getInstance()->getPageProps();
67 $this->linkBatchFactory = $linkBatchFactory;
68 $this->loadBalancer = $loadBalancer;
77 if ( $this->cache->getMaxSize() < $size ) {
78 $this->cache->setMaxSize( $size );
101 if ( is_array( $propertyNames ) ) {
104 $propertyNames = [ $propertyNames ];
109 $goodIDs = $this->getGoodIDs( $titles );
111 foreach ( $goodIDs as $pageID ) {
112 foreach ( $propertyNames as $propertyName ) {
113 $propertyValue = $this->getCachedProperty( $pageID, $propertyName );
114 if ( $propertyValue ===
false ) {
115 $queryIDs[] = $pageID;
117 } elseif ( $gotArray ) {
118 $values[$pageID][$propertyName] = $propertyValue;
120 $values[$pageID] = $propertyValue;
126 $queryBuilder = $this->loadBalancer->getConnectionRef(
DB_REPLICA )->newSelectQueryBuilder();
127 $queryBuilder->select( [
'pp_page',
'pp_propname',
'pp_value' ] )
128 ->from(
'page_props' )
129 ->where( [
'pp_page' => $queryIDs,
'pp_propname' => $propertyNames ] )
130 ->caller( __METHOD__ );
131 $result = $queryBuilder->fetchResultSet();
133 foreach ( $result as $row ) {
134 $pageID = $row->pp_page;
135 $propertyName = $row->pp_propname;
136 $propertyValue = $row->pp_value;
137 $this->cache->setField( $pageID, $propertyName, $propertyValue );
139 $values[$pageID][$propertyName] = $propertyValue;
141 $values[$pageID] = $propertyValue;
164 $goodIDs = $this->getGoodIDs( $titles );
166 foreach ( $goodIDs as $pageID ) {
167 $pageProperties = $this->getCachedProperties( $pageID );
168 if ( $pageProperties ===
false ) {
169 $queryIDs[] = $pageID;
171 $values[$pageID] = $pageProperties;
175 if ( $queryIDs != [] ) {
176 $queryBuilder = $this->loadBalancer->getConnectionRef(
DB_REPLICA )->newSelectQueryBuilder();
177 $queryBuilder->select( [
'pp_page',
'pp_propname',
'pp_value' ] )
178 ->from(
'page_props' )
179 ->where( [
'pp_page' => $queryIDs ] )
180 ->caller( __METHOD__ );
181 $result = $queryBuilder->fetchResultSet();
184 $pageProperties = [];
185 foreach ( $result as $row ) {
186 $pageID = $row->pp_page;
187 if ( $currentPageID != $pageID ) {
188 if ( $pageProperties ) {
190 $this->cacheProperties( $currentPageID, $pageProperties );
191 $values[$currentPageID] = $pageProperties;
193 $currentPageID = $pageID;
194 $pageProperties = [];
196 $pageProperties[$row->pp_propname] = $row->pp_value;
198 if ( $pageProperties != [] ) {
201 $this->cacheProperties( $pageID, $pageProperties );
204 $values[$pageID] = $pageProperties;
215 private function getGoodIDs( $titles ) {
217 if ( is_iterable( $titles ) ) {
219 ( is_array( $titles ) && reset( $titles ) instanceof
Title
225 $this->linkBatchFactory->newLinkBatch( $titles )->execute();
228 foreach ( $titles as
$title ) {
231 if (
$title->canExist() ) {
232 $pageID =
$title->getId();
241 if ( $titles->canExist() ) {
242 $pageID = $titles->getId();
258 private function getCachedProperty( $pageID, $propertyName ) {
259 if ( $this->cache->hasField( $pageID, $propertyName, self::CACHE_TTL ) ) {
260 return $this->cache->getField( $pageID, $propertyName );
262 if ( $this->cache->hasField( 0, $pageID, self::CACHE_TTL ) ) {
263 $pageProperties = $this->cache->getField( 0, $pageID );
264 if ( isset( $pageProperties[$propertyName] ) ) {
265 return $pageProperties[$propertyName];
277 private function getCachedProperties( $pageID ) {
278 if ( $this->cache->hasField( 0, $pageID, self::CACHE_TTL ) ) {
279 return $this->cache->getField( 0, $pageID );
290 private function cacheProperties( $pageID, $pageProperties ) {
291 $this->cache->clear( $pageID );
292 $this->cache->setField( 0, $pageID, $pageProperties );
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Gives access to properties of a page.
__construct(LinkBatchFactory $linkBatchFactory, ILoadBalancer $loadBalancer)
ensureCacheSize( $size)
Ensure that cache has at least this size.
getProperties( $titles, $propertyNames)
Given one or more Titles and one or more names of properties, returns an associative array mapping pa...
getAllProperties( $titles)
Get all page property values.