streeme /lib/task/mediafillTask.class.php

Language PHP Lines 93
MD5 Hash 7de2d0d713936bbd833a4b39b28d8b02 Estimated Cost $1,905 (why?)
Repository http://streeme.googlecode.com/svn/trunk/ View Raw File
 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
<?php
class mediafillTask extends sfBaseTask
{
  protected function configure()
  {
    $this->addOptions(array(
      new sfCommandOption( 'application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'client' ),
      new sfCommandOption( 'env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'test' ),
      new sfCommandOption( 'connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine' ),
      new sfCommandOption( 'count', null, sfCommandOption::PARAMETER_REQUIRED, 'The number of fake records to add', 7500),
      new sfCommandOption( 'max_album_size', null, sfCommandOption::PARAMETER_REQUIRED, 'The largest number of songs to add to an album', 24),
      new sfCommandOption('no-confirmation', null, sfCommandOption::PARAMETER_NONE, 'proceed without prompts')
    ));

    $this->namespace        = '';
    $this->name             = 'media-fill';
    $this->briefDescription = 'Fill the library with records';
    $this->detailedDescription = <<<EOF
The [media-fill|INFO] task will add a specified number of records in a semi
random order to help benchmark Streeme's song list performance on small and
large music libraries.

Types:
  --count=[int]         - The number of records to add
  --max_album_size=[int]- the maximum number of member song in an album

Call it with:

  [php symfony media-fill --count="..."|INFO]
EOF;
  }

  protected function execute( $arguments = array(), $options = array() )
  {
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    $environment = $this->configuration instanceof sfApplicationConfiguration ? $this->configuration->getEnvironment() : 'all';
    $connection = $databaseManager->getDatabase($options['connection'])->getConnection();
    $media_scanner = new MediaScan();
    
    if (
      !$options['no-confirmation']
      &&
      !$this->askConfirmation(array_merge(
        array(sprintf('This command will append data in the following "%s" connection(s):', $environment), ''),
        array('', 'Are you sure you want to proceed? (y/N)')
      ), 'QUESTION_LARGE', false)
    )
    {
      exit;
    }
    
    $counter=1;
    for( $i=0; $i < (int) $options['count']; $i++ )
    {
      if($counter >= $group_size)
      {
        $artist_name = substr( sha1(uniqid().mt_rand(29098,209120392093029)), 0, mt_rand(4, 12));
        $album_name = substr( sha1(uniqid().mt_rand(3048,209120393)), 0, mt_rand(6,16));
        $genre_name = substr( sha1(uniqid().mt_rand(30203,2091029309213)), 0, mt_rand(10,12));
        $label_name = substr( sha1(uniqid().mt_rand(30203,2091029309213)), 0, mt_rand(10,12));
        $group_size = mt_rand(1,(int) $options['max_album_size']);
        $bitrate = mt_rand(48,512);
        $yearpublished = mt_rand(1900,2069);
        $counter = 1;
      }
      
      $song_name = substr( sha1(uniqid().mt_rand(360,20912039209213)), 0, mt_rand(10,20));
      $song_array = array();
      @$song_array[ 'artist_name' ]      = $artist_name;
      @$song_array[ 'album_name' ]       = $album_name;
      @$song_array[ 'song_name' ]        = $song_name;
      @$song_array[ 'song_length' ]      = mt_rand(0,60) . ':' . mt_rand(11,60);
      @$song_array[ 'accurate_length' ]  = mt_rand(1,30409092);
      @$song_array[ 'genre_name' ]       = $genre_name;
      @$song_array[ 'filesize' ]         = mt_rand(256, 3141592653);
      @$song_array[ 'bitrate' ]          = $bitrate;
      @$song_array[ 'yearpublished' ]    = $yearpublished;
      @$song_array[ 'tracknumber']       = $counter;
      @$song_array[ 'label' ]            = $label_name;
      @$song_array[ 'mtime' ]            = mt_rand(1, 3141592653);
      @$song_array[ 'atime' ]            = mt_rand(1, 3141592653);
      @$song_array[ 'filename' ]         = 'file://localhost/home/user/' . $artist_name . '/' . $album_name . '/' . $song_name . '.mp3';
      
      $media_scanner->add_song( $song_array );
      
      $counter++;
    }
    echo sprintf('Filled Database %s with %d record%s', $options['env'], $i, ($i==1) ? '' : 's' );
    echo "\r\n";
  }
}
Back to Top