]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-infos.php
Whitespaces, bitches!! Because, you know, CodingStandards.
[Github/YOURLS.git] / includes / functions-infos.php
1 <?php
2
3 // Echoes an image tag of Google Charts map from sorted array of 'country_code' => 'number of visits' (sort by DESC)
4 function yourls_stats_countries_map( $countries, $id = null ) {
5
6         yourls_do_action( 'pre_stats_countries_map' );
7         
8         // if $id is null then assign a random string
9         if( $id === null )
10                 $id = uniqid ( 'yourls_stats_map_' );
11
12         $data = array_merge( array( 'Country' => 'Hits' ), $countries );
13         $data = yourls_google_array_to_data_table( $data );
14
15         $options = array(
16                 'backgroundColor' => "white",
17                 'colorAxis'       => "{colors:['A8D0ED','99C4E4','8AB8DB','7BACD2','6BA1C9','5C95C0','4D89B7','3E7DAE','2E72A5','1F669C']}",
18                 'width'           => "550",
19                 'height'          => "340",
20                 'theme'           => 'maximized'
21         );
22         $options = yourls_apply_filter( 'stats_countries_map_options', $options );
23         
24         $map = yourls_google_viz_code( 'GeoChart', $data, $options, $id );
25
26         echo yourls_apply_filter( 'stats_countries_map', $map, $countries, $options, $id );
27 }
28
29 // Echoes an image tag of Google Charts pie from sorted array of 'data' => 'value' (sort by DESC). Optional $limit = (integer) limit list of X first countries, sorted by most visits
30 function yourls_stats_pie( $data, $limit = 10, $size = '340x220', $id = null ) {
31
32         yourls_do_action( 'pre_stats_pie' );
33         
34         // if $id is null then assign a random string
35         if( $id === null )
36                 $id = uniqid ( 'yourls_stats_pie_' );
37
38         // Trim array: $limit first item + the sum of all others
39         if ( count( $data ) > $limit ) {
40                 $i= 0;
41                 $trim_data = array( 'Others' => 0 );
42                 foreach( $data as $item=>$value ) {
43                         $i++;
44                         if( $i <= $limit ) {
45                                 $trim_data[$item] = $value;
46                         } else {
47                                 $trim_data['Others'] += $value;
48                         }
49                 }
50                 $data = $trim_data;
51         }
52         
53         // Scale items
54         $_data = yourls_scale_data( $data );
55         
56         list($width, $height) = explode( 'x', $size );
57         
58         $options = array(
59                 'theme'  => 'maximized',
60                 'width'   => $width,
61                 'height'   => $height,
62                 'colors'    => "['A8D0ED','99C4E4','8AB8DB','7BACD2','6BA1C9','5C95C0','4D89B7','3E7DAE','2E72A5','1F669C']",
63                 'legend'     => 'none',
64                 'chartArea'   => '{top: "5%", height: "90%"}',
65                 'pieSliceText' => 'label',
66         );
67         $options = yourls_apply_filter( 'stats_pie_options', $options );
68
69         $script_data = array_merge( array( 'Country' => 'Value' ), $_data );
70         $script_data = yourls_google_array_to_data_table( $script_data );
71
72         $pie = yourls_google_viz_code( 'PieChart', $script_data, $options, $id );
73
74         echo yourls_apply_filter( 'stats_pie', $pie, $data, $limit, $size, $options, $id );
75 }
76
77 // Build a list of all daily values between d1/m1/y1 to d2/m2/y2.
78 function yourls_build_list_of_days( $dates ) {
79         /* Say we have an array like:
80         $dates = array (
81                 2009 => array (
82                         '08' => array (
83                                 29 => 15,
84                                 30 => 5,
85                                 ),
86                         '09' => array (
87                                 '02' => 3,
88                                 '03' => 5,
89                                 '04' => 2,
90                                 '05' => 99,
91                                 ),
92                         ),
93                 )
94         */
95         
96         if( !$dates )
97                 return array();
98
99         // Get first & last years from our range. In our example: 2009 & 2009
100         $first_year = key( $dates );
101         $last_year  = end( array_keys( $dates ) );
102         reset( $dates );
103
104         // Get first & last months from our range. In our example: 08 & 09
105         $first_month = key( $dates[ $first_year ] );
106         $last_month  = end( array_keys( $dates[ $last_year ] ) );
107         reset( $dates );
108         
109         // Get first & last days from our range. In our example: 29 & 05
110         $first_day = key( $dates[ $first_year ][ $first_month ] );
111         $last_day  = end( array_keys( $dates[ $last_year ][ $last_month ] ) );
112
113         // Now build a list of all years (2009), month (08 & 09) and days (all from 2009-08-29 to 2009-09-05)
114         $list_of_years  = array();
115         $list_of_months = array();
116         $list_of_days   = array();
117         for ( $year = $first_year; $year <= $last_year; $year++ ) {
118                 $_year = sprintf( '%04d', $year );
119                 $list_of_years[ $_year ] = $_year;
120                 $current_first_month = ( $year == $first_year ? $first_month : '01' );
121                 $current_last_month  = ( $year == $last_year ? $last_month : '12' );
122                 for ( $month = $current_first_month; $month <= $current_last_month; $month++ ) {
123                         $_month = sprintf( '%02d', $month );
124                         $list_of_months[ $_month ] = $_month;
125                         $current_first_day = ( $year == $first_year && $month == $first_month ? $first_day : '01' );
126                         $current_last_day  = ( $year == $last_year && $month == $last_month ? $last_day : yourls_days_in_month( $month, $year) );
127                         for ( $day = $current_first_day; $day <= $current_last_day; $day++ ) {
128                                 $day = sprintf( '%02d', $day );
129                                 $key = date( 'M d, Y', mktime( 0, 0, 0, $_month, $day, $_year ) );
130                                 $list_of_days[ $key ] = isset( $dates[$_year][$_month][$day] ) ? $dates[$_year][$_month][$day] : 0;
131                         }
132                 }
133         }
134         
135         return array(
136                 'list_of_days'   => $list_of_days,
137                 'list_of_months' => $list_of_months,
138                 'list_of_years'  => $list_of_years,
139         );
140 }
141
142 // Echoes an image tag of Google Charts line graph from array of values (eg 'number of clicks').
143 // $legend1_list & legend2_list are values used for the 2 x-axis labels. $id is an HTML/JS id
144 function yourls_stats_line( $values, $id = null ) {
145
146         yourls_do_action( 'pre_stats_line' );
147         
148         // if $id is null then assign a random string
149         if( $id === null )
150                 $id = uniqid ( 'yourls_stats_line_' );
151                 
152         // If we have only 1 day of data, prepend a fake day with 0 hits for a prettier graph
153         if ( count( $values ) == 1 )
154                 array_unshift( $values, 0 );
155                 
156         // Keep only a subset of values to keep graph smooth
157         $values = yourls_array_granularity( $values, 30 );
158         
159         $data = array_merge( array( 'Time' => 'Hits' ), $values );
160         $data = yourls_google_array_to_data_table( $data );
161         
162         $options = array(
163                 "legend"      => "none",
164                 "pointSize"   => "3",
165                 "theme"       => "maximized",
166                 "curveType"   => "function",
167                 "width"       => 430,
168                 "height"          => 220,
169                 "hAxis"       => "{minTextSpacing: 80, maxTextLines: 1, maxAlternation: 1}",
170                 "vAxis"       => "{minValue: -0.5, format: '#'}",
171                 "colors"          => "['#2a85b3']",
172         );
173         $options = yourls_apply_filter( 'stats_line_options', $options );
174         
175         $lineChart = yourls_google_viz_code( 'LineChart', $data, $options, $id );
176
177         echo yourls_apply_filter( 'stats_line', $lineChart, $values, $options, $id );
178 }
179
180 // Return the number of days in a month. From php.net, used if PHP built without calendar functions
181 function yourls_days_in_month( $month, $year ) {
182         // calculate number of days in a month
183         return $month == 2 ? ( $year % 4 ? 28 : ( $year % 100 ? 29 : ( $year % 400 ? 28 : 29 ) ) ) : ( ( $month - 1 ) % 7 % 2 ? 30 : 31 );
184 }
185
186 // Get max value from date array of 'Aug 12, 2012' = '1337'
187 function yourls_stats_get_best_day( $list_of_days ) {
188         $max = 0; $day = 0;
189         $max = max( $list_of_days );
190         foreach( $list_of_days as $k=>$v ) {
191                 if ( $v == $max )
192                         return array( 'day' => $k, 'max' => $max );
193         }
194 }
195
196 // Return domain of a URL
197 function yourls_get_domain( $url, $include_scheme = false ) {
198         $parse = @parse_url( $url ); // Hiding ugly stuff coming from malformed referrer URLs
199
200         // Get host & scheme. Fall back to path if not found.
201         $host = isset( $parse['host'] ) ? $parse['host'] : '';
202         $scheme = isset( $parse['scheme'] ) ? $parse['scheme'] : '';
203         $path = isset( $parse['path'] ) ? $parse['path'] : '';
204         if( !$host )
205                 $host = $path;  
206                 
207         if ( $include_scheme && $scheme )
208                 $host = $scheme.'://'.$host;
209                 
210         return $host;
211 }
212
213 // Return favicon URL
214 function yourls_get_favicon_url( $url ) {
215         return yourls_match_current_protocol( 'http://www.google.com/s2/u/0/favicons?domain=' . yourls_get_domain( $url, false ) );
216 }
217
218 // Scale array of data from 0 to 100 max
219 function yourls_scale_data( $data ) {
220         $max = max( $data );
221         if( $max > 100 ) {
222                 foreach( $data as $k=>$v ) {
223                         $data[$k] = intval( $v / $max * 100 );
224                 }
225         }
226         return $data;
227 }
228
229 // Tweak granularity of array $array: keep only $grain values. This make less accurate but less messy graphs when too much values. See http://code.google.com/apis/chart/formats.html#granularity
230 function yourls_array_granularity( $array, $grain = 100, $preserve_max = true ) {
231         if ( count( $array ) > $grain ) {
232                 $max = max( $array );
233                 $step = intval( count( $array ) / $grain );
234                 $i = 0;
235                 // Loop through each item and unset except every $step (optional preserve the max value)
236                 foreach( $array as $k=>$v ) {
237                         $i++;
238                         if ( $i % $step != 0 ) {
239                                 if ( $preserve_max == false ) {
240                                         unset( $array[$k] );
241                                 } else {
242                                         if ( $v < $max )
243                                                 unset( $array[$k] );
244                                 }
245                         }
246                 }
247         }
248         return $array;
249 }
250
251 // Transform data array to data table for Google API
252 function yourls_google_array_to_data_table( $data ){
253         $str  = "var data = google.visualization.arrayToDataTable([\n";
254         foreach( $data as $label => $values ){
255                 if( !is_array( $values ) ) {
256                         $values = array( $values );
257                 }
258                 $str .= "\t['$label',"; 
259                 foreach( $values as $value ){
260                         if( !is_numeric( $value ) && strpos( $value, '[' ) !== 0 && strpos( $value, '{' ) !== 0 ) { 
261                                 $value = "'$value'";
262                         }
263                         $str .= "$value";
264                 }               
265                 $str .= "],\n";
266         }
267         $str = substr( $str, 0, -2 ) . "\n"; // remove the trailing comma/return, reappend the return
268         $str .= "]);\n"; // wrap it up  
269         return $str;
270 }
271
272 // Return javascript code that will display the Google Chart
273 function yourls_google_viz_code( $graph_type, $data, $options, $id ) {
274         $function_name = 'yourls_graph' . $id;
275         $code  = "\n<script id=\"$function_name\" type=\"text/javascript\">\n";
276         $code .= "function $function_name() { \n";
277
278         $code .= "$data\n";
279
280         $code .= "var options = {\n";
281         foreach( $options as $field => $value ) {
282                 if( !is_numeric( $value ) && strpos( $value, '[' ) !== 0 && strpos( $value, '{' ) !== 0 ) { 
283                         $value = "\"$value\"";
284                 }
285                 $code .= "\t'$field': $value,\n";
286         }
287         $code  = substr( $code, 0, -2 ) . "\n"; // remove the trailing comma/return, reappend the return
288         $code .= "\t}\n";
289
290         $code .= "new google.visualization.$graph_type( document.getElementById('visualization_$id') ).draw( data, options );";
291         $code .= "}\n";
292         $code .= "google.setOnLoadCallback( $function_name );\n";
293         $code .= "</script>\n";
294         $code .= "<div id=\"visualization_$id\"></div>\n";
295         
296         return $code;
297 }
298