MediaWiki  1.29.1
ResourceLoaderImageModule.php
Go to the documentation of this file.
1 <?php
30 
31  protected $definition = null;
32 
37  protected $localBasePath = '';
38 
40 
41  protected $images = [];
42  protected $variants = [];
43  protected $prefix = null;
44  protected $selectorWithoutVariant = '.{prefix}-{name}';
45  protected $selectorWithVariant = '.{prefix}-{name}-{variant}';
46  protected $targets = [ 'desktop', 'mobile' ];
47 
104  public function __construct( $options = [], $localBasePath = null ) {
105  $this->localBasePath = self::extractLocalBasePath( $options, $localBasePath );
106 
107  $this->definition = $options;
108  }
109 
113  protected function loadFromDefinition() {
114  if ( $this->definition === null ) {
115  return;
116  }
117 
119  $this->definition = null;
120 
121  if ( isset( $options['data'] ) ) {
122  $dataPath = $this->localBasePath . '/' . $options['data'];
123  $data = json_decode( file_get_contents( $dataPath ), true );
124  $options = array_merge( $data, $options );
125  }
126 
127  // Accepted combinations:
128  // * prefix
129  // * selector
130  // * selectorWithoutVariant + selectorWithVariant
131  // * prefix + selector
132  // * prefix + selectorWithoutVariant + selectorWithVariant
133 
134  $prefix = isset( $options['prefix'] ) && $options['prefix'];
135  $selector = isset( $options['selector'] ) && $options['selector'];
136  $selectorWithoutVariant = isset( $options['selectorWithoutVariant'] )
137  && $options['selectorWithoutVariant'];
138  $selectorWithVariant = isset( $options['selectorWithVariant'] )
139  && $options['selectorWithVariant'];
140 
142  throw new InvalidArgumentException(
143  "Given 'selectorWithoutVariant' but no 'selectorWithVariant'."
144  );
145  }
147  throw new InvalidArgumentException(
148  "Given 'selectorWithVariant' but no 'selectorWithoutVariant'."
149  );
150  }
151  if ( $selector && $selectorWithVariant ) {
152  throw new InvalidArgumentException(
153  "Incompatible 'selector' and 'selectorWithVariant'+'selectorWithoutVariant' given."
154  );
155  }
156  if ( !$prefix && !$selector && !$selectorWithVariant ) {
157  throw new InvalidArgumentException(
158  "None of 'prefix', 'selector' or 'selectorWithVariant'+'selectorWithoutVariant' given."
159  );
160  }
161 
162  foreach ( $options as $member => $option ) {
163  switch ( $member ) {
164  case 'images':
165  case 'variants':
166  if ( !is_array( $option ) ) {
167  throw new InvalidArgumentException(
168  "Invalid list error. '$option' given, array expected."
169  );
170  }
171  if ( !isset( $option['default'] ) ) {
172  // Backwards compatibility
173  $option = [ 'default' => $option ];
174  }
175  foreach ( $option as $skin => $data ) {
176  if ( !is_array( $option ) ) {
177  throw new InvalidArgumentException(
178  "Invalid list error. '$option' given, array expected."
179  );
180  }
181  }
182  $this->{$member} = $option;
183  break;
184 
185  case 'prefix':
186  case 'selectorWithoutVariant':
187  case 'selectorWithVariant':
188  $this->{$member} = (string)$option;
189  break;
190 
191  case 'selector':
192  $this->selectorWithoutVariant = $this->selectorWithVariant = (string)$option;
193  }
194  }
195  }
196 
201  public function getPrefix() {
202  $this->loadFromDefinition();
203  return $this->prefix;
204  }
205 
210  public function getSelectors() {
211  $this->loadFromDefinition();
212  return [
213  'selectorWithoutVariant' => $this->selectorWithoutVariant,
214  'selectorWithVariant' => $this->selectorWithVariant,
215  ];
216  }
217 
225  $this->loadFromDefinition();
226  $images = $this->getImages( $context );
227  return isset( $images[$name] ) ? $images[$name] : null;
228  }
229 
236  $skin = $context->getSkin();
237  if ( !isset( $this->imageObjects ) ) {
238  $this->loadFromDefinition();
239  $this->imageObjects = [];
240  }
241  if ( !isset( $this->imageObjects[$skin] ) ) {
242  $this->imageObjects[$skin] = [];
243  if ( !isset( $this->images[$skin] ) ) {
244  $this->images[$skin] = isset( $this->images['default'] ) ?
245  $this->images['default'] :
246  [];
247  }
248  foreach ( $this->images[$skin] as $name => $options ) {
249  $fileDescriptor = is_string( $options ) ? $options : $options['file'];
250 
251  $allowedVariants = array_merge(
252  is_array( $options ) && isset( $options['variants'] ) ? $options['variants'] : [],
253  $this->getGlobalVariants( $context )
254  );
255  if ( isset( $this->variants[$skin] ) ) {
256  $variantConfig = array_intersect_key(
257  $this->variants[$skin],
258  array_fill_keys( $allowedVariants, true )
259  );
260  } else {
261  $variantConfig = [];
262  }
263 
265  $name,
266  $this->getName(),
267  $fileDescriptor,
268  $this->localBasePath,
269  $variantConfig
270  );
271  $this->imageObjects[$skin][$image->getName()] = $image;
272  }
273  }
274 
275  return $this->imageObjects[$skin];
276  }
277 
285  $skin = $context->getSkin();
286  if ( !isset( $this->globalVariants ) ) {
287  $this->loadFromDefinition();
288  $this->globalVariants = [];
289  }
290  if ( !isset( $this->globalVariants[$skin] ) ) {
291  $this->globalVariants[$skin] = [];
292  if ( !isset( $this->variants[$skin] ) ) {
293  $this->variants[$skin] = isset( $this->variants['default'] ) ?
294  $this->variants['default'] :
295  [];
296  }
297  foreach ( $this->variants[$skin] as $name => $config ) {
298  if ( isset( $config['global'] ) && $config['global'] ) {
299  $this->globalVariants[$skin][] = $name;
300  }
301  }
302  }
303 
304  return $this->globalVariants[$skin];
305  }
306 
312  $this->loadFromDefinition();
313 
314  // Build CSS rules
315  $rules = [];
316  $script = $context->getResourceLoader()->getLoadScript( $this->getSource() );
317  $selectors = $this->getSelectors();
318 
319  foreach ( $this->getImages( $context ) as $name => $image ) {
320  $declarations = $this->getStyleDeclarations( $context, $image, $script );
321  $selector = strtr(
322  $selectors['selectorWithoutVariant'],
323  [
324  '{prefix}' => $this->getPrefix(),
325  '{name}' => $name,
326  '{variant}' => '',
327  ]
328  );
329  $rules[] = "$selector {\n\t$declarations\n}";
330 
331  foreach ( $image->getVariants() as $variant ) {
332  $declarations = $this->getStyleDeclarations( $context, $image, $script, $variant );
333  $selector = strtr(
334  $selectors['selectorWithVariant'],
335  [
336  '{prefix}' => $this->getPrefix(),
337  '{name}' => $name,
338  '{variant}' => $variant,
339  ]
340  );
341  $rules[] = "$selector {\n\t$declarations\n}";
342  }
343  }
344 
345  $style = implode( "\n", $rules );
346  return [ 'all' => $style ];
347  }
348 
356  private function getStyleDeclarations(
359  $script,
360  $variant = null
361  ) {
362  $imageDataUri = $image->getDataUri( $context, $variant, 'original' );
363  $primaryUrl = $imageDataUri ?: $image->getUrl( $context, $script, $variant, 'original' );
364  $declarations = $this->getCssDeclarations(
365  $primaryUrl,
366  $image->getUrl( $context, $script, $variant, 'rasterized' )
367  );
368  return implode( "\n\t", $declarations );
369  }
370 
383  protected function getCssDeclarations( $primary, $fallback ) {
384  return [
385  "background-image: url($fallback);",
386  "background-image: linear-gradient(transparent, transparent), url($primary);",
387  // Do not serve SVG to Opera 12, bad rendering with border-radius or background-size (T87504)
388  "background-image: -o-linear-gradient(transparent, transparent), url($fallback);",
389  ];
390  }
391 
395  public function supportsURLLoading() {
396  return false;
397  }
398 
406  $this->loadFromDefinition();
407  $summary = parent::getDefinitionSummary( $context );
408 
409  $options = [];
410  foreach ( [
411  'localBasePath',
412  'images',
413  'variants',
414  'prefix',
415  'selectorWithoutVariant',
416  'selectorWithVariant',
417  ] as $member ) {
418  $options[$member] = $this->{$member};
419  };
420 
421  $summary[] = [
422  'options' => $options,
423  'fileHashes' => $this->getFileHashes( $context ),
424  ];
425  return $summary;
426  }
427 
432  $this->loadFromDefinition();
433  $files = [];
434  foreach ( $this->getImages( $context ) as $name => $image ) {
435  $files[] = $image->getPath( $context );
436  }
437  $files = array_values( array_unique( $files ) );
438  return array_map( [ __CLASS__, 'safeFileHash' ], $files );
439  }
440 
449  public static function extractLocalBasePath( $options, $localBasePath = null ) {
450  global $IP;
451 
452  if ( $localBasePath === null ) {
454  }
455 
456  if ( array_key_exists( 'localBasePath', $options ) ) {
457  $localBasePath = (string)$options['localBasePath'];
458  }
459 
460  return $localBasePath;
461  }
462 
466  public function getType() {
467  return self::LOAD_STYLES;
468  }
469 }
ResourceLoaderContext
Object passed around to modules which contains information about the state of a specific loader reque...
Definition: ResourceLoaderContext.php:32
$context
error also a ContextSource you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2612
ResourceLoaderImageModule\getType
getType()
Definition: ResourceLoaderImageModule.php:466
ResourceLoaderImageModule\extractLocalBasePath
static extractLocalBasePath( $options, $localBasePath=null)
Extract a local base path from module definition information.
Definition: ResourceLoaderImageModule.php:449
ResourceLoaderModule\ORIGIN_CORE_SITEWIDE
const ORIGIN_CORE_SITEWIDE
Definition: ResourceLoaderModule.php:47
$fallback
$fallback
Definition: MessagesAb.php:11
ResourceLoaderImageModule\$targets
$targets
Definition: ResourceLoaderImageModule.php:46
ResourceLoaderImageModule\getGlobalVariants
getGlobalVariants(ResourceLoaderContext $context)
Get list of variants in this module that are 'global', i.e., available for every image regardless of ...
Definition: ResourceLoaderImageModule.php:284
ResourceLoaderImageModule\getStyles
getStyles(ResourceLoaderContext $context)
Definition: ResourceLoaderImageModule.php:311
ResourceLoaderImageModule\__construct
__construct( $options=[], $localBasePath=null)
Constructs a new module from an options array.
Definition: ResourceLoaderImageModule.php:104
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ResourceLoaderImageModule\$localBasePath
string $localBasePath
Local base path, see __construct()
Definition: ResourceLoaderImageModule.php:37
ResourceLoaderImageModule\getPrefix
getPrefix()
Get CSS class prefix used by this module.
Definition: ResourceLoaderImageModule.php:201
ResourceLoaderImageModule\$prefix
$prefix
Definition: ResourceLoaderImageModule.php:43
ResourceLoaderImageModule\supportsURLLoading
supportsURLLoading()
Definition: ResourceLoaderImageModule.php:395
ResourceLoaderImageModule\$definition
$definition
Definition: ResourceLoaderImageModule.php:31
ResourceLoaderImageModule\$selectorWithVariant
$selectorWithVariant
Definition: ResourceLoaderImageModule.php:45
ResourceLoaderImageModule\$origin
$origin
Definition: ResourceLoaderImageModule.php:39
$IP
$IP
Definition: update.php:3
ResourceLoaderImageModule\$images
$images
Definition: ResourceLoaderImageModule.php:41
ContextSource\getSkin
getSkin()
Get the Skin object.
Definition: ContextSource.php:153
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
string
This code would result in ircNotify being run twice when an article is and once for brion Hooks can return three possible true was required This is the default since MediaWiki *some string
Definition: hooks.txt:177
ResourceLoaderImageModule\$selectorWithoutVariant
$selectorWithoutVariant
Definition: ResourceLoaderImageModule.php:44
$image
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check $image
Definition: hooks.txt:783
ResourceLoaderImage
Class encapsulating an image used in a ResourceLoaderImageModule.
Definition: ResourceLoaderImage.php:28
$selector
$selector
Definition: styleTest.css.php:43
ResourceLoaderImageModule\getCssDeclarations
getCssDeclarations( $primary, $fallback)
SVG support using a transparent gradient to guarantee cross-browser compatibility (browsers able to u...
Definition: ResourceLoaderImageModule.php:383
ResourceLoaderModule\$name
$name
Definition: ResourceLoaderModule.php:70
ResourceLoaderModule\LOAD_STYLES
const LOAD_STYLES
Definition: ResourceLoaderModule.php:42
ResourceLoaderImageModule\$variants
$variants
Definition: ResourceLoaderImageModule.php:42
ResourceLoaderImageModule\getFileHashes
getFileHashes(ResourceLoaderContext $context)
Helper method for getDefinitionSummary.
Definition: ResourceLoaderImageModule.php:431
ResourceLoaderModule
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
Definition: ResourceLoaderModule.php:34
ResourceLoaderImageModule\getStyleDeclarations
getStyleDeclarations(ResourceLoaderContext $context, ResourceLoaderImage $image, $script, $variant=null)
Definition: ResourceLoaderImageModule.php:356
ResourceLoaderModule\$config
Config $config
Definition: ResourceLoaderModule.php:85
ResourceLoaderImageModule\getImages
getImages(ResourceLoaderContext $context)
Get ResourceLoaderImage objects for all images.
Definition: ResourceLoaderImageModule.php:235
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$skin
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned $skin
Definition: hooks.txt:1956
ResourceLoaderImageModule\loadFromDefinition
loadFromDefinition()
Parse definition and external JSON data, if referenced.
Definition: ResourceLoaderImageModule.php:113
ResourceLoaderImageModule\getDefinitionSummary
getDefinitionSummary(ResourceLoaderContext $context)
Get the definition summary for this module.
Definition: ResourceLoaderImageModule.php:405
ResourceLoaderModule\getSource
getSource()
Get the origin of this module.
Definition: ResourceLoaderModule.php:328
definition
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable or merely the Work and Derivative Works thereof Contribution shall mean any work of including the original version of the Work and any modifications or additions to that Work or Derivative Works that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner For the purposes of this definition
Definition: APACHE-LICENSE-2.0.txt:49
ResourceLoaderImageModule\getImage
getImage( $name, ResourceLoaderContext $context)
Get a ResourceLoaderImage object for given image.
Definition: ResourceLoaderImageModule.php:224
ResourceLoaderImageModule
ResourceLoader module for generated and embedded images.
Definition: ResourceLoaderImageModule.php:29
ResourceLoaderModule\getName
getName()
Get this module's name.
Definition: ResourceLoaderModule.php:105
$options
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1049
ResourceLoaderImageModule\getSelectors
getSelectors()
Get CSS selector templates used by this module.
Definition: ResourceLoaderImageModule.php:210