You are here

private function BrowscapImporter::processData in Browscap 8.3

Processes Browscap data.

The purpose of this function is to perform the queries on the {browscap} table as a transaction. This vastly improves performance with database engines such as InnoDB and ensures that queries will work while new data is being imported.

Parameters

string $browscap_data: Unparsed Browscap data.

Return value

int A code indicating the result:

  • BROWSCAP_IMPORT_OK: New data was imported.
  • BROWSCAP_IMPORT_DATA_ERROR: The data could not be downloaded or parsed.
1 call to BrowscapImporter::processData()
BrowscapImporter::import in src/BrowscapImporter.php
Helper function to update the browscap data.

File

src/BrowscapImporter.php, line 152

Class

BrowscapImporter
Class BrowscapImporter.

Namespace

Drupal\browscap

Code

private function processData(&$browscap_data) {

  // Start a transaction. The variable is unused. That's on purpose.
  $transaction = Database::getConnection()
    ->startTransaction();

  // Delete all data from database.
  Database::getConnection()
    ->delete('browscap')
    ->execute();

  // Skip the header division.
  $header_division = $this
    ->getNextIniDivision($browscap_data);

  // Assert that header division less than length of entire INI string.
  if (strlen($header_division) >= strlen($browscap_data)) {
    return static::BROWSCAP_IMPORT_DATA_ERROR;
  }

  // Skip the version division.
  $version_divison = $this
    ->getNextIniDivision($browscap_data);

  // Assert that Version section in division string.
  if (strpos($version_divison, "Browscap Version") === FALSE) {
    return static::BROWSCAP_IMPORT_DATA_ERROR;
  }

  // Get default properties division.
  // Assumption: The default properties division is the third division.
  $default_properties_division = $this
    ->getNextIniDivision($browscap_data);

  // Assert that DefaultProperties section in division string.
  if (strpos($default_properties_division, "[DefaultProperties]") === FALSE) {
    return static::BROWSCAP_IMPORT_DATA_ERROR;
  }

  // Parse and save remaining divisions.
  while ($division = $this
    ->getNextIniDivision($browscap_data)) {

    // The division is concatenated with the default properties division
    // because each division has at least one section that inherits properties
    // from the default properties section.
    $divisions = $default_properties_division . $division;
    $parsed_divisions = $this
      ->parseData($divisions);
    if (!$parsed_divisions) {

      // There was an error parsing the data.
      return static::BROWSCAP_IMPORT_DATA_ERROR;
    }
    $this
      ->saveParsedData($parsed_divisions);
  }
  return static::BROWSCAP_IMPORT_OK;
}