revert r110340 after talking with krinle
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
1 <?php
2 /**
3 * Global functions used everywhere
4 * @file
5 */
6
7 if ( !defined( 'MEDIAWIKI' ) ) {
8 die( "This file is part of MediaWiki, it is not a valid entry point" );
9 }
10
11 // Hide compatibility functions from Doxygen
12 /// @cond
13
14 /**
15 * Compatibility functions
16 *
17 * We support PHP 5.2.3 and up.
18 * Re-implementations of newer functions or functions in non-standard
19 * PHP extensions may be included here.
20 */
21
22 if( !function_exists( 'iconv' ) ) {
23 /** @codeCoverageIgnore */
24 function iconv( $from, $to, $string ) {
25 return Fallback::iconv( $from, $to, $string );
26 }
27 }
28
29 if ( !function_exists( 'mb_substr' ) ) {
30 /** @codeCoverageIgnore */
31 function mb_substr( $str, $start, $count='end' ) {
32 return Fallback::mb_substr( $str, $start, $count );
33 }
34
35 /** @codeCoverageIgnore */
36 function mb_substr_split_unicode( $str, $splitPos ) {
37 return Fallback::mb_substr_split_unicode( $str, $splitPos );
38 }
39 }
40
41 if ( !function_exists( 'mb_strlen' ) ) {
42 /** @codeCoverageIgnore */
43 function mb_strlen( $str, $enc = '' ) {
44 return Fallback::mb_strlen( $str, $enc );
45 }
46 }
47
48 if( !function_exists( 'mb_strpos' ) ) {
49 /** @codeCoverageIgnore */
50 function mb_strpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
51 return Fallback::mb_strpos( $haystack, $needle, $offset, $encoding );
52 }
53
54 }
55
56 if( !function_exists( 'mb_strrpos' ) ) {
57 /** @codeCoverageIgnore */
58 function mb_strrpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
59 return Fallback::mb_strrpos( $haystack, $needle, $offset, $encoding );
60 }
61 }
62
63
64 // Support for Wietse Venema's taint feature
65 if ( !function_exists( 'istainted' ) ) {
66 /** @codeCoverageIgnore */
67 function istainted( $var ) {
68 return 0;
69 }
70 /** @codeCoverageIgnore */
71 function taint( $var, $level = 0 ) {}
72 /** @codeCoverageIgnore */
73 function untaint( $var, $level = 0 ) {}
74 define( 'TC_HTML', 1 );
75 define( 'TC_SHELL', 1 );
76 define( 'TC_MYSQL', 1 );
77 define( 'TC_PCRE', 1 );
78 define( 'TC_SELF', 1 );
79 }
80 /// @endcond
81
82 /**
83 * Like array_diff( $a, $b ) except that it works with two-dimensional arrays.
84 * @param $a array
85 * @param $b array
86 * @return array
87 */
88 function wfArrayDiff2( $a, $b ) {
89 return array_udiff( $a, $b, 'wfArrayDiff2_cmp' );
90 }
91
92 /**
93 * @param $a
94 * @param $b
95 * @return int
96 */
97 function wfArrayDiff2_cmp( $a, $b ) {
98 if ( !is_array( $a ) ) {
99 return strcmp( $a, $b );
100 } elseif ( count( $a ) !== count( $b ) ) {
101 return count( $a ) < count( $b ) ? -1 : 1;
102 } else {
103 reset( $a );
104 reset( $b );
105 while( ( list( , $valueA ) = each( $a ) ) && ( list( , $valueB ) = each( $b ) ) ) {
106 $cmp = strcmp( $valueA, $valueB );
107 if ( $cmp !== 0 ) {
108 return $cmp;
109 }
110 }
111 return 0;
112 }
113 }
114
115 /**
116 * Array lookup
117 * Returns an array where the values in the first array are replaced by the
118 * values in the second array with the corresponding keys
119 *
120 * @param $a Array
121 * @param $b Array
122 * @return array
123 */
124 function wfArrayLookup( $a, $b ) {
125 return array_flip( array_intersect( array_flip( $a ), array_keys( $b ) ) );
126 }
127
128 /**
129 * Appends to second array if $value differs from that in $default
130 *
131 * @param $key String|Int
132 * @param $value Mixed
133 * @param $default Mixed
134 * @param $changed Array to alter
135 */
136 function wfAppendToArrayIfNotDefault( $key, $value, $default, &$changed ) {
137 if ( is_null( $changed ) ) {
138 throw new MWException( 'GlobalFunctions::wfAppendToArrayIfNotDefault got null' );
139 }
140 if ( $default[$key] !== $value ) {
141 $changed[$key] = $value;
142 }
143 }
144
145 /**
146 * Backwards array plus for people who haven't bothered to read the PHP manual
147 * XXX: will not darn your socks for you.
148 *
149 * @param $array1 Array
150 * @param [$array2, [...]] Arrays
151 * @return Array
152 */
153 function wfArrayMerge( $array1/* ... */ ) {
154 $args = func_get_args();
155 $args = array_reverse( $args, true );
156 $out = array();
157 foreach ( $args as $arg ) {
158 $out += $arg;
159 }
160 return $out;
161 }
162
163 /**
164 * Merge arrays in the style of getUserPermissionsErrors, with duplicate removal
165 * e.g.
166 * wfMergeErrorArrays(
167 * array( array( 'x' ) ),
168 * array( array( 'x', '2' ) ),
169 * array( array( 'x' ) ),
170 * array( array( 'y' ) )
171 * );
172 * returns:
173 * array(
174 * array( 'x', '2' ),
175 * array( 'x' ),
176 * array( 'y' )
177 * )
178 * @param varargs
179 * @return Array
180 */
181 function wfMergeErrorArrays( /*...*/ ) {
182 $args = func_get_args();
183 $out = array();
184 foreach ( $args as $errors ) {
185 foreach ( $errors as $params ) {
186 # @todo FIXME: Sometimes get nested arrays for $params,
187 # which leads to E_NOTICEs
188 $spec = implode( "\t", $params );
189 $out[$spec] = $params;
190 }
191 }
192 return array_values( $out );
193 }
194
195 /**
196 * Insert array into another array after the specified *KEY*
197 *
198 * @param $array Array: The array.
199 * @param $insert Array: The array to insert.
200 * @param $after Mixed: The key to insert after
201 * @return Array
202 */
203 function wfArrayInsertAfter( $array, $insert, $after ) {
204 // Find the offset of the element to insert after.
205 $keys = array_keys( $array );
206 $offsetByKey = array_flip( $keys );
207
208 $offset = $offsetByKey[$after];
209
210 // Insert at the specified offset
211 $before = array_slice( $array, 0, $offset + 1, true );
212 $after = array_slice( $array, $offset + 1, count( $array ) - $offset, true );
213
214 $output = $before + $insert + $after;
215
216 return $output;
217 }
218
219 /**
220 * Recursively converts the parameter (an object) to an array with the same data
221 *
222 * @param $objOrArray Object|Array
223 * @param $recursive Bool
224 * @return Array
225 */
226 function wfObjectToArray( $objOrArray, $recursive = true ) {
227 $array = array();
228 if( is_object( $objOrArray ) ) {
229 $objOrArray = get_object_vars( $objOrArray );
230 }
231 foreach ( $objOrArray as $key => $value ) {
232 if ( $recursive && ( is_object( $value ) || is_array( $value ) ) ) {
233 $value = wfObjectToArray( $value );
234 }
235
236 $array[$key] = $value;
237 }
238
239 return $array;
240 }
241
242 /**
243 * Wrapper around array_map() which also taints variables
244 *
245 * @param $function Callback
246 * @param $input Array
247 * @return Array
248 */
249 function wfArrayMap( $function, $input ) {
250 $ret = array_map( $function, $input );
251 foreach ( $ret as $key => $value ) {
252 $taint = istainted( $input[$key] );
253 if ( $taint ) {
254 taint( $ret[$key], $taint );
255 }
256 }
257 return $ret;
258 }
259
260 /**
261 * Get a random decimal value between 0 and 1, in a way
262 * not likely to give duplicate values for any realistic
263 * number of articles.
264 *
265 * @return string
266 */
267 function wfRandom() {
268 # The maximum random value is "only" 2^31-1, so get two random
269 # values to reduce the chance of dupes
270 $max = mt_getrandmax() + 1;
271 $rand = number_format( ( mt_rand() * $max + mt_rand() )
272 / $max / $max, 12, '.', '' );
273 return $rand;
274 }
275
276 /**
277 * We want some things to be included as literal characters in our title URLs
278 * for prettiness, which urlencode encodes by default. According to RFC 1738,
279 * all of the following should be safe:
280 *
281 * ;:@&=$-_.+!*'(),
282 *
283 * But + is not safe because it's used to indicate a space; &= are only safe in
284 * paths and not in queries (and we don't distinguish here); ' seems kind of
285 * scary; and urlencode() doesn't touch -_. to begin with. Plus, although /
286 * is reserved, we don't care. So the list we unescape is:
287 *
288 * ;:@$!*(),/
289 *
290 * However, IIS7 redirects fail when the url contains a colon (Bug 22709),
291 * so no fancy : for IIS7.
292 *
293 * %2F in the page titles seems to fatally break for some reason.
294 *
295 * @param $s String:
296 * @return string
297 */
298 function wfUrlencode( $s ) {
299 static $needle;
300 if ( is_null( $s ) ) {
301 $needle = null;
302 return '';
303 }
304
305 if ( is_null( $needle ) ) {
306 $needle = array( '%3B', '%40', '%24', '%21', '%2A', '%28', '%29', '%2C', '%2F' );
307 if ( !isset( $_SERVER['SERVER_SOFTWARE'] ) || ( strpos( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS/7' ) === false ) ) {
308 $needle[] = '%3A';
309 }
310 }
311
312 $s = urlencode( $s );
313 $s = str_ireplace(
314 $needle,
315 array( ';', '@', '$', '!', '*', '(', ')', ',', '/', ':' ),
316 $s
317 );
318
319 return $s;
320 }
321
322 /**
323 * This function takes two arrays as input, and returns a CGI-style string, e.g.
324 * "days=7&limit=100". Options in the first array override options in the second.
325 * Options set to null or false will not be output.
326 *
327 * @param $array1 Array ( String|Array )
328 * @param $array2 Array ( String|Array )
329 * @param $prefix String
330 * @return String
331 */
332 function wfArrayToCGI( $array1, $array2 = null, $prefix = '' ) {
333 if ( !is_null( $array2 ) ) {
334 $array1 = $array1 + $array2;
335 }
336
337 $cgi = '';
338 foreach ( $array1 as $key => $value ) {
339 if ( !is_null($value) && $value !== false ) {
340 if ( $cgi != '' ) {
341 $cgi .= '&';
342 }
343 if ( $prefix !== '' ) {
344 $key = $prefix . "[$key]";
345 }
346 if ( is_array( $value ) ) {
347 $firstTime = true;
348 foreach ( $value as $k => $v ) {
349 $cgi .= $firstTime ? '' : '&';
350 if ( is_array( $v ) ) {
351 $cgi .= wfArrayToCGI( $v, null, $key . "[$k]" );
352 } else {
353 $cgi .= urlencode( $key . "[$k]" ) . '=' . urlencode( $v );
354 }
355 $firstTime = false;
356 }
357 } else {
358 if ( is_object( $value ) ) {
359 $value = $value->__toString();
360 }
361 $cgi .= urlencode( $key ) . '=' . urlencode( $value );
362 }
363 }
364 }
365 return $cgi;
366 }
367
368 /**
369 * This is the logical opposite of wfArrayToCGI(): it accepts a query string as
370 * its argument and returns the same string in array form. This allows compa-
371 * tibility with legacy functions that accept raw query strings instead of nice
372 * arrays. Of course, keys and values are urldecode()d.
373 *
374 * @param $query String: query string
375 * @return array Array version of input
376 */
377 function wfCgiToArray( $query ) {
378 if ( isset( $query[0] ) && $query[0] == '?' ) {
379 $query = substr( $query, 1 );
380 }
381 $bits = explode( '&', $query );
382 $ret = array();
383 foreach ( $bits as $bit ) {
384 if ( $bit === '' ) {
385 continue;
386 }
387 if ( strpos( $bit, '=' ) === false ) {
388 // Pieces like &qwerty become 'qwerty' => '' (at least this is what php does)
389 $key = $bit;
390 $value = '';
391 } else {
392 list( $key, $value ) = explode( '=', $bit );
393 }
394 $key = urldecode( $key );
395 $value = urldecode( $value );
396 if ( strpos( $key, '[' ) !== false ) {
397 $keys = array_reverse( explode( '[', $key ) );
398 $key = array_pop( $keys );
399 $temp = $value;
400 foreach ( $keys as $k ) {
401 $k = substr( $k, 0, -1 );
402 $temp = array( $k => $temp );
403 }
404 if ( isset( $ret[$key] ) ) {
405 $ret[$key] = array_merge( $ret[$key], $temp );
406 } else {
407 $ret[$key] = $temp;
408 }
409 } else {
410 $ret[$key] = $value;
411 }
412 }
413 return $ret;
414 }
415
416 /**
417 * Append a query string to an existing URL, which may or may not already
418 * have query string parameters already. If so, they will be combined.
419 *
420 * @param $url String
421 * @param $query Mixed: string or associative array
422 * @return string
423 */
424 function wfAppendQuery( $url, $query ) {
425 if ( is_array( $query ) ) {
426 $query = wfArrayToCGI( $query );
427 }
428 if( $query != '' ) {
429 if( false === strpos( $url, '?' ) ) {
430 $url .= '?';
431 } else {
432 $url .= '&';
433 }
434 $url .= $query;
435 }
436 return $url;
437 }
438
439 /**
440 * Expand a potentially local URL to a fully-qualified URL. Assumes $wgServer
441 * is correct.
442 *
443 * The meaning of the PROTO_* constants is as follows:
444 * PROTO_HTTP: Output a URL starting with http://
445 * PROTO_HTTPS: Output a URL starting with https://
446 * PROTO_RELATIVE: Output a URL starting with // (protocol-relative URL)
447 * PROTO_CURRENT: Output a URL starting with either http:// or https:// , depending on which protocol was used for the current incoming request
448 * PROTO_CANONICAL: For URLs without a domain, like /w/index.php , use $wgCanonicalServer. For protocol-relative URLs, use the protocol of $wgCanonicalServer
449 * PROTO_INTERNAL: Like PROTO_CANONICAL, but uses $wgInternalServer instead of $wgCanonicalServer
450 *
451 * @todo this won't work with current-path-relative URLs
452 * like "subdir/foo.html", etc.
453 *
454 * @param $url String: either fully-qualified or a local path + query
455 * @param $defaultProto Mixed: one of the PROTO_* constants. Determines the
456 * protocol to use if $url or $wgServer is
457 * protocol-relative
458 * @return string Fully-qualified URL, current-path-relative URL or false if
459 * no valid URL can be constructed
460 */
461 function wfExpandUrl( $url, $defaultProto = PROTO_CURRENT ) {
462 global $wgServer, $wgCanonicalServer, $wgInternalServer;
463 $serverUrl = $wgServer;
464 if ( $defaultProto === PROTO_CANONICAL ) {
465 $serverUrl = $wgCanonicalServer;
466 }
467 // Make $wgInternalServer fall back to $wgServer if not set
468 if ( $defaultProto === PROTO_INTERNAL && $wgInternalServer !== false ) {
469 $serverUrl = $wgInternalServer;
470 }
471 if ( $defaultProto === PROTO_CURRENT ) {
472 $defaultProto = WebRequest::detectProtocol() . '://';
473 }
474
475 // Analyze $serverUrl to obtain its protocol
476 $bits = wfParseUrl( $serverUrl );
477 $serverHasProto = $bits && $bits['scheme'] != '';
478
479 if ( $defaultProto === PROTO_CANONICAL || $defaultProto === PROTO_INTERNAL ) {
480 if ( $serverHasProto ) {
481 $defaultProto = $bits['scheme'] . '://';
482 } else {
483 // $wgCanonicalServer or $wgInternalServer doesn't have a protocol. This really isn't supposed to happen
484 // Fall back to HTTP in this ridiculous case
485 $defaultProto = PROTO_HTTP;
486 }
487 }
488
489 $defaultProtoWithoutSlashes = substr( $defaultProto, 0, -2 );
490
491 if ( substr( $url, 0, 2 ) == '//' ) {
492 $url = $defaultProtoWithoutSlashes . $url;
493 } elseif ( substr( $url, 0, 1 ) == '/' ) {
494 // If $serverUrl is protocol-relative, prepend $defaultProtoWithoutSlashes, otherwise leave it alone
495 $url = ( $serverHasProto ? '' : $defaultProtoWithoutSlashes ) . $serverUrl . $url;
496 }
497
498 $bits = wfParseUrl( $url );
499 if ( $bits && isset( $bits['path'] ) ) {
500 $bits['path'] = wfRemoveDotSegments( $bits['path'] );
501 return wfAssembleUrl( $bits );
502 } elseif ( $bits ) {
503 # No path to expand
504 return $url;
505 } elseif ( substr( $url, 0, 1 ) != '/' ) {
506 # URL is a relative path
507 return wfRemoveDotSegments( $url );
508 }
509
510 # Expanded URL is not valid.
511 return false;
512 }
513
514 /**
515 * This function will reassemble a URL parsed with wfParseURL. This is useful
516 * if you need to edit part of a URL and put it back together.
517 *
518 * This is the basic structure used (brackets contain keys for $urlParts):
519 * [scheme][delimiter][user]:[pass]@[host]:[port][path]?[query]#[fragment]
520 *
521 * @todo Need to integrate this into wfExpandUrl (bug 32168)
522 *
523 * @param $urlParts Array URL parts, as output from wfParseUrl
524 * @return string URL assembled from its component parts
525 */
526 function wfAssembleUrl( $urlParts ) {
527 $result = '';
528
529 if ( isset( $urlParts['delimiter'] ) ) {
530 if ( isset( $urlParts['scheme'] ) ) {
531 $result .= $urlParts['scheme'];
532 }
533
534 $result .= $urlParts['delimiter'];
535 }
536
537 if ( isset( $urlParts['host'] ) ) {
538 if ( isset( $urlParts['user'] ) ) {
539 $result .= $urlParts['user'];
540 if ( isset( $urlParts['pass'] ) ) {
541 $result .= ':' . $urlParts['pass'];
542 }
543 $result .= '@';
544 }
545
546 $result .= $urlParts['host'];
547
548 if ( isset( $urlParts['port'] ) ) {
549 $result .= ':' . $urlParts['port'];
550 }
551 }
552
553 if ( isset( $urlParts['path'] ) ) {
554 $result .= $urlParts['path'];
555 }
556
557 if ( isset( $urlParts['query'] ) ) {
558 $result .= '?' . $urlParts['query'];
559 }
560
561 if ( isset( $urlParts['fragment'] ) ) {
562 $result .= '#' . $urlParts['fragment'];
563 }
564
565 return $result;
566 }
567
568 /**
569 * Remove all dot-segments in the provided URL path. For example,
570 * '/a/./b/../c/' becomes '/a/c/'. For details on the algorithm, please see
571 * RFC3986 section 5.2.4.
572 *
573 * @todo Need to integrate this into wfExpandUrl (bug 32168)
574 *
575 * @param $urlPath String URL path, potentially containing dot-segments
576 * @return string URL path with all dot-segments removed
577 */
578 function wfRemoveDotSegments( $urlPath ) {
579 $output = '';
580 $inputOffset = 0;
581 $inputLength = strlen( $urlPath );
582
583 while ( $inputOffset < $inputLength ) {
584 $prefixLengthOne = substr( $urlPath, $inputOffset, 1 );
585 $prefixLengthTwo = substr( $urlPath, $inputOffset, 2 );
586 $prefixLengthThree = substr( $urlPath, $inputOffset, 3 );
587 $prefixLengthFour = substr( $urlPath, $inputOffset, 4 );
588 $trimOutput = false;
589
590 if ( $prefixLengthTwo == './' ) {
591 # Step A, remove leading "./"
592 $inputOffset += 2;
593 } elseif ( $prefixLengthThree == '../' ) {
594 # Step A, remove leading "../"
595 $inputOffset += 3;
596 } elseif ( ( $prefixLengthTwo == '/.' ) && ( $inputOffset + 2 == $inputLength ) ) {
597 # Step B, replace leading "/.$" with "/"
598 $inputOffset += 1;
599 $urlPath[$inputOffset] = '/';
600 } elseif ( $prefixLengthThree == '/./' ) {
601 # Step B, replace leading "/./" with "/"
602 $inputOffset += 2;
603 } elseif ( $prefixLengthThree == '/..' && ( $inputOffset + 3 == $inputLength ) ) {
604 # Step C, replace leading "/..$" with "/" and
605 # remove last path component in output
606 $inputOffset += 2;
607 $urlPath[$inputOffset] = '/';
608 $trimOutput = true;
609 } elseif ( $prefixLengthFour == '/../' ) {
610 # Step C, replace leading "/../" with "/" and
611 # remove last path component in output
612 $inputOffset += 3;
613 $trimOutput = true;
614 } elseif ( ( $prefixLengthOne == '.' ) && ( $inputOffset + 1 == $inputLength ) ) {
615 # Step D, remove "^.$"
616 $inputOffset += 1;
617 } elseif ( ( $prefixLengthTwo == '..' ) && ( $inputOffset + 2 == $inputLength ) ) {
618 # Step D, remove "^..$"
619 $inputOffset += 2;
620 } else {
621 # Step E, move leading path segment to output
622 if ( $prefixLengthOne == '/' ) {
623 $slashPos = strpos( $urlPath, '/', $inputOffset + 1 );
624 } else {
625 $slashPos = strpos( $urlPath, '/', $inputOffset );
626 }
627 if ( $slashPos === false ) {
628 $output .= substr( $urlPath, $inputOffset );
629 $inputOffset = $inputLength;
630 } else {
631 $output .= substr( $urlPath, $inputOffset, $slashPos - $inputOffset );
632 $inputOffset += $slashPos - $inputOffset;
633 }
634 }
635
636 if ( $trimOutput ) {
637 $slashPos = strrpos( $output, '/' );
638 if ( $slashPos === false ) {
639 $output = '';
640 } else {
641 $output = substr( $output, 0, $slashPos );
642 }
643 }
644 }
645
646 return $output;
647 }
648
649 /**
650 * Returns a regular expression of url protocols
651 *
652 * @param $includeProtocolRelative bool If false, remove '//' from the returned protocol list.
653 * DO NOT USE this directly, use wfUrlProtocolsWithoutProtRel() instead
654 * @return String
655 */
656 function wfUrlProtocols( $includeProtocolRelative = true ) {
657 global $wgUrlProtocols;
658
659 // Cache return values separately based on $includeProtocolRelative
660 static $withProtRel = null, $withoutProtRel = null;
661 $cachedValue = $includeProtocolRelative ? $withProtRel : $withoutProtRel;
662 if ( !is_null( $cachedValue ) ) {
663 return $cachedValue;
664 }
665
666 // Support old-style $wgUrlProtocols strings, for backwards compatibility
667 // with LocalSettings files from 1.5
668 if ( is_array( $wgUrlProtocols ) ) {
669 $protocols = array();
670 foreach ( $wgUrlProtocols as $protocol ) {
671 // Filter out '//' if !$includeProtocolRelative
672 if ( $includeProtocolRelative || $protocol !== '//' ) {
673 $protocols[] = preg_quote( $protocol, '/' );
674 }
675 }
676
677 $retval = implode( '|', $protocols );
678 } else {
679 // Ignore $includeProtocolRelative in this case
680 // This case exists for pre-1.6 compatibility, and we can safely assume
681 // that '//' won't appear in a pre-1.6 config because protocol-relative
682 // URLs weren't supported until 1.18
683 $retval = $wgUrlProtocols;
684 }
685
686 // Cache return value
687 if ( $includeProtocolRelative ) {
688 $withProtRel = $retval;
689 } else {
690 $withoutProtRel = $retval;
691 }
692 return $retval;
693 }
694
695 /**
696 * Like wfUrlProtocols(), but excludes '//' from the protocol list. Use this if
697 * you need a regex that matches all URL protocols but does not match protocol-
698 * relative URLs
699 * @return String
700 */
701 function wfUrlProtocolsWithoutProtRel() {
702 return wfUrlProtocols( false );
703 }
704
705 /**
706 * parse_url() work-alike, but non-broken. Differences:
707 *
708 * 1) Does not raise warnings on bad URLs (just returns false)
709 * 2) Handles protocols that don't use :// (e.g., mailto: and news: , as well as protocol-relative URLs) correctly
710 * 3) Adds a "delimiter" element to the array, either '://', ':' or '//' (see (2))
711 *
712 * @param $url String: a URL to parse
713 * @return Array: bits of the URL in an associative array, per PHP docs
714 */
715 function wfParseUrl( $url ) {
716 global $wgUrlProtocols; // Allow all protocols defined in DefaultSettings/LocalSettings.php
717
718 // Protocol-relative URLs are handled really badly by parse_url(). It's so bad that the easiest
719 // way to handle them is to just prepend 'http:' and strip the protocol out later
720 $wasRelative = substr( $url, 0, 2 ) == '//';
721 if ( $wasRelative ) {
722 $url = "http:$url";
723 }
724 wfSuppressWarnings();
725 $bits = parse_url( $url );
726 wfRestoreWarnings();
727 // parse_url() returns an array without scheme for some invalid URLs, e.g.
728 // parse_url("%0Ahttp://example.com") == array( 'host' => '%0Ahttp', 'path' => 'example.com' )
729 if ( !$bits || !isset( $bits['scheme'] ) ) {
730 return false;
731 }
732
733 // most of the protocols are followed by ://, but mailto: and sometimes news: not, check for it
734 if ( in_array( $bits['scheme'] . '://', $wgUrlProtocols ) ) {
735 $bits['delimiter'] = '://';
736 } elseif ( in_array( $bits['scheme'] . ':', $wgUrlProtocols ) ) {
737 $bits['delimiter'] = ':';
738 // parse_url detects for news: and mailto: the host part of an url as path
739 // We have to correct this wrong detection
740 if ( isset( $bits['path'] ) ) {
741 $bits['host'] = $bits['path'];
742 $bits['path'] = '';
743 }
744 } else {
745 return false;
746 }
747
748 /* Provide an empty host for eg. file:/// urls (see bug 28627) */
749 if ( !isset( $bits['host'] ) ) {
750 $bits['host'] = '';
751
752 /* parse_url loses the third / for file:///c:/ urls (but not on variants) */
753 if ( substr( $bits['path'], 0, 1 ) !== '/' ) {
754 $bits['path'] = '/' . $bits['path'];
755 }
756 }
757
758 // If the URL was protocol-relative, fix scheme and delimiter
759 if ( $wasRelative ) {
760 $bits['scheme'] = '';
761 $bits['delimiter'] = '//';
762 }
763 return $bits;
764 }
765
766 /**
767 * Make URL indexes, appropriate for the el_index field of externallinks.
768 *
769 * @param $url String
770 * @return array
771 */
772 function wfMakeUrlIndexes( $url ) {
773 $bits = wfParseUrl( $url );
774
775 // Reverse the labels in the hostname, convert to lower case
776 // For emails reverse domainpart only
777 if ( $bits['scheme'] == 'mailto' ) {
778 $mailparts = explode( '@', $bits['host'], 2 );
779 if ( count( $mailparts ) === 2 ) {
780 $domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
781 } else {
782 // No domain specified, don't mangle it
783 $domainpart = '';
784 }
785 $reversedHost = $domainpart . '@' . $mailparts[0];
786 } else {
787 $reversedHost = strtolower( implode( '.', array_reverse( explode( '.', $bits['host'] ) ) ) );
788 }
789 // Add an extra dot to the end
790 // Why? Is it in wrong place in mailto links?
791 if ( substr( $reversedHost, -1, 1 ) !== '.' ) {
792 $reversedHost .= '.';
793 }
794 // Reconstruct the pseudo-URL
795 $prot = $bits['scheme'];
796 $index = $prot . $bits['delimiter'] . $reversedHost;
797 // Leave out user and password. Add the port, path, query and fragment
798 if ( isset( $bits['port'] ) ) {
799 $index .= ':' . $bits['port'];
800 }
801 if ( isset( $bits['path'] ) ) {
802 $index .= $bits['path'];
803 } else {
804 $index .= '/';
805 }
806 if ( isset( $bits['query'] ) ) {
807 $index .= '?' . $bits['query'];
808 }
809 if ( isset( $bits['fragment'] ) ) {
810 $index .= '#' . $bits['fragment'];
811 }
812
813 if ( $prot == '' ) {
814 return array( "http:$index", "https:$index" );
815 } else {
816 return array( $index );
817 }
818 }
819
820 /**
821 * Check whether a given URL has a domain that occurs in a given set of domains
822 * @param $url string URL
823 * @param $domains array Array of domains (strings)
824 * @return bool True if the host part of $url ends in one of the strings in $domains
825 */
826 function wfMatchesDomainList( $url, $domains ) {
827 $bits = wfParseUrl( $url );
828 if ( is_array( $bits ) && isset( $bits['host'] ) ) {
829 foreach ( (array)$domains as $domain ) {
830 // FIXME: This gives false positives. http://nds-nl.wikipedia.org will match nl.wikipedia.org
831 // We should use something that interprets dots instead
832 if ( substr( $bits['host'], -strlen( $domain ) ) === $domain ) {
833 return true;
834 }
835 }
836 }
837 return false;
838 }
839
840 /**
841 * Sends a line to the debug log if enabled or, optionally, to a comment in output.
842 * In normal operation this is a NOP.
843 *
844 * Controlling globals:
845 * $wgDebugLogFile - points to the log file
846 * $wgProfileOnly - if set, normal debug messages will not be recorded.
847 * $wgDebugRawPage - if false, 'action=raw' hits will not result in debug output.
848 * $wgDebugComments - if on, some debug items may appear in comments in the HTML output.
849 *
850 * @param $text String
851 * @param $logonly Bool: set true to avoid appearing in HTML when $wgDebugComments is set
852 */
853 function wfDebug( $text, $logonly = false ) {
854 global $wgOut, $wgDebugLogFile, $wgDebugComments, $wgProfileOnly, $wgDebugRawPage;
855 global $wgDebugLogPrefix, $wgShowDebug;
856
857 static $cache = array(); // Cache of unoutputted messages
858 $text = wfDebugTimer() . $text;
859
860 if ( !$wgDebugRawPage && wfIsDebugRawPage() ) {
861 return;
862 }
863
864 if ( ( $wgDebugComments || $wgShowDebug ) && !$logonly ) {
865 $cache[] = $text;
866
867 if ( isset( $wgOut ) && is_object( $wgOut ) ) {
868 // add the message and any cached messages to the output
869 array_map( array( $wgOut, 'debug' ), $cache );
870 $cache = array();
871 }
872 }
873 if ( wfRunHooks( 'Debug', array( $text, null /* no log group */ ) ) ) {
874 if ( $wgDebugLogFile != '' && !$wgProfileOnly ) {
875 # Strip unprintables; they can switch terminal modes when binary data
876 # gets dumped, which is pretty annoying.
877 $text = preg_replace( '![\x00-\x08\x0b\x0c\x0e-\x1f]!', ' ', $text );
878 $text = $wgDebugLogPrefix . $text;
879 wfErrorLog( $text, $wgDebugLogFile );
880 }
881 }
882
883 MWDebug::debugMsg( $text );
884 }
885
886 /**
887 * Returns true if debug logging should be suppressed if $wgDebugRawPage = false
888 */
889 function wfIsDebugRawPage() {
890 static $cache;
891 if ( $cache !== null ) {
892 return $cache;
893 }
894 # Check for raw action using $_GET not $wgRequest, since the latter might not be initialised yet
895 if ( ( isset( $_GET['action'] ) && $_GET['action'] == 'raw' )
896 || (
897 isset( $_SERVER['SCRIPT_NAME'] )
898 && substr( $_SERVER['SCRIPT_NAME'], -8 ) == 'load.php'
899 ) )
900 {
901 $cache = true;
902 } else {
903 $cache = false;
904 }
905 return $cache;
906 }
907
908 /**
909 * Get microsecond timestamps for debug logs
910 *
911 * @return string
912 */
913 function wfDebugTimer() {
914 global $wgDebugTimestamps;
915 if ( !$wgDebugTimestamps ) {
916 return '';
917 }
918 static $start = null;
919
920 if ( $start === null ) {
921 $start = microtime( true );
922 $prefix = "\n$start";
923 } else {
924 $prefix = sprintf( "%6.4f", microtime( true ) - $start );
925 }
926 $mem = sprintf( "%5.1fM", ( memory_get_usage( true ) / (1024*1024) ) );
927 return "$prefix $mem " ;
928 }
929
930 /**
931 * Send a line giving PHP memory usage.
932 *
933 * @param $exact Bool: print exact values instead of kilobytes (default: false)
934 */
935 function wfDebugMem( $exact = false ) {
936 $mem = memory_get_usage();
937 if( !$exact ) {
938 $mem = floor( $mem / 1024 ) . ' kilobytes';
939 } else {
940 $mem .= ' bytes';
941 }
942 wfDebug( "Memory usage: $mem\n" );
943 }
944
945 /**
946 * Send a line to a supplementary debug log file, if configured, or main debug log if not.
947 * $wgDebugLogGroups[$logGroup] should be set to a filename to send to a separate log.
948 *
949 * @param $logGroup String
950 * @param $text String
951 * @param $public Bool: whether to log the event in the public log if no private
952 * log file is specified, (default true)
953 */
954 function wfDebugLog( $logGroup, $text, $public = true ) {
955 global $wgDebugLogGroups, $wgShowHostnames;
956 $text = trim( $text ) . "\n";
957 if( isset( $wgDebugLogGroups[$logGroup] ) ) {
958 $time = wfTimestamp( TS_DB );
959 $wiki = wfWikiID();
960 if ( $wgShowHostnames ) {
961 $host = wfHostname();
962 } else {
963 $host = '';
964 }
965 if ( wfRunHooks( 'Debug', array( $text, $logGroup ) ) ) {
966 wfErrorLog( "$time $host $wiki: $text", $wgDebugLogGroups[$logGroup] );
967 }
968 } elseif ( $public === true ) {
969 wfDebug( $text, true );
970 }
971 }
972
973 /**
974 * Log for database errors
975 *
976 * @param $text String: database error message.
977 */
978 function wfLogDBError( $text ) {
979 global $wgDBerrorLog, $wgDBname;
980 if ( $wgDBerrorLog ) {
981 $host = trim(`hostname`);
982 $text = date( 'D M j G:i:s T Y' ) . "\t$host\t$wgDBname\t$text";
983 wfErrorLog( $text, $wgDBerrorLog );
984 }
985 }
986
987 /**
988 * Log to a file without getting "file size exceeded" signals.
989 *
990 * Can also log to TCP or UDP with the syntax udp://host:port/prefix. This will
991 * send lines to the specified port, prefixed by the specified prefix and a space.
992 *
993 * @param $text String
994 * @param $file String filename
995 */
996 function wfErrorLog( $text, $file ) {
997 if ( substr( $file, 0, 4 ) == 'udp:' ) {
998 # Needs the sockets extension
999 if ( preg_match( '!^(tcp|udp):(?://)?\[([0-9a-fA-F:]+)\]:(\d+)(?:/(.*))?$!', $file, $m ) ) {
1000 // IPv6 bracketed host
1001 $host = $m[2];
1002 $port = intval( $m[3] );
1003 $prefix = isset( $m[4] ) ? $m[4] : false;
1004 $domain = AF_INET6;
1005 } elseif ( preg_match( '!^(tcp|udp):(?://)?([a-zA-Z0-9.-]+):(\d+)(?:/(.*))?$!', $file, $m ) ) {
1006 $host = $m[2];
1007 if ( !IP::isIPv4( $host ) ) {
1008 $host = gethostbyname( $host );
1009 }
1010 $port = intval( $m[3] );
1011 $prefix = isset( $m[4] ) ? $m[4] : false;
1012 $domain = AF_INET;
1013 } else {
1014 throw new MWException( __METHOD__ . ': Invalid UDP specification' );
1015 }
1016
1017 // Clean it up for the multiplexer
1018 if ( strval( $prefix ) !== '' ) {
1019 $text = preg_replace( '/^/m', $prefix . ' ', $text );
1020
1021 // Limit to 64KB
1022 if ( strlen( $text ) > 65506 ) {
1023 $text = substr( $text, 0, 65506 );
1024 }
1025
1026 if ( substr( $text, -1 ) != "\n" ) {
1027 $text .= "\n";
1028 }
1029 } elseif ( strlen( $text ) > 65507 ) {
1030 $text = substr( $text, 0, 65507 );
1031 }
1032
1033 $sock = socket_create( $domain, SOCK_DGRAM, SOL_UDP );
1034 if ( !$sock ) {
1035 return;
1036 }
1037
1038 socket_sendto( $sock, $text, strlen( $text ), 0, $host, $port );
1039 socket_close( $sock );
1040 } else {
1041 wfSuppressWarnings();
1042 $exists = file_exists( $file );
1043 $size = $exists ? filesize( $file ) : false;
1044 if ( !$exists || ( $size !== false && $size + strlen( $text ) < 0x7fffffff ) ) {
1045 file_put_contents( $file, $text, FILE_APPEND );
1046 }
1047 wfRestoreWarnings();
1048 }
1049 }
1050
1051 /**
1052 * @todo document
1053 */
1054 function wfLogProfilingData() {
1055 global $wgRequestTime, $wgDebugLogFile, $wgDebugRawPage, $wgRequest;
1056 global $wgProfileLimit, $wgUser;
1057
1058 $profiler = Profiler::instance();
1059
1060 # Profiling must actually be enabled...
1061 if ( $profiler->isStub() ) {
1062 return;
1063 }
1064
1065 // Get total page request time and only show pages that longer than
1066 // $wgProfileLimit time (default is 0)
1067 $now = wfTime();
1068 $elapsed = $now - $wgRequestTime;
1069 if ( $elapsed <= $wgProfileLimit ) {
1070 return;
1071 }
1072
1073 $profiler->logData();
1074
1075 // Check whether this should be logged in the debug file.
1076 if ( $wgDebugLogFile == '' || ( !$wgDebugRawPage && wfIsDebugRawPage() ) ) {
1077 return;
1078 }
1079
1080 $forward = '';
1081 if ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
1082 $forward = ' forwarded for ' . $_SERVER['HTTP_X_FORWARDED_FOR'];
1083 }
1084 if ( !empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
1085 $forward .= ' client IP ' . $_SERVER['HTTP_CLIENT_IP'];
1086 }
1087 if ( !empty( $_SERVER['HTTP_FROM'] ) ) {
1088 $forward .= ' from ' . $_SERVER['HTTP_FROM'];
1089 }
1090 if ( $forward ) {
1091 $forward = "\t(proxied via {$_SERVER['REMOTE_ADDR']}{$forward})";
1092 }
1093 // Don't load $wgUser at this late stage just for statistics purposes
1094 // @todo FIXME: We can detect some anons even if it is not loaded. See User::getId()
1095 if ( $wgUser->isItemLoaded( 'id' ) && $wgUser->isAnon() ) {
1096 $forward .= ' anon';
1097 }
1098 $log = sprintf( "%s\t%04.3f\t%s\n",
1099 gmdate( 'YmdHis' ), $elapsed,
1100 urldecode( $wgRequest->getRequestURL() . $forward ) );
1101
1102 wfErrorLog( $log . $profiler->getOutput(), $wgDebugLogFile );
1103 }
1104
1105 /**
1106 * Check if the wiki read-only lock file is present. This can be used to lock
1107 * off editing functions, but doesn't guarantee that the database will not be
1108 * modified.
1109 *
1110 * @return bool
1111 */
1112 function wfReadOnly() {
1113 global $wgReadOnlyFile, $wgReadOnly;
1114
1115 if ( !is_null( $wgReadOnly ) ) {
1116 return (bool)$wgReadOnly;
1117 }
1118 if ( $wgReadOnlyFile == '' ) {
1119 return false;
1120 }
1121 // Set $wgReadOnly for faster access next time
1122 if ( is_file( $wgReadOnlyFile ) ) {
1123 $wgReadOnly = file_get_contents( $wgReadOnlyFile );
1124 } else {
1125 $wgReadOnly = false;
1126 }
1127 return (bool)$wgReadOnly;
1128 }
1129
1130 /**
1131 * @return bool
1132 */
1133 function wfReadOnlyReason() {
1134 global $wgReadOnly;
1135 wfReadOnly();
1136 return $wgReadOnly;
1137 }
1138
1139 /**
1140 * Return a Language object from $langcode
1141 *
1142 * @param $langcode Mixed: either:
1143 * - a Language object
1144 * - code of the language to get the message for, if it is
1145 * a valid code create a language for that language, if
1146 * it is a string but not a valid code then make a basic
1147 * language object
1148 * - a boolean: if it's false then use the global object for
1149 * the current user's language (as a fallback for the old parameter
1150 * functionality), or if it is true then use global object
1151 * for the wiki's content language.
1152 * @return Language object
1153 */
1154 function wfGetLangObj( $langcode = false ) {
1155 # Identify which language to get or create a language object for.
1156 # Using is_object here due to Stub objects.
1157 if( is_object( $langcode ) ) {
1158 # Great, we already have the object (hopefully)!
1159 return $langcode;
1160 }
1161
1162 global $wgContLang, $wgLanguageCode;
1163 if( $langcode === true || $langcode === $wgLanguageCode ) {
1164 # $langcode is the language code of the wikis content language object.
1165 # or it is a boolean and value is true
1166 return $wgContLang;
1167 }
1168
1169 global $wgLang;
1170 if( $langcode === false || $langcode === $wgLang->getCode() ) {
1171 # $langcode is the language code of user language object.
1172 # or it was a boolean and value is false
1173 return $wgLang;
1174 }
1175
1176 $validCodes = array_keys( Language::getLanguageNames() );
1177 if( in_array( $langcode, $validCodes ) ) {
1178 # $langcode corresponds to a valid language.
1179 return Language::factory( $langcode );
1180 }
1181
1182 # $langcode is a string, but not a valid language code; use content language.
1183 wfDebug( "Invalid language code passed to wfGetLangObj, falling back to content language.\n" );
1184 return $wgContLang;
1185 }
1186
1187 /**
1188 * Old function when $wgBetterDirectionality existed
1189 * Removed in core, kept in extensions for backwards compat.
1190 *
1191 * @deprecated since 1.18
1192 * @return Language
1193 */
1194 function wfUILang() {
1195 wfDeprecated( __METHOD__, '1.18' );
1196 global $wgLang;
1197 return $wgLang;
1198 }
1199
1200 /**
1201 * This is the new function for getting translated interface messages.
1202 * See the Message class for documentation how to use them.
1203 * The intention is that this function replaces all old wfMsg* functions.
1204 * @param $key \string Message key.
1205 * Varargs: normal message parameters.
1206 * @return Message
1207 * @since 1.17
1208 */
1209 function wfMessage( $key /*...*/) {
1210 $params = func_get_args();
1211 array_shift( $params );
1212 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
1213 $params = $params[0];
1214 }
1215 return new Message( $key, $params );
1216 }
1217
1218 /**
1219 * This function accepts multiple message keys and returns a message instance
1220 * for the first message which is non-empty. If all messages are empty then an
1221 * instance of the first message key is returned.
1222 * @param varargs: message keys
1223 * @return Message
1224 * @since 1.18
1225 */
1226 function wfMessageFallback( /*...*/ ) {
1227 $args = func_get_args();
1228 return MWFunction::callArray( 'Message::newFallbackSequence', $args );
1229 }
1230
1231 /**
1232 * Get a message from anywhere, for the current user language.
1233 *
1234 * Use wfMsgForContent() instead if the message should NOT
1235 * change depending on the user preferences.
1236 *
1237 * @param $key String: lookup key for the message, usually
1238 * defined in languages/Language.php
1239 *
1240 * Parameters to the message, which can be used to insert variable text into
1241 * it, can be passed to this function in the following formats:
1242 * - One per argument, starting at the second parameter
1243 * - As an array in the second parameter
1244 * These are not shown in the function definition.
1245 *
1246 * @return String
1247 */
1248 function wfMsg( $key ) {
1249 $args = func_get_args();
1250 array_shift( $args );
1251 return wfMsgReal( $key, $args );
1252 }
1253
1254 /**
1255 * Same as above except doesn't transform the message
1256 *
1257 * @param $key String
1258 * @return String
1259 */
1260 function wfMsgNoTrans( $key ) {
1261 $args = func_get_args();
1262 array_shift( $args );
1263 return wfMsgReal( $key, $args, true, false, false );
1264 }
1265
1266 /**
1267 * Get a message from anywhere, for the current global language
1268 * set with $wgLanguageCode.
1269 *
1270 * Use this if the message should NOT change dependent on the
1271 * language set in the user's preferences. This is the case for
1272 * most text written into logs, as well as link targets (such as
1273 * the name of the copyright policy page). Link titles, on the
1274 * other hand, should be shown in the UI language.
1275 *
1276 * Note that MediaWiki allows users to change the user interface
1277 * language in their preferences, but a single installation
1278 * typically only contains content in one language.
1279 *
1280 * Be wary of this distinction: If you use wfMsg() where you should
1281 * use wfMsgForContent(), a user of the software may have to
1282 * customize potentially hundreds of messages in
1283 * order to, e.g., fix a link in every possible language.
1284 *
1285 * @param $key String: lookup key for the message, usually
1286 * defined in languages/Language.php
1287 * @return String
1288 */
1289 function wfMsgForContent( $key ) {
1290 global $wgForceUIMsgAsContentMsg;
1291 $args = func_get_args();
1292 array_shift( $args );
1293 $forcontent = true;
1294 if( is_array( $wgForceUIMsgAsContentMsg ) &&
1295 in_array( $key, $wgForceUIMsgAsContentMsg ) )
1296 {
1297 $forcontent = false;
1298 }
1299 return wfMsgReal( $key, $args, true, $forcontent );
1300 }
1301
1302 /**
1303 * Same as above except doesn't transform the message
1304 *
1305 * @param $key String
1306 * @return String
1307 */
1308 function wfMsgForContentNoTrans( $key ) {
1309 global $wgForceUIMsgAsContentMsg;
1310 $args = func_get_args();
1311 array_shift( $args );
1312 $forcontent = true;
1313 if( is_array( $wgForceUIMsgAsContentMsg ) &&
1314 in_array( $key, $wgForceUIMsgAsContentMsg ) )
1315 {
1316 $forcontent = false;
1317 }
1318 return wfMsgReal( $key, $args, true, $forcontent, false );
1319 }
1320
1321 /**
1322 * Really get a message
1323 *
1324 * @param $key String: key to get.
1325 * @param $args
1326 * @param $useDB Boolean
1327 * @param $forContent Mixed: Language code, or false for user lang, true for content lang.
1328 * @param $transform Boolean: Whether or not to transform the message.
1329 * @return String: the requested message.
1330 */
1331 function wfMsgReal( $key, $args, $useDB = true, $forContent = false, $transform = true ) {
1332 wfProfileIn( __METHOD__ );
1333 $message = wfMsgGetKey( $key, $useDB, $forContent, $transform );
1334 $message = wfMsgReplaceArgs( $message, $args );
1335 wfProfileOut( __METHOD__ );
1336 return $message;
1337 }
1338
1339 /**
1340 * Fetch a message string value, but don't replace any keys yet.
1341 *
1342 * @param $key String
1343 * @param $useDB Bool
1344 * @param $langCode String: Code of the language to get the message for, or
1345 * behaves as a content language switch if it is a boolean.
1346 * @param $transform Boolean: whether to parse magic words, etc.
1347 * @return string
1348 */
1349 function wfMsgGetKey( $key, $useDB = true, $langCode = false, $transform = true ) {
1350 wfRunHooks( 'NormalizeMessageKey', array( &$key, &$useDB, &$langCode, &$transform ) );
1351
1352 $cache = MessageCache::singleton();
1353 $message = $cache->get( $key, $useDB, $langCode );
1354 if( $message === false ) {
1355 $message = '&lt;' . htmlspecialchars( $key ) . '&gt;';
1356 } elseif ( $transform ) {
1357 $message = $cache->transform( $message );
1358 }
1359 return $message;
1360 }
1361
1362 /**
1363 * Replace message parameter keys on the given formatted output.
1364 *
1365 * @param $message String
1366 * @param $args Array
1367 * @return string
1368 * @private
1369 */
1370 function wfMsgReplaceArgs( $message, $args ) {
1371 # Fix windows line-endings
1372 # Some messages are split with explode("\n", $msg)
1373 $message = str_replace( "\r", '', $message );
1374
1375 // Replace arguments
1376 if ( count( $args ) ) {
1377 if ( is_array( $args[0] ) ) {
1378 $args = array_values( $args[0] );
1379 }
1380 $replacementKeys = array();
1381 foreach( $args as $n => $param ) {
1382 $replacementKeys['$' . ( $n + 1 )] = $param;
1383 }
1384 $message = strtr( $message, $replacementKeys );
1385 }
1386
1387 return $message;
1388 }
1389
1390 /**
1391 * Return an HTML-escaped version of a message.
1392 * Parameter replacements, if any, are done *after* the HTML-escaping,
1393 * so parameters may contain HTML (eg links or form controls). Be sure
1394 * to pre-escape them if you really do want plaintext, or just wrap
1395 * the whole thing in htmlspecialchars().
1396 *
1397 * @param $key String
1398 * @param string ... parameters
1399 * @return string
1400 */
1401 function wfMsgHtml( $key ) {
1402 $args = func_get_args();
1403 array_shift( $args );
1404 return wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $key ) ), $args );
1405 }
1406
1407 /**
1408 * Return an HTML version of message
1409 * Parameter replacements, if any, are done *after* parsing the wiki-text message,
1410 * so parameters may contain HTML (eg links or form controls). Be sure
1411 * to pre-escape them if you really do want plaintext, or just wrap
1412 * the whole thing in htmlspecialchars().
1413 *
1414 * @param $key String
1415 * @param string ... parameters
1416 * @return string
1417 */
1418 function wfMsgWikiHtml( $key ) {
1419 $args = func_get_args();
1420 array_shift( $args );
1421 return wfMsgReplaceArgs(
1422 MessageCache::singleton()->parse( wfMsgGetKey( $key ), null,
1423 /* can't be set to false */ true, /* interface */ true )->getText(),
1424 $args );
1425 }
1426
1427 /**
1428 * Returns message in the requested format
1429 * @param $key String: key of the message
1430 * @param $options Array: processing rules. Can take the following options:
1431 * <i>parse</i>: parses wikitext to HTML
1432 * <i>parseinline</i>: parses wikitext to HTML and removes the surrounding
1433 * p's added by parser or tidy
1434 * <i>escape</i>: filters message through htmlspecialchars
1435 * <i>escapenoentities</i>: same, but allows entity references like &#160; through
1436 * <i>replaceafter</i>: parameters are substituted after parsing or escaping
1437 * <i>parsemag</i>: transform the message using magic phrases
1438 * <i>content</i>: fetch message for content language instead of interface
1439 * Also can accept a single associative argument, of the form 'language' => 'xx':
1440 * <i>language</i>: Language object or language code to fetch message for
1441 * (overriden by <i>content</i>).
1442 * Behavior for conflicting options (e.g., parse+parseinline) is undefined.
1443 *
1444 * @return String
1445 */
1446 function wfMsgExt( $key, $options ) {
1447 $args = func_get_args();
1448 array_shift( $args );
1449 array_shift( $args );
1450 $options = (array)$options;
1451
1452 foreach( $options as $arrayKey => $option ) {
1453 if( !preg_match( '/^[0-9]+|language$/', $arrayKey ) ) {
1454 # An unknown index, neither numeric nor "language"
1455 wfWarn( "wfMsgExt called with incorrect parameter key $arrayKey", 1, E_USER_WARNING );
1456 } elseif( preg_match( '/^[0-9]+$/', $arrayKey ) && !in_array( $option,
1457 array( 'parse', 'parseinline', 'escape', 'escapenoentities',
1458 'replaceafter', 'parsemag', 'content' ) ) ) {
1459 # A numeric index with unknown value
1460 wfWarn( "wfMsgExt called with incorrect parameter $option", 1, E_USER_WARNING );
1461 }
1462 }
1463
1464 if( in_array( 'content', $options, true ) ) {
1465 $forContent = true;
1466 $langCode = true;
1467 $langCodeObj = null;
1468 } elseif( array_key_exists( 'language', $options ) ) {
1469 $forContent = false;
1470 $langCode = wfGetLangObj( $options['language'] );
1471 $langCodeObj = $langCode;
1472 } else {
1473 $forContent = false;
1474 $langCode = false;
1475 $langCodeObj = null;
1476 }
1477
1478 $string = wfMsgGetKey( $key, /*DB*/true, $langCode, /*Transform*/false );
1479
1480 if( !in_array( 'replaceafter', $options, true ) ) {
1481 $string = wfMsgReplaceArgs( $string, $args );
1482 }
1483
1484 $messageCache = MessageCache::singleton();
1485 if( in_array( 'parse', $options, true ) ) {
1486 $string = $messageCache->parse( $string, null, true, !$forContent, $langCodeObj )->getText();
1487 } elseif ( in_array( 'parseinline', $options, true ) ) {
1488 $string = $messageCache->parse( $string, null, true, !$forContent, $langCodeObj )->getText();
1489 $m = array();
1490 if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
1491 $string = $m[1];
1492 }
1493 } elseif ( in_array( 'parsemag', $options, true ) ) {
1494 $string = $messageCache->transform( $string,
1495 !$forContent, $langCodeObj );
1496 }
1497
1498 if ( in_array( 'escape', $options, true ) ) {
1499 $string = htmlspecialchars ( $string );
1500 } elseif ( in_array( 'escapenoentities', $options, true ) ) {
1501 $string = Sanitizer::escapeHtmlAllowEntities( $string );
1502 }
1503
1504 if( in_array( 'replaceafter', $options, true ) ) {
1505 $string = wfMsgReplaceArgs( $string, $args );
1506 }
1507
1508 return $string;
1509 }
1510
1511 /**
1512 * Since wfMsg() and co suck, they don't return false if the message key they
1513 * looked up didn't exist but a XHTML string, this function checks for the
1514 * nonexistance of messages by checking the MessageCache::get() result directly.
1515 *
1516 * @param $key String: the message key looked up
1517 * @return Boolean True if the message *doesn't* exist.
1518 */
1519 function wfEmptyMsg( $key ) {
1520 return MessageCache::singleton()->get( $key, /*useDB*/true, /*content*/false ) === false;
1521 }
1522
1523 /**
1524 * Throw a debugging exception. This function previously once exited the process,
1525 * but now throws an exception instead, with similar results.
1526 *
1527 * @param $msg String: message shown when dying.
1528 */
1529 function wfDebugDieBacktrace( $msg = '' ) {
1530 throw new MWException( $msg );
1531 }
1532
1533 /**
1534 * Fetch server name for use in error reporting etc.
1535 * Use real server name if available, so we know which machine
1536 * in a server farm generated the current page.
1537 *
1538 * @return string
1539 */
1540 function wfHostname() {
1541 static $host;
1542 if ( is_null( $host ) ) {
1543 if ( function_exists( 'posix_uname' ) ) {
1544 // This function not present on Windows
1545 $uname = posix_uname();
1546 } else {
1547 $uname = false;
1548 }
1549 if( is_array( $uname ) && isset( $uname['nodename'] ) ) {
1550 $host = $uname['nodename'];
1551 } elseif ( getenv( 'COMPUTERNAME' ) ) {
1552 # Windows computer name
1553 $host = getenv( 'COMPUTERNAME' );
1554 } else {
1555 # This may be a virtual server.
1556 $host = $_SERVER['SERVER_NAME'];
1557 }
1558 }
1559 return $host;
1560 }
1561
1562 /**
1563 * Returns a HTML comment with the elapsed time since request.
1564 * This method has no side effects.
1565 *
1566 * @return string
1567 */
1568 function wfReportTime() {
1569 global $wgRequestTime, $wgShowHostnames;
1570
1571 $now = wfTime();
1572 $elapsed = $now - $wgRequestTime;
1573
1574 return $wgShowHostnames
1575 ? sprintf( '<!-- Served by %s in %01.3f secs. -->', wfHostname(), $elapsed )
1576 : sprintf( '<!-- Served in %01.3f secs. -->', $elapsed );
1577 }
1578
1579 /**
1580 * Safety wrapper for debug_backtrace().
1581 *
1582 * With Zend Optimizer 3.2.0 loaded, this causes segfaults under somewhat
1583 * murky circumstances, which may be triggered in part by stub objects
1584 * or other fancy talkin'.
1585 *
1586 * Will return an empty array if Zend Optimizer is detected or if
1587 * debug_backtrace is disabled, otherwise the output from
1588 * debug_backtrace() (trimmed).
1589 *
1590 * @param $limit int This parameter can be used to limit the number of stack frames returned
1591 *
1592 * @return array of backtrace information
1593 */
1594 function wfDebugBacktrace( $limit = 0 ) {
1595 static $disabled = null;
1596
1597 if( extension_loaded( 'Zend Optimizer' ) ) {
1598 wfDebug( "Zend Optimizer detected; skipping debug_backtrace for safety.\n" );
1599 return array();
1600 }
1601
1602 if ( is_null( $disabled ) ) {
1603 $disabled = false;
1604 $functions = explode( ',', ini_get( 'disable_functions' ) );
1605 $functions = array_map( 'trim', $functions );
1606 $functions = array_map( 'strtolower', $functions );
1607 if ( in_array( 'debug_backtrace', $functions ) ) {
1608 wfDebug( "debug_backtrace is in disabled_functions\n" );
1609 $disabled = true;
1610 }
1611 }
1612 if ( $disabled ) {
1613 return array();
1614 }
1615
1616 if ( $limit && version_compare( PHP_VERSION, '5.4.0', '>=' ) ) {
1617 return array_slice( debug_backtrace( DEBUG_BACKTRACE_PROVIDE_OBJECT, $limit ), 1 );
1618 } else {
1619 return array_slice( debug_backtrace(), 1 );
1620 }
1621 }
1622
1623 /**
1624 * Get a debug backtrace as a string
1625 *
1626 * @return string
1627 */
1628 function wfBacktrace() {
1629 global $wgCommandLineMode;
1630
1631 if ( $wgCommandLineMode ) {
1632 $msg = '';
1633 } else {
1634 $msg = "<ul>\n";
1635 }
1636 $backtrace = wfDebugBacktrace();
1637 foreach( $backtrace as $call ) {
1638 if( isset( $call['file'] ) ) {
1639 $f = explode( DIRECTORY_SEPARATOR, $call['file'] );
1640 $file = $f[count( $f ) - 1];
1641 } else {
1642 $file = '-';
1643 }
1644 if( isset( $call['line'] ) ) {
1645 $line = $call['line'];
1646 } else {
1647 $line = '-';
1648 }
1649 if ( $wgCommandLineMode ) {
1650 $msg .= "$file line $line calls ";
1651 } else {
1652 $msg .= '<li>' . $file . ' line ' . $line . ' calls ';
1653 }
1654 if( !empty( $call['class'] ) ) {
1655 $msg .= $call['class'] . $call['type'];
1656 }
1657 $msg .= $call['function'] . '()';
1658
1659 if ( $wgCommandLineMode ) {
1660 $msg .= "\n";
1661 } else {
1662 $msg .= "</li>\n";
1663 }
1664 }
1665 if ( $wgCommandLineMode ) {
1666 $msg .= "\n";
1667 } else {
1668 $msg .= "</ul>\n";
1669 }
1670
1671 return $msg;
1672 }
1673
1674 /**
1675 * Get the name of the function which called this function
1676 *
1677 * @param $level Int
1678 * @return Bool|string
1679 */
1680 function wfGetCaller( $level = 2 ) {
1681 $backtrace = wfDebugBacktrace( $level );
1682 if ( isset( $backtrace[$level] ) ) {
1683 return wfFormatStackFrame( $backtrace[$level] );
1684 } else {
1685 $caller = 'unknown';
1686 }
1687 return $caller;
1688 }
1689
1690 /**
1691 * Return a string consisting of callers in the stack. Useful sometimes
1692 * for profiling specific points.
1693 *
1694 * @param $limit The maximum depth of the stack frame to return, or false for
1695 * the entire stack.
1696 * @return String
1697 */
1698 function wfGetAllCallers( $limit = 3 ) {
1699 $trace = array_reverse( wfDebugBacktrace() );
1700 if ( !$limit || $limit > count( $trace ) - 1 ) {
1701 $limit = count( $trace ) - 1;
1702 }
1703 $trace = array_slice( $trace, -$limit - 1, $limit );
1704 return implode( '/', array_map( 'wfFormatStackFrame', $trace ) );
1705 }
1706
1707 /**
1708 * Return a string representation of frame
1709 *
1710 * @param $frame Array
1711 * @return Bool
1712 */
1713 function wfFormatStackFrame( $frame ) {
1714 return isset( $frame['class'] ) ?
1715 $frame['class'] . '::' . $frame['function'] :
1716 $frame['function'];
1717 }
1718
1719
1720 /* Some generic result counters, pulled out of SearchEngine */
1721
1722
1723 /**
1724 * @todo document
1725 *
1726 * @param $offset Int
1727 * @param $limit Int
1728 * @return String
1729 */
1730 function wfShowingResults( $offset, $limit ) {
1731 global $wgLang;
1732 return wfMsgExt(
1733 'showingresults',
1734 array( 'parseinline' ),
1735 $wgLang->formatNum( $limit ),
1736 $wgLang->formatNum( $offset + 1 )
1737 );
1738 }
1739
1740 /**
1741 * Generate (prev x| next x) (20|50|100...) type links for paging
1742 *
1743 * @param $offset String
1744 * @param $limit Integer
1745 * @param $link String
1746 * @param $query String: optional URL query parameter string
1747 * @param $atend Bool: optional param for specified if this is the last page
1748 * @return String
1749 * @deprecated in 1.19; use Language::viewPrevNext() instead
1750 */
1751 function wfViewPrevNext( $offset, $limit, $link, $query = '', $atend = false ) {
1752 wfDeprecated( __METHOD__, '1.19' );
1753
1754 global $wgLang;
1755
1756 $query = wfCgiToArray( $query );
1757
1758 if( is_object( $link ) ) {
1759 $title = $link;
1760 } else {
1761 $title = Title::newFromText( $link );
1762 if( is_null( $title ) ) {
1763 return false;
1764 }
1765 }
1766
1767 return $wgLang->viewPrevNext( $title, $offset, $limit, $query, $atend );
1768 }
1769
1770 /**
1771 * Make a list item, used by various special pages
1772 *
1773 * @param $page String Page link
1774 * @param $details String Text between brackets
1775 * @param $oppositedm Boolean Add the direction mark opposite to your
1776 * language, to display text properly
1777 * @return String
1778 * @deprecated since 1.19; use Language::specialList() instead
1779 */
1780 function wfSpecialList( $page, $details, $oppositedm = true ) {
1781 global $wgLang;
1782 return $wgLang->specialList( $page, $details, $oppositedm );
1783 }
1784
1785 /**
1786 * @todo document
1787 * @todo FIXME: We may want to blacklist some broken browsers
1788 *
1789 * @param $force Bool
1790 * @return bool Whereas client accept gzip compression
1791 */
1792 function wfClientAcceptsGzip( $force = false ) {
1793 static $result = null;
1794 if ( $result === null || $force ) {
1795 $result = false;
1796 if( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
1797 # @todo FIXME: We may want to blacklist some broken browsers
1798 $m = array();
1799 if( preg_match(
1800 '/\bgzip(?:;(q)=([0-9]+(?:\.[0-9]+)))?\b/',
1801 $_SERVER['HTTP_ACCEPT_ENCODING'],
1802 $m )
1803 )
1804 {
1805 if( isset( $m[2] ) && ( $m[1] == 'q' ) && ( $m[2] == 0 ) ) {
1806 $result = false;
1807 return $result;
1808 }
1809 wfDebug( "wfClientAcceptsGzip: client accepts gzip.\n" );
1810 $result = true;
1811 }
1812 }
1813 }
1814 return $result;
1815 }
1816
1817 /**
1818 * Obtain the offset and limit values from the request string;
1819 * used in special pages
1820 *
1821 * @param $deflimit Int default limit if none supplied
1822 * @param $optionname String Name of a user preference to check against
1823 * @return array
1824 *
1825 */
1826 function wfCheckLimits( $deflimit = 50, $optionname = 'rclimit' ) {
1827 global $wgRequest;
1828 return $wgRequest->getLimitOffset( $deflimit, $optionname );
1829 }
1830
1831 /**
1832 * Escapes the given text so that it may be output using addWikiText()
1833 * without any linking, formatting, etc. making its way through. This
1834 * is achieved by substituting certain characters with HTML entities.
1835 * As required by the callers, <nowiki> is not used.
1836 *
1837 * @param $text String: text to be escaped
1838 * @return String
1839 */
1840 function wfEscapeWikiText( $text ) {
1841 $text = strtr( "\n$text", array(
1842 '"' => '&#34;', '&' => '&#38;', "'" => '&#39;', '<' => '&#60;',
1843 '=' => '&#61;', '>' => '&#62;', '[' => '&#91;', ']' => '&#93;',
1844 '{' => '&#123;', '|' => '&#124;', '}' => '&#125;',
1845 "\n#" => "\n&#35;", "\n*" => "\n&#42;",
1846 "\n:" => "\n&#58;", "\n;" => "\n&#59;",
1847 '://' => '&#58;//', 'ISBN ' => 'ISBN&#32;', 'RFC ' => 'RFC&#32;',
1848 ) );
1849 return substr( $text, 1 );
1850 }
1851
1852 /**
1853 * Get the current unix timetstamp with microseconds. Useful for profiling
1854 * @return Float
1855 */
1856 function wfTime() {
1857 return microtime( true );
1858 }
1859
1860 /**
1861 * Sets dest to source and returns the original value of dest
1862 * If source is NULL, it just returns the value, it doesn't set the variable
1863 * If force is true, it will set the value even if source is NULL
1864 *
1865 * @param $dest Mixed
1866 * @param $source Mixed
1867 * @param $force Bool
1868 * @return Mixed
1869 */
1870 function wfSetVar( &$dest, $source, $force = false ) {
1871 $temp = $dest;
1872 if ( !is_null( $source ) || $force ) {
1873 $dest = $source;
1874 }
1875 return $temp;
1876 }
1877
1878 /**
1879 * As for wfSetVar except setting a bit
1880 *
1881 * @param $dest Int
1882 * @param $bit Int
1883 * @param $state Bool
1884 *
1885 * @return bool
1886 */
1887 function wfSetBit( &$dest, $bit, $state = true ) {
1888 $temp = (bool)( $dest & $bit );
1889 if ( !is_null( $state ) ) {
1890 if ( $state ) {
1891 $dest |= $bit;
1892 } else {
1893 $dest &= ~$bit;
1894 }
1895 }
1896 return $temp;
1897 }
1898
1899 /**
1900 * A wrapper around the PHP function var_export().
1901 * Either print it or add it to the regular output ($wgOut).
1902 *
1903 * @param $var A PHP variable to dump.
1904 */
1905 function wfVarDump( $var ) {
1906 global $wgOut;
1907 $s = str_replace( "\n", "<br />\n", var_export( $var, true ) . "\n" );
1908 if ( headers_sent() || !isset( $wgOut ) || !is_object( $wgOut ) ) {
1909 print $s;
1910 } else {
1911 $wgOut->addHTML( $s );
1912 }
1913 }
1914
1915 /**
1916 * Provide a simple HTTP error.
1917 *
1918 * @param $code Int|String
1919 * @param $label String
1920 * @param $desc String
1921 */
1922 function wfHttpError( $code, $label, $desc ) {
1923 global $wgOut;
1924 $wgOut->disable();
1925 header( "HTTP/1.0 $code $label" );
1926 header( "Status: $code $label" );
1927 $wgOut->sendCacheControl();
1928
1929 header( 'Content-type: text/html; charset=utf-8' );
1930 print "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">".
1931 '<html><head><title>' .
1932 htmlspecialchars( $label ) .
1933 '</title></head><body><h1>' .
1934 htmlspecialchars( $label ) .
1935 '</h1><p>' .
1936 nl2br( htmlspecialchars( $desc ) ) .
1937 "</p></body></html>\n";
1938 }
1939
1940 /**
1941 * Clear away any user-level output buffers, discarding contents.
1942 *
1943 * Suitable for 'starting afresh', for instance when streaming
1944 * relatively large amounts of data without buffering, or wanting to
1945 * output image files without ob_gzhandler's compression.
1946 *
1947 * The optional $resetGzipEncoding parameter controls suppression of
1948 * the Content-Encoding header sent by ob_gzhandler; by default it
1949 * is left. See comments for wfClearOutputBuffers() for why it would
1950 * be used.
1951 *
1952 * Note that some PHP configuration options may add output buffer
1953 * layers which cannot be removed; these are left in place.
1954 *
1955 * @param $resetGzipEncoding Bool
1956 */
1957 function wfResetOutputBuffers( $resetGzipEncoding = true ) {
1958 if( $resetGzipEncoding ) {
1959 // Suppress Content-Encoding and Content-Length
1960 // headers from 1.10+s wfOutputHandler
1961 global $wgDisableOutputCompression;
1962 $wgDisableOutputCompression = true;
1963 }
1964 while( $status = ob_get_status() ) {
1965 if( $status['type'] == 0 /* PHP_OUTPUT_HANDLER_INTERNAL */ ) {
1966 // Probably from zlib.output_compression or other
1967 // PHP-internal setting which can't be removed.
1968 //
1969 // Give up, and hope the result doesn't break
1970 // output behavior.
1971 break;
1972 }
1973 if( !ob_end_clean() ) {
1974 // Could not remove output buffer handler; abort now
1975 // to avoid getting in some kind of infinite loop.
1976 break;
1977 }
1978 if( $resetGzipEncoding ) {
1979 if( $status['name'] == 'ob_gzhandler' ) {
1980 // Reset the 'Content-Encoding' field set by this handler
1981 // so we can start fresh.
1982 if ( function_exists( 'header_remove' ) ) {
1983 // Available since PHP 5.3.0
1984 header_remove( 'Content-Encoding' );
1985 } else {
1986 // We need to provide a valid content-coding. See bug 28069
1987 header( 'Content-Encoding: identity' );
1988 }
1989 break;
1990 }
1991 }
1992 }
1993 }
1994
1995 /**
1996 * More legible than passing a 'false' parameter to wfResetOutputBuffers():
1997 *
1998 * Clear away output buffers, but keep the Content-Encoding header
1999 * produced by ob_gzhandler, if any.
2000 *
2001 * This should be used for HTTP 304 responses, where you need to
2002 * preserve the Content-Encoding header of the real result, but
2003 * also need to suppress the output of ob_gzhandler to keep to spec
2004 * and avoid breaking Firefox in rare cases where the headers and
2005 * body are broken over two packets.
2006 */
2007 function wfClearOutputBuffers() {
2008 wfResetOutputBuffers( false );
2009 }
2010
2011 /**
2012 * Converts an Accept-* header into an array mapping string values to quality
2013 * factors
2014 *
2015 * @param $accept String
2016 * @param $def String default
2017 * @return Array
2018 */
2019 function wfAcceptToPrefs( $accept, $def = '*/*' ) {
2020 # No arg means accept anything (per HTTP spec)
2021 if( !$accept ) {
2022 return array( $def => 1.0 );
2023 }
2024
2025 $prefs = array();
2026
2027 $parts = explode( ',', $accept );
2028
2029 foreach( $parts as $part ) {
2030 # @todo FIXME: Doesn't deal with params like 'text/html; level=1'
2031 $values = explode( ';', trim( $part ) );
2032 $match = array();
2033 if ( count( $values ) == 1 ) {
2034 $prefs[$values[0]] = 1.0;
2035 } elseif ( preg_match( '/q\s*=\s*(\d*\.\d+)/', $values[1], $match ) ) {
2036 $prefs[$values[0]] = floatval( $match[1] );
2037 }
2038 }
2039
2040 return $prefs;
2041 }
2042
2043 /**
2044 * Checks if a given MIME type matches any of the keys in the given
2045 * array. Basic wildcards are accepted in the array keys.
2046 *
2047 * Returns the matching MIME type (or wildcard) if a match, otherwise
2048 * NULL if no match.
2049 *
2050 * @param $type String
2051 * @param $avail Array
2052 * @return string
2053 * @private
2054 */
2055 function mimeTypeMatch( $type, $avail ) {
2056 if( array_key_exists( $type, $avail ) ) {
2057 return $type;
2058 } else {
2059 $parts = explode( '/', $type );
2060 if( array_key_exists( $parts[0] . '/*', $avail ) ) {
2061 return $parts[0] . '/*';
2062 } elseif( array_key_exists( '*/*', $avail ) ) {
2063 return '*/*';
2064 } else {
2065 return null;
2066 }
2067 }
2068 }
2069
2070 /**
2071 * Returns the 'best' match between a client's requested internet media types
2072 * and the server's list of available types. Each list should be an associative
2073 * array of type to preference (preference is a float between 0.0 and 1.0).
2074 * Wildcards in the types are acceptable.
2075 *
2076 * @param $cprefs Array: client's acceptable type list
2077 * @param $sprefs Array: server's offered types
2078 * @return string
2079 *
2080 * @todo FIXME: Doesn't handle params like 'text/plain; charset=UTF-8'
2081 * XXX: generalize to negotiate other stuff
2082 */
2083 function wfNegotiateType( $cprefs, $sprefs ) {
2084 $combine = array();
2085
2086 foreach( array_keys( $sprefs ) as $type ) {
2087 $parts = explode( '/', $type );
2088 if( $parts[1] != '*' ) {
2089 $ckey = mimeTypeMatch( $type, $cprefs );
2090 if( $ckey ) {
2091 $combine[$type] = $sprefs[$type] * $cprefs[$ckey];
2092 }
2093 }
2094 }
2095
2096 foreach( array_keys( $cprefs ) as $type ) {
2097 $parts = explode( '/', $type );
2098 if( $parts[1] != '*' && !array_key_exists( $type, $sprefs ) ) {
2099 $skey = mimeTypeMatch( $type, $sprefs );
2100 if( $skey ) {
2101 $combine[$type] = $sprefs[$skey] * $cprefs[$type];
2102 }
2103 }
2104 }
2105
2106 $bestq = 0;
2107 $besttype = null;
2108
2109 foreach( array_keys( $combine ) as $type ) {
2110 if( $combine[$type] > $bestq ) {
2111 $besttype = $type;
2112 $bestq = $combine[$type];
2113 }
2114 }
2115
2116 return $besttype;
2117 }
2118
2119 /**
2120 * Reference-counted warning suppression
2121 *
2122 * @param $end Bool
2123 */
2124 function wfSuppressWarnings( $end = false ) {
2125 static $suppressCount = 0;
2126 static $originalLevel = false;
2127
2128 if ( $end ) {
2129 if ( $suppressCount ) {
2130 --$suppressCount;
2131 if ( !$suppressCount ) {
2132 error_reporting( $originalLevel );
2133 }
2134 }
2135 } else {
2136 if ( !$suppressCount ) {
2137 // E_DEPRECATED is undefined in PHP 5.2
2138 if( !defined( 'E_DEPRECATED' ) ) {
2139 define( 'E_DEPRECATED', 8192 );
2140 }
2141 $originalLevel = error_reporting( E_ALL & ~( E_WARNING | E_NOTICE | E_USER_WARNING | E_USER_NOTICE | E_DEPRECATED ) );
2142 }
2143 ++$suppressCount;
2144 }
2145 }
2146
2147 /**
2148 * Restore error level to previous value
2149 */
2150 function wfRestoreWarnings() {
2151 wfSuppressWarnings( true );
2152 }
2153
2154 # Autodetect, convert and provide timestamps of various types
2155
2156 /**
2157 * Unix time - the number of seconds since 1970-01-01 00:00:00 UTC
2158 */
2159 define( 'TS_UNIX', 0 );
2160
2161 /**
2162 * MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
2163 */
2164 define( 'TS_MW', 1 );
2165
2166 /**
2167 * MySQL DATETIME (YYYY-MM-DD HH:MM:SS)
2168 */
2169 define( 'TS_DB', 2 );
2170
2171 /**
2172 * RFC 2822 format, for E-mail and HTTP headers
2173 */
2174 define( 'TS_RFC2822', 3 );
2175
2176 /**
2177 * ISO 8601 format with no timezone: 1986-02-09T20:00:00Z
2178 *
2179 * This is used by Special:Export
2180 */
2181 define( 'TS_ISO_8601', 4 );
2182
2183 /**
2184 * An Exif timestamp (YYYY:MM:DD HH:MM:SS)
2185 *
2186 * @see http://exif.org/Exif2-2.PDF The Exif 2.2 spec, see page 28 for the
2187 * DateTime tag and page 36 for the DateTimeOriginal and
2188 * DateTimeDigitized tags.
2189 */
2190 define( 'TS_EXIF', 5 );
2191
2192 /**
2193 * Oracle format time.
2194 */
2195 define( 'TS_ORACLE', 6 );
2196
2197 /**
2198 * Postgres format time.
2199 */
2200 define( 'TS_POSTGRES', 7 );
2201
2202 /**
2203 * DB2 format time
2204 */
2205 define( 'TS_DB2', 8 );
2206
2207 /**
2208 * ISO 8601 basic format with no timezone: 19860209T200000Z. This is used by ResourceLoader
2209 */
2210 define( 'TS_ISO_8601_BASIC', 9 );
2211
2212 /**
2213 * Get a timestamp string in one of various formats
2214 *
2215 * @param $outputtype Mixed: A timestamp in one of the supported formats, the
2216 * function will autodetect which format is supplied and act
2217 * accordingly.
2218 * @param $ts Mixed: the timestamp to convert or 0 for the current timestamp
2219 * @return Mixed: String / false The same date in the format specified in $outputtype or false
2220 */
2221 function wfTimestamp( $outputtype = TS_UNIX, $ts = 0 ) {
2222 $uts = 0;
2223 $da = array();
2224 $strtime = '';
2225
2226 if ( !$ts ) { // We want to catch 0, '', null... but not date strings starting with a letter.
2227 $uts = time();
2228 $strtime = "@$uts";
2229 } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) {
2230 # TS_DB
2231 } elseif ( preg_match( '/^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) {
2232 # TS_EXIF
2233 } elseif ( preg_match( '/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/D', $ts, $da ) ) {
2234 # TS_MW
2235 } elseif ( preg_match( '/^-?\d{1,13}$/D', $ts ) ) {
2236 # TS_UNIX
2237 $uts = $ts;
2238 $strtime = "@$ts"; // http://php.net/manual/en/datetime.formats.compound.php
2239 } elseif ( preg_match( '/^\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}.\d{6}$/', $ts ) ) {
2240 # TS_ORACLE // session altered to DD-MM-YYYY HH24:MI:SS.FF6
2241 $strtime = preg_replace( '/(\d\d)\.(\d\d)\.(\d\d)(\.(\d+))?/', "$1:$2:$3",
2242 str_replace( '+00:00', 'UTC', $ts ) );
2243 } elseif ( preg_match( '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.*\d*)?Z$/', $ts, $da ) ) {
2244 # TS_ISO_8601
2245 } elseif ( preg_match( '/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(?:\.*\d*)?Z$/', $ts, $da ) ) {
2246 #TS_ISO_8601_BASIC
2247 } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d*[\+\- ](\d\d)$/', $ts, $da ) ) {
2248 # TS_POSTGRES
2249 } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d* GMT$/', $ts, $da ) ) {
2250 # TS_POSTGRES
2251 } elseif (preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.\d\d\d$/', $ts, $da ) ) {
2252 # TS_DB2
2253 } elseif ( preg_match( '/^[ \t\r\n]*([A-Z][a-z]{2},[ \t\r\n]*)?' . # Day of week
2254 '\d\d?[ \t\r\n]*[A-Z][a-z]{2}[ \t\r\n]*\d{2}(?:\d{2})?' . # dd Mon yyyy
2255 '[ \t\r\n]*\d\d[ \t\r\n]*:[ \t\r\n]*\d\d[ \t\r\n]*:[ \t\r\n]*\d\d/S', $ts ) ) { # hh:mm:ss
2256 # TS_RFC2822, accepting a trailing comment. See http://www.squid-cache.org/mail-archive/squid-users/200307/0122.html / r77171
2257 # The regex is a superset of rfc2822 for readability
2258 $strtime = strtok( $ts, ';' );
2259 } elseif ( preg_match( '/^[A-Z][a-z]{5,8}, \d\d-[A-Z][a-z]{2}-\d{2} \d\d:\d\d:\d\d/', $ts ) ) {
2260 # TS_RFC850
2261 $strtime = $ts;
2262 } elseif ( preg_match( '/^[A-Z][a-z]{2} [A-Z][a-z]{2} +\d{1,2} \d\d:\d\d:\d\d \d{4}/', $ts ) ) {
2263 # asctime
2264 $strtime = $ts;
2265 } else {
2266 # Bogus value...
2267 wfDebug("wfTimestamp() fed bogus time value: TYPE=$outputtype; VALUE=$ts\n");
2268
2269 return false;
2270 }
2271
2272 static $formats = array(
2273 TS_UNIX => 'U',
2274 TS_MW => 'YmdHis',
2275 TS_DB => 'Y-m-d H:i:s',
2276 TS_ISO_8601 => 'Y-m-d\TH:i:s\Z',
2277 TS_ISO_8601_BASIC => 'Ymd\THis\Z',
2278 TS_EXIF => 'Y:m:d H:i:s', // This shouldn't ever be used, but is included for completeness
2279 TS_RFC2822 => 'D, d M Y H:i:s',
2280 TS_ORACLE => 'd-m-Y H:i:s.000000', // Was 'd-M-y h.i.s A' . ' +00:00' before r51500
2281 TS_POSTGRES => 'Y-m-d H:i:s',
2282 TS_DB2 => 'Y-m-d H:i:s',
2283 );
2284
2285 if ( !isset( $formats[$outputtype] ) ) {
2286 throw new MWException( 'wfTimestamp() called with illegal output type.' );
2287 }
2288
2289 if ( function_exists( "date_create" ) ) {
2290 if ( count( $da ) ) {
2291 $ds = sprintf("%04d-%02d-%02dT%02d:%02d:%02d.00+00:00",
2292 (int)$da[1], (int)$da[2], (int)$da[3],
2293 (int)$da[4], (int)$da[5], (int)$da[6]);
2294
2295 $d = date_create( $ds, new DateTimeZone( 'GMT' ) );
2296 } elseif ( $strtime ) {
2297 $d = date_create( $strtime, new DateTimeZone( 'GMT' ) );
2298 } else {
2299 return false;
2300 }
2301
2302 if ( !$d ) {
2303 wfDebug("wfTimestamp() fed bogus time value: $outputtype; $ts\n");
2304 return false;
2305 }
2306
2307 $output = $d->format( $formats[$outputtype] );
2308 } else {
2309 if ( count( $da ) ) {
2310 // Warning! gmmktime() acts oddly if the month or day is set to 0
2311 // We may want to handle that explicitly at some point
2312 $uts = gmmktime( (int)$da[4], (int)$da[5], (int)$da[6],
2313 (int)$da[2], (int)$da[3], (int)$da[1] );
2314 } elseif ( $strtime ) {
2315 $uts = strtotime( $strtime );
2316 }
2317
2318 if ( $uts === false ) {
2319 wfDebug("wfTimestamp() can't parse the timestamp (non 32-bit time? Update php): $outputtype; $ts\n");
2320 return false;
2321 }
2322
2323 if ( TS_UNIX == $outputtype ) {
2324 return $uts;
2325 }
2326 $output = gmdate( $formats[$outputtype], $uts );
2327 }
2328
2329 if ( ( $outputtype == TS_RFC2822 ) || ( $outputtype == TS_POSTGRES ) ) {
2330 $output .= ' GMT';
2331 }
2332
2333 return $output;
2334 }
2335
2336 /**
2337 * Return a formatted timestamp, or null if input is null.
2338 * For dealing with nullable timestamp columns in the database.
2339 *
2340 * @param $outputtype Integer
2341 * @param $ts String
2342 * @return String
2343 */
2344 function wfTimestampOrNull( $outputtype = TS_UNIX, $ts = null ) {
2345 if( is_null( $ts ) ) {
2346 return null;
2347 } else {
2348 return wfTimestamp( $outputtype, $ts );
2349 }
2350 }
2351
2352 /**
2353 * Convenience function; returns MediaWiki timestamp for the present time.
2354 *
2355 * @return string
2356 */
2357 function wfTimestampNow() {
2358 # return NOW
2359 return wfTimestamp( TS_MW, time() );
2360 }
2361
2362 /**
2363 * Check if the operating system is Windows
2364 *
2365 * @return Bool: true if it's Windows, False otherwise.
2366 */
2367 function wfIsWindows() {
2368 static $isWindows = null;
2369 if ( $isWindows === null ) {
2370 $isWindows = substr( php_uname(), 0, 7 ) == 'Windows';
2371 }
2372 return $isWindows;
2373 }
2374
2375 /**
2376 * Check if we are running under HipHop
2377 *
2378 * @return Bool
2379 */
2380 function wfIsHipHop() {
2381 return function_exists( 'hphp_thread_set_warmup_enabled' );
2382 }
2383
2384 /**
2385 * Swap two variables
2386 *
2387 * @param $x Mixed
2388 * @param $y Mixed
2389 */
2390 function swap( &$x, &$y ) {
2391 $z = $x;
2392 $x = $y;
2393 $y = $z;
2394 }
2395
2396 /**
2397 * Tries to get the system directory for temporary files. The TMPDIR, TMP, and
2398 * TEMP environment variables are then checked in sequence, and if none are set
2399 * try sys_get_temp_dir() for PHP >= 5.2.1. All else fails, return /tmp for Unix
2400 * or C:\Windows\Temp for Windows and hope for the best.
2401 * It is common to call it with tempnam().
2402 *
2403 * NOTE: When possible, use instead the tmpfile() function to create
2404 * temporary files to avoid race conditions on file creation, etc.
2405 *
2406 * @return String
2407 */
2408 function wfTempDir() {
2409 foreach( array( 'TMPDIR', 'TMP', 'TEMP' ) as $var ) {
2410 $tmp = getenv( $var );
2411 if( $tmp && file_exists( $tmp ) && is_dir( $tmp ) && is_writable( $tmp ) ) {
2412 return $tmp;
2413 }
2414 }
2415 if( function_exists( 'sys_get_temp_dir' ) ) {
2416 return sys_get_temp_dir();
2417 }
2418 # Usual defaults
2419 return wfIsWindows() ? 'C:\Windows\Temp' : '/tmp';
2420 }
2421
2422 /**
2423 * Make directory, and make all parent directories if they don't exist
2424 *
2425 * @param $dir String: full path to directory to create
2426 * @param $mode Integer: chmod value to use, default is $wgDirectoryMode
2427 * @param $caller String: optional caller param for debugging.
2428 * @return bool
2429 */
2430 function wfMkdirParents( $dir, $mode = null, $caller = null ) {
2431 global $wgDirectoryMode;
2432
2433 if ( FileBackend::isStoragePath( $dir ) ) { // sanity
2434 throw new MWException( __FUNCTION__ . " given storage path `$dir`.");
2435 }
2436
2437 if ( !is_null( $caller ) ) {
2438 wfDebug( "$caller: called wfMkdirParents($dir)\n" );
2439 }
2440
2441 if( strval( $dir ) === '' || file_exists( $dir ) ) {
2442 return true;
2443 }
2444
2445 $dir = str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, $dir );
2446
2447 if ( is_null( $mode ) ) {
2448 $mode = $wgDirectoryMode;
2449 }
2450
2451 // Turn off the normal warning, we're doing our own below
2452 wfSuppressWarnings();
2453 $ok = mkdir( $dir, $mode, true ); // PHP5 <3
2454 wfRestoreWarnings();
2455
2456 if( !$ok ) {
2457 // PHP doesn't report the path in its warning message, so add our own to aid in diagnosis.
2458 trigger_error( __FUNCTION__ . ": failed to mkdir \"$dir\" mode $mode", E_USER_WARNING );
2459 }
2460 return $ok;
2461 }
2462
2463 /**
2464 * Increment a statistics counter
2465 *
2466 * @param $key String
2467 * @param $count Int
2468 */
2469 function wfIncrStats( $key, $count = 1 ) {
2470 global $wgStatsMethod;
2471
2472 $count = intval( $count );
2473
2474 if( $wgStatsMethod == 'udp' ) {
2475 global $wgUDPProfilerHost, $wgUDPProfilerPort, $wgDBname, $wgAggregateStatsID;
2476 static $socket;
2477
2478 $id = $wgAggregateStatsID !== false ? $wgAggregateStatsID : $wgDBname;
2479
2480 if ( !$socket ) {
2481 $socket = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
2482 $statline = "stats/{$id} - {$count} 1 1 1 1 -total\n";
2483 socket_sendto(
2484 $socket,
2485 $statline,
2486 strlen( $statline ),
2487 0,
2488 $wgUDPProfilerHost,
2489 $wgUDPProfilerPort
2490 );
2491 }
2492 $statline = "stats/{$id} - {$count} 1 1 1 1 {$key}\n";
2493 wfSuppressWarnings();
2494 socket_sendto(
2495 $socket,
2496 $statline,
2497 strlen( $statline ),
2498 0,
2499 $wgUDPProfilerHost,
2500 $wgUDPProfilerPort
2501 );
2502 wfRestoreWarnings();
2503 } elseif( $wgStatsMethod == 'cache' ) {
2504 global $wgMemc;
2505 $key = wfMemcKey( 'stats', $key );
2506 if ( is_null( $wgMemc->incr( $key, $count ) ) ) {
2507 $wgMemc->add( $key, $count );
2508 }
2509 } else {
2510 // Disabled
2511 }
2512 }
2513
2514 /**
2515 * @param $nr Mixed: the number to format
2516 * @param $acc Integer: the number of digits after the decimal point, default 2
2517 * @param $round Boolean: whether or not to round the value, default true
2518 * @return float
2519 */
2520 function wfPercent( $nr, $acc = 2, $round = true ) {
2521 $ret = sprintf( "%.${acc}f", $nr );
2522 return $round ? round( $ret, $acc ) . '%' : "$ret%";
2523 }
2524
2525 /**
2526 * Find out whether or not a mixed variable exists in a string
2527 *
2528 * @param $needle String
2529 * @param $str String
2530 * @param $insensitive Boolean
2531 * @return Boolean
2532 */
2533 function in_string( $needle, $str, $insensitive = false ) {
2534 $func = 'strpos';
2535 if( $insensitive ) $func = 'stripos';
2536
2537 return $func( $str, $needle ) !== false;
2538 }
2539
2540 /**
2541 * Safety wrapper around ini_get() for boolean settings.
2542 * The values returned from ini_get() are pre-normalized for settings
2543 * set via php.ini or php_flag/php_admin_flag... but *not*
2544 * for those set via php_value/php_admin_value.
2545 *
2546 * It's fairly common for people to use php_value instead of php_flag,
2547 * which can leave you with an 'off' setting giving a false positive
2548 * for code that just takes the ini_get() return value as a boolean.
2549 *
2550 * To make things extra interesting, setting via php_value accepts
2551 * "true" and "yes" as true, but php.ini and php_flag consider them false. :)
2552 * Unrecognized values go false... again opposite PHP's own coercion
2553 * from string to bool.
2554 *
2555 * Luckily, 'properly' set settings will always come back as '0' or '1',
2556 * so we only have to worry about them and the 'improper' settings.
2557 *
2558 * I frickin' hate PHP... :P
2559 *
2560 * @param $setting String
2561 * @return Bool
2562 */
2563 function wfIniGetBool( $setting ) {
2564 $val = ini_get( $setting );
2565 // 'on' and 'true' can't have whitespace around them, but '1' can.
2566 return strtolower( $val ) == 'on'
2567 || strtolower( $val ) == 'true'
2568 || strtolower( $val ) == 'yes'
2569 || preg_match( "/^\s*[+-]?0*[1-9]/", $val ); // approx C atoi() function
2570 }
2571
2572 /**
2573 * Wrapper function for PHP's dl(). This doesn't work in most situations from
2574 * PHP 5.3 onward, and is usually disabled in shared environments anyway.
2575 *
2576 * @param $extension String A PHP extension. The file suffix (.so or .dll)
2577 * should be omitted
2578 * @param $fileName String Name of the library, if not $extension.suffix
2579 * @return Bool - Whether or not the extension is loaded
2580 */
2581 function wfDl( $extension, $fileName = null ) {
2582 if( extension_loaded( $extension ) ) {
2583 return true;
2584 }
2585
2586 $canDl = false;
2587 $sapi = php_sapi_name();
2588 if( version_compare( PHP_VERSION, '5.3.0', '<' ) ||
2589 $sapi == 'cli' || $sapi == 'cgi' || $sapi == 'embed' )
2590 {
2591 $canDl = ( function_exists( 'dl' ) && is_callable( 'dl' )
2592 && wfIniGetBool( 'enable_dl' ) && !wfIniGetBool( 'safe_mode' ) );
2593 }
2594
2595 if( $canDl ) {
2596 $fileName = $fileName ? $fileName : $extension;
2597 if( wfIsWindows() ) {
2598 $fileName = 'php_' . $fileName;
2599 }
2600 wfSuppressWarnings();
2601 dl( $fileName . '.' . PHP_SHLIB_SUFFIX );
2602 wfRestoreWarnings();
2603 }
2604 return extension_loaded( $extension );
2605 }
2606
2607 /**
2608 * Windows-compatible version of escapeshellarg()
2609 * Windows doesn't recognise single-quotes in the shell, but the escapeshellarg()
2610 * function puts single quotes in regardless of OS.
2611 *
2612 * Also fixes the locale problems on Linux in PHP 5.2.6+ (bug backported to
2613 * earlier distro releases of PHP)
2614 *
2615 * @param varargs
2616 * @return String
2617 */
2618 function wfEscapeShellArg( ) {
2619 wfInitShellLocale();
2620
2621 $args = func_get_args();
2622 $first = true;
2623 $retVal = '';
2624 foreach ( $args as $arg ) {
2625 if ( !$first ) {
2626 $retVal .= ' ';
2627 } else {
2628 $first = false;
2629 }
2630
2631 if ( wfIsWindows() ) {
2632 // Escaping for an MSVC-style command line parser and CMD.EXE
2633 // Refs:
2634 // * http://web.archive.org/web/20020708081031/http://mailman.lyra.org/pipermail/scite-interest/2002-March/000436.html
2635 // * http://technet.microsoft.com/en-us/library/cc723564.aspx
2636 // * Bug #13518
2637 // * CR r63214
2638 // Double the backslashes before any double quotes. Escape the double quotes.
2639 $tokens = preg_split( '/(\\\\*")/', $arg, -1, PREG_SPLIT_DELIM_CAPTURE );
2640 $arg = '';
2641 $iteration = 0;
2642 foreach ( $tokens as $token ) {
2643 if ( $iteration % 2 == 1 ) {
2644 // Delimiter, a double quote preceded by zero or more slashes
2645 $arg .= str_replace( '\\', '\\\\', substr( $token, 0, -1 ) ) . '\\"';
2646 } elseif ( $iteration % 4 == 2 ) {
2647 // ^ in $token will be outside quotes, need to be escaped
2648 $arg .= str_replace( '^', '^^', $token );
2649 } else { // $iteration % 4 == 0
2650 // ^ in $token will appear inside double quotes, so leave as is
2651 $arg .= $token;
2652 }
2653 $iteration++;
2654 }
2655 // Double the backslashes before the end of the string, because
2656 // we will soon add a quote
2657 $m = array();
2658 if ( preg_match( '/^(.*?)(\\\\+)$/', $arg, $m ) ) {
2659 $arg = $m[1] . str_replace( '\\', '\\\\', $m[2] );
2660 }
2661
2662 // Add surrounding quotes
2663 $retVal .= '"' . $arg . '"';
2664 } else {
2665 $retVal .= escapeshellarg( $arg );
2666 }
2667 }
2668 return $retVal;
2669 }
2670
2671 /**
2672 * Execute a shell command, with time and memory limits mirrored from the PHP
2673 * configuration if supported.
2674 * @param $cmd String Command line, properly escaped for shell.
2675 * @param &$retval optional, will receive the program's exit code.
2676 * (non-zero is usually failure)
2677 * @param $environ Array optional environment variables which should be
2678 * added to the executed command environment.
2679 * @return collected stdout as a string (trailing newlines stripped)
2680 */
2681 function wfShellExec( $cmd, &$retval = null, $environ = array() ) {
2682 global $IP, $wgMaxShellMemory, $wgMaxShellFileSize, $wgMaxShellTime;
2683
2684 static $disabled;
2685 if ( is_null( $disabled ) ) {
2686 $disabled = false;
2687 if( wfIniGetBool( 'safe_mode' ) ) {
2688 wfDebug( "wfShellExec can't run in safe_mode, PHP's exec functions are too broken.\n" );
2689 $disabled = 'safemode';
2690 } else {
2691 $functions = explode( ',', ini_get( 'disable_functions' ) );
2692 $functions = array_map( 'trim', $functions );
2693 $functions = array_map( 'strtolower', $functions );
2694 if ( in_array( 'passthru', $functions ) ) {
2695 wfDebug( "passthru is in disabled_functions\n" );
2696 $disabled = 'passthru';
2697 }
2698 }
2699 }
2700 if ( $disabled ) {
2701 $retval = 1;
2702 return $disabled == 'safemode' ?
2703 'Unable to run external programs in safe mode.' :
2704 'Unable to run external programs, passthru() is disabled.';
2705 }
2706
2707 wfInitShellLocale();
2708
2709 $envcmd = '';
2710 foreach( $environ as $k => $v ) {
2711 if ( wfIsWindows() ) {
2712 /* Surrounding a set in quotes (method used by wfEscapeShellArg) makes the quotes themselves
2713 * appear in the environment variable, so we must use carat escaping as documented in
2714 * http://technet.microsoft.com/en-us/library/cc723564.aspx
2715 * Note however that the quote isn't listed there, but is needed, and the parentheses
2716 * are listed there but doesn't appear to need it.
2717 */
2718 $envcmd .= "set $k=" . preg_replace( '/([&|()<>^"])/', '^\\1', $v ) . '&& ';
2719 } else {
2720 /* Assume this is a POSIX shell, thus required to accept variable assignments before the command
2721 * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_09_01
2722 */
2723 $envcmd .= "$k=" . escapeshellarg( $v ) . ' ';
2724 }
2725 }
2726 $cmd = $envcmd . $cmd;
2727
2728 if ( wfIsWindows() ) {
2729 if ( version_compare( PHP_VERSION, '5.3.0', '<' ) && /* Fixed in 5.3.0 :) */
2730 ( version_compare( PHP_VERSION, '5.2.1', '>=' ) || php_uname( 's' ) == 'Windows NT' ) )
2731 {
2732 # Hack to work around PHP's flawed invocation of cmd.exe
2733 # http://news.php.net/php.internals/21796
2734 # Windows 9x doesn't accept any kind of quotes
2735 $cmd = '"' . $cmd . '"';
2736 }
2737 } elseif ( php_uname( 's' ) == 'Linux' ) {
2738 $time = intval( $wgMaxShellTime );
2739 $mem = intval( $wgMaxShellMemory );
2740 $filesize = intval( $wgMaxShellFileSize );
2741
2742 if ( $time > 0 && $mem > 0 ) {
2743 $script = "$IP/bin/ulimit4.sh";
2744 if ( is_executable( $script ) ) {
2745 $cmd = '/bin/bash ' . escapeshellarg( $script ) . " $time $mem $filesize " . escapeshellarg( $cmd );
2746 }
2747 }
2748 }
2749 wfDebug( "wfShellExec: $cmd\n" );
2750
2751 $retval = 1; // error by default?
2752 ob_start();
2753 passthru( $cmd, $retval );
2754 $output = ob_get_contents();
2755 ob_end_clean();
2756
2757 if ( $retval == 127 ) {
2758 wfDebugLog( 'exec', "Possibly missing executable file: $cmd\n" );
2759 }
2760 return $output;
2761 }
2762
2763 /**
2764 * Workaround for http://bugs.php.net/bug.php?id=45132
2765 * escapeshellarg() destroys non-ASCII characters if LANG is not a UTF-8 locale
2766 */
2767 function wfInitShellLocale() {
2768 static $done = false;
2769 if ( $done ) {
2770 return;
2771 }
2772 $done = true;
2773 global $wgShellLocale;
2774 if ( !wfIniGetBool( 'safe_mode' ) ) {
2775 putenv( "LC_CTYPE=$wgShellLocale" );
2776 setlocale( LC_CTYPE, $wgShellLocale );
2777 }
2778 }
2779
2780 /**
2781 * Generate a shell-escaped command line string to run a maintenance script.
2782 * Note that $parameters should be a flat array and an option with an argument
2783 * should consist of two consecutive items in the array (do not use "--option value").
2784 * @param $script string MediaWiki maintenance script path
2785 * @param $parameters Array Arguments and options to the script
2786 * @param $options Array Associative array of options:
2787 * 'php': The path to the php executable
2788 * 'wrapper': Path to a PHP wrapper to handle the maintenance script
2789 * @return Array
2790 */
2791 function wfShellMaintenanceCmd( $script, array $parameters = array(), array $options = array() ) {
2792 global $wgPhpCli;
2793 // Give site config file a chance to run the script in a wrapper.
2794 // The caller may likely want to call wfBasename() on $script.
2795 wfRunHooks( 'wfShellMaintenanceCmd', array( &$script, &$parameters, &$options ) );
2796 $cmd = isset( $options['php'] ) ? array( $options['php'] ) : array( $wgPhpCli );
2797 if ( isset( $options['wrapper'] ) ) {
2798 $cmd[] = $options['wrapper'];
2799 }
2800 $cmd[] = $script;
2801 // Escape each parameter for shell
2802 return implode( " ", array_map( 'wfEscapeShellArg', array_merge( $cmd, $parameters ) ) );
2803 }
2804
2805 /**
2806 * wfMerge attempts to merge differences between three texts.
2807 * Returns true for a clean merge and false for failure or a conflict.
2808 *
2809 * @param $old String
2810 * @param $mine String
2811 * @param $yours String
2812 * @param $result String
2813 * @return Bool
2814 */
2815 function wfMerge( $old, $mine, $yours, &$result ) {
2816 global $wgDiff3;
2817
2818 # This check may also protect against code injection in
2819 # case of broken installations.
2820 wfSuppressWarnings();
2821 $haveDiff3 = $wgDiff3 && file_exists( $wgDiff3 );
2822 wfRestoreWarnings();
2823
2824 if( !$haveDiff3 ) {
2825 wfDebug( "diff3 not found\n" );
2826 return false;
2827 }
2828
2829 # Make temporary files
2830 $td = wfTempDir();
2831 $oldtextFile = fopen( $oldtextName = tempnam( $td, 'merge-old-' ), 'w' );
2832 $mytextFile = fopen( $mytextName = tempnam( $td, 'merge-mine-' ), 'w' );
2833 $yourtextFile = fopen( $yourtextName = tempnam( $td, 'merge-your-' ), 'w' );
2834
2835 fwrite( $oldtextFile, $old );
2836 fclose( $oldtextFile );
2837 fwrite( $mytextFile, $mine );
2838 fclose( $mytextFile );
2839 fwrite( $yourtextFile, $yours );
2840 fclose( $yourtextFile );
2841
2842 # Check for a conflict
2843 $cmd = $wgDiff3 . ' -a --overlap-only ' .
2844 wfEscapeShellArg( $mytextName ) . ' ' .
2845 wfEscapeShellArg( $oldtextName ) . ' ' .
2846 wfEscapeShellArg( $yourtextName );
2847 $handle = popen( $cmd, 'r' );
2848
2849 if( fgets( $handle, 1024 ) ) {
2850 $conflict = true;
2851 } else {
2852 $conflict = false;
2853 }
2854 pclose( $handle );
2855
2856 # Merge differences
2857 $cmd = $wgDiff3 . ' -a -e --merge ' .
2858 wfEscapeShellArg( $mytextName, $oldtextName, $yourtextName );
2859 $handle = popen( $cmd, 'r' );
2860 $result = '';
2861 do {
2862 $data = fread( $handle, 8192 );
2863 if ( strlen( $data ) == 0 ) {
2864 break;
2865 }
2866 $result .= $data;
2867 } while ( true );
2868 pclose( $handle );
2869 unlink( $mytextName );
2870 unlink( $oldtextName );
2871 unlink( $yourtextName );
2872
2873 if ( $result === '' && $old !== '' && !$conflict ) {
2874 wfDebug( "Unexpected null result from diff3. Command: $cmd\n" );
2875 $conflict = true;
2876 }
2877 return !$conflict;
2878 }
2879
2880 /**
2881 * Returns unified plain-text diff of two texts.
2882 * Useful for machine processing of diffs.
2883 *
2884 * @param $before String: the text before the changes.
2885 * @param $after String: the text after the changes.
2886 * @param $params String: command-line options for the diff command.
2887 * @return String: unified diff of $before and $after
2888 */
2889 function wfDiff( $before, $after, $params = '-u' ) {
2890 if ( $before == $after ) {
2891 return '';
2892 }
2893
2894 global $wgDiff;
2895 wfSuppressWarnings();
2896 $haveDiff = $wgDiff && file_exists( $wgDiff );
2897 wfRestoreWarnings();
2898
2899 # This check may also protect against code injection in
2900 # case of broken installations.
2901 if( !$haveDiff ) {
2902 wfDebug( "diff executable not found\n" );
2903 $diffs = new Diff( explode( "\n", $before ), explode( "\n", $after ) );
2904 $format = new UnifiedDiffFormatter();
2905 return $format->format( $diffs );
2906 }
2907
2908 # Make temporary files
2909 $td = wfTempDir();
2910 $oldtextFile = fopen( $oldtextName = tempnam( $td, 'merge-old-' ), 'w' );
2911 $newtextFile = fopen( $newtextName = tempnam( $td, 'merge-your-' ), 'w' );
2912
2913 fwrite( $oldtextFile, $before );
2914 fclose( $oldtextFile );
2915 fwrite( $newtextFile, $after );
2916 fclose( $newtextFile );
2917
2918 // Get the diff of the two files
2919 $cmd = "$wgDiff " . $params . ' ' . wfEscapeShellArg( $oldtextName, $newtextName );
2920
2921 $h = popen( $cmd, 'r' );
2922
2923 $diff = '';
2924
2925 do {
2926 $data = fread( $h, 8192 );
2927 if ( strlen( $data ) == 0 ) {
2928 break;
2929 }
2930 $diff .= $data;
2931 } while ( true );
2932
2933 // Clean up
2934 pclose( $h );
2935 unlink( $oldtextName );
2936 unlink( $newtextName );
2937
2938 // Kill the --- and +++ lines. They're not useful.
2939 $diff_lines = explode( "\n", $diff );
2940 if ( strpos( $diff_lines[0], '---' ) === 0 ) {
2941 unset( $diff_lines[0] );
2942 }
2943 if ( strpos( $diff_lines[1], '+++' ) === 0 ) {
2944 unset( $diff_lines[1] );
2945 }
2946
2947 $diff = implode( "\n", $diff_lines );
2948
2949 return $diff;
2950 }
2951
2952 /**
2953 * This function works like "use VERSION" in Perl, the program will die with a
2954 * backtrace if the current version of PHP is less than the version provided
2955 *
2956 * This is useful for extensions which due to their nature are not kept in sync
2957 * with releases, and might depend on other versions of PHP than the main code
2958 *
2959 * Note: PHP might die due to parsing errors in some cases before it ever
2960 * manages to call this function, such is life
2961 *
2962 * @see perldoc -f use
2963 *
2964 * @param $req_ver Mixed: the version to check, can be a string, an integer, or
2965 * a float
2966 */
2967 function wfUsePHP( $req_ver ) {
2968 $php_ver = PHP_VERSION;
2969
2970 if ( version_compare( $php_ver, (string)$req_ver, '<' ) ) {
2971 throw new MWException( "PHP $req_ver required--this is only $php_ver" );
2972 }
2973 }
2974
2975 /**
2976 * This function works like "use VERSION" in Perl except it checks the version
2977 * of MediaWiki, the program will die with a backtrace if the current version
2978 * of MediaWiki is less than the version provided.
2979 *
2980 * This is useful for extensions which due to their nature are not kept in sync
2981 * with releases
2982 *
2983 * @see perldoc -f use
2984 *
2985 * @param $req_ver Mixed: the version to check, can be a string, an integer, or
2986 * a float
2987 */
2988 function wfUseMW( $req_ver ) {
2989 global $wgVersion;
2990
2991 if ( version_compare( $wgVersion, (string)$req_ver, '<' ) ) {
2992 throw new MWException( "MediaWiki $req_ver required--this is only $wgVersion" );
2993 }
2994 }
2995
2996 /**
2997 * Return the final portion of a pathname.
2998 * Reimplemented because PHP5's basename() is buggy with multibyte text.
2999 * http://bugs.php.net/bug.php?id=33898
3000 *
3001 * PHP's basename() only considers '\' a pathchar on Windows and Netware.
3002 * We'll consider it so always, as we don't want \s in our Unix paths either.
3003 *
3004 * @param $path String
3005 * @param $suffix String: to remove if present
3006 * @return String
3007 */
3008 function wfBaseName( $path, $suffix = '' ) {
3009 $encSuffix = ( $suffix == '' )
3010 ? ''
3011 : ( '(?:' . preg_quote( $suffix, '#' ) . ')?' );
3012 $matches = array();
3013 if( preg_match( "#([^/\\\\]*?){$encSuffix}[/\\\\]*$#", $path, $matches ) ) {
3014 return $matches[1];
3015 } else {
3016 return '';
3017 }
3018 }
3019
3020 /**
3021 * Generate a relative path name to the given file.
3022 * May explode on non-matching case-insensitive paths,
3023 * funky symlinks, etc.
3024 *
3025 * @param $path String: absolute destination path including target filename
3026 * @param $from String: Absolute source path, directory only
3027 * @return String
3028 */
3029 function wfRelativePath( $path, $from ) {
3030 // Normalize mixed input on Windows...
3031 $path = str_replace( '/', DIRECTORY_SEPARATOR, $path );
3032 $from = str_replace( '/', DIRECTORY_SEPARATOR, $from );
3033
3034 // Trim trailing slashes -- fix for drive root
3035 $path = rtrim( $path, DIRECTORY_SEPARATOR );
3036 $from = rtrim( $from, DIRECTORY_SEPARATOR );
3037
3038 $pieces = explode( DIRECTORY_SEPARATOR, dirname( $path ) );
3039 $against = explode( DIRECTORY_SEPARATOR, $from );
3040
3041 if( $pieces[0] !== $against[0] ) {
3042 // Non-matching Windows drive letters?
3043 // Return a full path.
3044 return $path;
3045 }
3046
3047 // Trim off common prefix
3048 while( count( $pieces ) && count( $against )
3049 && $pieces[0] == $against[0] ) {
3050 array_shift( $pieces );
3051 array_shift( $against );
3052 }
3053
3054 // relative dots to bump us to the parent
3055 while( count( $against ) ) {
3056 array_unshift( $pieces, '..' );
3057 array_shift( $against );
3058 }
3059
3060 array_push( $pieces, wfBaseName( $path ) );
3061
3062 return implode( DIRECTORY_SEPARATOR, $pieces );
3063 }
3064
3065 /**
3066 * Do any deferred updates and clear the list
3067 *
3068 * @deprecated since 1.19
3069 * @see DeferredUpdates::doUpdate()
3070 * @param $commit string
3071 */
3072 function wfDoUpdates( $commit = '' ) {
3073 wfDeprecated( __METHOD__, '1.19' );
3074 DeferredUpdates::doUpdates( $commit );
3075 }
3076
3077 /**
3078 * Convert an arbitrarily-long digit string from one numeric base
3079 * to another, optionally zero-padding to a minimum column width.
3080 *
3081 * Supports base 2 through 36; digit values 10-36 are represented
3082 * as lowercase letters a-z. Input is case-insensitive.
3083 *
3084 * @param $input String: of digits
3085 * @param $sourceBase Integer: 2-36
3086 * @param $destBase Integer: 2-36
3087 * @param $pad Integer: 1 or greater
3088 * @param $lowercase Boolean
3089 * @return String or false on invalid input
3090 */
3091 function wfBaseConvert( $input, $sourceBase, $destBase, $pad = 1, $lowercase = true ) {
3092 $input = strval( $input );
3093 if( $sourceBase < 2 ||
3094 $sourceBase > 36 ||
3095 $destBase < 2 ||
3096 $destBase > 36 ||
3097 $pad < 1 ||
3098 $sourceBase != intval( $sourceBase ) ||
3099 $destBase != intval( $destBase ) ||
3100 $pad != intval( $pad ) ||
3101 !is_string( $input ) ||
3102 $input == '' ) {
3103 return false;
3104 }
3105 $digitChars = ( $lowercase ) ? '0123456789abcdefghijklmnopqrstuvwxyz' : '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
3106 $inDigits = array();
3107 $outChars = '';
3108
3109 // Decode and validate input string
3110 $input = strtolower( $input );
3111 for( $i = 0; $i < strlen( $input ); $i++ ) {
3112 $n = strpos( $digitChars, $input[$i] );
3113 if( $n === false || $n > $sourceBase ) {
3114 return false;
3115 }
3116 $inDigits[] = $n;
3117 }
3118
3119 // Iterate over the input, modulo-ing out an output digit
3120 // at a time until input is gone.
3121 while( count( $inDigits ) ) {
3122 $work = 0;
3123 $workDigits = array();
3124
3125 // Long division...
3126 foreach( $inDigits as $digit ) {
3127 $work *= $sourceBase;
3128 $work += $digit;
3129
3130 if( $work < $destBase ) {
3131 // Gonna need to pull another digit.
3132 if( count( $workDigits ) ) {
3133 // Avoid zero-padding; this lets us find
3134 // the end of the input very easily when
3135 // length drops to zero.
3136 $workDigits[] = 0;
3137 }
3138 } else {
3139 // Finally! Actual division!
3140 $workDigits[] = intval( $work / $destBase );
3141
3142 // Isn't it annoying that most programming languages
3143 // don't have a single divide-and-remainder operator,
3144 // even though the CPU implements it that way?
3145 $work = $work % $destBase;
3146 }
3147 }
3148
3149 // All that division leaves us with a remainder,
3150 // which is conveniently our next output digit.
3151 $outChars .= $digitChars[$work];
3152
3153 // And we continue!
3154 $inDigits = $workDigits;
3155 }
3156
3157 while( strlen( $outChars ) < $pad ) {
3158 $outChars .= '0';
3159 }
3160
3161 return strrev( $outChars );
3162 }
3163
3164 /**
3165 * Create an object with a given name and an array of construct parameters
3166 *
3167 * @param $name String
3168 * @param $p Array: parameters
3169 * @return object
3170 * @deprecated since 1.18, warnings in 1.18, removal in 1.20
3171 */
3172 function wfCreateObject( $name, $p ) {
3173 wfDeprecated( __FUNCTION__, '1.18' );
3174 return MWFunction::newObj( $name, $p );
3175 }
3176
3177 /**
3178 * @return bool
3179 */
3180 function wfHttpOnlySafe() {
3181 global $wgHttpOnlyBlacklist;
3182
3183 if( isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
3184 foreach( $wgHttpOnlyBlacklist as $regex ) {
3185 if( preg_match( $regex, $_SERVER['HTTP_USER_AGENT'] ) ) {
3186 return false;
3187 }
3188 }
3189 }
3190
3191 return true;
3192 }
3193
3194 /**
3195 * Initialise php session
3196 *
3197 * @param $sessionId Bool
3198 */
3199 function wfSetupSession( $sessionId = false ) {
3200 global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain,
3201 $wgCookieSecure, $wgCookieHttpOnly, $wgSessionHandler;
3202 if( $wgSessionsInMemcached ) {
3203 if ( !defined( 'MW_COMPILED' ) ) {
3204 global $IP;
3205 require_once( "$IP/includes/cache/MemcachedSessions.php" );
3206 }
3207 session_set_save_handler( 'memsess_open', 'memsess_close', 'memsess_read',
3208 'memsess_write', 'memsess_destroy', 'memsess_gc' );
3209
3210 // It's necessary to register a shutdown function to call session_write_close(),
3211 // because by the time the request shutdown function for the session module is
3212 // called, $wgMemc has already been destroyed. Shutdown functions registered
3213 // this way are called before object destruction.
3214 register_shutdown_function( 'memsess_write_close' );
3215 } elseif( $wgSessionHandler && $wgSessionHandler != ini_get( 'session.save_handler' ) ) {
3216 # Only set this if $wgSessionHandler isn't null and session.save_handler
3217 # hasn't already been set to the desired value (that causes errors)
3218 ini_set( 'session.save_handler', $wgSessionHandler );
3219 }
3220 $httpOnlySafe = wfHttpOnlySafe() && $wgCookieHttpOnly;
3221 wfDebugLog( 'cookie',
3222 'session_set_cookie_params: "' . implode( '", "',
3223 array(
3224 0,
3225 $wgCookiePath,
3226 $wgCookieDomain,
3227 $wgCookieSecure,
3228 $httpOnlySafe ) ) . '"' );
3229 session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain, $wgCookieSecure, $httpOnlySafe );
3230 session_cache_limiter( 'private, must-revalidate' );
3231 if ( $sessionId ) {
3232 session_id( $sessionId );
3233 }
3234 wfSuppressWarnings();
3235 session_start();
3236 wfRestoreWarnings();
3237 }
3238
3239 /**
3240 * Get an object from the precompiled serialized directory
3241 *
3242 * @param $name String
3243 * @return Mixed: the variable on success, false on failure
3244 */
3245 function wfGetPrecompiledData( $name ) {
3246 global $IP;
3247
3248 $file = "$IP/serialized/$name";
3249 if ( file_exists( $file ) ) {
3250 $blob = file_get_contents( $file );
3251 if ( $blob ) {
3252 return unserialize( $blob );
3253 }
3254 }
3255 return false;
3256 }
3257
3258 /**
3259 * Get a cache key
3260 *
3261 * @param varargs
3262 * @return String
3263 */
3264 function wfMemcKey( /*... */ ) {
3265 global $wgCachePrefix;
3266 $prefix = $wgCachePrefix === false ? wfWikiID() : $wgCachePrefix;
3267 $args = func_get_args();
3268 $key = $prefix . ':' . implode( ':', $args );
3269 $key = str_replace( ' ', '_', $key );
3270 return $key;
3271 }
3272
3273 /**
3274 * Get a cache key for a foreign DB
3275 *
3276 * @param $db String
3277 * @param $prefix String
3278 * @param varargs String
3279 * @return String
3280 */
3281 function wfForeignMemcKey( $db, $prefix /*, ... */ ) {
3282 $args = array_slice( func_get_args(), 2 );
3283 if ( $prefix ) {
3284 $key = "$db-$prefix:" . implode( ':', $args );
3285 } else {
3286 $key = $db . ':' . implode( ':', $args );
3287 }
3288 return $key;
3289 }
3290
3291 /**
3292 * Get an ASCII string identifying this wiki
3293 * This is used as a prefix in memcached keys
3294 *
3295 * @return String
3296 */
3297 function wfWikiID() {
3298 global $wgDBprefix, $wgDBname;
3299 if ( $wgDBprefix ) {
3300 return "$wgDBname-$wgDBprefix";
3301 } else {
3302 return $wgDBname;
3303 }
3304 }
3305
3306 /**
3307 * Split a wiki ID into DB name and table prefix
3308 *
3309 * @param $wiki String
3310 *
3311 * @return array
3312 */
3313 function wfSplitWikiID( $wiki ) {
3314 $bits = explode( '-', $wiki, 2 );
3315 if ( count( $bits ) < 2 ) {
3316 $bits[] = '';
3317 }
3318 return $bits;
3319 }
3320
3321 /**
3322 * Get a Database object.
3323 *
3324 * @param $db Integer: index of the connection to get. May be DB_MASTER for the
3325 * master (for write queries), DB_SLAVE for potentially lagged read
3326 * queries, or an integer >= 0 for a particular server.
3327 *
3328 * @param $groups Mixed: query groups. An array of group names that this query
3329 * belongs to. May contain a single string if the query is only
3330 * in one group.
3331 *
3332 * @param $wiki String: the wiki ID, or false for the current wiki
3333 *
3334 * Note: multiple calls to wfGetDB(DB_SLAVE) during the course of one request
3335 * will always return the same object, unless the underlying connection or load
3336 * balancer is manually destroyed.
3337 *
3338 * Note 2: use $this->getDB() in maintenance scripts that may be invoked by
3339 * updater to ensure that a proper database is being updated.
3340 *
3341 * @return DatabaseBase
3342 */
3343 function &wfGetDB( $db, $groups = array(), $wiki = false ) {
3344 return wfGetLB( $wiki )->getConnection( $db, $groups, $wiki );
3345 }
3346
3347 /**
3348 * Get a load balancer object.
3349 *
3350 * @param $wiki String: wiki ID, or false for the current wiki
3351 * @return LoadBalancer
3352 */
3353 function wfGetLB( $wiki = false ) {
3354 return wfGetLBFactory()->getMainLB( $wiki );
3355 }
3356
3357 /**
3358 * Get the load balancer factory object
3359 *
3360 * @return LBFactory
3361 */
3362 function &wfGetLBFactory() {
3363 return LBFactory::singleton();
3364 }
3365
3366 /**
3367 * Find a file.
3368 * Shortcut for RepoGroup::singleton()->findFile()
3369 *
3370 * @param $title String or Title object
3371 * @param $options Associative array of options:
3372 * time: requested time for an archived image, or false for the
3373 * current version. An image object will be returned which was
3374 * created at the specified time.
3375 *
3376 * ignoreRedirect: If true, do not follow file redirects
3377 *
3378 * private: If true, return restricted (deleted) files if the current
3379 * user is allowed to view them. Otherwise, such files will not
3380 * be found.
3381 *
3382 * bypassCache: If true, do not use the process-local cache of File objects
3383 *
3384 * @return File, or false if the file does not exist
3385 */
3386 function wfFindFile( $title, $options = array() ) {
3387 return RepoGroup::singleton()->findFile( $title, $options );
3388 }
3389
3390 /**
3391 * Get an object referring to a locally registered file.
3392 * Returns a valid placeholder object if the file does not exist.
3393 *
3394 * @param $title Title|String
3395 * @return File|null A File, or null if passed an invalid Title
3396 */
3397 function wfLocalFile( $title ) {
3398 return RepoGroup::singleton()->getLocalRepo()->newFile( $title );
3399 }
3400
3401 /**
3402 * Stream a file to the browser. Back-compat alias for StreamFile::stream()
3403 * @deprecated since 1.19
3404 */
3405 function wfStreamFile( $fname, $headers = array() ) {
3406 wfDeprecated( __FUNCTION__, '1.19' );
3407 StreamFile::stream( $fname, $headers );
3408 }
3409
3410 /**
3411 * Should low-performance queries be disabled?
3412 *
3413 * @return Boolean
3414 * @codeCoverageIgnore
3415 */
3416 function wfQueriesMustScale() {
3417 global $wgMiserMode;
3418 return $wgMiserMode
3419 || ( SiteStats::pages() > 100000
3420 && SiteStats::edits() > 1000000
3421 && SiteStats::users() > 10000 );
3422 }
3423
3424 /**
3425 * Get the path to a specified script file, respecting file
3426 * extensions; this is a wrapper around $wgScriptExtension etc.
3427 *
3428 * @param $script String: script filename, sans extension
3429 * @return String
3430 */
3431 function wfScript( $script = 'index' ) {
3432 global $wgScriptPath, $wgScriptExtension;
3433 return "{$wgScriptPath}/{$script}{$wgScriptExtension}";
3434 }
3435
3436 /**
3437 * Get the script URL.
3438 *
3439 * @return script URL
3440 */
3441 function wfGetScriptUrl() {
3442 if( isset( $_SERVER['SCRIPT_NAME'] ) ) {
3443 #
3444 # as it was called, minus the query string.
3445 #
3446 # Some sites use Apache rewrite rules to handle subdomains,
3447 # and have PHP set up in a weird way that causes PHP_SELF
3448 # to contain the rewritten URL instead of the one that the
3449 # outside world sees.
3450 #
3451 # If in this mode, use SCRIPT_URL instead, which mod_rewrite
3452 # provides containing the "before" URL.
3453 return $_SERVER['SCRIPT_NAME'];
3454 } else {
3455 return $_SERVER['URL'];
3456 }
3457 }
3458
3459 /**
3460 * Convenience function converts boolean values into "true"
3461 * or "false" (string) values
3462 *
3463 * @param $value Boolean
3464 * @return String
3465 */
3466 function wfBoolToStr( $value ) {
3467 return $value ? 'true' : 'false';
3468 }
3469
3470 /**
3471 * Load an extension messages file
3472 *
3473 * @deprecated since 1.16, warnings in 1.18, remove in 1.20
3474 * @codeCoverageIgnore
3475 */
3476 function wfLoadExtensionMessages() {
3477 wfDeprecated( __FUNCTION__, '1.16' );
3478 }
3479
3480 /**
3481 * Get a platform-independent path to the null file, e.g. /dev/null
3482 *
3483 * @return string
3484 */
3485 function wfGetNull() {
3486 return wfIsWindows()
3487 ? 'NUL'
3488 : '/dev/null';
3489 }
3490
3491 /**
3492 * Throws a warning that $function is deprecated
3493 *
3494 * @param $function String
3495 * @param $version String|false: Added in 1.19.
3496 * @param $component String|false: Added in 1.19.
3497 *
3498 * @return null
3499 */
3500 function wfDeprecated( $function, $version = false, $component = false ) {
3501 static $functionsWarned = array();
3502
3503 MWDebug::deprecated( $function, $version, $component );
3504
3505 if ( !isset( $functionsWarned[$function] ) ) {
3506 $functionsWarned[$function] = true;
3507
3508 if ( $version ) {
3509 global $wgDeprecationReleaseLimit;
3510
3511 if ( $wgDeprecationReleaseLimit && $component === false ) {
3512 # Strip -* off the end of $version so that branches can use the
3513 # format #.##-branchname to avoid issues if the branch is merged into
3514 # a version of MediaWiki later than what it was branched from
3515 $comparableVersion = preg_replace( '/-.*$/', '', $version );
3516
3517 # If the comparableVersion is larger than our release limit then
3518 # skip the warning message for the deprecation
3519 if ( version_compare( $wgDeprecationReleaseLimit, $comparableVersion, '<' ) ) {
3520 return;
3521 }
3522 }
3523
3524 $component = $component === false ? 'MediaWiki' : $component;
3525 wfWarn( "Use of $function was deprecated in $component $version.", 2 );
3526 } else {
3527 wfWarn( "Use of $function is deprecated.", 2 );
3528 }
3529 }
3530 }
3531
3532 /**
3533 * Send a warning either to the debug log or in a PHP error depending on
3534 * $wgDevelopmentWarnings
3535 *
3536 * @param $msg String: message to send
3537 * @param $callerOffset Integer: number of items to go back in the backtrace to
3538 * find the correct caller (1 = function calling wfWarn, ...)
3539 * @param $level Integer: PHP error level; only used when $wgDevelopmentWarnings
3540 * is true
3541 */
3542 function wfWarn( $msg, $callerOffset = 1, $level = E_USER_NOTICE ) {
3543 global $wgDevelopmentWarnings;
3544
3545 MWDebug::warning( $msg, $callerOffset + 2 );
3546
3547 $callers = wfDebugBacktrace();
3548 if ( isset( $callers[$callerOffset + 1] ) ) {
3549 $callerfunc = $callers[$callerOffset + 1];
3550 $callerfile = $callers[$callerOffset];
3551 if ( isset( $callerfile['file'] ) && isset( $callerfile['line'] ) ) {
3552 $file = $callerfile['file'] . ' at line ' . $callerfile['line'];
3553 } else {
3554 $file = '(internal function)';
3555 }
3556 $func = '';
3557 if ( isset( $callerfunc['class'] ) ) {
3558 $func .= $callerfunc['class'] . '::';
3559 }
3560 if ( isset( $callerfunc['function'] ) ) {
3561 $func .= $callerfunc['function'];
3562 }
3563 $msg .= " [Called from $func in $file]";
3564 }
3565
3566 if ( $wgDevelopmentWarnings ) {
3567 trigger_error( $msg, $level );
3568 } else {
3569 wfDebug( "$msg\n" );
3570 }
3571 }
3572
3573 /**
3574 * Modern version of wfWaitForSlaves(). Instead of looking at replication lag
3575 * and waiting for it to go down, this waits for the slaves to catch up to the
3576 * master position. Use this when updating very large numbers of rows, as
3577 * in maintenance scripts, to avoid causing too much lag. Of course, this is
3578 * a no-op if there are no slaves.
3579 *
3580 * @param $maxLag Integer (deprecated)
3581 * @param $wiki mixed Wiki identifier accepted by wfGetLB
3582 */
3583 function wfWaitForSlaves( $maxLag = false, $wiki = false ) {
3584 $lb = wfGetLB( $wiki );
3585 // bug 27975 - Don't try to wait for slaves if there are none
3586 // Prevents permission error when getting master position
3587 if ( $lb->getServerCount() > 1 ) {
3588 $dbw = $lb->getConnection( DB_MASTER );
3589 $pos = $dbw->getMasterPos();
3590 $lb->waitForAll( $pos );
3591 }
3592 }
3593
3594 /**
3595 * Used to be used for outputting text in the installer/updater
3596 * @deprecated since 1.18, warnings in 1.18, remove in 1.20
3597 */
3598 function wfOut( $s ) {
3599 wfDeprecated( __FUNCTION__, '1.18' );
3600 global $wgCommandLineMode;
3601 if ( $wgCommandLineMode ) {
3602 echo $s;
3603 } else {
3604 echo htmlspecialchars( $s );
3605 }
3606 flush();
3607 }
3608
3609 /**
3610 * Count down from $n to zero on the terminal, with a one-second pause
3611 * between showing each number. For use in command-line scripts.
3612 * @codeCoverageIgnore
3613 * @param $n int
3614 */
3615 function wfCountDown( $n ) {
3616 for ( $i = $n; $i >= 0; $i-- ) {
3617 if ( $i != $n ) {
3618 echo str_repeat( "\x08", strlen( $i + 1 ) );
3619 }
3620 echo $i;
3621 flush();
3622 if ( $i ) {
3623 sleep( 1 );
3624 }
3625 }
3626 echo "\n";
3627 }
3628
3629 /**
3630 * Generate a random 32-character hexadecimal token.
3631 * @param $salt Mixed: some sort of salt, if necessary, to add to random
3632 * characters before hashing.
3633 * @return string
3634 * @codeCoverageIgnore
3635 */
3636 function wfGenerateToken( $salt = '' ) {
3637 $salt = serialize( $salt );
3638 return md5( mt_rand( 0, 0x7fffffff ) . $salt );
3639 }
3640
3641 /**
3642 * Replace all invalid characters with -
3643 *
3644 * @param $name Mixed: filename to process
3645 * @return String
3646 */
3647 function wfStripIllegalFilenameChars( $name ) {
3648 global $wgIllegalFileChars;
3649 $name = wfBaseName( $name );
3650 $name = preg_replace(
3651 "/[^" . Title::legalChars() . "]" .
3652 ( $wgIllegalFileChars ? "|[" . $wgIllegalFileChars . "]" : '' ) .
3653 "/",
3654 '-',
3655 $name
3656 );
3657 return $name;
3658 }
3659
3660 /**
3661 * Set PHP's memory limit to the larger of php.ini or $wgMemoryLimit;
3662 *
3663 * @return Integer value memory was set to.
3664 */
3665 function wfMemoryLimit() {
3666 global $wgMemoryLimit;
3667 $memlimit = wfShorthandToInteger( ini_get( 'memory_limit' ) );
3668 if( $memlimit != -1 ) {
3669 $conflimit = wfShorthandToInteger( $wgMemoryLimit );
3670 if( $conflimit == -1 ) {
3671 wfDebug( "Removing PHP's memory limit\n" );
3672 wfSuppressWarnings();
3673 ini_set( 'memory_limit', $conflimit );
3674 wfRestoreWarnings();
3675 return $conflimit;
3676 } elseif ( $conflimit > $memlimit ) {
3677 wfDebug( "Raising PHP's memory limit to $conflimit bytes\n" );
3678 wfSuppressWarnings();
3679 ini_set( 'memory_limit', $conflimit );
3680 wfRestoreWarnings();
3681 return $conflimit;
3682 }
3683 }
3684 return $memlimit;
3685 }
3686
3687 /**
3688 * Converts shorthand byte notation to integer form
3689 *
3690 * @param $string String
3691 * @return Integer
3692 */
3693 function wfShorthandToInteger( $string = '' ) {
3694 $string = trim( $string );
3695 if( $string === '' ) {
3696 return -1;
3697 }
3698 $last = $string[strlen( $string ) - 1];
3699 $val = intval( $string );
3700 switch( $last ) {
3701 case 'g':
3702 case 'G':
3703 $val *= 1024;
3704 // break intentionally missing
3705 case 'm':
3706 case 'M':
3707 $val *= 1024;
3708 // break intentionally missing
3709 case 'k':
3710 case 'K':
3711 $val *= 1024;
3712 }
3713
3714 return $val;
3715 }
3716
3717 /**
3718 * Get the normalised IETF language tag
3719 * See unit test for examples.
3720 *
3721 * @param $code String: The language code.
3722 * @return String: The language code which complying with BCP 47 standards.
3723 */
3724 function wfBCP47( $code ) {
3725 $codeSegment = explode( '-', $code );
3726 $codeBCP = array();
3727 foreach ( $codeSegment as $segNo => $seg ) {
3728 if ( count( $codeSegment ) > 0 ) {
3729 // when previous segment is x, it is a private segment and should be lc
3730 if( $segNo > 0 && strtolower( $codeSegment[( $segNo - 1 )] ) == 'x' ) {
3731 $codeBCP[$segNo] = strtolower( $seg );
3732 // ISO 3166 country code
3733 } elseif ( ( strlen( $seg ) == 2 ) && ( $segNo > 0 ) ) {
3734 $codeBCP[$segNo] = strtoupper( $seg );
3735 // ISO 15924 script code
3736 } elseif ( ( strlen( $seg ) == 4 ) && ( $segNo > 0 ) ) {
3737 $codeBCP[$segNo] = ucfirst( strtolower( $seg ) );
3738 // Use lowercase for other cases
3739 } else {
3740 $codeBCP[$segNo] = strtolower( $seg );
3741 }
3742 } else {
3743 // Use lowercase for single segment
3744 $codeBCP[$segNo] = strtolower( $seg );
3745 }
3746 }
3747 $langCode = implode( '-', $codeBCP );
3748 return $langCode;
3749 }
3750
3751 /**
3752 * Get a cache object.
3753 *
3754 * @param $inputType integer Cache type, one the the CACHE_* constants.
3755 * @return BagOStuff
3756 */
3757 function wfGetCache( $inputType ) {
3758 return ObjectCache::getInstance( $inputType );
3759 }
3760
3761 /**
3762 * Get the main cache object
3763 *
3764 * @return BagOStuff
3765 */
3766 function wfGetMainCache() {
3767 global $wgMainCacheType;
3768 return ObjectCache::getInstance( $wgMainCacheType );
3769 }
3770
3771 /**
3772 * Get the cache object used by the message cache
3773 *
3774 * @return BagOStuff
3775 */
3776 function wfGetMessageCacheStorage() {
3777 global $wgMessageCacheType;
3778 return ObjectCache::getInstance( $wgMessageCacheType );
3779 }
3780
3781 /**
3782 * Get the cache object used by the parser cache
3783 *
3784 * @return BagOStuff
3785 */
3786 function wfGetParserCacheStorage() {
3787 global $wgParserCacheType;
3788 return ObjectCache::getInstance( $wgParserCacheType );
3789 }
3790
3791 /**
3792 * Call hook functions defined in $wgHooks
3793 *
3794 * @param $event String: event name
3795 * @param $args Array: parameters passed to hook functions
3796 * @return Boolean True if no handler aborted the hook
3797 */
3798 function wfRunHooks( $event, $args = array() ) {
3799 return Hooks::run( $event, $args );
3800 }
3801
3802 /**
3803 * Wrapper around php's unpack.
3804 *
3805 * @param $format String: The format string (See php's docs)
3806 * @param $data: A binary string of binary data
3807 * @param $length integer or false: The minimun length of $data. This is to
3808 * prevent reading beyond the end of $data. false to disable the check.
3809 *
3810 * Also be careful when using this function to read unsigned 32 bit integer
3811 * because php might make it negative.
3812 *
3813 * @throws MWException if $data not long enough, or if unpack fails
3814 * @return Associative array of the extracted data
3815 */
3816 function wfUnpack( $format, $data, $length=false ) {
3817 if ( $length !== false ) {
3818 $realLen = strlen( $data );
3819 if ( $realLen < $length ) {
3820 throw new MWException( "Tried to use wfUnpack on a "
3821 . "string of length $realLen, but needed one "
3822 . "of at least length $length."
3823 );
3824 }
3825 }
3826
3827 wfSuppressWarnings();
3828 $result = unpack( $format, $data );
3829 wfRestoreWarnings();
3830
3831 if ( $result === false ) {
3832 // If it cannot extract the packed data.
3833 throw new MWException( "unpack could not unpack binary data" );
3834 }
3835 return $result;
3836 }