File Coverage

blib/lib/Slackware/Slackget/Media.pm
Criterion Covered Total %
statement 25 142 17.6
branch 2 60 3.3
condition 0 24 0.0
subroutine 10 24 41.6
pod 20 20 100.0
total 57 270 21.1


line stmt bran cond sub pod time code
1             package Slackware::Slackget::Media;
2              
3 3     3   59122 use warnings;
  3         8  
  3         99  
4 3     3   16 use strict;
  3         7  
  3         11969  
5              
6             =head1 NAME
7              
8             Slackware::Slackget::Media - A class to represent a Media from the medias.xml file.
9              
10             =head1 VERSION
11              
12             Version 0.9.9
13              
14             =cut
15              
16             our $VERSION = '0.9.9';
17              
18             =head1 SYNOPSIS
19              
20             This class is used by slack-get to represent a media store in the medias.xml file. In this class (and in the related MediaList), the word "media" is used to describe an update source, a media entity of the medias.xml file.
21              
22             use Slackware::Slackget::Media;
23              
24             my $Media = Slackware::Slackget::Media->new('slackware');
25             my $xml = XML::Simple::XMLin($medias_file,,KeyAttr => {'media' => 'id'});
26             $media->fill_object_from_xml($xml->{'slackware'});
27             $media->set_value('description','The official Slackware web site');
28              
29             This class' usage is mostly the same that the Slackware::Slackget::Package one. There is one big difference with the package class : you must use the accessors for setting the fast and slow medias list.
30              
31             =head1 CONSTRUCTOR
32              
33             =head2 new
34              
35             The constructor require the following argument :
36              
37             - an id (stricly needed)
38              
39             Additionnaly you can pass the followings :
40              
41             description => a string which describe the mirror
42             web-link => a web site URL for the mirror.
43             update-repository => A hash reference build on the model of the medias.xml file. For example for the faster mirror (the one you want you use for this Media object) :
44            
45             my $media = Slackware::Slackget::Media->new('slackware','update-repository' => {faster => http://ftp.belnet.be/packages/slackware/slackware-10.1/});
46              
47             Some examples:
48              
49             # the simpliest and recommended way
50             my $media = Slackware::Slackget::Media->new('slackware');
51             $media->fill_object_from_xml($xml_simple_hashref);
52            
53             or
54            
55             # The harder and realy not recommended unless you know what you are doing.
56            
57             my $media = Slackware::Slackget::Media->new('slackware',
58             'description'=>'The official Slackware web site',
59             'web-link' => 'http://www.slackware.com/',
60             'update-repository' => {faster => 'http://ftp.belnet.be/packages/slackware/slackware-10.1/'}
61             'files' => {
62             'filelist' => 'FILELIST.TXT',
63             'checksums' => 'CHECKSUMS.md5',
64             'packages' => 'PACKAGES.TXT.gz'
65             }
66             );
67              
68             =cut
69              
70             sub new
71             {
72 2     2 1 53 my ($class,$id,%args) = @_ ;
73 2 50       8 return undef unless(defined($id));
74 2         4 my $self={};
75 2         7 $self->{ID} = $id ;
76 2         11 $self->{DATA} = {%args};
77 2         10 $self->{DATA}->{hosts}->{old} = [] ;
78 2         8 bless($self,$class);
79 2 50       19 $self->set_value('host',$args{'update-repository'}->{'faster'}) if(defined($args{'update-repository'}->{'faster'}));
80 2         8 return $self;
81             }
82              
83             =head1 FUNCTIONS
84              
85             =head2 set_value
86              
87             Set the value of a named key to the value passed in argument.
88              
89             $package->set_value($key,$value);
90              
91             Return the value you just tried to set (usefull for integrity checks).
92              
93             =cut
94              
95             sub set_value {
96 4     4 1 8 my ($self,$key,$value) = @_ ;
97             # print "Setting $key=$value for $self\n";
98 4         22 $self->{DATA}->{$key} = $value ;
99 4         16 return $self->{DATA}->{$key};
100             }
101              
102             =head2 setValue (deprecated)
103              
104             Same as set_value(), provided for backward compatibility.
105              
106             =cut
107              
108             sub setValue {
109 1     1 1 5 return set_value(@_);
110             }
111              
112             =head2 getValue (deprecated)
113              
114             Same as get_value(), provided for backward compatibility.
115              
116             =cut
117              
118             sub getValue {
119 2     2 1 694 return get_value(@_);
120             }
121              
122             =head2 get_value
123              
124             Return the value of a key :
125              
126             $string = $media->get_value($key);
127              
128             =cut
129              
130             sub get_value {
131 4     4 1 697 my ($self,$key) = @_ ;
132 4         21 return $self->{DATA}->{$key};
133             }
134              
135             =head2 fill_object_from_xml
136              
137             Fill the data section of the Slackware::Slackget::Media object with information from a medias.xml section.
138              
139             $media->fill_object_from_xml($xml->{'slackware'});
140              
141             =cut
142              
143             sub fill_object_from_xml {
144 0     0 1 0 my ($self,$xml) = @_ ;
145             # require Data::Dumper ;
146             # print Data::Dumper::Dumper($xml);
147            
148 0 0       0 defined($xml->{'description'}) ? $self->set_value('description',$xml->{'description'}) : $self->set_value('description','no description for this media.') ;
149 0 0       0 defined($xml->{'web-link'}) ? $self->set_value('web-link',$xml->{'web-link'}) : $self->set_value('web-link','no website for this media.');
150 0 0       0 defined($xml->{'download-signature'}) ? $self->set_value('download-signature',$xml->{'download-signature'}) : $self->set_value('download-signature',0);
151 0 0       0 if(defined($xml->{'files'}))
152             {
153 0         0 $self->set_value('filelist',$xml->{'files'}->{'filelist'});
154 0         0 $self->set_value('packages',$xml->{'files'}->{'packages'});
155 0         0 $self->set_value('checksums',$xml->{'files'}->{'checksums'});
156             }
157             else
158             {
159 0         0 $self->set_value('filelist','FILELIST.TXT');
160 0         0 $self->set_value('packages','PACKAGES.TXT');
161 0         0 $self->set_value('checksums','CHECKSUMS.md5');
162             }
163 0 0       0 if(defined($xml->{'update-repository'}))
164             {
165 0 0       0 if(defined($xml->{'update-repository'}->{faster})){
166 0         0 require Slackware::Slackget::Network::Connection;
167 0 0       0 unless(Slackware::Slackget::Network::Connection::is_url(undef,$xml->{'update-repository'}->{faster})){
168 0         0 warn "[Slackware::Slackget::Media] the faster host of the update-repository section will not be accepted as a valid URL by Slackware::Slackget::Connection class !\n";
169             }
170 0 0       0 return undef unless(defined($xml->{'update-repository'}->{faster}));
171 0         0 $self->set_value('host',$xml->{'update-repository'}->{faster});
172             }
173 0 0 0     0 if(defined($xml->{'update-repository'}->{fast}) && defined($xml->{'update-repository'}->{fast}->{li}) && ref($xml->{'update-repository'}->{fast}->{li}) eq 'ARRAY')
      0        
174             {
175 0         0 $self->_fill_fast_host_section($xml->{'update-repository'}->{fast});
176             }
177             else
178             {
179 0         0 $self->{DATA}->{hosts}->{fast} = [] ;
180             }
181 0 0 0     0 if(defined($xml->{'update-repository'}->{slow}) && defined($xml->{'update-repository'}->{slow}->{li}) && ref($xml->{'update-repository'}->{slow}->{li}) eq 'ARRAY')
      0        
182             {
183 0         0 $self->_fill_slow_host_section($xml->{'update-repository'}->{slow});
184             }
185             else
186             {
187 0         0 $self->{DATA}->{hosts}->{slow} = [] ;
188             }
189             }
190             else
191             {
192 0         0 warn "[Slackware::Slackget::Media] no update-repository found for the update source '$self->{ID}'\n";
193 0         0 return undef;
194             }
195 0         0 return 1;
196             }
197              
198             =head2 _fill_fast_host_section [PRIVATE]
199              
200             fill the DATA section of the object (sub-section fast host), with a part of the XML tree of a medias.xml file.
201              
202             In normal use you don't have to use this method. In all case prefer pass all required argument to the constructor, and call the fill_object_from_xml() method.
203              
204             $self->_fill_fast_host_section($xml->{'update-repository'}->{fast});
205              
206             =cut
207              
208             sub _fill_fast_host_section
209             {
210 0     0   0 my ($self,$xml) = @_ ;
211 0 0 0     0 if(defined($xml->{li}) && ref($xml->{li}) eq 'ARRAY')
212             {
213 0         0 $self->{DATA}->{hosts}->{fast} = $xml->{li} ;
214             }
215             else
216             {
217 0         0 $self->{DATA}->{hosts}->{fast} = [] ;
218             }
219             }
220              
221             =head2 _fill_slow_host_section [PRIVATE]
222              
223             fill the DATA section of the object (sub-section slow host), with a part of the XML tree of a medias.xml file.
224              
225             In normal use you don't have to use this method. In all case prefer pass all required argument to the constructor, and call the fill_object_from_xml() method.
226              
227             $self->_fill_slow_host_section($xml->{'update-repository'}->{slow});
228              
229             =cut
230              
231             sub _fill_slow_host_section
232             {
233 0     0   0 my ($self,$xml) = @_ ;
234 0 0 0     0 if(defined($xml->{li}) && ref($xml->{li}) eq 'ARRAY')
235             {
236 0         0 $self->{DATA}->{hosts}->{slow} = $xml->{li} ;
237             }
238             else
239             {
240 0         0 $self->{DATA}->{hosts}->{slow} = [] ;
241             }
242             }
243              
244             =head2 add_slow_host( )
245              
246             Add an host to the slow section of the current media.
247              
248             $media->add_slow_host("ftp://ftp.fe.up.pt/disk1/ftp.slackware.com/pub/slackware/slackware-current/");
249              
250             =cut
251              
252             sub add_slow_host {
253 0     0 1 0 my ($self,$url) = @_;
254 0 0       0 $self->{DATA}->{hosts}->{slow} = [] unless(exists($self->{DATA}->{hosts}->{slow}));
255 0         0 push @{$self->{DATA}->{hosts}->{slow}}, $url;
  0         0  
256             }
257              
258             =head2 add_fast_host( )
259              
260             Add an host to the fast section of the current media.
261              
262             $media->add_fast_host("http://mirror.switch.ch/ftp/mirror/slackware/slackware-current/");
263              
264             =cut
265              
266             sub add_fast_host {
267 0     0 1 0 my ($self,$url) = @_;
268 0 0       0 $self->{DATA}->{hosts}->{fast} = [] unless(exists($self->{DATA}->{hosts}->{fast}));
269 0         0 push @{$self->{DATA}->{hosts}->{fast}}, $url;
  0         0  
270             }
271              
272             =head2 next_host
273              
274             This method have 3 functionnalities : return the next fastest host, set it as the current host, and add the old host to the old hosts list.
275              
276             my $host = $media->next_host ;
277              
278             return undef if no new host is found
279              
280             =cut
281              
282             sub next_host
283             {
284 0     0 1 0 my $self = shift;
285 0         0 push @{$self->{DATA}->{hosts}->{old}}, $self->host;
  0         0  
286 0         0 $self->{DATA}->{host} = undef ;
287 0 0       0 if(defined(my $host = shift(@{$self->{DATA}->{hosts}->{fast}})))
  0         0  
288             {
289 0         0 $self->{DATA}->{host} = $host ;
290             }
291             else
292             {
293 0         0 warn "[Slackware::Slackget::Media] no more host in the 'fast' category for update source '$self->{ID}'\n";
294 0 0       0 if(defined(my $host = shift(@{$self->{DATA}->{hosts}->{slow}})))
  0         0  
295             {
296 0         0 $self->{DATA}->{host} = $host ;
297             }
298             else
299             {
300 0         0 warn "[Slackware::Slackget::Media] no more host in the 'slow' category for update source '$self->{ID}'\n";
301 0         0 return undef;
302             }
303             }
304 0         0 return $self->host ;
305             }
306              
307             =head2 print_info
308              
309             This method is used to print the content of the current Media object.
310              
311             $media->print_info ;
312              
313             =cut
314              
315             sub print_info
316             {
317 0     0 1 0 my $self = shift ;
318 0         0 print "Information for the '$self->{ID}' update source :\n";
319 0 0       0 if(defined($self->getValue('description')))
320             {
321 0         0 print "\tDescription: ".$self->getValue('description')."\n";
322             }
323             else
324             {
325 0         0 print "\tDescription: no descrition found\n";
326             }
327 0 0       0 if(defined($self->getValue('web-link')))
328             {
329 0         0 print "\tWeb site: ".$self->getValue('web-link')."\n";
330             }
331             else
332             {
333 0         0 print "\tWeb site: no link found\n";
334             }
335 0 0       0 if(defined($self->getValue('host')))
336             {
337 0         0 print "\tCurrent host: ".$self->getValue('host')."\n";
338             }
339             else
340             {
341 0         0 print "\tCurrent host: no current host configured !\n";
342             }
343             }
344              
345             =head2 to_string
346              
347             return the same information that the print_info() method as a string.
348              
349             my $string = $media->to_string ;
350              
351             =cut
352              
353             sub to_string
354             {
355 0     0 1 0 my $self = shift ;
356 0         0 my $str = "Information for the '$self->{ID}' update source :\n";
357 0 0       0 if(defined($self->getValue('description'))){
358 0         0 $str .= "\tDescription: ".$self->getValue('description')."\n";
359             }
360             else
361             {
362 0         0 $str .= "\tDescription: no descrition found\n";
363             }
364 0 0       0 if(defined($self->getValue('web-link'))){
365 0         0 $str .= "\tWeb site: ".$self->getValue('web-link')."\n";
366             }
367             else
368             {
369 0         0 $str .= "\tWeb site: no link found\n";
370             }
371 0 0       0 if(defined($self->getValue('host'))){
372 0         0 $str .= "\tCurrent host: ".$self->getValue('host')."\n";
373             }
374             else
375             {
376 0         0 $str .= "\tCurrent host: no current host configured !\n";
377             }
378 0         0 return $str ;
379             }
380              
381             =head1 ACCESSORS
382              
383             Some accessors for the current object.
384              
385             =cut
386              
387             =head2 host
388              
389             return the current host :
390              
391             my $host = $media->host
392              
393             =cut
394              
395             sub host {
396 2     2 1 12 return $_[0]->{DATA}->{host};
397             }
398              
399             =head2 description
400              
401             return the description of the media.
402              
403             my $descr = $media->description ;
404              
405             =cut
406              
407             sub description {
408 0     0 1 0 return $_[0]->{DATA}->{description};
409             }
410              
411             =head2 url
412              
413             return the URL of the website for the media.
414              
415             system("$config->{common}->{'default-browser'} $media->url &");
416              
417             =cut
418              
419             sub url {
420 2     2 1 13 return $_[0]->{DATA}->{'web-link'};
421             }
422              
423             =head2 shortname
424              
425             Return the shortname of the media. The shortname is the name of the id attribute of the media tag in medias.xml =>
426              
427             my $id = $media->shortname ;
428              
429             =cut
430              
431             sub shortname {
432 2     2 1 12 return $_[0]->{ID};
433             }
434              
435              
436              
437             =head2 set_fast_medias_array
438              
439             ...not yet implemented...
440              
441             =cut
442              
443 0     0 1   sub set_fast_medias_array {1;}
444              
445             =head1 FORMATTED OUTPUT
446              
447             Different methods to properly output a media.
448              
449             =cut
450              
451             =head2 to_XML (deprecated)
452              
453             Same as to_xml(), provided for backward compatibility.
454              
455             =cut
456              
457             sub to_XML {
458 0     0 1   return to_xml(@_);
459             }
460              
461             =head2 to_xml
462              
463             return the media info as an XML encoded string.
464              
465             $xml = $media->to_xml();
466              
467             =cut
468              
469             sub to_xml
470             {
471 0     0 1   my $self = shift;
472 0 0         return undef unless(defined($self->{ID}));
473 0 0         if($self->{DATA}->{hosts}->{old})
474             {
475 0           $self->{DATA}->{hosts}->{slow} = [@{$self->{DATA}->{hosts}->{slow}},@{$self->{DATA}->{hosts}->{old}}] ;
  0            
  0            
476 0           $self->{DATA}->{hosts}->{old} = undef;
477 0           delete($self->{DATA}->{hosts}->{old});
478             }
479            
480 0           my $xml = "\t{ID}\">\n";
481 0           $xml .= "\t\t".$self->url."\n";
482 0           $xml .= "\t\t".$self->description."\n";
483 0           $xml .= "\t\t\n";
484 0           $xml .= "\t\t\t".$self->host."\n";
485 0 0 0       if(defined($self->{DATA}->{hosts}->{fast}) && defined($self->{DATA}->{hosts}->{fast}->[0]))
486             {
487 0           $xml .= "\t\t\t\t\n";
488 0           foreach my $serv (@{$self->{DATA}->{hosts}->{fast}})
  0            
489             {
490 0           $xml .= "\t\t\t\t\t
  • $serv
  • \n";
    491             }
    492 0           $xml .= "\t\t\t\t\n";
    493             }
    494 0 0 0       if(defined($self->{DATA}->{hosts}->{slow}) && defined($self->{DATA}->{hosts}->{slow}->[0]))
    495             {
    496 0           $xml .= "\t\t\t\t\n";
    497 0           foreach my $serv (@{$self->{DATA}->{hosts}->{slow}})
      0            
    498             {
    499 0           $xml .= "\t\t\t\t\t
  • $serv
  • \n";
    500             }
    501 0           $xml .= "\t\t\t\t\n";
    502             }
    503 0           $xml .= "\t\t\n";
    504             # foreach my $key (keys(%{$self->{DATA}})){
    505             # if($key eq 'update-repository')
    506             # {
    507             # foreach my $key2 (keys(%{$self->{DATA}->{'update-repository'}}))
    508             # {
    509             # if($key2 eq 'fast' or $key2 eq 'slow' && ref($self->{DATA}->{'update-repository'}->{$key2}) eq 'HASH' && defined($self->{DATA}->{'update-repository'}->{$key2}->{li}) && ref($self->{DATA}->{'update-repository'}->{$key2}->{li}) eq 'ARRAY' ) {
    510             # $xml .= "\t\t<$key2>\n";
    511             # foreach (@{$self->{DATA}->{'update-repository'}->{$key2}->{li}}){
    512             # $xml .= "\t\t\t
  • $_
  • \n";
    513             # }
    514             # $xml .= "\t\t\n";
    515             # }
    516             # }
    517             # }
    518             # else
    519             # {
    520             # $xml .= "\t\t<$key>$self->{DATA}->{$key}\n";
    521             # }
    522             # }
    523 0           $xml .= "\t\n";
    524 0           return $xml;
    525             }
    526              
    527             =head2 to_HTML (deprecated)
    528              
    529             Same as to_html(), provided for backward compatibility.
    530              
    531             =cut
    532              
    533             sub to_HTML {
    534 0     0 1   return to_html(@_);
    535             }
    536              
    537             =head2 to_html
    538              
    539             return the media info as an HTML encoded string.
    540              
    541             $xml = $media->to_html();
    542              
    543             =cut
    544              
    545             sub to_html
    546             {
    547 0     0 1   my $self = shift;
    548 0 0         return undef unless(defined($self->{ID}));
    549 0           my $host = $self->host ;
    550 0 0         $host = "not reachable" unless($host);
    551 0           return "
  • current host for $self->{ID} is $host

  • \n";
    552             }
    553              
    554             =head1 AUTHOR
    555              
    556             DUPUIS Arnaud, C<< >>
    557              
    558             =head1 BUGS
    559              
    560             Please report any bugs or feature requests to
    561             C, or through the web interface at
    562             L.
    563             I will be notified, and then you'll automatically be notified of progress on
    564             your bug as I make changes.
    565              
    566             =head1 SUPPORT
    567              
    568             You can find documentation for this module with the perldoc command.
    569              
    570             perldoc Slackware::Slackget::Media
    571              
    572              
    573             You can also look for information at:
    574              
    575             =over 4
    576              
    577             =item * Infinity Perl website
    578              
    579             L
    580              
    581             =item * slack-get specific website
    582              
    583             L
    584              
    585             =item * RT: CPAN's request tracker
    586              
    587             L
    588              
    589             =item * AnnoCPAN: Annotated CPAN documentation
    590              
    591             L
    592              
    593             =item * CPAN Ratings
    594              
    595             L
    596              
    597             =item * Search CPAN
    598              
    599             L
    600              
    601             =back
    602              
    603             =head1 ACKNOWLEDGEMENTS
    604              
    605             Thanks to Bertrand Dupuis (yes my brother) for his contribution to the documentation.
    606              
    607             =head1 COPYRIGHT & LICENSE
    608              
    609             Copyright 2005 DUPUIS Arnaud, All Rights Reserved.
    610              
    611             This program is free software; you can redistribute it and/or modify it
    612             under the same terms as Perl itself.
    613              
    614             =cut
    615              
    616             1; # End of Slackware::Slackget::Media