1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
<?php
/*
Copyright 2001 - 2007 Ampache.org
All Rights Reserved
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/**
* xmlData
* This class takes care of all of the xml document stuff in Ampache these
* are all static calls
*/
class xmlData {
// This is added so that we don't pop any webservers
private static $limit = '5000';
private static $offset = '0';
/**
* constructor
* We don't use this, as its really a static class
*/
private function __construct() {
// Rien a faire
} // constructor
/**
* set_offset
* This takes an int and changes the offset
*/
public static function set_offset($offset) {
$offset = intval($offset);
self::$offset = $offset;
} // set_offset
/**
* set_limit
* This sets the limit for any ampache transactions
*/
public static function set_limit($limit) {
if (!$limit) { return false; }
$limit = intval($limit);
self::$limit = $limit;
} // set_limit
/**
* error
* This generates a standard XML Error message
* nothing fancy here...
*/
public static function error($string) {
$string = self::_header() . "\t<error><![CDATA[$string]]></error>" . self::_footer();
return $string;
} // error
/**
* single_string
* This takes two values, first the key second the string
*/
public static function single_string($key,$string) {
$final = self::_header() . "\t<$key><![CDATA[$string]]></$key>" . self::_footer();
return $final;
} // single_string
/**
* keyed_array
* This will build an xml document from a key'd array,
*/
public static function keyed_array($array,$callback='') {
$string = '';
// Foreach it
foreach ($array as $key=>$value) {
// If it's an array, run again
if (is_array($value)) {
$value = self::keyed_array($value,1);
$string .= "\t<$key>$value</$key>\n";
}
else {
$string .= "\t<$key><![CDATA[$value]]></$key>\n";
}
} // end foreach
if (!$callback) {
$string = self::_header() . $string . self::_footer();
}
return $string;
} // keyed_array
/**
* artists
* This takes an array of artists and then returns a pretty xml document with the information
* we want
*/
public static function artists($artists) {
if (count($artists) > self::$limit) {
$artists = array_splice($artists,self::$offset,self::$limit);
}
$string = '';
foreach ($artists as $artist_id) {
$artist = new Artist($artist_id);
$artist->format();
$string .= "<artist id=\"$artist->id\">\n" .
"\t<name><![CDATA[$artist->f_full_name]]></name>\n" .
"</artist>\n";
} // end foreach artists
$final = self::_header() . $string . self::_footer();
return $final;
} // artists
/**
* albums
* This echos out a standard albums XML document, it pays attention to the limit
*/
public static function albums($albums) {
if (count($albums) > self::$limit) {
$albums = array_splice($albums,self::$offset,self::$limit);
}
foreach ($albums as $album_id) {
$album = new Album($album_id);
$album->format();
// Build the Art URL, include session
$art_url = Config::get('web_path') . '/image.php?id=' . $album->id . '&auth=' . scrub_out($_REQUEST['auth']);
$string .= "<album id=\"$album->id\">\n" .
"\t<name><![CDATA[$album->name]]></name>\n";
// Do a little check for artist stuff
if ($album->artist_count != 1) {
$string .= "\t<artist id=\"0\"><![CDATA[Various]]></artist>\n";
}
else {
$string .= "\t<artist id=\"$album->artist_id\"><![CDATA[$album->artist_name]]></artist>\n";
}
$string .= "\t<year>$album->year</year>\n" .
"\t<tracks>$album->song_count</tracks>\n" .
"\t<disk>$album->disk</disk>\n" .
"\t<art><![CDATA[$art_url]]></art>\n" .
"</album>\n";
} // end foreach
$final = self::_header() . $string . self::_footer();
return $final;
} // albums
/**
* genres
* This takes an array of genre ids and returns a nice little pretty xml doc
*/
public static function genres($genres) {
if (count($genres) > self::$limit) {
$genres = array_slice($genres,self::$offset,self::$limit);
}
// Foreach the ids
foreach ($genres as $genre_id) {
$genre = new Genre($genre_id);
$genre->format();
$song_count = $genre->get_song_count();
$album_count = $genre->get_album_count();
$artist_count = $genre->get_artist_count();
$string .= "<genre id=\"$genre->id\">\n" .
"\t<name><![CDATA[$genre->name]]></name>\n" .
"\t<songs>$song_count</songs>\n" .
"\t<albums>$album_count</albums>\n" .
"\t<artists>$artist_count</artists>\n" .
"</genre>\n";
} // end foreach
$final = self::_header() . $string . self::_footer();
return $final;
} // genres
/**
* playlists
* This takes an array of playlist ids and then returns a nice pretty XML document
*/
public static function playlists($playlists) {
if (count($playlists) > self::$limit) {
$playlists = array_slice($playlists,self::$offset,self::$limit);
}
$string = '';
// Foreach the playlist ids
foreach ($playlists as $playlist_id) {
$playlist = new Playlist($playlist_id);
$playlist->format();
$item_total = $playlist->get_song_count();
// Build this element
$string .= "<playlist id=\"$playlist->id\">\n" .
"\t<name><![CDATA[$playlist->name]]></name>\n" .
"\t<owner><![CDATA[$playlist->f_user]]</owner>\n" .
"\t<items>$item_total</items>\n" .
"\t<type>$playlist->type</type>\n" .
"</playlist>\n";
} // end foreach
// Build the final and then send her off
$final = self::_header() . $string . self::_footer();
return $final;
} // playlists
/**
* songs
* This returns an xml document from an array of song ids spiffy isn't it!
*/
public static function songs($songs) {
if (count($songs) > self::$limit) {
$songs = array_slice($songs,self::$offset,self::$limit);
}
// Foreach the ids!
foreach ($songs as $song_id) {
$song = new Song($song_id);
$song->format();
$string .= "<song id=\"$song->id\">\n" .
"\t<title><![CDATA[$song->title]]></title>\n" .
"\t<artist id=\"$song->artist\"><![CDATA[$song->f_artist_full]]></artist>\n" .
"\t<album id=\"$song->album\"><![CDATA[$song->f_album_full]]></album>\n" .
"\t<genre id=\"$song->genre\"><![CDATA[$song->genre]]></genre>\n" .
"\t<track>$song->track</track>\n" .
"\t<time>$song->time</time>\n" .
"\t<url><![CDATA[" . $song->get_url($_REQUEST['auth']) . "]]></url>\n" .
"</song>\n";
} // end foreach
$final = self::_header() . $string . self::_footer();
return $final;
} // songs
/**
* _header
* this returns a standard header, there are a few types
* so we allow them to pass a type if they want to
*/
private static function _header($type='') {
switch ($type) {
case 'xspf':
break;
case 'itunes':
break;
default:
$header = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<root>\n";
break;
} // end switch
return $header;
} // _header
/**
* _footer
* this returns the footer for this document, these are pretty boring
*/
private static function _footer($type='') {
switch ($type) {
default:
$footer = "\n</root>\n";
break;
} // end switch on type
return $footer;
} // _footer
} // xmlData
?>
|