23 if ( !isset( $gadgets[$id] ) ) {
24 throw new InvalidArgumentException(
"No gadget registered for '$id'" );
33 return array_keys( $gadgets );
50 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
55 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
71 if ( $this->definitionCache !==
null ) {
77 $wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
82 $value = $t1Cache->get( $key );
85 $cutoffAge = mt_rand( 7 * 1e6, 15 * 1e6 ) / 1e6;
87 if ( $value && ( microtime(
true ) - $value[
'time'] ) < $cutoffAge ) {
88 $this->definitionCache = $value[
'gadgets'];
92 $ckTime = $wanCache->getCheckKeyTime( $key ) + WANObjectCache::HOLDOFF_TTL;
93 if ( $value && $value[
'time'] > $ckTime ) {
94 $this->definitionCache = $value[
'gadgets'];
101 $value = $wanCache->getWithSetCallback(
104 function ( $old, &$ttl, &$setOpts ) use ( $us ) {
107 $now = microtime(
true );
108 $gadgets = $us->fetchStructuredList();
109 if ( $gadgets ===
false ) {
110 $ttl = WANObjectCache::TTL_UNCACHEABLE;
113 return [
'gadgets' => $gadgets,
'time' => $now ];
115 [
'checkKeys' => [ $key ],
'lockTSE' => 300 ]
119 if ( $value[
'gadgets'] !==
false && $value[
'time'] > $ckTime ) {
121 $t1Cache->set( $key, $value, mt_rand( 300, 600 ) );
124 $this->definitionCache = $value[
'gadgets'];
136 if ( $forceNewText ===
null ) {
140 if ( !$rev || !$rev->getContent() || $rev->getContent()->isEmpty() ) {
144 $g = $rev->getContent()->getNativeData();
150 if ( !count( $gadgets ) ) {
154 $source = $forceNewText !==
null ?
'input text' :
'MediaWiki:Gadgets-definition';
155 wfDebug( __METHOD__ .
": $source parsed, cache entry should be updated\n" );
167 $definition = preg_replace(
'/<!--.*?-->/s',
'', $definition );
168 $lines = preg_split(
'/(\r\n|\r|\n)+/', $definition );
175 if ( preg_match(
'/^==+ *([^*:\s|]+?)\s*==+\s*$/',
$line, $m ) ) {
180 $gadgets[$gadget->getName()] = $gadget;
197 '/^\*+ *([a-zA-Z](?:[-_:.\w\d ]*[a-zA-Z0-9])?)(\s*\[.*?\])?\s*((\|[^|]*)+)\s*$/',
206 $info = [
'category' => $category ];
207 $info[
'name'] = trim( str_replace(
' ',
'_', $m[1] ) );
213 $info[
'definition'] = $definition;
214 $options = trim( $m[2],
' []' );
216 foreach ( preg_split(
'/\s*\|\s*/', $options, -1, PREG_SPLIT_NO_EMPTY ) as $option ) {
217 $arr = preg_split(
'/\s*=\s*/', $option, 2 );
219 if ( isset( $arr[1] ) ) {
220 $params = explode(
',', $arr[1] );
221 $params = array_map(
'trim', $params );
227 case 'ResourceLoader':
228 $info[
'resourceLoaded'] =
true;
231 $info[
'dependencies'] = $params;
234 $info[
'peers'] = $params;
237 $info[
'requiredRights'] = $params;
240 $info[
'hidden'] =
true;
243 $info[
'requiredSkins'] = $params;
246 $info[
'onByDefault'] =
true;
249 $info[
'targets'] = $params;
253 $info[
'type'] = $params[0] ??
'';
258 foreach ( preg_split(
'/\s*\|\s*/', $m[3], -1, PREG_SPLIT_NO_EMPTY ) as $page ) {
259 $page =
"MediaWiki:Gadget-$page";
261 if ( preg_match(
'/\.js/', $page ) ) {
262 $info[
'scripts'][] = $page;
263 } elseif ( preg_match(
'/\.css/', $page ) ) {
264 $info[
'styles'][] = $page;
268 return new Gadget( $info );