MediaWiki REL1_34
HttpAcceptParser.php
Go to the documentation of this file.
1<?php
2
11namespace Wikimedia\Http;
12
14
35 public function parseWeights( $rawHeader ) {
36 //FIXME: The code below was copied and adapted from WebRequest::getAcceptLang.
37 // Move this utility class into core for reuse!
38
39 // first, strip header name
40 $rawHeader = preg_replace( '/^[-\w]+:\s*/', '', $rawHeader );
41
42 // Return values in lower case
43 $rawHeader = strtolower( $rawHeader );
44
45 // Break up string into pieces (values and q factors)
46 $value_parse = null;
47 preg_match_all( '@([a-z\d*]+([-+/.][a-z\d*]+)*)\s*(;\s*q\s*=\s*(1(\.0{0,3})?|0(\.\d{0,3})?)?)?@',
48 $rawHeader, $value_parse );
49
50 if ( !count( $value_parse[1] ) ) {
51 return [];
52 }
53
54 $values = $value_parse[1];
55 $qvalues = $value_parse[4];
56 $indices = range( 0, count( $value_parse[1] ) - 1 );
57
58 // Set default q factor to 1
59 foreach ( $indices as $index ) {
60 if ( $qvalues[$index] === '' ) {
61 $qvalues[$index] = 1;
62 } elseif ( $qvalues[$index] == 0 ) {
63 unset( $values[$index], $qvalues[$index], $indices[$index] );
64 } else {
65 $qvalues[$index] = (float)$qvalues[$index];
66 }
67 }
68
69 // Sort list. First by $qvalues, then by order. Reorder $values the same way
70 array_multisort( $qvalues, SORT_DESC, SORT_NUMERIC, $indices, $values );
71
72 // Create a list like "en" => 0.8
73 $weights = array_combine( $values, $qvalues );
74
75 return $weights;
76 }
77
78}
parseWeights( $rawHeader)
Parses an HTTP header into a weight map, that is an associative array mapping values to their respect...
Utility for parsing a HTTP Accept header value into a weight map.