| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Page; |
| 22 |
|
| 23 |
use MapCacheLRU; |
| 24 |
use MediaWiki\Cache\LinkBatchFactory; |
| 25 |
use MediaWiki\Title\Title; |
| 26 |
use MediaWiki\Title\TitleArrayFromResult; |
| 27 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class PageProps { |
| 36 |
|
| 37 |
private const CACHE_TTL = 10; |
| 38 |
|
| 39 |
private const CACHE_SIZE = 100; |
| 40 |
|
| 41 |
private LinkBatchFactory $linkBatchFactory; |
| 42 |
private IConnectionProvider $dbProvider; |
| 43 |
private MapCacheLRU $cache; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public function __construct( |
| 50 |
LinkBatchFactory $linkBatchFactory, |
| 51 |
IConnectionProvider $dbProvider |
| 52 |
) { |
| 53 |
$this->linkBatchFactory = $linkBatchFactory; |
| 54 |
$this->dbProvider = $dbProvider; |
| 55 |
$this->cache = new MapCacheLRU( self::CACHE_SIZE ); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public function ensureCacheSize( $size ) { |
| 63 |
if ( $this->cache->getMaxSize() < $size ) { |
| 64 |
$this->cache->setMaxSize( $size ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function getProperties( $titles, $propertyNames ) { |
| 92 |
if ( is_array( $propertyNames ) ) { |
| 93 |
$gotArray = true; |
| 94 |
} else { |
| 95 |
$propertyNames = [ $propertyNames ]; |
| 96 |
$gotArray = false; |
| 97 |
} |
| 98 |
|
| 99 |
$values = []; |
| 100 |
$goodIDs = $this->getGoodIDs( $titles ); |
| 101 |
$queryIDs = []; |
| 102 |
foreach ( $goodIDs as $pageID ) { |
| 103 |
foreach ( $propertyNames as $propertyName ) { |
| 104 |
$propertyValue = $this->getCachedProperty( $pageID, $propertyName ); |
| 105 |
if ( $propertyValue === false ) { |
| 106 |
$queryIDs[] = $pageID; |
| 107 |
break; |
| 108 |
} elseif ( $gotArray ) { |
| 109 |
$values[$pageID][$propertyName] = $propertyValue; |
| 110 |
} else { |
| 111 |
$values[$pageID] = $propertyValue; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
if ( $queryIDs ) { |
| 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(); |
| 123 |
|
| 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 ); |
| 129 |
if ( $gotArray ) { |
| 130 |
$values[$pageID][$propertyName] = $propertyValue; |
| 131 |
} else { |
| 132 |
$values[$pageID] = $propertyValue; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
return $values; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public function getAllProperties( $titles ) { |
| 158 |
$values = []; |
| 159 |
$goodIDs = $this->getGoodIDs( $titles ); |
| 160 |
$queryIDs = []; |
| 161 |
foreach ( $goodIDs as $pageID ) { |
| 162 |
$pageProperties = $this->getCachedProperties( $pageID ); |
| 163 |
if ( $pageProperties === false ) { |
| 164 |
$queryIDs[] = $pageID; |
| 165 |
} else { |
| 166 |
$values[$pageID] = $pageProperties; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 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(); |
| 177 |
|
| 178 |
$currentPageID = 0; |
| 179 |
$pageProperties = []; |
| 180 |
foreach ( $result as $row ) { |
| 181 |
$pageID = $row->pp_page; |
| 182 |
if ( $currentPageID != $pageID ) { |
| 183 |
if ( $pageProperties ) { |
| 184 |
|
| 185 |
$this->cacheProperties( $currentPageID, $pageProperties ); |
| 186 |
$values[$currentPageID] = $pageProperties; |
| 187 |
} |
| 188 |
$currentPageID = $pageID; |
| 189 |
$pageProperties = []; |
| 190 |
} |
| 191 |
$pageProperties[$row->pp_propname] = $row->pp_value; |
| 192 |
} |
| 193 |
if ( $pageProperties != [] ) { |
| 194 |
|
| 195 |
|
| 196 |
$this->cacheProperties( $pageID, $pageProperties ); |
| 197 |
|
| 198 |
|
| 199 |
$values[$pageID] = $pageProperties; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
return $values; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
private function getGoodIDs( $titles ) { |
| 211 |
$result = []; |
| 212 |
if ( is_iterable( $titles ) ) { |
| 213 |
if ( $titles instanceof TitleArrayFromResult || |
| 214 |
( is_array( $titles ) && reset( $titles ) instanceof Title |
| 215 |
) ) { |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
$this->linkBatchFactory->newLinkBatch( $titles )->execute(); |
| 221 |
} |
| 222 |
|
| 223 |
foreach ( $titles as $title ) { |
| 224 |
|
| 225 |
|
| 226 |
if ( $title->canExist() ) { |
| 227 |
$pageID = $title->getId(); |
| 228 |
if ( $pageID > 0 ) { |
| 229 |
$result[] = $pageID; |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
} else { |
| 234 |
|
| 235 |
|
| 236 |
if ( $titles->canExist() ) { |
| 237 |
$pageID = $titles->getId(); |
| 238 |
if ( $pageID > 0 ) { |
| 239 |
$result[] = $pageID; |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
return $result; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
private function getCachedProperty( $pageID, $propertyName ) { |
| 254 |
if ( $this->cache->hasField( $pageID, $propertyName, self::CACHE_TTL ) ) { |
| 255 |
return $this->cache->getField( $pageID, $propertyName ); |
| 256 |
} |
| 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]; |
| 261 |
} |
| 262 |
} |
| 263 |
return false; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
private function getCachedProperties( $pageID ) { |
| 273 |
if ( $this->cache->hasField( 0, $pageID, self::CACHE_TTL ) ) { |
| 274 |
return $this->cache->getField( 0, $pageID ); |
| 275 |
} |
| 276 |
return false; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
private function cacheProperties( $pageID, $pageProperties ) { |
| 286 |
$this->cache->clear( $pageID ); |
| 287 |
$this->cache->setField( 0, $pageID, $pageProperties ); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
class_alias( PageProps::class, 'PageProps' ); |