-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGlobalFunctions.php
4454 lines (4056 loc) · 127 KB
/
GlobalFunctions.php
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
<?php
/**
* Global functions used everywhere.
*
* 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
*/
if ( !defined( 'MEDIAWIKI' ) ) {
die( "This file is part of MediaWiki, it is not a valid entry point" );
}
use Liuggio\StatsdClient\Sender\SocketSender;
use MediaWiki\Logger\LoggerFactory;
// Hide compatibility functions from Doxygen
/// @cond
/**
* Compatibility functions
*
* We support PHP 5.3.3 and up.
* Re-implementations of newer functions or functions in non-standard
* PHP extensions may be included here.
*/
if ( !function_exists( 'mb_substr' ) ) {
/**
* @codeCoverageIgnore
* @see Fallback::mb_substr
* @return string
*/
function mb_substr( $str, $start, $count = 'end' ) {
return Fallback::mb_substr( $str, $start, $count );
}
/**
* @codeCoverageIgnore
* @see Fallback::mb_substr_split_unicode
* @return int
*/
function mb_substr_split_unicode( $str, $splitPos ) {
return Fallback::mb_substr_split_unicode( $str, $splitPos );
}
}
if ( !function_exists( 'mb_strlen' ) ) {
/**
* @codeCoverageIgnore
* @see Fallback::mb_strlen
* @return int
*/
function mb_strlen( $str, $enc = '' ) {
return Fallback::mb_strlen( $str, $enc );
}
}
if ( !function_exists( 'mb_strpos' ) ) {
/**
* @codeCoverageIgnore
* @see Fallback::mb_strpos
* @return int
*/
function mb_strpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
return Fallback::mb_strpos( $haystack, $needle, $offset, $encoding );
}
}
if ( !function_exists( 'mb_strrpos' ) ) {
/**
* @codeCoverageIgnore
* @see Fallback::mb_strrpos
* @return int
*/
function mb_strrpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
return Fallback::mb_strrpos( $haystack, $needle, $offset, $encoding );
}
}
// gzdecode function only exists in PHP >= 5.4.0
// http://php.net/gzdecode
if ( !function_exists( 'gzdecode' ) ) {
/**
* @codeCoverageIgnore
* @param string $data
* @return string
*/
function gzdecode( $data ) {
return gzinflate( substr( $data, 10, -8 ) );
}
}
// hash_equals function only exists in PHP >= 5.6.0
// http://php.net/hash_equals
if ( !function_exists( 'hash_equals' ) ) {
/**
* Check whether a user-provided string is equal to a fixed-length secret string
* without revealing bytes of the secret string through timing differences.
*
* The usual way to compare strings (PHP's === operator or the underlying memcmp()
* function in C) is to compare corresponding bytes and stop at the first difference,
* which would take longer for a partial match than for a complete mismatch. This
* is not secure when one of the strings (e.g. an HMAC or token) must remain secret
* and the other may come from an attacker. Statistical analysis of timing measurements
* over many requests may allow the attacker to guess the string's bytes one at a time
* (and check his guesses) even if the timing differences are extremely small.
*
* When making such a security-sensitive comparison, it is essential that the sequence
* in which instructions are executed and memory locations are accessed not depend on
* the secret string's value. HOWEVER, for simplicity, we do not attempt to minimize
* the inevitable leakage of the string's length. That is generally known anyway as
* a chararacteristic of the hash function used to compute the secret value.
*
* Longer explanation: http://www.emerose.com/timing-attacks-explained
*
* @codeCoverageIgnore
* @param string $known_string Fixed-length secret string to compare against
* @param string $user_string User-provided string
* @return bool True if the strings are the same, false otherwise
*/
function hash_equals( $known_string, $user_string ) {
// Strict type checking as in PHP's native implementation
if ( !is_string( $known_string ) ) {
trigger_error( 'hash_equals(): Expected known_string to be a string, ' .
gettype( $known_string ) . ' given', E_USER_WARNING );
return false;
}
if ( !is_string( $user_string ) ) {
trigger_error( 'hash_equals(): Expected user_string to be a string, ' .
gettype( $user_string ) . ' given', E_USER_WARNING );
return false;
}
$known_string_len = strlen( $known_string );
if ( $known_string_len !== strlen( $user_string ) ) {
return false;
}
$result = 0;
for ( $i = 0; $i < $known_string_len; $i++ ) {
$result |= ord( $known_string[$i] ) ^ ord( $user_string[$i] );
}
return ( $result === 0 );
}
}
/// @endcond
/**
* Load an extension
*
* This queues an extension to be loaded through
* the ExtensionRegistry system.
*
* @param string $ext Name of the extension to load
* @param string|null $path Absolute path of where to find the extension.json file
* @since 1.25
*/
function wfLoadExtension( $ext, $path = null ) {
if ( !$path ) {
global $wgExtensionDirectory;
$path = "$wgExtensionDirectory/$ext/extension.json";
}
ExtensionRegistry::getInstance()->queue( $path );
}
/**
* Load multiple extensions at once
*
* Same as wfLoadExtension, but more efficient if you
* are loading multiple extensions.
*
* If you want to specify custom paths, you should interact with
* ExtensionRegistry directly.
*
* @see wfLoadExtension
* @param string[] $exts Array of extension names to load
* @since 1.25
*/
function wfLoadExtensions( array $exts ) {
global $wgExtensionDirectory;
$registry = ExtensionRegistry::getInstance();
foreach ( $exts as $ext ) {
$registry->queue( "$wgExtensionDirectory/$ext/extension.json" );
}
}
/**
* Load a skin
*
* @see wfLoadExtension
* @param string $skin Name of the extension to load
* @param string|null $path Absolute path of where to find the skin.json file
* @since 1.25
*/
function wfLoadSkin( $skin, $path = null ) {
if ( !$path ) {
global $wgStyleDirectory;
$path = "$wgStyleDirectory/$skin/skin.json";
}
ExtensionRegistry::getInstance()->queue( $path );
}
/**
* Load multiple skins at once
*
* @see wfLoadExtensions
* @param string[] $skins Array of extension names to load
* @since 1.25
*/
function wfLoadSkins( array $skins ) {
global $wgStyleDirectory;
$registry = ExtensionRegistry::getInstance();
foreach ( $skins as $skin ) {
$registry->queue( "$wgStyleDirectory/$skin/skin.json" );
}
}
/**
* Like array_diff( $a, $b ) except that it works with two-dimensional arrays.
* @param array $a
* @param array $b
* @return array
*/
function wfArrayDiff2( $a, $b ) {
return array_udiff( $a, $b, 'wfArrayDiff2_cmp' );
}
/**
* @param array|string $a
* @param array|string $b
* @return int
*/
function wfArrayDiff2_cmp( $a, $b ) {
if ( is_string( $a ) && is_string( $b ) ) {
return strcmp( $a, $b );
} elseif ( count( $a ) !== count( $b ) ) {
return count( $a ) < count( $b ) ? -1 : 1;
} else {
reset( $a );
reset( $b );
while ( ( list( , $valueA ) = each( $a ) ) && ( list( , $valueB ) = each( $b ) ) ) {
$cmp = strcmp( $valueA, $valueB );
if ( $cmp !== 0 ) {
return $cmp;
}
}
return 0;
}
}
/**
* Appends to second array if $value differs from that in $default
*
* @param string|int $key
* @param mixed $value
* @param mixed $default
* @param array $changed Array to alter
* @throws MWException
*/
function wfAppendToArrayIfNotDefault( $key, $value, $default, &$changed ) {
if ( is_null( $changed ) ) {
throw new MWException( 'GlobalFunctions::wfAppendToArrayIfNotDefault got null' );
}
if ( $default[$key] !== $value ) {
$changed[$key] = $value;
}
}
/**
* Merge arrays in the style of getUserPermissionsErrors, with duplicate removal
* e.g.
* wfMergeErrorArrays(
* array( array( 'x' ) ),
* array( array( 'x', '2' ) ),
* array( array( 'x' ) ),
* array( array( 'y' ) )
* );
* returns:
* array(
* array( 'x', '2' ),
* array( 'x' ),
* array( 'y' )
* )
*
* @param array $array1,...
* @return array
*/
function wfMergeErrorArrays( /*...*/ ) {
$args = func_get_args();
$out = array();
foreach ( $args as $errors ) {
foreach ( $errors as $params ) {
# @todo FIXME: Sometimes get nested arrays for $params,
# which leads to E_NOTICEs
$spec = implode( "\t", $params );
$out[$spec] = $params;
}
}
return array_values( $out );
}
/**
* Insert array into another array after the specified *KEY*
*
* @param array $array The array.
* @param array $insert The array to insert.
* @param mixed $after The key to insert after
* @return array
*/
function wfArrayInsertAfter( array $array, array $insert, $after ) {
// Find the offset of the element to insert after.
$keys = array_keys( $array );
$offsetByKey = array_flip( $keys );
$offset = $offsetByKey[$after];
// Insert at the specified offset
$before = array_slice( $array, 0, $offset + 1, true );
$after = array_slice( $array, $offset + 1, count( $array ) - $offset, true );
$output = $before + $insert + $after;
return $output;
}
/**
* Recursively converts the parameter (an object) to an array with the same data
*
* @param object|array $objOrArray
* @param bool $recursive
* @return array
*/
function wfObjectToArray( $objOrArray, $recursive = true ) {
$array = array();
if ( is_object( $objOrArray ) ) {
$objOrArray = get_object_vars( $objOrArray );
}
foreach ( $objOrArray as $key => $value ) {
if ( $recursive && ( is_object( $value ) || is_array( $value ) ) ) {
$value = wfObjectToArray( $value );
}
$array[$key] = $value;
}
return $array;
}
/**
* Get a random decimal value between 0 and 1, in a way
* not likely to give duplicate values for any realistic
* number of articles.
*
* @return string
*/
function wfRandom() {
# The maximum random value is "only" 2^31-1, so get two random
# values to reduce the chance of dupes
$max = mt_getrandmax() + 1;
$rand = number_format( ( mt_rand() * $max + mt_rand() ) / $max / $max, 12, '.', '' );
return $rand;
}
/**
* Get a random string containing a number of pseudo-random hex
* characters.
* @note This is not secure, if you are trying to generate some sort
* of token please use MWCryptRand instead.
*
* @param int $length The length of the string to generate
* @return string
* @since 1.20
*/
function wfRandomString( $length = 32 ) {
$str = '';
for ( $n = 0; $n < $length; $n += 7 ) {
$str .= sprintf( '%07x', mt_rand() & 0xfffffff );
}
return substr( $str, 0, $length );
}
/**
* We want some things to be included as literal characters in our title URLs
* for prettiness, which urlencode encodes by default. According to RFC 1738,
* all of the following should be safe:
*
* ;:@&=$-_.+!*'(),
*
* RFC 1738 says ~ is unsafe, however RFC 3986 considers it an unreserved
* character which should not be encoded. More importantly, google chrome
* always converts %7E back to ~, and converting it in this function can
* cause a redirect loop (T105265).
*
* But + is not safe because it's used to indicate a space; &= are only safe in
* paths and not in queries (and we don't distinguish here); ' seems kind of
* scary; and urlencode() doesn't touch -_. to begin with. Plus, although /
* is reserved, we don't care. So the list we unescape is:
*
* ;:@$!*(),/~
*
* However, IIS7 redirects fail when the url contains a colon (Bug 22709),
* so no fancy : for IIS7.
*
* %2F in the page titles seems to fatally break for some reason.
*
* @param string $s
* @return string
*/
function wfUrlencode( $s ) {
static $needle;
if ( is_null( $s ) ) {
$needle = null;
return '';
}
if ( is_null( $needle ) ) {
$needle = array( '%3B', '%40', '%24', '%21', '%2A', '%28', '%29', '%2C', '%2F', '%7E' );
if ( !isset( $_SERVER['SERVER_SOFTWARE'] ) ||
( strpos( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS/7' ) === false )
) {
$needle[] = '%3A';
}
}
$s = urlencode( $s );
$s = str_ireplace(
$needle,
array( ';', '@', '$', '!', '*', '(', ')', ',', '/', '~', ':' ),
$s
);
return $s;
}
/**
* This function takes two arrays as input, and returns a CGI-style string, e.g.
* "days=7&limit=100". Options in the first array override options in the second.
* Options set to null or false will not be output.
*
* @param array $array1 ( String|Array )
* @param array $array2 ( String|Array )
* @param string $prefix
* @return string
*/
function wfArrayToCgi( $array1, $array2 = null, $prefix = '' ) {
if ( !is_null( $array2 ) ) {
$array1 = $array1 + $array2;
}
$cgi = '';
foreach ( $array1 as $key => $value ) {
if ( !is_null( $value ) && $value !== false ) {
if ( $cgi != '' ) {
$cgi .= '&';
}
if ( $prefix !== '' ) {
$key = $prefix . "[$key]";
}
if ( is_array( $value ) ) {
$firstTime = true;
foreach ( $value as $k => $v ) {
$cgi .= $firstTime ? '' : '&';
if ( is_array( $v ) ) {
$cgi .= wfArrayToCgi( $v, null, $key . "[$k]" );
} else {
$cgi .= urlencode( $key . "[$k]" ) . '=' . urlencode( $v );
}
$firstTime = false;
}
} else {
if ( is_object( $value ) ) {
$value = $value->__toString();
}
$cgi .= urlencode( $key ) . '=' . urlencode( $value );
}
}
}
return $cgi;
}
/**
* This is the logical opposite of wfArrayToCgi(): it accepts a query string as
* its argument and returns the same string in array form. This allows compatibility
* with legacy functions that accept raw query strings instead of nice
* arrays. Of course, keys and values are urldecode()d.
*
* @param string $query Query string
* @return string[] Array version of input
*/
function wfCgiToArray( $query ) {
if ( isset( $query[0] ) && $query[0] == '?' ) {
$query = substr( $query, 1 );
}
$bits = explode( '&', $query );
$ret = array();
foreach ( $bits as $bit ) {
if ( $bit === '' ) {
continue;
}
if ( strpos( $bit, '=' ) === false ) {
// Pieces like &qwerty become 'qwerty' => '' (at least this is what php does)
$key = $bit;
$value = '';
} else {
list( $key, $value ) = explode( '=', $bit );
}
$key = urldecode( $key );
$value = urldecode( $value );
if ( strpos( $key, '[' ) !== false ) {
$keys = array_reverse( explode( '[', $key ) );
$key = array_pop( $keys );
$temp = $value;
foreach ( $keys as $k ) {
$k = substr( $k, 0, -1 );
$temp = array( $k => $temp );
}
if ( isset( $ret[$key] ) ) {
$ret[$key] = array_merge( $ret[$key], $temp );
} else {
$ret[$key] = $temp;
}
} else {
$ret[$key] = $value;
}
}
return $ret;
}
/**
* Append a query string to an existing URL, which may or may not already
* have query string parameters already. If so, they will be combined.
*
* @param string $url
* @param string|string[] $query String or associative array
* @return string
*/
function wfAppendQuery( $url, $query ) {
if ( is_array( $query ) ) {
$query = wfArrayToCgi( $query );
}
if ( $query != '' ) {
if ( false === strpos( $url, '?' ) ) {
$url .= '?';
} else {
$url .= '&';
}
$url .= $query;
}
return $url;
}
/**
* Expand a potentially local URL to a fully-qualified URL. Assumes $wgServer
* is correct.
*
* The meaning of the PROTO_* constants is as follows:
* PROTO_HTTP: Output a URL starting with http://
* PROTO_HTTPS: Output a URL starting with https://
* PROTO_RELATIVE: Output a URL starting with // (protocol-relative URL)
* PROTO_CURRENT: Output a URL starting with either http:// or https:// , depending
* on which protocol was used for the current incoming request
* PROTO_CANONICAL: For URLs without a domain, like /w/index.php , use $wgCanonicalServer.
* For protocol-relative URLs, use the protocol of $wgCanonicalServer
* PROTO_INTERNAL: Like PROTO_CANONICAL, but uses $wgInternalServer instead of $wgCanonicalServer
*
* @todo this won't work with current-path-relative URLs
* like "subdir/foo.html", etc.
*
* @param string $url Either fully-qualified or a local path + query
* @param string $defaultProto One of the PROTO_* constants. Determines the
* protocol to use if $url or $wgServer is protocol-relative
* @return string Fully-qualified URL, current-path-relative URL or false if
* no valid URL can be constructed
*/
function wfExpandUrl( $url, $defaultProto = PROTO_CURRENT ) {
global $wgServer, $wgCanonicalServer, $wgInternalServer, $wgRequest,
$wgHttpsPort;
if ( $defaultProto === PROTO_CANONICAL ) {
$serverUrl = $wgCanonicalServer;
} elseif ( $defaultProto === PROTO_INTERNAL && $wgInternalServer !== false ) {
// Make $wgInternalServer fall back to $wgServer if not set
$serverUrl = $wgInternalServer;
} else {
$serverUrl = $wgServer;
if ( $defaultProto === PROTO_CURRENT ) {
$defaultProto = $wgRequest->getProtocol() . '://';
}
}
// Analyze $serverUrl to obtain its protocol
$bits = wfParseUrl( $serverUrl );
$serverHasProto = $bits && $bits['scheme'] != '';
if ( $defaultProto === PROTO_CANONICAL || $defaultProto === PROTO_INTERNAL ) {
if ( $serverHasProto ) {
$defaultProto = $bits['scheme'] . '://';
} else {
// $wgCanonicalServer or $wgInternalServer doesn't have a protocol.
// This really isn't supposed to happen. Fall back to HTTP in this
// ridiculous case.
$defaultProto = PROTO_HTTP;
}
}
$defaultProtoWithoutSlashes = substr( $defaultProto, 0, -2 );
if ( substr( $url, 0, 2 ) == '//' ) {
$url = $defaultProtoWithoutSlashes . $url;
} elseif ( substr( $url, 0, 1 ) == '/' ) {
// If $serverUrl is protocol-relative, prepend $defaultProtoWithoutSlashes,
// otherwise leave it alone.
$url = ( $serverHasProto ? '' : $defaultProtoWithoutSlashes ) . $serverUrl . $url;
}
$bits = wfParseUrl( $url );
// ensure proper port for HTTPS arrives in URL
// https://bugzilla.wikimedia.org/show_bug.cgi?id=65184
if ( $defaultProto === PROTO_HTTPS && $wgHttpsPort != 443 ) {
$bits['port'] = $wgHttpsPort;
}
if ( $bits && isset( $bits['path'] ) ) {
$bits['path'] = wfRemoveDotSegments( $bits['path'] );
return wfAssembleUrl( $bits );
} elseif ( $bits ) {
# No path to expand
return $url;
} elseif ( substr( $url, 0, 1 ) != '/' ) {
# URL is a relative path
return wfRemoveDotSegments( $url );
}
# Expanded URL is not valid.
return false;
}
/**
* This function will reassemble a URL parsed with wfParseURL. This is useful
* if you need to edit part of a URL and put it back together.
*
* This is the basic structure used (brackets contain keys for $urlParts):
* [scheme][delimiter][user]:[pass]@[host]:[port][path]?[query]#[fragment]
*
* @todo Need to integrate this into wfExpandUrl (bug 32168)
*
* @since 1.19
* @param array $urlParts URL parts, as output from wfParseUrl
* @return string URL assembled from its component parts
*/
function wfAssembleUrl( $urlParts ) {
$result = '';
if ( isset( $urlParts['delimiter'] ) ) {
if ( isset( $urlParts['scheme'] ) ) {
$result .= $urlParts['scheme'];
}
$result .= $urlParts['delimiter'];
}
if ( isset( $urlParts['host'] ) ) {
if ( isset( $urlParts['user'] ) ) {
$result .= $urlParts['user'];
if ( isset( $urlParts['pass'] ) ) {
$result .= ':' . $urlParts['pass'];
}
$result .= '@';
}
$result .= $urlParts['host'];
if ( isset( $urlParts['port'] ) ) {
$result .= ':' . $urlParts['port'];
}
}
if ( isset( $urlParts['path'] ) ) {
$result .= $urlParts['path'];
}
if ( isset( $urlParts['query'] ) ) {
$result .= '?' . $urlParts['query'];
}
if ( isset( $urlParts['fragment'] ) ) {
$result .= '#' . $urlParts['fragment'];
}
return $result;
}
/**
* Remove all dot-segments in the provided URL path. For example,
* '/a/./b/../c/' becomes '/a/c/'. For details on the algorithm, please see
* RFC3986 section 5.2.4.
*
* @todo Need to integrate this into wfExpandUrl (bug 32168)
*
* @param string $urlPath URL path, potentially containing dot-segments
* @return string URL path with all dot-segments removed
*/
function wfRemoveDotSegments( $urlPath ) {
$output = '';
$inputOffset = 0;
$inputLength = strlen( $urlPath );
while ( $inputOffset < $inputLength ) {
$prefixLengthOne = substr( $urlPath, $inputOffset, 1 );
$prefixLengthTwo = substr( $urlPath, $inputOffset, 2 );
$prefixLengthThree = substr( $urlPath, $inputOffset, 3 );
$prefixLengthFour = substr( $urlPath, $inputOffset, 4 );
$trimOutput = false;
if ( $prefixLengthTwo == './' ) {
# Step A, remove leading "./"
$inputOffset += 2;
} elseif ( $prefixLengthThree == '../' ) {
# Step A, remove leading "../"
$inputOffset += 3;
} elseif ( ( $prefixLengthTwo == '/.' ) && ( $inputOffset + 2 == $inputLength ) ) {
# Step B, replace leading "/.$" with "/"
$inputOffset += 1;
$urlPath[$inputOffset] = '/';
} elseif ( $prefixLengthThree == '/./' ) {
# Step B, replace leading "/./" with "/"
$inputOffset += 2;
} elseif ( $prefixLengthThree == '/..' && ( $inputOffset + 3 == $inputLength ) ) {
# Step C, replace leading "/..$" with "/" and
# remove last path component in output
$inputOffset += 2;
$urlPath[$inputOffset] = '/';
$trimOutput = true;
} elseif ( $prefixLengthFour == '/../' ) {
# Step C, replace leading "/../" with "/" and
# remove last path component in output
$inputOffset += 3;
$trimOutput = true;
} elseif ( ( $prefixLengthOne == '.' ) && ( $inputOffset + 1 == $inputLength ) ) {
# Step D, remove "^.$"
$inputOffset += 1;
} elseif ( ( $prefixLengthTwo == '..' ) && ( $inputOffset + 2 == $inputLength ) ) {
# Step D, remove "^..$"
$inputOffset += 2;
} else {
# Step E, move leading path segment to output
if ( $prefixLengthOne == '/' ) {
$slashPos = strpos( $urlPath, '/', $inputOffset + 1 );
} else {
$slashPos = strpos( $urlPath, '/', $inputOffset );
}
if ( $slashPos === false ) {
$output .= substr( $urlPath, $inputOffset );
$inputOffset = $inputLength;
} else {
$output .= substr( $urlPath, $inputOffset, $slashPos - $inputOffset );
$inputOffset += $slashPos - $inputOffset;
}
}
if ( $trimOutput ) {
$slashPos = strrpos( $output, '/' );
if ( $slashPos === false ) {
$output = '';
} else {
$output = substr( $output, 0, $slashPos );
}
}
}
return $output;
}
/**
* Returns a regular expression of url protocols
*
* @param bool $includeProtocolRelative If false, remove '//' from the returned protocol list.
* DO NOT USE this directly, use wfUrlProtocolsWithoutProtRel() instead
* @return string
*/
function wfUrlProtocols( $includeProtocolRelative = true ) {
global $wgUrlProtocols;
// Cache return values separately based on $includeProtocolRelative
static $withProtRel = null, $withoutProtRel = null;
$cachedValue = $includeProtocolRelative ? $withProtRel : $withoutProtRel;
if ( !is_null( $cachedValue ) ) {
return $cachedValue;
}
// Support old-style $wgUrlProtocols strings, for backwards compatibility
// with LocalSettings files from 1.5
if ( is_array( $wgUrlProtocols ) ) {
$protocols = array();
foreach ( $wgUrlProtocols as $protocol ) {
// Filter out '//' if !$includeProtocolRelative
if ( $includeProtocolRelative || $protocol !== '//' ) {
$protocols[] = preg_quote( $protocol, '/' );
}
}
$retval = implode( '|', $protocols );
} else {
// Ignore $includeProtocolRelative in this case
// This case exists for pre-1.6 compatibility, and we can safely assume
// that '//' won't appear in a pre-1.6 config because protocol-relative
// URLs weren't supported until 1.18
$retval = $wgUrlProtocols;
}
// Cache return value
if ( $includeProtocolRelative ) {
$withProtRel = $retval;
} else {
$withoutProtRel = $retval;
}
return $retval;
}
/**
* Like wfUrlProtocols(), but excludes '//' from the protocol list. Use this if
* you need a regex that matches all URL protocols but does not match protocol-
* relative URLs
* @return string
*/
function wfUrlProtocolsWithoutProtRel() {
return wfUrlProtocols( false );
}
/**
* parse_url() work-alike, but non-broken. Differences:
*
* 1) Does not raise warnings on bad URLs (just returns false).
* 2) Handles protocols that don't use :// (e.g., mailto: and news:, as well as
* protocol-relative URLs) correctly.
* 3) Adds a "delimiter" element to the array, either '://', ':' or '//' (see (2)).
*
* @param string $url A URL to parse
* @return string[] Bits of the URL in an associative array, per PHP docs
*/
function wfParseUrl( $url ) {
global $wgUrlProtocols; // Allow all protocols defined in DefaultSettings/LocalSettings.php
// Protocol-relative URLs are handled really badly by parse_url(). It's so
// bad that the easiest way to handle them is to just prepend 'http:' and
// strip the protocol out later.
$wasRelative = substr( $url, 0, 2 ) == '//';
if ( $wasRelative ) {
$url = "http:$url";
}
MediaWiki\suppressWarnings();
$bits = parse_url( $url );
MediaWiki\restoreWarnings();
// parse_url() returns an array without scheme for some invalid URLs, e.g.
// parse_url("%0Ahttp://example.com") == array( 'host' => '%0Ahttp', 'path' => 'example.com' )
if ( !$bits || !isset( $bits['scheme'] ) ) {
return false;
}
// parse_url() incorrectly handles schemes case-sensitively. Convert it to lowercase.
$bits['scheme'] = strtolower( $bits['scheme'] );
// most of the protocols are followed by ://, but mailto: and sometimes news: not, check for it
if ( in_array( $bits['scheme'] . '://', $wgUrlProtocols ) ) {
$bits['delimiter'] = '://';
} elseif ( in_array( $bits['scheme'] . ':', $wgUrlProtocols ) ) {
$bits['delimiter'] = ':';
// parse_url detects for news: and mailto: the host part of an url as path
// We have to correct this wrong detection
if ( isset( $bits['path'] ) ) {
$bits['host'] = $bits['path'];
$bits['path'] = '';
}
} else {
return false;
}
/* Provide an empty host for eg. file:/// urls (see bug 28627) */
if ( !isset( $bits['host'] ) ) {
$bits['host'] = '';
// bug 45069
if ( isset( $bits['path'] ) ) {
/* parse_url loses the third / for file:///c:/ urls (but not on variants) */
if ( substr( $bits['path'], 0, 1 ) !== '/' ) {
$bits['path'] = '/' . $bits['path'];
}
} else {
$bits['path'] = '';
}
}
// If the URL was protocol-relative, fix scheme and delimiter
if ( $wasRelative ) {
$bits['scheme'] = '';
$bits['delimiter'] = '//';
}
return $bits;
}
/**
* Take a URL, make sure it's expanded to fully qualified, and replace any
* encoded non-ASCII Unicode characters with their UTF-8 original forms
* for more compact display and legibility for local audiences.
*
* @todo handle punycode domains too
*
* @param string $url
* @return string
*/
function wfExpandIRI( $url ) {
return preg_replace_callback(
'/((?:%[89A-F][0-9A-F])+)/i',
'wfExpandIRI_callback',
wfExpandUrl( $url )
);
}
/**
* Private callback for wfExpandIRI
* @param array $matches
* @return string
*/
function wfExpandIRI_callback( $matches ) {
return urldecode( $matches[1] );
}
/**
* Make URL indexes, appropriate for the el_index field of externallinks.
*
* @param string $url
* @return array
*/
function wfMakeUrlIndexes( $url ) {
$bits = wfParseUrl( $url );
// Reverse the labels in the hostname, convert to lower case
// For emails reverse domainpart only
if ( $bits['scheme'] == 'mailto' ) {
$mailparts = explode( '@', $bits['host'], 2 );
if ( count( $mailparts ) === 2 ) {
$domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
} else {
// No domain specified, don't mangle it
$domainpart = '';
}
$reversedHost = $domainpart . '@' . $mailparts[0];
} else {
$reversedHost = strtolower( implode( '.', array_reverse( explode( '.', $bits['host'] ) ) ) );
}
// Add an extra dot to the end
// Why? Is it in wrong place in mailto links?
if ( substr( $reversedHost, -1, 1 ) !== '.' ) {
$reversedHost .= '.';
}
// Reconstruct the pseudo-URL
$prot = $bits['scheme'];
$index = $prot . $bits['delimiter'] . $reversedHost;
// Leave out user and password. Add the port, path, query and fragment
if ( isset( $bits['port'] ) ) {
$index .= ':' . $bits['port'];
}
if ( isset( $bits['path'] ) ) {
$index .= $bits['path'];
} else {
$index .= '/';
}
if ( isset( $bits['query'] ) ) {
$index .= '?' . $bits['query'];
}
if ( isset( $bits['fragment'] ) ) {
$index .= '#' . $bits['fragment'];
}
if ( $prot == '' ) {
return array( "http:$index", "https:$index" );
} else {
return array( $index );
}
}