| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Extension\Gadgets; |
| 22 |
|
| 23 |
use InvalidArgumentException; |
| 24 |
use MediaWiki\Content\TextContent; |
| 25 |
use MediaWiki\Linker\LinkTarget; |
| 26 |
use MediaWiki\MediaWikiServices; |
| 27 |
use MediaWiki\Revision\RevisionLookup; |
| 28 |
use MediaWiki\Revision\SlotRecord; |
| 29 |
use MediaWiki\Title\Title; |
| 30 |
use Wikimedia\ObjectCache\BagOStuff; |
| 31 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 32 |
use Wikimedia\Rdbms\Database; |
| 33 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class MediaWikiGadgetsDefinitionRepo extends GadgetRepo { |
| 39 |
private const CACHE_VERSION = 4; |
| 40 |
|
| 41 |
|
| 42 |
private $definitions; |
| 43 |
|
| 44 |
private IConnectionProvider $dbProvider; |
| 45 |
private WANObjectCache $wanCache; |
| 46 |
private RevisionLookup $revLookup; |
| 47 |
private BagOStuff $srvCache; |
| 48 |
|
| 49 |
public function __construct( |
| 50 |
IConnectionProvider $dbProvider, |
| 51 |
WANObjectCache $wanCache, |
| 52 |
RevisionLookup $revLookup, |
| 53 |
BagOStuff $srvCache |
| 54 |
) { |
| 55 |
$this->dbProvider = $dbProvider; |
| 56 |
$this->wanCache = $wanCache; |
| 57 |
$this->revLookup = $revLookup; |
| 58 |
$this->srvCache = $srvCache; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function getGadget( string $id ): Gadget { |
| 67 |
$gadgets = $this->loadGadgets(); |
| 68 |
if ( !isset( $gadgets[$id] ) ) { |
| 69 |
throw new InvalidArgumentException( "No gadget registered for '$id'" ); |
| 70 |
} |
| 71 |
|
| 72 |
return new Gadget( $gadgets[$id] ); |
| 73 |
} |
| 74 |
|
| 75 |
public function getGadgetIds(): array { |
| 76 |
return array_keys( $this->loadGadgets() ); |
| 77 |
} |
| 78 |
|
| 79 |
public function handlePageUpdate( LinkTarget $target ): void { |
| 80 |
if ( $target->inNamespace( NS_MEDIAWIKI ) && $target->getDBkey() === 'Gadgets-definition' ) { |
| 81 |
$this->purgeDefinitionCache(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
private function purgeDefinitionCache(): void { |
| 89 |
$key = $this->makeDefinitionCacheKey( $this->wanCache ); |
| 90 |
|
| 91 |
$this->wanCache->delete( $key ); |
| 92 |
$this->srvCache->delete( $key ); |
| 93 |
$this->definitions = null; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
private function makeDefinitionCacheKey( WANObjectCache $cache ) { |
| 101 |
return $cache->makeKey( |
| 102 |
'gadgets-definition', |
| 103 |
Gadget::GADGET_CLASS_VERSION, |
| 104 |
self::CACHE_VERSION |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
protected function loadGadgets(): array { |
| 114 |
if ( defined( 'MW_PHPUNIT_TEST' ) && MediaWikiServices::getInstance()->isStorageDisabled() ) { |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
return []; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
if ( $this->definitions === null ) { |
| 132 |
$key = $this->makeDefinitionCacheKey( $this->wanCache ); |
| 133 |
$this->definitions = $this->srvCache->getWithSetCallback( |
| 134 |
$key, |
| 135 |
|
| 136 |
mt_rand( 7, 15 ), |
| 137 |
function () use ( $key ) { |
| 138 |
return $this->wanCache->getWithSetCallback( |
| 139 |
$key, |
| 140 |
|
| 141 |
Gadget::CACHE_TTL, |
| 142 |
function ( $old, &$ttl, &$setOpts ) { |
| 143 |
|
| 144 |
$setOpts += Database::getCacheSetOptions( $this->dbProvider->getReplicaDatabase() ); |
| 145 |
|
| 146 |
return $this->fetchStructuredList(); |
| 147 |
}, |
| 148 |
[ |
| 149 |
'version' => 2, |
| 150 |
|
| 151 |
'lockTSE' => 300, |
| 152 |
] |
| 153 |
); |
| 154 |
} |
| 155 |
); |
| 156 |
} |
| 157 |
return $this->definitions; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public function fetchStructuredList() { |
| 166 |
|
| 167 |
$title = Title::makeTitle( NS_MEDIAWIKI, 'Gadgets-definition' ); |
| 168 |
$revRecord = $this->revLookup->getRevisionByTitle( $title ); |
| 169 |
if ( !$revRecord |
| 170 |
|| !$revRecord->getContent( SlotRecord::MAIN ) |
| 171 |
|| $revRecord->getContent( SlotRecord::MAIN )->isEmpty() |
| 172 |
) { |
| 173 |
return []; |
| 174 |
} |
| 175 |
|
| 176 |
$content = $revRecord->getContent( SlotRecord::MAIN ); |
| 177 |
$g = ( $content instanceof TextContent ) ? $content->getText() : ''; |
| 178 |
|
| 179 |
$gadgets = $this->listFromDefinition( $g ); |
| 180 |
|
| 181 |
wfDebug( __METHOD__ . ": MediaWiki:Gadgets-definition parsed, cache entry should be updated\n" ); |
| 182 |
|
| 183 |
return $gadgets; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
private function listFromDefinition( $definition ): array { |
| 193 |
$definition = preg_replace( '/<!--.*?-->/s', '', $definition ); |
| 194 |
$lines = preg_split( '/(\r\n|\r|\n)+/', $definition ); |
| 195 |
|
| 196 |
$gadgets = []; |
| 197 |
$section = ''; |
| 198 |
|
| 199 |
foreach ( $lines as $line ) { |
| 200 |
$m = []; |
| 201 |
if ( preg_match( '/^==+ *([^*:\s|]+)\s*(?<!=)==+\s*$/', $line, $m ) ) { |
| 202 |
$section = $m[1]; |
| 203 |
} else { |
| 204 |
$gadget = $this->newFromDefinition( $line, $section ); |
| 205 |
if ( $gadget ) { |
| 206 |
$gadgets[$gadget->getName()] = $gadget->toArray(); |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
return $gadgets; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public function newFromDefinition( $definition, $category ) { |
| 221 |
if ( !preg_match( |
| 222 |
'/^\*+ *([a-zA-Z](?:[-_:.\w ]*[a-zA-Z0-9])?)(\s*\[.*?\])?\s*((\|[^|]*)+)\s*$/', |
| 223 |
$definition, |
| 224 |
$matches |
| 225 |
) ) { |
| 226 |
return false; |
| 227 |
} |
| 228 |
[ , $name, $options, $pages ] = $matches; |
| 229 |
$options = trim( $options, ' []' ); |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
$name = str_replace( ' ', '_', $name ); |
| 235 |
|
| 236 |
|
| 237 |
if ( !Gadget::isValidGadgetID( $name ) ) { |
| 238 |
return false; |
| 239 |
} |
| 240 |
|
| 241 |
$info = [ |
| 242 |
'category' => $category, |
| 243 |
'name' => $name, |
| 244 |
'definition' => $definition, |
| 245 |
]; |
| 246 |
|
| 247 |
foreach ( preg_split( '/\s*\|\s*/', $options, -1, PREG_SPLIT_NO_EMPTY ) as $option ) { |
| 248 |
$arr = preg_split( '/\s*=\s*/', $option, 2 ); |
| 249 |
$option = $arr[0]; |
| 250 |
if ( isset( $arr[1] ) ) { |
| 251 |
$params = explode( ',', $arr[1] ); |
| 252 |
$params = array_map( 'trim', $params ); |
| 253 |
} else { |
| 254 |
$params = []; |
| 255 |
} |
| 256 |
|
| 257 |
switch ( $option ) { |
| 258 |
case 'ResourceLoader': |
| 259 |
$info['resourceLoaded'] = true; |
| 260 |
break; |
| 261 |
case 'requiresES6': |
| 262 |
$info['requiresES6'] = true; |
| 263 |
break; |
| 264 |
case 'dependencies': |
| 265 |
$info['dependencies'] = $params; |
| 266 |
break; |
| 267 |
case 'peers': |
| 268 |
$info['peers'] = $params; |
| 269 |
break; |
| 270 |
case 'rights': |
| 271 |
$info['requiredRights'] = $params; |
| 272 |
break; |
| 273 |
case 'hidden': |
| 274 |
$info['hidden'] = true; |
| 275 |
break; |
| 276 |
case 'actions': |
| 277 |
$info['requiredActions'] = $params; |
| 278 |
break; |
| 279 |
case 'skins': |
| 280 |
$info['requiredSkins'] = $params; |
| 281 |
break; |
| 282 |
case 'namespaces': |
| 283 |
$info['requiredNamespaces'] = $params; |
| 284 |
break; |
| 285 |
case 'categories': |
| 286 |
$info['requiredCategories'] = $params; |
| 287 |
break; |
| 288 |
case 'contentModels': |
| 289 |
$info['requiredContentModels'] = $params; |
| 290 |
break; |
| 291 |
case 'default': |
| 292 |
$info['onByDefault'] = true; |
| 293 |
break; |
| 294 |
case 'package': |
| 295 |
$info['package'] = true; |
| 296 |
break; |
| 297 |
case 'type': |
| 298 |
|
| 299 |
$info['type'] = $params[0] ?? ''; |
| 300 |
break; |
| 301 |
case 'supportsUrlLoad': |
| 302 |
$val = $params[0] ?? ''; |
| 303 |
$info['supportsUrlLoad'] = $val !== 'false'; |
| 304 |
break; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
foreach ( preg_split( '/\s*\|\s*/', $pages, -1, PREG_SPLIT_NO_EMPTY ) as $page ) { |
| 309 |
$info['pages'][] = self::RESOURCE_TITLE_PREFIX . trim( $page ); |
| 310 |
} |
| 311 |
|
| 312 |
return new Gadget( $info ); |
| 313 |
} |
| 314 |
} |