aboutsummaryrefslogtreecommitdiff
path: root/lib/sfIsisImporterBase.class.php
blob: 62e7ead6191c443cf6b31a27e977664fff42ee65 (plain)
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
<?php

/**
 * IsisImporterBase: provides base ISIS import methods for importing data into
 * a Symfony project.
 *
 * Importing can be done either on actions or tasks.
 */
class sfIsisImporterBase extends IsisConnector
{
  /**
   * @var string $loglevel Log level.
   */
  var $loglevel = 'info';

  /**
   * @var int $processed Number of processed entries.
   */
  var $processed = 0;

  /**
   * Constructor.
   */
  public function __construct($loglevel = 'info')
  {
    parent::__construct();
    $this->loglevel = $loglevel;
  }

  /**
   * Create an ISIS configuration for a given database.
   *
   * @param  string $database Database name
   * @return array            Basic configuration
   */
  public function config($database)
  {
    // Default cinisis config file.
    $config = $this->isis->file();

    if (file_exists($config))
    {
      $this->log("Using Cinisis config file $config.");
      $config = sfYaml::load($config);
    }
    else {
      // Default configuration.
      $this->log("Cinisis config file not found, building configuration.");
      $config = array(
        'implementation' => 'BiblioIsis',
        );
    }

    $config['database'] = $database;
    return $config;
  }

  /**
   * List available ISIS databases.
   *
   * @return array Available databases
   */
  public function databases()
  {
    foreach (glob(sfConfig::get('sf_lib_dir') .'/cinisis/schemas/*.yaml') as $file)
    {
      $databases[] = basename($file, '.yaml');
    }

    return $databases;
  }

  /**
   * Log dispatcher.
   *
   * @param string $message Log message
   * @param string $level   Log level
   */
  public function log($message, $level = 'info')
  {
    // Available log levels ordered by verbosity.
    $levels = array_flip(array('fatal', 'info', 'warn', 'error', 'debug'));

    // Log level checking.
    if (array_search($level, $levels) === FALSE)
    {
      $this->log("Invalid log level $level.", 'error');
      return;
    }
    elseif ($levels[$level] > $levels[$this->loglevel])
    {
      return;
    }

    // Dispatch.
    if ($this->section == 'action')
    {
      $this->caller->logMessage($message, $level);
    }
    else
    {
      $this->caller->logSection('isisImporter', "[$level] $message");
    }
  }

  /**
   * Progress notifier.
   *
   * @param int $n Row number
   */
  public function progress($n)
  {
    $this->processed = $n;

    if ($this->section == 'action')
    {
      $this->caller->output .= "Saved item $n\n";
    }
    else
    {
      // Progress bar is just shown if loglevel is 'fatal'.
      if ($this->loglevel == 'fatal')
      {
        $this->caller->progressBar($n, $this->entries);
      }
    }
  }

  /**
   * Guess a method name from a type.
   *
   * @param  string $type Mapping type
   * @return string       Method name
   */
  static function methodName($type)
  {
    return 'import'. ucfirst($type);
  }

  /**
   * Get the model foreign table id.
   *
   * @param  object $model Model
   * @return string        Model table id
   */
  static function getModelId($model) {
    return sfInflector::underscore(get_class($model)) .'_id';
  }

  /**
   * Get the relation foreign table id.
   *
   * @param  string $model Relation name
   * @return string        Relation table id
   */
  static function getRelationId($relation) {
    return sfInflector::underscore($relation) .'_id';
  }

  /**
   * Get the model and relation tablename.
   *
   * @param  object $model Model
   * @param  string $model Relation name
   * @return string        Relation table name
   */
  static function getModelRelation($model, $relation)
  {
    return sfInflector::camelize(self::getModelName($model)) . sfInflector::camelize($relation);
  }

  /**
   * Get the entity name from a subfield.
   *
   * @param  string $subfield Subfield name
   * @return string           Genre name
   */
  static function entityName($subfield)
  {
    return ucfirst($subfield);
  }  

  /**
   * Get the model name.
   *
   * @param  object $model Model
   * @return string        Model name
   */
  static function getModelName($model)
  {
    return get_class($model);
  }

  /**
   * After import procedure.
   */
  public function afterImport() {
    // Output ISIS log messages.
    if (is_array($this->isis->db->log))
    {
      foreach ($this->isis->db->log as $log)
      {
        $this->log("[isis] $log");
      }
    }

    $this->log("Finished mass import procedure.");
    $this->log("Total entries processed: $this->processed.");
  }
}