1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @author Trevor Parscal
* @author Roan Kattouw
*/
namespace MediaWiki\ResourceLoader;
use CSSJanus;
use Exception;
use ExtensionRegistry;
use FileContentsHasher;
use InvalidArgumentException;
use LogicException;
use MediaWiki\Languages\LanguageFallback;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\Output\OutputPage;
use ObjectCache;
use RuntimeException;
use Wikimedia\Minify\CSSMin;
use Wikimedia\RequestTimeout\TimeoutException;
/**
* Module based on local JavaScript/CSS files.
*
* The following public methods can query the database:
*
* - getDefinitionSummary / … / Module::getFileDependencies.
* - getVersionHash / getDefinitionSummary / … / Module::getFileDependencies.
* - getStyles / Module::saveFileDependencies.
*
* @ingroup ResourceLoader
* @see $wgResourceModules
* @since 1.17
*/
class FileModule extends Module {
/** @var string Local base path, see __construct() */
protected $localBasePath = '';
/** @var string Remote base path, see __construct() */
protected $remoteBasePath = '';
/**
* @var array<int,string|FilePath> List of JavaScript file paths to always include
*/
protected $scripts = [];
/**
* @var array<string,array<int,string|FilePath>> Lists of JavaScript files by language code
*/
protected $languageScripts = [];
/**
* @var array<string,array<int,string|FilePath>> Lists of JavaScript files by skin name
*/
protected $skinScripts = [];
/**
* @var array<int,string|FilePath> List of paths to JavaScript files to include in debug mode
*/
protected $debugScripts = [];
/**
* @var array<int,string|FilePath> List of CSS file files to always include
*/
protected $styles = [];
/**
* @var array<string,array<int,string|FilePath>> Lists of CSS files by skin name
*/
protected $skinStyles = [];
/**
* Packaged files definition, to bundle and make available client-side via `require()`.
*
* @see FileModule::expandPackageFiles()
* @var null|array
* @phan-var null|array<int,string|FilePath|array{main?:bool,name?:string,file?:string|FilePath,type?:string,content?:mixed,config?:array,callback?:callable,callbackParam?:mixed,versionCallback?:callable}>
*/
protected $packageFiles = null;
/**
* @var array Expanded versions of $packageFiles, lazy-computed by expandPackageFiles();
* keyed by context hash
*/
private $expandedPackageFiles = [];
/**
* @var array Further expanded versions of $expandedPackageFiles, lazy-computed by
* getPackageFiles(); keyed by context hash
*/
private $fullyExpandedPackageFiles = [];
/**
* @var string[] List of modules this module depends on
*/
protected $dependencies = [];
/**
* @var null|string File name containing the body of the skip function
*/
protected $skipFunction = null;
/**
* @var string[] List of message keys used by this module
*/
protected $messages = [];
/** @var array<int|string,string|FilePath> List of the named templates used by this module */
protected $templates = [];
/** @var null|string Name of group to load this module in */
protected $group = null;
/** @var bool Link to raw files in debug mode */
protected $debugRaw = true;
/** @var bool Whether CSSJanus flipping should be skipped for this module */
protected $noflip = false;
/** @var bool Whether to skip the structure test ResourcesTest::testRespond() */
protected $skipStructureTest = false;
/**
* @var bool Whether getStyleURLsForDebug should return raw file paths,
* or return load.php urls
*/
protected $hasGeneratedStyles = false;
/**
* @var string[] Place where readStyleFile() tracks file dependencies
*/
protected $localFileRefs = [];
/**
* @var string[] Place where readStyleFile() tracks file dependencies for non-existent files.
* Used in tests to detect missing dependencies.
*/
protected $missingLocalFileRefs = [];
/**
* @var VueComponentParser|null Lazy-created by getVueComponentParser()
*/
protected $vueComponentParser = null;
/**
* Construct a new module from an options array.
*
* @param array $options See $wgResourceModules for the available options.
* @param string|null $localBasePath Base path to prepend to all local paths in $options.
* Defaults to MW_INSTALL_PATH
* @param string|null $remoteBasePath Base path to prepend to all remote paths in $options.
* Defaults to $wgResourceBasePath
*/
public function __construct(
array $options = [],
string $localBasePath = null,
string $remoteBasePath = null
) {
// Flag to decide whether to automagically add the mediawiki.template module
$hasTemplates = false;
// localBasePath and remoteBasePath both have unbelievably long fallback chains
// and need to be handled separately.
[ $this->localBasePath, $this->remoteBasePath ] =
self::extractBasePaths( $options, $localBasePath, $remoteBasePath );
// Extract, validate and normalise remaining options
foreach ( $options as $member => $option ) {
switch ( $member ) {
// Lists of file paths
case 'scripts':
case 'debugScripts':
case 'styles':
case 'packageFiles':
$this->{$member} = is_array( $option ) ? $option : [ $option ];
break;
case 'templates':
$hasTemplates = true;
$this->{$member} = is_array( $option ) ? $option : [ $option ];
break;
// Collated lists of file paths
case 'languageScripts':
case 'skinScripts':
case 'skinStyles':
if ( !is_array( $option ) ) {
throw new InvalidArgumentException(
"Invalid collated file path list error. " .
"'$option' given, array expected."
);
}
foreach ( $option as $key => $value ) {
if ( !is_string( $key ) ) {
throw new InvalidArgumentException(
"Invalid collated file path list key error. " .
"'$key' given, string expected."
);
}
$this->{$member}[$key] = is_array( $value ) ? $value : [ $value ];
}
break;
case 'deprecated':
$this->deprecated = $option;
break;
// Lists of strings
case 'dependencies':
case 'messages':
// Normalise
$option = array_values( array_unique( (array)$option ) );
sort( $option );
$this->{$member} = $option;
break;
// Single strings
case 'group':
case 'skipFunction':
$this->{$member} = (string)$option;
break;
// Single booleans
case 'debugRaw':
case 'noflip':
case 'skipStructureTest':
$this->{$member} = (bool)$option;
break;
}
}
if ( isset( $options['scripts'] ) && isset( $options['packageFiles'] ) ) {
throw new InvalidArgumentException( "A module may not set both 'scripts' and 'packageFiles'" );
}
if ( isset( $options['packageFiles'] ) && isset( $options['skinScripts'] ) ) {
throw new InvalidArgumentException( "Options 'skinScripts' and 'packageFiles' cannot be used together." );
}
if ( $hasTemplates ) {
$this->dependencies[] = 'mediawiki.template';
// Ensure relevant template compiler module gets loaded
foreach ( $this->templates as $alias => $templatePath ) {
if ( is_int( $alias ) ) {
$alias = $this->getPath( $templatePath );
}
$suffix = explode( '.', $alias );
$suffix = end( $suffix );
$compilerModule = 'mediawiki.template.' . $suffix;
if ( $suffix !== 'html' && !in_array( $compilerModule, $this->dependencies ) ) {
$this->dependencies[] = $compilerModule;
}
}
}
}
/**
* Extract a pair of local and remote base paths from module definition information.
* Implementation note: the amount of global state used in this function is staggering.
*
* @param array $options Module definition
* @param string|null $localBasePath Path to use if not provided in module definition. Defaults
* to MW_INSTALL_PATH
* @param string|null $remoteBasePath Path to use if not provided in module definition. Defaults
* to $wgResourceBasePath
* @return string[] [ localBasePath, remoteBasePath ]
*/
public static function extractBasePaths(
array $options = [],
$localBasePath = null,
$remoteBasePath = null
) {
// The different ways these checks are done, and their ordering, look very silly,
// but were preserved for backwards-compatibility just in case. Tread lightly.
if ( $remoteBasePath === null ) {
$remoteBasePath = MediaWikiServices::getInstance()->getMainConfig()
->get( MainConfigNames::ResourceBasePath );
}
if ( isset( $options['remoteExtPath'] ) ) {
$extensionAssetsPath = MediaWikiServices::getInstance()->getMainConfig()
->get( MainConfigNames::ExtensionAssetsPath );
$remoteBasePath = $extensionAssetsPath . '/' . $options['remoteExtPath'];
}
if ( isset( $options['remoteSkinPath'] ) ) {
$stylePath = MediaWikiServices::getInstance()->getMainConfig()
->get( MainConfigNames::StylePath );
$remoteBasePath = $stylePath . '/' . $options['remoteSkinPath'];
}
if ( array_key_exists( 'localBasePath', $options ) ) {
$localBasePath = (string)$options['localBasePath'];
}
if ( array_key_exists( 'remoteBasePath', $options ) ) {
$remoteBasePath = (string)$options['remoteBasePath'];
}
if ( $remoteBasePath === '' ) {
// If MediaWiki is installed at the document root (not recommended),
// then wgScriptPath is set to the empty string by the installer to
// ensure safe concatenating of file paths (avoid "/" + "/foo" being "//foo").
// However, this also means the path itself can be an invalid URI path,
// as those must start with a slash. Within ResourceLoader, we will not
// do such primitive/unsafe slash concatenation and use URI resolution
// instead, so beyond this point, to avoid fatal errors in CSSMin::resolveUrl(),
// do a best-effort support for docroot installs by casting this to a slash.
$remoteBasePath = '/';
}
return [ $localBasePath ?? MW_INSTALL_PATH, $remoteBasePath ];
}
public function getScript( Context $context ) {
$packageFiles = $this->getPackageFiles( $context );
if ( $packageFiles !== null ) {
foreach ( $packageFiles['files'] as &$file ) {
if ( $file['type'] === 'script+style' ) {
$file['content'] = $file['content']['script'];
$file['type'] = 'script';
}
}
return $packageFiles;
}
$files = $this->getScriptFiles( $context );
foreach ( $files as &$file ) {
$this->readFileInfo( $context, $file );
}
return [ 'plainScripts' => $files ];
}
/**
* @param Context $context
* @return string[] URLs
*/
public function getScriptURLsForDebug( Context $context ) {
$rl = $context->getResourceLoader();
$config = $this->getConfig();
$server = $config->get( MainConfigNames::Server );
$urls = [];
foreach ( $this->getScriptFiles( $context ) as $file ) {
if ( isset( $file['filePath'] ) ) {
$url = OutputPage::transformResourcePath( $config, $this->getRemotePath( $file['filePath'] ) );
// Expand debug URL in case we are another wiki's module source (T255367)
$url = $rl->expandUrl( $server, $url );
$urls[] = $url;
}
}
return $urls;
}
/**
* @return bool
*/
public function supportsURLLoading() {
// phpcs:ignore Generic.WhiteSpace.LanguageConstructSpacing.IncorrectSingle
return
// Denied by options?
$this->debugRaw
// If package files are involved, don't support URL loading, because that breaks
// scoped require() functions
&& !$this->packageFiles
// Can't link to scripts generated by callbacks
&& !$this->hasGeneratedScripts();
}
public function shouldSkipStructureTest() {
return $this->skipStructureTest || parent::shouldSkipStructureTest();
}
/**
* Determine whether the module may potentially have generated scripts.
*
* @return bool
*/
private function hasGeneratedScripts() {
foreach (
[ $this->scripts, $this->languageScripts, $this->skinScripts, $this->debugScripts ]
as $scripts
) {
foreach ( $scripts as $script ) {
if ( is_array( $script ) ) {
if ( isset( $script['callback'] ) || isset( $script['versionCallback'] ) ) {
return true;
}
}
}
}
return false;
}
/**
* Get all styles for a given context.
*
* @param Context $context
* @return string[] CSS code for $context as an associative array mapping media type to CSS text.
*/
public function getStyles( Context $context ) {
$styles = $this->readStyleFiles(
$this->getStyleFiles( $context ),
$context
);
$packageFiles = $this->getPackageFiles( $context );
if ( $packageFiles !== null ) {
foreach ( $packageFiles['files'] as $fileName => $file ) {
if ( $file['type'] === 'script+style' ) {
$style = $this->processStyle(
$file['content']['style'],
$file['content']['styleLang'],
$fileName,
$context
);
$styles['all'] = ( $styles['all'] ?? '' ) . "\n" . $style;
}
}
}
// Track indirect file dependencies so that StartUpModule can check for
// on-disk file changes to any of this files without having to recompute the file list
$this->saveFileDependencies( $context, $this->localFileRefs );
return $styles;
}
/**
* @param Context $context
* @return string[][] Lists of URLs by media type
*/
public function getStyleURLsForDebug( Context $context ) {
if ( $this->hasGeneratedStyles ) {
// Do the default behaviour of returning a url back to load.php
// but with only=styles.
return parent::getStyleURLsForDebug( $context );
}
// Our module consists entirely of real css files,
// in debug mode we can load those directly.
$urls = [];
foreach ( $this->getStyleFiles( $context ) as $mediaType => $list ) {
$urls[$mediaType] = [];
foreach ( $list as $file ) {
$urls[$mediaType][] = OutputPage::transformResourcePath(
$this->getConfig(),
$this->getRemotePath( $file )
);
}
}
return $urls;
}
/**
* Get message keys used by this module.
*
* @return string[] List of message keys
*/
public function getMessages() {
return $this->messages;
}
/**
* Get the name of the group this module should be loaded in.
*
* @return null|string Group name
*/
public function getGroup() {
return $this->group;
}
/**
* Get names of modules this module depends on.
*
* @param Context|null $context
* @return string[] List of module names
*/
public function getDependencies( Context $context = null ) {
return $this->dependencies;
}
/**
* Helper method for getting a file.
*
* @param string $localPath The path to the resource to load
* @param string $type The type of resource being loaded (for error reporting only)
* @return string
*/
private function getFileContents( $localPath, $type ) {
if ( !is_file( $localPath ) ) {
throw new RuntimeException( "$type file not found or not a file: \"$localPath\"" );
}
return $this->stripBom( file_get_contents( $localPath ) );
}
/**
* @return null|string
*/
public function getSkipFunction() {
if ( !$this->skipFunction ) {
return null;
}
$localPath = $this->getLocalPath( $this->skipFunction );
return $this->getFileContents( $localPath, 'skip function' );
}
public function requiresES6() {
return true;
}
/**
* Disable module content versioning.
*
* This class uses getDefinitionSummary() instead, to avoid filesystem overhead
* involved with building the full module content inside a startup request.
*
* @return bool
*/
public function enableModuleContentVersion() {
return false;
}
/**
* Helper method for getDefinitionSummary.
*
* @param Context $context
* @return string Hash
*/
private function getFileHashes( Context $context ) {
$files = [];
foreach ( $this->getStyleFiles( $context ) as $filePaths ) {
foreach ( $filePaths as $filePath ) {
$files[] = $this->getLocalPath( $filePath );
}
}
// Extract file paths for package files
// Optimisation: Use foreach() and isset() instead of array_map/array_filter.
// This is a hot code path, called by StartupModule for thousands of modules.
$expandedPackageFiles = $this->expandPackageFiles( $context );
if ( $expandedPackageFiles ) {
foreach ( $expandedPackageFiles['files'] as $fileInfo ) {
if ( isset( $fileInfo['filePath'] ) ) {
/** @var FilePath $filePath */
$filePath = $fileInfo['filePath'];
$files[] = $filePath->getLocalPath();
}
}
}
// Add other configured paths
$scriptFileInfos = $this->getScriptFiles( $context );
foreach ( $scriptFileInfos as $fileInfo ) {
$filePath = $fileInfo['filePath'] ?? $fileInfo['versionFilePath'] ?? null;
if ( $filePath instanceof FilePath ) {
$files[] = $filePath->getLocalPath();
}
}
foreach ( $this->templates as $filePath ) {
$files[] = $this->getLocalPath( $filePath );
}
if ( $this->skipFunction ) {
$files[] = $this->getLocalPath( $this->skipFunction );
}
// Add any lazily discovered file dependencies from previous module builds.
// These are already absolute paths.
foreach ( $this->getFileDependencies( $context ) as $file ) {
$files[] = $file;
}
// Filter out any duplicates. Typically introduced by getFileDependencies() which
// may lazily re-discover a primary file.
$files = array_unique( $files );
// Don't return array keys or any other form of file path here, only the hashes.
// Including file paths would needlessly cause global cache invalidation when files
// move on disk or if e.g. the MediaWiki directory name changes.
// Anything where order is significant is already detected by the definition summary.
return FileContentsHasher::getFileContentsHash( $files );
}
/**
* Get the definition summary for this module.
*
* @param Context $context
* @return array
*/
public function getDefinitionSummary( Context $context ) {
$summary = parent::getDefinitionSummary( $context );
$options = [];
foreach ( [
// The following properties are omitted because they don't affect the module response:
// - localBasePath (Per T104950; Changes when absolute directory name changes. If
// this affects 'scripts' and other file paths, getFileHashes accounts for that.)
// - remoteBasePath (Per T104950)
// - dependencies (provided via startup module)
// - group (provided via startup module)
'styles',
'skinStyles',
'messages',
'templates',
'skipFunction',
'debugRaw',
] as $member ) {
$options[$member] = $this->{$member};
}
$packageFiles = $this->expandPackageFiles( $context );
$packageSummaries = [];
if ( $packageFiles ) {
// Extract the minimum needed:
// - The 'main' pointer (included as-is).
// - The 'files' array, simplified to only which files exist (the keys of
// this array), and something that represents their non-file content.
// For packaged files that reflect files directly from disk, the
// 'getFileHashes' method tracks their content already.
// It is important that the keys of the $packageFiles['files'] array
// are preserved, as they do affect the module output.
foreach ( $packageFiles['files'] as $fileName => $fileInfo ) {
$packageSummaries[$fileName] =
$fileInfo['definitionSummary'] ?? $fileInfo['content'] ?? null;
}
}
$scriptFiles = $this->getScriptFiles( $context );
$scriptSummaries = [];
foreach ( $scriptFiles as $fileName => $fileInfo ) {
$scriptSummaries[$fileName] =
$fileInfo['definitionSummary'] ?? $fileInfo['content'] ?? null;
}
$summary[] = [
'options' => $options,
'packageFiles' => $packageSummaries,
'scripts' => $scriptSummaries,
'fileHashes' => $this->getFileHashes( $context ),
'messageBlob' => $this->getMessageBlob( $context ),
];
$lessVars = $this->getLessVars( $context );
if ( $lessVars ) {
$summary[] = [ 'lessVars' => $lessVars ];
}
return $summary;
}
/**
* @return VueComponentParser
*/
protected function getVueComponentParser() {
if ( $this->vueComponentParser === null ) {
$this->vueComponentParser = new VueComponentParser;
}
return $this->vueComponentParser;
}
/**
* @param string|FilePath $path
* @return string
*/
protected function getPath( $path ) {
if ( $path instanceof FilePath ) {
return $path->getPath();
}
return $path;
}
/**
* @param string|FilePath $path
* @return string
*/
protected function getLocalPath( $path ) {
if ( $path instanceof FilePath ) {
if ( $path->getLocalBasePath() !== null ) {
return $path->getLocalPath();
}
$path = $path->getPath();
}
return "{$this->localBasePath}/$path";
}
/**
* @param string|FilePath $path
* @return string
*/
protected function getRemotePath( $path ) {
if ( $path instanceof FilePath ) {
if ( $path->getRemoteBasePath() !== null ) {
return $path->getRemotePath();
}
$path = $path->getPath();
}
if ( $this->remoteBasePath === '/' ) {
return "/$path";
} else {
return "{$this->remoteBasePath}/$path";
}
}
/**
* Infer the stylesheet language from a stylesheet file path.
*
* @since 1.22
* @param string $path
* @return string The stylesheet language name
*/
public function getStyleSheetLang( $path ) {
return preg_match( '/\.less$/i', $path ) ? 'less' : 'css';
}
/**
* Infer the file type from a package file path.
*
* @param string $path
* @return string 'script', 'script-vue', or 'data'
*/
public static function getPackageFileType( $path ) {
if ( preg_match( '/\.json$/i', $path ) ) {
return 'data';
}
if ( preg_match( '/\.vue$/i', $path ) ) {
return 'script-vue';
}
return 'script';
}
/**
* Collate style file paths by 'media' option (or 'all' if 'media' is not set)
*
* @param array $list List of file paths in any combination of index/path
* or path/options pairs
* @return string[][] List of collated file paths
*/
private static function collateStyleFilesByMedia( array $list ) {
$collatedFiles = [];
foreach ( $list as $key => $value ) {
if ( is_int( $key ) ) {
// File name as the value
$collatedFiles['all'][] = $value;
} elseif ( is_array( $value ) ) {
// File name as the key, options array as the value
$optionValue = $value['media'] ?? 'all';
$collatedFiles[$optionValue][] = $key;
}
}
return $collatedFiles;
}
/**
* Get a list of element that match a key, optionally using a fallback key.
*
* @param array[] $list List of lists to select from
* @param string $key Key to look for in $list
* @param string|null $fallback Key to look for in $list if $key doesn't exist
* @return array List of elements from $list which matched $key or $fallback,
* or an empty list in case of no match
*/
protected static function tryForKey( array $list, $key, $fallback = null ) {
if ( isset( $list[$key] ) && is_array( $list[$key] ) ) {
return $list[$key];
} elseif ( is_string( $fallback )
&& isset( $list[$fallback] )
&& is_array( $list[$fallback] )
) {
return $list[$fallback];
}
return [];
}
/**
* Get script file paths for this module, in order of proper execution.
*
* @param Context $context
* @return array An array of file info arrays as returned by expandFileInfo()
*/
private function getScriptFiles( Context $context ): array {
// List in execution order: scripts, languageScripts, skinScripts, debugScripts.
// Documented at MediaWiki\MainConfigSchema::ResourceModules.
$filesByCategory = [
'scripts' => $this->scripts,
'languageScripts' => $this->getLanguageScripts( $context->getLanguage() ),
'skinScripts' => self::tryForKey( $this->skinScripts, $context->getSkin(), 'default' ),
];
if ( $context->getDebug() ) {
$filesByCategory['debugScripts'] = $this->debugScripts;
}
$expandedFiles = [];
foreach ( $filesByCategory as $category => $files ) {
foreach ( $files as $key => $fileInfo ) {
$expandedFileInfo = $this->expandFileInfo( $context, $fileInfo, "$category\[$key]" );
$expandedFiles[$expandedFileInfo['name']] = $expandedFileInfo;
}
}
return $expandedFiles;
}
/**
* Get the set of language scripts for the given language,
* possibly using a fallback language.
*
* @param string $lang
* @return array<int,string|FilePath> File paths
*/
private function getLanguageScripts( string $lang ): array {
$scripts = self::tryForKey( $this->languageScripts, $lang );
if ( $scripts ) {
return $scripts;
}
// Optimization: Avoid initialising and calling into language services
// for the majority of modules that don't use this option.
if ( $this->languageScripts ) {
$fallbacks = MediaWikiServices::getInstance()
->getLanguageFallback()
->getAll( $lang, LanguageFallback::MESSAGES );
foreach ( $fallbacks as $lang ) {
$scripts = self::tryForKey( $this->languageScripts, $lang );
if ( $scripts ) {
return $scripts;
}
}
}
return [];
}
public function setSkinStylesOverride( array $moduleSkinStyles ): void {
$moduleName = $this->getName();
foreach ( $moduleSkinStyles as $skinName => $overrides ) {
// If a module provides overrides for a skin, and that skin also provides overrides
// for the same module, then the module has precedence.
if ( isset( $this->skinStyles[$skinName] ) ) {
continue;
}
// If $moduleName in ResourceModuleSkinStyles is preceded with a '+', the defined style
// files will be added to 'default' skinStyles, otherwise 'default' will be ignored.
if ( isset( $overrides[$moduleName] ) ) {
$paths = (array)$overrides[$moduleName];
$styleFiles = [];
} elseif ( isset( $overrides['+' . $moduleName] ) ) {
$paths = (array)$overrides['+' . $moduleName];
$styleFiles = isset( $this->skinStyles['default'] ) ?
(array)$this->skinStyles['default'] :
[];
} else {
continue;
}
// Add new file paths, remapping them to refer to our directories and not use settings
// from the module we're modifying, which come from the base definition.
[ $localBasePath, $remoteBasePath ] = self::extractBasePaths( $overrides );
foreach ( $paths as $path ) {
$styleFiles[] = new FilePath( $path, $localBasePath, $remoteBasePath );
}
$this->skinStyles[$skinName] = $styleFiles;
}
}
/**
* Get a list of file paths for all styles in this module, in order of proper inclusion.
*
* @internal Exposed only for use by structure phpunit tests.
* @param Context $context
* @return array<string,array<int,string|FilePath>> Map from media type to list of file paths
*/
public function getStyleFiles( Context $context ) {
return array_merge_recursive(
self::collateStyleFilesByMedia( $this->styles ),
self::collateStyleFilesByMedia(
self::tryForKey( $this->skinStyles, $context->getSkin(), 'default' )
)
);
}
/**
* Get a list of file paths for all skin styles in the module used by
* the skin.
*
* @param string $skinName The name of the skin
* @return array A list of file paths collated by media type
*/
protected function getSkinStyleFiles( $skinName ) {
return self::collateStyleFilesByMedia(
self::tryForKey( $this->skinStyles, $skinName )
);
}
/**
* Get a list of file paths for all skin style files in the module,
* for all available skins.
*
* @return array A list of file paths collated by media type
*/
protected function getAllSkinStyleFiles() {
$skinFactory = MediaWikiServices::getInstance()->getSkinFactory();
$styleFiles = [];
$internalSkinNames = array_keys( $skinFactory->getInstalledSkins() );
$internalSkinNames[] = 'default';
foreach ( $internalSkinNames as $internalSkinName ) {
$styleFiles = array_merge_recursive(
$styleFiles,
$this->getSkinStyleFiles( $internalSkinName )
);
}
return $styleFiles;
}
/**
* Get all style files and all skin style files used by this module.
*
* @return array
*/
public function getAllStyleFiles() {
$collatedStyleFiles = array_merge_recursive(
self::collateStyleFilesByMedia( $this->styles ),
$this->getAllSkinStyleFiles()
);
$result = [];
foreach ( $collatedStyleFiles as $styleFiles ) {
foreach ( $styleFiles as $styleFile ) {
$result[] = $this->getLocalPath( $styleFile );
}
}
return $result;
}
/**
* Read the contents of a list of CSS files and remap and concatenate these.
*
* @internal This is considered a private method. Exposed for internal use by WebInstallerOutput.
* @param array<string,array<int,string|FilePath>> $styles Map of media type to file paths
* @param Context $context
* @return array<string,string> Map of combined CSS code, keyed by media type
*/
public function readStyleFiles( array $styles, Context $context ) {
if ( !$styles ) {
return [];
}
foreach ( $styles as $media => $files ) {
$uniqueFiles = array_unique( $files, SORT_REGULAR );
$styleFiles = [];
foreach ( $uniqueFiles as $file ) {
$styleFiles[] = $this->readStyleFile( $file, $context );
}
$styles[$media] = implode( "\n", $styleFiles );
}
return $styles;
}
/**
* Read and process a style file. Reads a file from disk and runs it through processStyle().
*
* This method can be used as a callback for array_map()
*
* @internal
* @param string|FilePath $path Path of style file to read
* @param Context $context
* @return string CSS code
*/
protected function readStyleFile( $path, Context $context ) {
$localPath = $this->getLocalPath( $path );
$style = $this->getFileContents( $localPath, 'style' );
$styleLang = $this->getStyleSheetLang( $localPath );
return $this->processStyle( $style, $styleLang, $path, $context );
}
/**
* Process a CSS/LESS string.
*
* This method performs the following processing steps:
* - LESS compilation (if $styleLang = 'less')
* - RTL flipping with CSSJanus (if getFlip() returns true)
* - Registration of references to local files in $localFileRefs and $missingLocalFileRefs
* - URL remapping and data URI embedding
*
* @internal
* @param string $style CSS or LESS code
* @param string $styleLang Language of $style code ('css' or 'less')
* @param string|FilePath $path Path to code file, used for resolving relative file paths
* @param Context $context
* @return string Processed CSS code
*/
protected function processStyle( $style, $styleLang, $path, Context $context ) {
$localPath = $this->getLocalPath( $path );
$remotePath = $this->getRemotePath( $path );
if ( $styleLang === 'less' ) {
$style = $this->compileLessString( $style, $localPath, $context );
$this->hasGeneratedStyles = true;
}
if ( $this->getFlip( $context ) ) {
$style = CSSJanus::transform(
$style,
/* $swapLtrRtlInURL = */ true,
/* $swapLeftRightInURL = */ false
);
}
$localDir = dirname( $localPath );
$remoteDir = dirname( $remotePath );
// Get and register local file references
$localFileRefs = CSSMin::getLocalFileReferences( $style, $localDir );
foreach ( $localFileRefs as $file ) {
if ( is_file( $file ) ) {
$this->localFileRefs[] = $file;
} else {
$this->missingLocalFileRefs[] = $file;
}
}
// Don't cache this call. remap() ensures data URIs embeds are up to date,
// and urls contain correct content hashes in their query string. (T128668)
return CSSMin::remap( $style, $localDir, $remoteDir, true );
}
/**
* Get whether CSS for this module should be flipped
* @param Context $context
* @return bool
*/
public function getFlip( Context $context ) {
return $context->getDirection() === 'rtl' && !$this->noflip;
}
/**
* Get the module's load type.
*
* @since 1.28
* @return string
*/
public function getType() {
$canBeStylesOnly = !(
// All options except 'styles', 'skinStyles' and 'debugRaw'
$this->scripts
|| $this->debugScripts
|| $this->templates
|| $this->languageScripts
|| $this->skinScripts
|| $this->dependencies
|| $this->messages
|| $this->skipFunction
|| $this->packageFiles
);
return $canBeStylesOnly ? self::LOAD_STYLES : self::LOAD_GENERAL;
}
/**
* Compile a LESS string into CSS.
*
* Keeps track of all used files and adds them to localFileRefs.
*
* @since 1.35
* @param string $style LESS source to compile
* @param string $stylePath File path of LESS source, used for resolving relative file paths
* @param Context $context Context in which to generate script
* @return string CSS source
*/
protected function compileLessString( $style, $stylePath, Context $context ) {
static $cache;
// @TODO: dependency injection
if ( !$cache ) {
$cache = ObjectCache::getLocalServerInstance( CACHE_ANYTHING );
}
$skinName = $context->getSkin();
$skinImportPaths = ExtensionRegistry::getInstance()->getAttribute( 'SkinLessImportPaths' );
$importDirs = [];
if ( isset( $skinImportPaths[ $skinName ] ) ) {
$importDirs[] = $skinImportPaths[ $skinName ];
}
$vars = $this->getLessVars( $context );
// Construct a cache key from a hash of the LESS source, and a hash digest
// of the LESS variables used for compilation.
ksort( $vars );
$compilerParams = [
'vars' => $vars,
'importDirs' => $importDirs,
];
$key = $cache->makeGlobalKey(
'resourceloader-less',
'v1',
hash( 'md4', $style ),
hash( 'md4', serialize( $compilerParams ) )
);
// If we got a cached value, we have to validate it by getting a checksum of all the
// files that were loaded by the parser and ensuring it matches the cached entry's.
$data = $cache->get( $key );
if (
!$data ||
$data['hash'] !== FileContentsHasher::getFileContentsHash( $data['files'] )
) {
$compiler = $context->getResourceLoader()->getLessCompiler( $vars, $importDirs );
$css = $compiler->parse( $style, $stylePath )->getCss();
// T253055: store the implicit dependency paths in a form relative to any install
// path so that multiple version of the application can share the cache for identical
// less stylesheets. This also avoids churn during application updates.
$files = $compiler->AllParsedFiles();
$data = [
'css' => $css,
'files' => Module::getRelativePaths( $files ),
'hash' => FileContentsHasher::getFileContentsHash( $files )
];
$cache->set( $key, $data, $cache::TTL_DAY );
}
foreach ( Module::expandRelativePaths( $data['files'] ) as $path ) {
$this->localFileRefs[] = $path;
}
return $data['css'];
}
/**
* Get content of named templates for this module.
*
* @return array<string,string> Templates mapping template alias to content
*/
public function getTemplates() {
$templates = [];
foreach ( $this->templates as $alias => $templatePath ) {
// Alias is optional
if ( is_int( $alias ) ) {
$alias = $this->getPath( $templatePath );
}
$localPath = $this->getLocalPath( $templatePath );
$content = $this->getFileContents( $localPath, 'template' );
$templates[$alias] = $this->stripBom( $content );
}
return $templates;
}
/**
* Internal helper for use by getPackageFiles(), getFileHashes() and getDefinitionSummary().
*
* This expands the 'packageFiles' definition into something that's (almost) the right format
* for getPackageFiles() to return. It expands shorthands, resolves config vars, and handles
* summarising any non-file data for getVersionHash(). For file-based data, getFileHashes()
* handles it instead, which also ends up in getDefinitionSummary().
*
* What it does not do is reading the actual contents of any specified files, nor invoking
* the computation callbacks. Those things are done by getPackageFiles() instead to improve
* backend performance by only doing this work when the module response is needed, and not
* when merely computing the version hash for StartupModule, or when checking
* If-None-Match headers for a HTTP 304 response.
*
* @param Context $context
* @return array|null Array of arrays as returned by expandFileInfo(), with the key being
* the file name, or null if this is not a package file module.
* @phan-return array{main:?string,files:array[]}|null
*/
private function expandPackageFiles( Context $context ) {
$hash = $context->getHash();
if ( isset( $this->expandedPackageFiles[$hash] ) ) {
return $this->expandedPackageFiles[$hash];
}
if ( $this->packageFiles === null ) {
return null;
}
$expandedFiles = [];
$mainFile = null;
foreach ( $this->packageFiles as $key => $fileInfo ) {
$expanded = $this->expandFileInfo( $context, $fileInfo, "packageFiles[$key]" );
$fileName = $expanded['name'];
if ( !empty( $expanded['main'] ) ) {
unset( $expanded['main'] );
$type = $expanded['type'];
$mainFile = $fileName;
if ( $type !== 'script' && $type !== 'script-vue' ) {
$msg = "Main file in package must be of type 'script', module " .
"'{$this->getName()}', main file '{$mainFile}' is '{$type}'.";
$this->getLogger()->error( $msg );
throw new LogicException( $msg );
}
}
$expandedFiles[$fileName] = $expanded;
}
if ( $expandedFiles && $mainFile === null ) {
// The first package file that is a script is the main file
foreach ( $expandedFiles as $path => $file ) {
if ( $file['type'] === 'script' || $file['type'] === 'script-vue' ) {
$mainFile = $path;
break;
}
}
}
$result = [
'main' => $mainFile,
'files' => $expandedFiles
];
$this->expandedPackageFiles[$hash] = $result;
return $result;
}
/**
* Process a file info array as specified in configuration or extension.json,
* expanding shortcuts and callbacks.
*
* @see MainConfigSchema::ResourceModules
*
* @param Context $context
* @param array|string|FilePath $fileInfo
* @param string $debugKey
* @return array An associative array with the following keys:
* - name: (string) The filename relative to the module base. This is unique only within
* the context of the current module. It may be a virtual name.
* - type: (string) May be 'script', 'script-vue', 'data' or 'text'
* - filePath: (FilePath) The FilePath object which should be used to load the content.
* This will be absent if the content was loaded another way.
* - virtualFilePath: (FilePath) A FilePath object for a virtual path which doesn't actually
* exist. This is used for source map generation. Optional.
* - versionFilePath: (FilePath) A FilePath object which is the ultimate source of a
* generated file. The timestamp and contents will be used for version generation.
* Generated by the callback specified in versionCallback. Optional.
* - content: (string|mixed) If the 'type' element is 'script', this is a string containing
* JS code, being the contents of the script file. For any other type, this contains data
* which will be JSON serialized. Optional, if not set, it will be set in readFileInfo().
* - callback: (callable) A callback to call to obtain the contents. This will be set if the
* version callback was present in the input, indicating that the callback is expensive.
* - callbackParam: (array) The parameters to be passed to the callback.
* - definitionSummary: (array) The data returned by the version callback.
* - main: (bool) Whether the file is the main file of the package.
*/
private function expandFileInfo( Context $context, $fileInfo, $debugKey ) {
if ( is_string( $fileInfo ) ) {
// Inline common case
return [
'name' => $fileInfo,
'type' => self::getPackageFileType( $fileInfo ),
'filePath' => new FilePath( $fileInfo, $this->localBasePath, $this->remoteBasePath )
];
} elseif ( $fileInfo instanceof FilePath ) {
$fileInfo = [
'name' => $fileInfo->getPath(),
'file' => $fileInfo
];
} elseif ( !is_array( $fileInfo ) ) {
$msg = "Invalid type in $debugKey for module '{$this->getName()}', " .
"must be array, string or FilePath";
$this->getLogger()->error( $msg );
throw new LogicException( $msg );
}
if ( !isset( $fileInfo['name'] ) ) {
$msg = "Missing 'name' key in $debugKey for module '{$this->getName()}'";
$this->getLogger()->error( $msg );
throw new LogicException( $msg );
}
$fileName = $this->getPath( $fileInfo['name'] );
// Infer type from alias if needed
$type = $fileInfo['type'] ?? self::getPackageFileType( $fileName );
$expanded = [
'name' => $fileName,
'type' => $type
];
if ( !empty( $fileInfo['main'] ) ) {
$expanded['main'] = true;
}
// Perform expansions (except 'file' and 'callback'), creating one of these keys:
// - 'content': literal value.
// - 'filePath': content to be read from a file.
// - 'callback': content computed by a callable.
if ( isset( $fileInfo['content'] ) ) {
$expanded['content'] = $fileInfo['content'];
} elseif ( isset( $fileInfo['file'] ) ) {
$expanded['filePath'] = $this->makeFilePath( $fileInfo['file'] );
} elseif ( isset( $fileInfo['callback'] ) ) {
// If no extra parameter for the callback is given, use null.
$expanded['callbackParam'] = $fileInfo['callbackParam'] ?? null;
if ( !is_callable( $fileInfo['callback'] ) ) {
$msg = "Invalid 'callback' for module '{$this->getName()}', file '{$fileName}'.";
$this->getLogger()->error( $msg );
throw new LogicException( $msg );
}
if ( isset( $fileInfo['versionCallback'] ) ) {
if ( !is_callable( $fileInfo['versionCallback'] ) ) {
throw new LogicException( "Invalid 'versionCallback' for "
. "module '{$this->getName()}', file '{$fileName}'."
);
}
// Execute the versionCallback with the same arguments that
// would be given to the callback
$callbackResult = ( $fileInfo['versionCallback'] )(
$context,
$this->getConfig(),
$expanded['callbackParam']
);
if ( $callbackResult instanceof FilePath ) {
$callbackResult->initBasePaths( $this->localBasePath, $this->remoteBasePath );
$expanded['versionFilePath'] = $callbackResult;
} else {
$expanded['definitionSummary'] = $callbackResult;
}
// Don't invoke 'callback' here as it may be expensive (T223260).
$expanded['callback'] = $fileInfo['callback'];
} else {
// Else go ahead invoke callback with its arguments.
$callbackResult = ( $fileInfo['callback'] )(
$context,
$this->getConfig(),
$expanded['callbackParam']
);
if ( $callbackResult instanceof FilePath ) {
$callbackResult->initBasePaths( $this->localBasePath, $this->remoteBasePath );
$expanded['filePath'] = $callbackResult;
} else {
$expanded['content'] = $callbackResult;
}
}
} elseif ( isset( $fileInfo['config'] ) ) {
if ( $type !== 'data' ) {
$msg = "Key 'config' only valid for data files. "
. " Module '{$this->getName()}', file '{$fileName}' is '{$type}'.";
$this->getLogger()->error( $msg );
throw new LogicException( $msg );
}
$expandedConfig = [];
foreach ( $fileInfo['config'] as $configKey => $var ) {
$expandedConfig[ is_numeric( $configKey ) ? $var : $configKey ] = $this->getConfig()->get( $var );
}
$expanded['content'] = $expandedConfig;
} elseif ( !empty( $fileInfo['main'] ) ) {
// [ 'name' => 'foo.js', 'main' => true ] is shorthand
$expanded['filePath'] = $this->makeFilePath( $fileName );
} else {
$msg = "Incomplete definition for module '{$this->getName()}', file '{$fileName}'. "
. "One of 'file', 'content', 'callback', or 'config' must be set.";
$this->getLogger()->error( $msg );
throw new LogicException( $msg );
}
if ( !isset( $expanded['filePath'] ) ) {
$expanded['virtualFilePath'] = $this->makeFilePath( $fileName );
}
return $expanded;
}
/**
* Cast a FilePath or string to a FilePath
*
* @param FilePath|string $path
* @return FilePath
*/
private function makeFilePath( $path ): FilePath {
if ( $path instanceof FilePath ) {
return $path;
} elseif ( is_string( $path ) ) {
return new FilePath( $path, $this->localBasePath, $this->remoteBasePath );
} else {
throw new InvalidArgumentException( '$path must be either FilePath or string' );
}
}
/**
* Resolve the package files definition and generate the content of each package file.
*
* @param Context $context
* @return array|null Package files data structure, see Module::getScript()
*/
public function getPackageFiles( Context $context ) {
if ( $this->packageFiles === null ) {
return null;
}
$hash = $context->getHash();
if ( isset( $this->fullyExpandedPackageFiles[ $hash ] ) ) {
return $this->fullyExpandedPackageFiles[ $hash ];
}
$expandedPackageFiles = $this->expandPackageFiles( $context ) ?? [];
foreach ( $expandedPackageFiles['files'] as &$fileInfo ) {
$this->readFileInfo( $context, $fileInfo );
}
$this->fullyExpandedPackageFiles[ $hash ] = $expandedPackageFiles;
return $expandedPackageFiles;
}
/**
* Given a file info array as returned by expandFileInfo(), expand the file paths and
* remaining callbacks, ensuring that the 'content' element is populated. Modify
* the array by reference, removing intermediate data such as callback parameters.
*
* @param Context $context
* @param array &$fileInfo
*/
private function readFileInfo( Context $context, array &$fileInfo ) {
// Turn any 'filePath' or 'callback' key into actual 'content',
// and remove the key after that. The callback could return a
// FilePath object; if that happens, fall through to the 'filePath'
// handling.
if ( !isset( $fileInfo['content'] ) && isset( $fileInfo['callback'] ) ) {
$callbackResult = ( $fileInfo['callback'] )(
$context,
$this->getConfig(),
$fileInfo['callbackParam']
);
if ( $callbackResult instanceof FilePath ) {
// Fall through to the filePath handling code below
$fileInfo['filePath'] = $callbackResult;
} else {
$fileInfo['content'] = $callbackResult;
}
unset( $fileInfo['callback'] );
}
// Only interpret 'filePath' if 'content' hasn't been set already.
// This can happen if 'versionCallback' provided 'filePath',
// while 'callback' provides 'content'. In that case both are set
// at this point. The 'filePath' from 'versionCallback' in that case is
// only to inform getDefinitionSummary().
if ( !isset( $fileInfo['content'] ) && isset( $fileInfo['filePath'] ) ) {
$localPath = $this->getLocalPath( $fileInfo['filePath'] );
$content = $this->getFileContents( $localPath, 'package' );
if ( $fileInfo['type'] === 'data' ) {
$content = json_decode( $content, false, 512, JSON_THROW_ON_ERROR );
}
$fileInfo['content'] = $content;
}
if ( $fileInfo['type'] === 'script-vue' ) {
try {
$parsedComponent = $this->getVueComponentParser()->parse(
// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
$fileInfo['content'],
[ 'minifyTemplate' => !$context->getDebug() ]
);
} catch ( TimeoutException $e ) {
throw $e;
} catch ( Exception $e ) {
$msg = "Error parsing file '{$fileInfo['name']}' in module '{$this->getName()}': " .
$e->getMessage();
$this->getLogger()->error( $msg );
throw new RuntimeException( $msg );
}
$encodedTemplate = json_encode( $parsedComponent['template'] );
if ( $context->getDebug() ) {
// Replace \n (backslash-n) with space + backslash-n + backslash-newline in debug mode
// The \n has to be preserved to prevent Vue parser issues (T351771)
// We only replace \n if not preceded by a backslash, to avoid breaking '\\n'
$encodedTemplate = preg_replace( '/(?<!\\\\)\\\\n/', " \\n\\\n", $encodedTemplate );
// Expand \t to real tabs in debug mode
$encodedTemplate = strtr( $encodedTemplate, [ "\\t" => "\t" ] );
}
$fileInfo['content'] = [
'script' => $parsedComponent['script'] .
";\nmodule.exports.template = $encodedTemplate;",
'style' => $parsedComponent['style'] ?? '',
'styleLang' => $parsedComponent['styleLang'] ?? 'css'
];
$fileInfo['type'] = 'script+style';
}
if ( !isset( $fileInfo['content'] ) ) {
// This should not be possible due to validation in expandFileInfo()
$msg = "Unable to resolve contents for file {$fileInfo['name']}";
$this->getLogger()->error( $msg );
throw new RuntimeException( $msg );
}
// Not needed for client response, exists for use by getDefinitionSummary().
unset( $fileInfo['definitionSummary'] );
// Not needed for client response, used by callbacks only.
unset( $fileInfo['callbackParam'] );
}
/**
* Take an input string and remove the UTF-8 BOM character if present
*
* We need to remove these after reading a file, because we concatenate our files and
* the BOM character is not valid in the middle of a string.
* We already assume UTF-8 everywhere, so this should be safe.
*
* @param string $input
* @return string Input minus the initial BOM char
*/
protected function stripBom( $input ) {
if ( str_starts_with( $input, "\xef\xbb\xbf" ) ) {
return substr( $input, 3 );
}
return $input;
}
}
|