aboutsummaryrefslogtreecommitdiff
path: root/lib/sfIsisImporterManager.class.php
blob: e615f0c59634b4682557231ea8ee3e9449a768c9 (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
<?php

/**
 * Manager class for importing ISIS data into a Symfony project.
 * Importing can be done either on actions or tasks.
 */
class sfIsisImporterManager 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();

    // Get a logger instance.
    $this->logger = sfIsisImporterLog::getInstance($loglevel);
  }

  /**
   * Log dispatcher.
   *
   * @param string $message Log message
   * @param string $level   Log level
   */
  public function log($message, $level = 'info')
  {
    $this->logger->log($message, $level);
  }

  /**
   * Progress notifier dispatcher.
   *
   * @param int $entry Entry number
   */
  public function progress($entry)
  {
    $this->processed = $this->logger->progress($entry, $this->entries);
  }

  /**
   * 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()
  {
    $databases = array();

    foreach (glob(sfConfig::get('sf_lib_dir') ."/cinisis/schemas/*.yaml") as $file)
    {
      $database = basename($file, '.yaml');
      $options  = sfYaml::load($file);
      
      if (isset($options['import']['order']))
      {
        $databases[$options['import']['order']] = $database;
      }
      else
      {
        $this->log("Database config $database has no order option, skipping.");
      }
    }

    ksort($databases);
    return $databases;
  }

  /**
   * 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.", 'notice');
    $this->log("Total entries processed: $this->processed.", 'notice');
  }

  /**
   * Execute a mass import of ISIS database entries. This function reads
   * each database entry and dispatch each field read to a custom
   * dispatch function.
   *
   * @param object $caller  Caller object
   * @param string $section Caller section (whether an action or task)
   * @param int    $entries Number of entries to import (defaults to all)
   */
  public function massImport($caller, $section, $entries = NULL)
  {
    // Additional logging settings.
    $this->logger->setCaller($caller);
    $this->logger->setSection($section);

    foreach ($this->databases() as $database)
    {
      // Open database.
      $importer = $this->newImporter($database);

      if ($importer)
      {
        // Determine base model and max entries.
        $this->log('Starting mass import procedure for config '. $database .'.', 'notice');
        $base_model    = $importer->format['import']['base_model'];
        $this->entries = ($entries != NULL && $entries <= $importer->entries) ? $entries : $importer->entries;

        if ($base_model) {
          for ($entry = 1; $entry <= $this->entries; $entry++)
          {
            $importer->addEntry($base_model, $entry);
            $this->progress($entry);
          }
        }

        $this->afterImport();
      }
    }
  }

  /**
   * Setup a new importer.
   *
   * @param  string $database Database name
   * @return mixed            Importer object or false
   */
  public function newImporter($database)
  {
    $class = 'IsisImporter'. ucfirst($database);
    if (class_exists($class))
    {
      return new $class($this->config($database));
    }

    return false;
  }
}