File Coverage

blib/lib/Slackware/Slackget/Media.pm
Criterion Covered Total %
statement 25 135 18.5
branch 2 54 3.7
condition 0 24 0.0
subroutine 10 22 45.4
pod 18 18 100.0
total 55 253 21.7


line stmt bran cond sub pod time code
1             package Slackware::Slackget::Media;
2              
3 5     5   100142 use warnings;
  5         8  
  5         140  
4 5     5   19 use strict;
  5         7  
  5         6746  
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.8
13              
14             =cut
15              
16             our $VERSION = '0.9.8';
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->setValue('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 256 my ($class,$id,%args) = @_ ;
73 2 50       7 return undef unless(defined($id));
74 2         3 my $self={};
75 2         7 $self->{ID} = $id ;
76 2         10 $self->{DATA} = {%args};
77 2         8 $self->{DATA}->{hosts}->{old} = [] ;
78 2         3 bless($self,$class);
79 2 50       13 $self->set_value('host',$args{'update-repository'}->{'faster'}) if(defined($args{'update-repository'}->{'faster'}));
80 2         6 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 7 my ($self,$key,$value) = @_ ;
97             # print "Setting $key=$value for $self\n";
98 4         13 $self->{DATA}->{$key} = $value ;
99 4         9 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 3 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 622 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 618 my ($self,$key) = @_ ;
132 4         15 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 0         0 $self->setValue('description','no description for this media.') ;
148 0 0       0 $self->setValue('description',$xml->{'description'}) if(defined($xml->{'description'}));
149 0         0 $self->setValue('web-link','no website for this media.');
150 0 0       0 $self->setValue('web-link',$xml->{'web-link'}) if(defined($xml->{'web-link'}));
151 0 0       0 if(defined($xml->{'files'}))
152             {
153 0         0 $self->setValue('filelist',$xml->{'files'}->{'filelist'});
154 0         0 $self->setValue('packages',$xml->{'files'}->{'packages'});
155 0         0 $self->setValue('checksums',$xml->{'files'}->{'checksums'});
156             }
157             else
158             {
159 0         0 $self->setValue('filelist','FILELIST.TXT');
160 0         0 $self->setValue('packages','PACKAGES.TXT');
161 0         0 $self->setValue('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->setValue('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 next_host
245              
246             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.
247              
248             my $host = $media->next_host ;
249              
250             return undef if no new host is found
251              
252             =cut
253              
254             sub next_host
255             {
256 0     0 1 0 my $self = shift;
257 0         0 push @{$self->{DATA}->{hosts}->{old}}, $self->host;
  0         0  
258 0         0 $self->{DATA}->{host} = undef ;
259 0 0       0 if(defined(my $host = shift(@{$self->{DATA}->{hosts}->{fast}})))
  0         0  
260             {
261 0         0 $self->{DATA}->{host} = $host ;
262             }
263             else
264             {
265 0         0 warn "[Slackware::Slackget::Media] no more host in the 'fast' category for update source '$self->{ID}'\n";
266 0 0       0 if(defined(my $host = shift(@{$self->{DATA}->{hosts}->{slow}})))
  0         0  
267             {
268 0         0 $self->{DATA}->{host} = $host ;
269             }
270             else
271             {
272 0         0 warn "[Slackware::Slackget::Media] no more host in the 'slow' category for update source '$self->{ID}'\n";
273 0         0 return undef;
274             }
275             }
276 0         0 return $self->host ;
277             }
278              
279             =head2 print_info
280              
281             This method is used to print the content of the current Media object.
282              
283             $media->print_info ;
284              
285             =cut
286              
287             sub print_info
288             {
289 0     0 1 0 my $self = shift ;
290 0         0 print "Information for the '$self->{ID}' update source :\n";
291 0 0       0 if(defined($self->getValue('description')))
292             {
293 0         0 print "\tDescription: ".$self->getValue('description')."\n";
294             }
295             else
296             {
297 0         0 print "\tDescription: no descrition found\n";
298             }
299 0 0       0 if(defined($self->getValue('web-link')))
300             {
301 0         0 print "\tWeb site: ".$self->getValue('web-link')."\n";
302             }
303             else
304             {
305 0         0 print "\tWeb site: no link found\n";
306             }
307 0 0       0 if(defined($self->getValue('host')))
308             {
309 0         0 print "\tCurrent host: ".$self->getValue('host')."\n";
310             }
311             else
312             {
313 0         0 print "\tCurrent host: no current host configured !\n";
314             }
315             }
316              
317             =head2 to_string
318              
319             return the same information that the print_info() method as a string.
320              
321             my $string = $media->to_string ;
322              
323             =cut
324              
325             sub to_string
326             {
327 0     0 1 0 my $self = shift ;
328 0         0 my $str = "Information for the '$self->{ID}' update source :\n";
329 0 0       0 if(defined($self->getValue('description'))){
330 0         0 $str .= "\tDescription: ".$self->getValue('description')."\n";
331             }
332             else
333             {
334 0         0 $str .= "\tDescription: no descrition found\n";
335             }
336 0 0       0 if(defined($self->getValue('web-link'))){
337 0         0 $str .= "\tWeb site: ".$self->getValue('web-link')."\n";
338             }
339             else
340             {
341 0         0 $str .= "\tWeb site: no link found\n";
342             }
343 0 0       0 if(defined($self->getValue('host'))){
344 0         0 $str .= "\tCurrent host: ".$self->getValue('host')."\n";
345             }
346             else
347             {
348 0         0 $str .= "\tCurrent host: no current host configured !\n";
349             }
350 0         0 return $str ;
351             }
352              
353             =head1 ACCESSORS
354              
355             Some accessors for the current object.
356              
357             =cut
358              
359             =head2 host
360              
361             return the current host :
362              
363             my $host = $media->host
364              
365             =cut
366              
367             sub host {
368 2     2 1 8 return $_[0]->{DATA}->{host};
369             }
370              
371             =head2 description
372              
373             return the description of the media.
374              
375             my $descr = $media->description ;
376              
377             =cut
378              
379             sub description {
380 0     0 1 0 return $_[0]->{DATA}->{description};
381             }
382              
383             =head2 url
384              
385             return the URL of the website for the media.
386              
387             system("$config->{common}->{'default-browser'} $media->url &");
388              
389             =cut
390              
391             sub url {
392 2     2 1 7 return $_[0]->{DATA}->{'web-link'};
393             }
394              
395             =head2 shortname
396              
397             Return the shortname of the media. The shortname is the name of the id attribute of the media tag in medias.xml => <media id="the_shortname">
398              
399             my $id = $media->shortname ;
400              
401             =cut
402              
403             sub shortname {
404 2     2 1 10 return $_[0]->{ID};
405             }
406              
407              
408              
409             =head2 set_fast_medias_array
410              
411             ...not yet implemented...
412              
413             =cut
414              
415 0     0 1   sub set_fast_medias_array {1;}
416              
417             =head1 FORMATTED OUTPUT
418              
419             Different methods to properly output a media.
420              
421             =cut
422              
423             =head2 to_XML (deprecated)
424              
425             Same as to_xml(), provided for backward compatibility.
426              
427             =cut
428              
429             sub to_XML {
430 0     0 1   return to_xml(@_);
431             }
432              
433             =head2 to_xml
434              
435             return the media info as an XML encoded string.
436              
437             $xml = $media->to_xml();
438              
439             =cut
440              
441             sub to_xml
442             {
443 0     0 1   my $self = shift;
444 0 0         return undef unless(defined($self->{ID}));
445 0 0         if($self->{DATA}->{hosts}->{old})
446             {
447 0           $self->{DATA}->{hosts}->{slow} = [@{$self->{DATA}->{hosts}->{slow}},@{$self->{DATA}->{hosts}->{old}}] ;
  0            
  0            
448 0           $self->{DATA}->{hosts}->{old} = undef;
449 0           delete($self->{DATA}->{hosts}->{old});
450             }
451            
452 0           my $xml = "\t<media id=\"$self->{ID}\">\n";
453 0           $xml .= "\t\t<web-link>".$self->url."</web-link>\n";
454 0           $xml .= "\t\t<description>".$self->description."</description>\n";
455 0           $xml .= "\t\t<update-repository>\n";
456 0           $xml .= "\t\t\t<faster>".$self->host."</faster>\n";
457 0 0 0       if(defined($self->{DATA}->{hosts}->{fast}) && defined($self->{DATA}->{hosts}->{fast}->[0]))
458             {
459 0           $xml .= "\t\t\t\t<fast>\n";
460 0           foreach my $serv (@{$self->{DATA}->{hosts}->{fast}})
  0            
461             {
462 0           $xml .= "\t\t\t\t\t<li>$serv</li>\n";
463             }
464 0           $xml .= "\t\t\t\t</fast>\n";
465             }
466 0 0 0       if(defined($self->{DATA}->{hosts}->{slow}) && defined($self->{DATA}->{hosts}->{slow}->[0]))
467             {
468 0           $xml .= "\t\t\t\t<slow>\n";
469 0           foreach my $serv (@{$self->{DATA}->{hosts}->{slow}})
  0            
470             {
471 0           $xml .= "\t\t\t\t\t<li>$serv</li>\n";
472             }
473 0           $xml .= "\t\t\t\t</slow>\n";
474             }
475 0           $xml .= "\t\t</update-repository>\n";
476             # foreach my $key (keys(%{$self->{DATA}})){
477             # if($key eq 'update-repository')
478             # {
479             # foreach my $key2 (keys(%{$self->{DATA}->{'update-repository'}}))
480             # {
481             # 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' ) {
482             # $xml .= "\t\t<$key2>\n";
483             # foreach (@{$self->{DATA}->{'update-repository'}->{$key2}->{li}}){
484             # $xml .= "\t\t\t<li>$_</li>\n";
485             # }
486             # $xml .= "\t\t</$key2>\n";
487             # }
488             # }
489             # }
490             # else
491             # {
492             # $xml .= "\t\t<$key>$self->{DATA}->{$key}</$key>\n";
493             # }
494             # }
495 0           $xml .= "\t</media>\n";
496 0           return $xml;
497             }
498              
499             =head2 to_HTML (deprecated)
500              
501             Same as to_html(), provided for backward compatibility.
502              
503             =cut
504              
505             sub to_HTML {
506 0     0 1   return to_html(@_);
507             }
508              
509             =head2 to_html
510              
511             return the media info as an HTML encoded string.
512              
513             $xml = $media->to_html();
514              
515             =cut
516              
517             sub to_html
518             {
519 0     0 1   my $self = shift;
520 0 0         return undef unless(defined($self->{ID}));
521 0           my $host = $self->host ;
522 0 0         $host = "<font color='red'>not reachable</font>" unless($host);
523 0           return "<li>current host for <a href='".$self->url."' target='_blank' title='".$self->description."'>$self->{ID}</a> is $host</li><br/>\n";
524             }
525              
526             =head1 AUTHOR
527              
528             DUPUIS Arnaud, C<< <a.dupuis@infinityperl.org> >>
529              
530             =head1 BUGS
531              
532             Please report any bugs or feature requests to
533             C<bug-Slackware-Slackget@rt.cpan.org>, or through the web interface at
534             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Slackware-Slackget>.
535             I will be notified, and then you'll automatically be notified of progress on
536             your bug as I make changes.
537              
538             =head1 SUPPORT
539              
540             You can find documentation for this module with the perldoc command.
541              
542             perldoc Slackware::Slackget::Media
543              
544              
545             You can also look for information at:
546              
547             =over 4
548              
549             =item * Infinity Perl website
550              
551             L<http://www.infinityperl.org/category/slack-get>
552              
553             =item * slack-get specific website
554              
555             L<http://slackget.infinityperl.org>
556              
557             =item * RT: CPAN's request tracker
558              
559             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Slackware-Slackget>
560              
561             =item * AnnoCPAN: Annotated CPAN documentation
562              
563             L<http://annocpan.org/dist/Slackware-Slackget>
564              
565             =item * CPAN Ratings
566              
567             L<http://cpanratings.perl.org/d/Slackware-Slackget>
568              
569             =item * Search CPAN
570              
571             L<http://search.cpan.org/dist/Slackware-Slackget>
572              
573             =back
574              
575             =head1 ACKNOWLEDGEMENTS
576              
577             Thanks to Bertrand Dupuis (yes my brother) for his contribution to the documentation.
578              
579             =head1 COPYRIGHT & LICENSE
580              
581             Copyright 2005 DUPUIS Arnaud, All Rights Reserved.
582              
583             This program is free software; you can redistribute it and/or modify it
584             under the same terms as Perl itself.
585              
586             =cut
587              
588             1; # End of Slackware::Slackget::Media