]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-infos.php
Big commit: the individual stat page is just fucking perfect now :)
[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 ) {
5         $map = array(
6                 'cht' => 't',
7                 'chs' => '440x220',
8                 'chtm'=> 'world',
9                 'chco'=> 'FFFFFF,9090AA,202040',
10                 'chld'=> join('' , array_keys( $countries ) ),
11                 'chd' => 't:'. join(',' ,  $countries ),
12                 'chf' => 'bg,s,EAF7FE'
13         );
14         $map_src = 'http://chart.apis.google.com/chart?' . http_build_query( $map );
15         echo "<img src='$map_src' witdh='440' height='220' border='0' />";
16 }
17
18 // 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
19 function yourls_stats_pie( $data, $limit = 10, $size = '340x220', $colors = '202040,9090AA' ) {
20         // Trim array: $limit first item + the sum of all others
21         if ( count( $data ) > $limit ) {
22                 $i= 0;
23                 $trim_data = array('Others' => 0);
24                 foreach( $data as $item=>$value ) {
25                         $i++;
26                         if( $i <= $limit ) {
27                                 $trim_data[$item] = $value;
28                         } else {
29                                 $trim_data['Others'] += $value;
30                         }
31                 }
32                 $data = $trim_data;
33         }
34         
35         // Scale items
36         $_data = yourls_scale_data( $data );
37         
38         // Hmmm, pie
39         $pie = array(
40                 'cht' => 'p',
41                 'chs' => $size,
42                 'chd' => 't:'.( join(',' ,  $_data ) ),
43                 'chco'=> $colors,
44                 'chl' => join('|' , array_keys( $data ) )
45         );
46         $pie_src = 'http://chart.apis.google.com/chart?' . http_build_query( $pie );
47         echo "<img src='$pie_src' witdh='440' height='220' border='0' />";
48 }
49
50 // Build a list of all daily values between d1/m1/y1 to d2/m2/y2.
51 function yourls_build_list_of_days( $dates ) {
52         /* Say we have an array like:
53         $dates = array (
54                 2009 => array (
55                         '08' => array (
56                                 29 => 15,
57                                 30 => 5,
58                                 ),
59                     '09' => array (
60                                 '02' => 3,
61                                 '03' => 5,
62                                 '04' => 2,
63                                 '05' => 99,
64                                 ),
65                         ),
66                 )
67         */
68         
69         if( !$dates )
70                 return array();
71
72         // Get first & last years from our range. In our example: 2009 & 2009
73         $first_year = key( $dates );
74         $last_year  = end( array_keys($dates) );
75         reset( $dates );
76
77         // Get first & last months from our range. In our example: 08 & 09
78         $first_month = key( $dates[$first_year] );
79         $last_month  = end( array_keys($dates[$last_year]) );
80         reset( $dates );
81         
82         // Get first & last days from our range. In our example: 29 & 05
83         $first_day = key( $dates[$first_year][$first_month] );
84         $last_day  = end( array_keys($dates[$last_year][$last_month]) );
85
86         // Now build a list of all years (2009), month (08 & 09) and days (all from 2009-08-29 to 2009-09-05)
87         $list_of_years = array();
88         $list_of_months = array();
89         $list_of_days = array();
90         for ( $year = $first_year; $year <= $last_year; $year++ ) {
91                 $_year = sprintf('%04d', $year);
92                 $list_of_years[$_year] = $_year;
93                 $current_first_month = ( $year == $first_year ? $first_month : '01' );
94                 $current_last_month  = ( $year == $last_year ? $last_month : '12' );
95                 for ( $month = $current_first_month; $month <= $current_last_month; $month++ ) {
96                         $_month = sprintf('%02d', $month);
97                         $list_of_months[$_month] = $_month;
98                         $current_first_day = ( $year == $first_year && $month == $first_month ? $first_day : '01' );
99                         $current_last_day  = ( $year == $last_year && $month == $last_month ? $last_day : yourls_days_in_month($month, $year) );
100                         for ( $day = $current_first_day; $day <= $current_last_day; $day++ ) {
101                                 $day = sprintf('%02d', $day);
102                                 $list_of_days["$_year-$_month-$day"] = isset( $dates[$_year][$_month][$day] ) ? $dates[$_year][$_month][$day] : 0;
103                         }
104                 }
105         }
106         
107         return array(
108                 'list_of_days' => $list_of_days,
109                 'list_of_months' => $list_of_months,
110                 'list_of_years' => $list_of_years,
111         );
112 }
113
114 // Echoes an image tag of Google Charts line graph from array of values (eg 'number of clicks'). $legend1_list & legend2_list are values used for the 2 x-axis labels
115 function yourls_stats_line( $values, $legend1_list, $legend2_list ) {
116
117         // If we have only 1 day of data, prepend a fake day with 0 hits for a prettier graph
118         if ( count( $values ) == 1 )
119                 array_unshift( $values, 0 );
120                 
121         $values = yourls_array_granularity( $values, 100 );
122         
123         // If x-axis labels have only 1 value, double it for a nicer graph
124         if( count( $legend1_list ) == 1 )
125                 $legend1_list[] = current( $legend1_list );
126         if( count( $legend2_list ) == 1 )
127                 $legend2_list[] = current( $legend2_list );
128
129         // Make the chart
130         $legend1 = join('|', $legend1_list );
131         $legend2 = join('|', $legend2_list );
132         $max = max( $values );
133         if ( $max >= 4 ) {
134                 $label_clicks = '0|'.intval( $max / 4 ).'|'.intval( $max / 2 ).'|'.intval( $max / 1.5 ).'|'.$max;
135         } else {
136                 $label_clicks = array();
137                 for ($i = 0; $i <= $max; $i++) {
138                         $label_clicks[] = $i;
139                 }
140                 $label_clicks = join( '|', $label_clicks );
141         }
142         $line = array(
143                 'cht' => 'lc',
144                 'chs' => '440x220',
145                 'chxt'=> 'x,x,y',
146                 'chd' => 't:'.( join(',' ,  $values ) ),
147                 'chds' => '0,'.$max,
148                 'chxl'=> '0:|'. $legend1 .'|1:|'. $legend2 .'|2:|'. $label_clicks
149         );
150         $line_src = 'http://chart.apis.google.com/chart?' . http_build_query( $line );
151         echo "<img src='$line_src' />";
152 }
153
154 // Return the number of days in a month. From php.net, used if PHP built without calendar functions
155 function yourls_days_in_month($month, $year) {
156         // calculate number of days in a month
157         return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
158 }
159
160 // Get max value from date array of 'year-month-day' = 'hits'
161 function yourls_stats_get_best_day( $list_of_days ) {
162         $max = 0; $day = 0;
163         $max = max( $list_of_days );
164         foreach( $list_of_days as $k=>$v ) {
165                 if ( $v == $max )
166                         return array( 'day' => $k, 'max' => $max );
167         }
168 }
169
170 // Fetch remote page title
171 function yourls_get_page_title( $url, $default = '' ) {
172         if ( !class_exists('DOMDocument') )
173                 return $default;
174
175         $dom = new DOMDocument();
176
177         if( @$dom->loadHTMLFile('http://planetozh.com/') ) {
178                 $list = $dom->getElementsByTagName("title");
179                 if ($list->length > 0) {
180                         return( $list->item(0)->textContent );
181                 }
182         }
183         
184         return $default;
185 }
186
187 // Return domain of a URL
188 function yourls_get_domain( $url, $include_scheme = false ) {
189         $parse = parse_url( $url );
190         $host = $parse['host'];
191         $scheme = $parse['scheme'];
192         
193         if ( $include_scheme )
194                 $host = $scheme.'://'.$host;
195                 
196         return $host;
197 }
198
199 // Scale array of data from 0 to 100 max
200 function yourls_scale_data( $data ) {
201         $max = max( $data );
202         if( $max > 100 ) {
203                 foreach( $data as $k=>$v ) {
204                         $data[$k] = intval( $v / $max * 100 );
205                 }
206         }
207         return $data;
208 }
209
210 // 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
211 function yourls_array_granularity( $array, $grain = 100, $preserve_max = true ) {
212         if ( count( $array ) > $grain ) {
213                 $max = max( $array );
214                 $step = intval( count( $array ) / $grain );
215                 $i = 0;
216                 $preserved = false;
217                 // Loop through each item and unset except every $step (optional preserver the max value)
218                 foreach( $array as $k=>$v ) {
219                         $i++;
220                         if ( $i % $step != 0 ) {
221                                 if ( $preserve_max == false ) {
222                                         unset( $array[$k] );
223                                 } else {
224                                         if ( $v == $max ) {
225                                                 if( $preserved == false )
226                                                         unset( $array[$k] );
227                                                 $preserved = true;
228                                         } else {
229                                                 unset( $array[$k] );
230                                         }
231                                 }
232                         }
233                 }
234         }
235         return $array;
236 }