diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | apk_browser.info | 7 | ||||
-rw-r--r-- | apk_browser.install | 205 | ||||
-rw-r--r-- | apk_browser.module | 327 | ||||
-rw-r--r-- | images/add.png | bin | 0 -> 733 bytes | |||
-rw-r--r-- | images/delete.png | bin | 0 -> 715 bytes | |||
-rw-r--r-- | images/page_white_edit.png | bin | 0 -> 618 bytes | |||
-rw-r--r-- | nbproject/project.properties | 7 | ||||
-rw-r--r-- | nbproject/project.xml | 9 |
9 files changed, 556 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..14bc68c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/nbproject/private/
\ No newline at end of file diff --git a/apk_browser.info b/apk_browser.info new file mode 100644 index 0000000..a744771 --- /dev/null +++ b/apk_browser.info @@ -0,0 +1,7 @@ +name = "APK browser" +description = "Package browser for Alpine Linux" +version = "7.x-1.x-dev" +core = "7.x" +dependencies[] = taxonomy +dependencies[] = field_ui +configure = "admin/config/apk_browser/settings"
\ No newline at end of file diff --git a/apk_browser.install b/apk_browser.install new file mode 100644 index 0000000..0786389 --- /dev/null +++ b/apk_browser.install @@ -0,0 +1,205 @@ +<?php + +function apk_browser_install() { + /* + * Alpine packages are stored as nodes. + * To make sure we have content type we + * create it with hook install so its a + * system content type which cannot be + * deleted. + */ + // use get_t() to get the name of our localization function for translation + // during install, when t() is not available. + $t = get_t(); + //basic new node settings + $node_fields = array( + 'type' => 'apk', //machine name + 'name' => 'Alpine package', // human name + 'base' => 'node_content', //api callback + 'description' => $t('Content type to store Alpine Linux packages'), + 'title_label' => $t('Package'), + //'has_title' => FALSE + ); + //set missing defaults and save the node type + $content_type = node_type_set_defaults($node_fields); + node_type_save($content_type); + + /* + * Create all the fields and instances we are adding to our content type. + */ + foreach (_apk_browser_field_vars() as $name => $fvars) { + $cardinality = ($name == 'apk_depends') ? '-1' : '1'; + if (!field_info_field($name)) { + $field = array( + 'field_name' => $name, + 'cardinality' => $cardinality, + 'type' => 'text', + 'settings' => array( + 'max_length' => $fvars['length'], + ), + + ); + field_create_field($field); + } + // now all instances + if (!field_info_instance('node', $name, 'apk')) { + $instance = array( + 'field_name' => $name, + 'label' => $fvars['label'], + 'type' => 'text', + 'entity_type' => 'node', + 'bundle' => 'apk', + 'widget' => array( + 'type' => 'text_textfield', + ), + 'display' => array( + 'default' => array( + 'label' => 'inline' + ) + ) + ); + field_create_instance($instance); + } + } + + /* + * Create taxonamy vocab + * create table and asign it + */ + $vnames = array( + 'repo' => $t('Repository') , + 'arch' => $t('Architecture') + ); + foreach ($vnames as $vname => $desc) { + $fieldname = 'taxonomy_apk_' . $vname; + $machinename = 'apk_' . $vname; + $vedit = array( + 'name' => $desc, + 'description' => $t('@desc taxonomy', array('@desc' => $desc)), + 'machine_name' => $machinename + ); + if (taxonomy_vocabulary_save((object) $vedit)) { + if (!field_info_field($fieldname)) { + $field = array( + 'field_name' => $fieldname, + 'type' => 'taxonomy_term_reference', + //sets the number of terms which can be selected + 'cardinality' => '1', + 'settings' => array( + 'allowed_values' => array( + array( + 'vocabulary' => $machinename, + 'parent' => 0, + ), + ), + ), + ); + field_create_field($field); + } + if (!field_info_instance('node', $fieldname, 'taxonomy_term_reference')) { + $instance = array( + 'field_name' => $fieldname, + 'entity_type' => 'node', + 'label' => $desc, + 'bundle' => 'apk', + 'required' => TRUE, + 'widget' => array( + 'type' => 'options_select', + ) + ); + field_create_instance($instance); + } + } + } +} + +//cleanup database, removing all apk entries +function apk_browser_uninstall() { + $nfields = array_keys(_apk_browser_field_vars()); + /* delete all apk nodes. + * disabled because too slow + * $nodes = db_select('node') + * ->fields('node', array('nid')) + * ->condition('type', 'apk') + * ->execute() + * ->fetchAll(); + * foreach ($nodes as $node) { + * node_delete($node->nid); + * } + */ + //delete the content type + node_type_delete('apk'); + //delete the apk node fields + foreach ($nfields as $nfield) { + field_delete_field($nfield); + } + //delete all apk related instaces + $instances = field_info_instances('node', 'apk'); + foreach ($instances as $instance) { + field_delete_instance($instance, TRUE); + } + field_delete_field('taxonomy_apk_repo'); + field_delete_field('taxonomy_apk_arch'); + //delete taxonomy + $vocabulary = taxonomy_vocabulary_machine_name_load('apk_arch'); + if ($vocabulary) { + taxonomy_vocabulary_delete($vocabulary->vid); + } + $vocabulary = taxonomy_vocabulary_machine_name_load('apk_repo'); + if ($vocabulary) { + taxonomy_vocabulary_delete($vocabulary->vid); + } + db_delete('variable')->condition('name', 'apk_%%', 'LIKE')->execute(); +} + +/* + * functions which return apk browser + * structure and variables will be used + * for install and uninstall + */ + +function _apk_browser_field_vars() { + $t = get_t(); + //return array of fields variables + return array( +/* + * use node title as package name + 'apk_name' => array( + 'label' => $t('Package'), + 'length' => '255' + ), + */ + 'apk_checksum' => array( + 'label' => $t('Checksum'), + 'length' => '255' + ), + 'apk_version' => array( + 'label' => $t('Version'), + 'length' => '60' + ), + 'apk_size' => array( + 'label' => $t('Size'), + 'length' => '255' + ), + 'apk_isize' => array( + 'label' => $t('Installed size'), + 'length' => '255' + ), + 'apk_description' => array( + 'label' => $t('Description'), + 'length' => '1000' + ), + 'apk_url' => array( + 'label' => $t('URL'), + 'length' => '255' + ), + 'apk_license' => array( + 'label' => $t('License'), + 'length' => '25' + ), + 'apk_depends' => array( + 'label' => $t('Dependencies'), + 'length' => '2000' + ) + ); +} diff --git a/apk_browser.module b/apk_browser.module new file mode 100644 index 0000000..5012cbd --- /dev/null +++ b/apk_browser.module @@ -0,0 +1,327 @@ +<?php + +function apk_browser_menu() { + //Menu item to test new functions. + $items['apk_browser'] = array( + 'title' => 'Import APK', + 'description' => 'Import APK packages', + 'page callback' => 'apk_browser_import_apk', + 'access arguments' => array('access apk browser'), + ); + $items['admin/config/apk_browser'] = array( + 'title' => 'Package browser', + 'description' => 'Configure package browser', + 'page callback' => 'system_admin_menu_block_page', + 'access arguments' => array('access apk browser'), + //system_admin_menu_block_page needs admin inc + 'file' => 'system.admin.inc', + 'file path' => drupal_get_path('module', 'system'), + ); + $items['admin/config/apk_browser/settings'] = array( + 'title' => 'Settings', + 'description' => 'Configure global APK browser settings', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('apk_browser_settings'), + 'access arguments' => array('access apk browser'), + ); + $items['admin/config/apk_browser/repositories/add'] = array( + 'title' => 'Add repository', + 'description' => 'Add repositoriy', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('apk_browser_repository_edit_form'), + 'access arguments' => array('access apk browser'), + ); + $items['admin/config/apk_browser/repositories'] = array( + 'title' => 'Repositories', + 'description' => 'Configure APK repositories', + 'page callback' => 'apk_browser_repositories_list', + 'access arguments' => array('access apk browser'), + ); + return $items; +} + +function apk_browser_permission() { + return array( + 'access apk browser' => array( + 'title' => t('Access APK browser'), + 'description' => t('Access permissions for APK browser'), + ), + ); +} + +function apk_browser_settings($form) { + $form['settings'] = array( + '#type' => 'fieldset', + '#title' => t('APK browser settings'), + ); + $form['settings']['apk_import_debug'] = array( + '#type' => 'checkbox', + '#title' => t('Import debugging'), + '#description' => t('Write import debugging to watchdog'), + '#default_value' => variable_get('apk_import_debug', '0') + ); + return system_settings_form($form); +} + +/* + * function apk_browser_settings_validate($form, &$form_state) { + * if (!valid_url($form_state['values']['apk_repo_url'], TRUE)) { + * form_set_error('apk_repo_url', t('Please specify a valid URL!')); + * } + * } + */ + +function apk_browser_repository_edit_form($form, &$form_state) { + $repo = taxonomy_vocabulary_machine_name_load('apk_repo'); + $repo_tree = taxonomy_get_tree($repo->vid); + $arch = taxonomy_vocabulary_machine_name_load('apk_arch'); + $arch_tree = taxonomy_get_tree($arch->vid); + if (empty($arch_tree) || empty($repo_tree)) { + $form['warning'] = array( + '#type' => 'item', + '#markup' => '<p>Please add terms to you your repositories and architectures</p>' + . l('Manage Taxonomy', 'admin/structure/taxonomy') + ); + } else { + $form['settings'] = array( + '#type' => 'fieldset', + '#title' => t('Repository settings'), + ); + $form['settings']['apk_repo_url'] = array( + '#type' => 'textfield', + '#title' => t('Repository URL'), + '#size' => '100', + '#default_value' => '', + '#description' => t('Link to the APKINDEX.tar.gz file inside the repository'), + '#required' => TRUE, + ); + foreach ($repo_tree as $key => $term) { + $repo_options[$term->tid] = $term->name; + } + //generate a form item to select terms + $form['settings']['repo'] = array( + '#type' => 'select', + '#options' => $repo_options, + '#title' => 'Select repository', + '#description' => t('Please make sure you select the correct repository. + Preventing to do so will break package listing') + ); + foreach ($arch_tree as $key => $term) { + $arch_options[$term->tid] = $term->name; + } + $form['settings']['arch'] = array( + '#type' => 'select', + '#options' => $arch_options, + '#title' => 'Select architecture', + '#description' => t('Please make sure you select the correct architecture. + Preventing to dox so will break package listing') + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Add repository') + ); + } + return $form; +} + +function apk_browser_repository_edit_form_validate($form, &$form_state) { + if (!valid_url($form_state['values']['apk_repo_url'], TRUE)) { + form_set_error('apk_repo_url', t('Please specify a valid URL!')); + } + foreach (variable_get('apk_repositories', array()) as $repo) { + if ($form_state['values']['apk_repo_url'] == $repo['url']) { + form_set_error('apk_repo_url', t('You are trying to add a double url')); + } + } +} + +function apk_browser_repository_edit_form_submit($form, &$form_state) { + $repos[] = array( + 'url' => $form_state['values']['apk_repo_url'], + 'arch' => $form_state['values']['arch'], + 'repo' => $form_state['values']['repo'] + ); + $current_repos = variable_get('apk_repositories', array()); + //$result = (empty($current_repos)) ? $repos : array_merge($repos, $current_repos); + variable_set('apk_repositories', array_merge($current_repos, $repos)); + $form_state['redirect'] = 'admin/config/apk_browser/repositories'; + drupal_set_message(t('Repositories are saved')); + //drupal_set_message(t('Submitting values: @values', array('@values' => var_export($result, TRUE)))); +} + +function apk_browser_repositories_list() { + $terms = ''; + $header = array('ID', 'URL', 'Repo', 'Arch', array('data' => 'Manage', 'colspan' => '2')); + $add = l('<img src="' . base_path() . drupal_get_path('module', 'apk_browser') . '/images/add.png" title="Add repo" />', 'admin/config/apk_browser/repositories/add', array('html' => TRUE)); + foreach (variable_get('apk_repositories', array()) as $key => $repo) { + $edit = l('<img src="' . base_path() . drupal_get_path('module', 'apk_browser') . '/images/page_white_edit.png" title="Edit repo" />', 'somelink', array('html' => TRUE)); + $delete = l('<img src="' . base_path() . drupal_get_path('module', 'apk_browser') . '/images/delete.png" title="Delete repo" />', 'somelink', array('html' => TRUE)); + $terms = '<table><tr>'; + $terms .= '</tr></table>'; + $tarch = taxonomy_term_load($repo['arch']); + $trepo = taxonomy_term_load($repo['repo']); + $rows[] = array($key, $repo['url'], $trepo->name, $tarch->name, $edit, $delete); + } + if (!empty($rows)) { + return theme('table', array('header' => $header, 'rows' => $rows)); + } else { + return t('No repositories added. Please add one.') . ' ' . $add; + } +} + +function apk_browser_import_apk() { + $repos = variable_get('apk_repositories', NULL); + $debug = variable_get('apk_import_debug', '0'); + $updated = array(); + $added = array(); + $skipped = '0'; + foreach ($repos as $repo) { + $apk_string = file_get_contents($repo['url']); + //no need to update if apkindex is not changed + $checksum = md5($apk_string); + $checksum_db = variable_get('apk_import_checksum', ''); + //print_r($checksum . '<br>' . $checksum_db. '<br>' . variable_get('apk_import_debug', '0')); + if (($debug == '1') || ($checksum != $checksum_db)) { + if ($debug == '1') { + watchdog('apk', 'APK importer running in debug mode!', array(), WATCHDOG_WARNING, NULL); + } + $packages = apk_browser_apkindex_reader($apk_string); + foreach ($packages as $package) { + $exist = apk_browser_apk_check($package['P'], $repo['arch'], $repo['repo']); + if ($exist) { + //check if both fields are set + if (count($exist) == '2') { + //if checksum has changed we set nid and update node + if ($exist['csum'] != $package['C']) { + $package['update'] = $exist['nid']; + $updated[] = apk_browser_add_apk($package, $repo['arch'], $repo['repo']); + //log to db if we are debugging + if ($debug == '1') { + watchdog('apk', 'Package @apk updated', array('@apk' => $package['P']), WATCHDOG_INFO, NULL); + } + } else { + if ($debug == '1') { + watchdog('apk', 'Package @apk already in database', array('@apk' => $package['P']), WATCHDOG_INFO, NULL); + } + $skipped++; + } + } else { + watchdog('apk', 'Package @apk has issues', array('@apk' => $package['P']), WATCHDOG_ERROR, NULL); + } + // this is a new package + } else { + $added[] = apk_browser_add_apk($package, $repo['arch'], $repo['repo']); + if ($debug == '1') { + watchdog('apk', 'Package @apk added', array('@apk' => $package['P']), WATCHDOG_INFO, NULL); + } + } + } + watchdog('apk', 'Added @added and updated @updated and skipped @skipped packages', array('@added' => count($added), '@updated' => count($updated), '@skipped' => $skipped), WATCHDOG_INFO, NULL); + variable_set('apk_import_checksum', $checksum); + } else { + watchdog('apk', 'APK index is up-to-date', array(), WATCHDOG_INFO, NULL); + } + } +} + +function apk_browser_add_apk($package, $arch, $repo) { + /* + * 1st array key is language, currently set to und + * second key is for multi value fields like depend + * php timeout set to 10min for initial repo import + */ + ini_set('max_execution_time', 600); + $node->type = 'apk'; + foreach ($package as $field => $value) { + switch ($field) { + case 'P': + $node->title = $value; + break; + case 'C': + $node->apk_checksum['und'][0]['value'] = $value; + break; + case 'V': + $node->apk_version['und'][0]['value'] = $value; + break; + case 'S': + $node->apk_size['und'][0]['value'] = $value; + break; + case 'I': + $node->apk_isize['und'][0]['value'] = $value; + break; + case 'T': + $node->apk_description['und'][0]['value'] = $value; + break; + case 'U': + $node->apk_url['und'][0]['value'] = $value; + break; + case 'L': + $node->apk_license['und'][0]['value'] = $value; + break; + //setting this will update package with this nid number + case 'update': + $node->nid = $value; + break; + case 'D': + foreach ($value as $depend) { + $node->apk_depends['und'][]['value'] = $depend; + } + break; + } + } + $node->taxonomy_apk_repo['und'][0]['tid'] = $repo; + $node->taxonomy_apk_arch['und'][0]['tid'] = $arch; + node_save($node); + return $node->nid; +} + +function apk_browser_apkindex_reader($apk_string) { + //file_get_contents outputs string and php tar needs file + $temp = tempnam("/tmp", "apk_"); + $fp = fopen($temp, 'w'); + fwrite($fp, $apk_string); + fclose($fp); + $tar_object = new Archive_Tar($temp); + $apkindex = $tar_object->extractInString("APKINDEX"); + //convert packages data into array + $packages = preg_split("`\n\W+`", $apkindex); + //remove last empty line + array_pop($packages); + foreach ($packages as $key => $package) { + //convert packages lines into array + $apackage = preg_split("`\n`", $package); + foreach ($apackage as $apk_line) { + //create apk variables and arrayify if its depend line + $value = substr($apk_line, 2); + $apackages[$key][$apk_line[0]] = ($apk_line[0] == 'D') ? explode(" ", $value) : $value; + } + } + unlink($temp); + return $apackages; +} + +function apk_browser_apk_check($title, $arch, $repo) { + /* query using 4 tables to fetch nid and checksum + * used to check if apk exist and return checksum + * to check if its updated + */ + return db_query(" + SELECT node.nid, + csum.apk_checksum_value as csum + FROM {node}, + {field_data_taxonomy_apk_arch} AS arch, + {field_data_taxonomy_apk_repo} AS repo, + {field_data_apk_checksum} AS csum + WHERE node.nid = arch.entity_id + AND node.nid = repo.entity_id + AND node.nid = csum.entity_id + AND node.title = :title + AND arch.taxonomy_apk_arch_tid = :arch + AND repo.taxonomy_apk_repo_tid = :repo", array( + ':title' => $title, + ':arch' => $arch, + ':repo' => $repo + ) + )->fetchAssoc(); +} diff --git a/images/add.png b/images/add.png Binary files differnew file mode 100644 index 0000000..6332fef --- /dev/null +++ b/images/add.png diff --git a/images/delete.png b/images/delete.png Binary files differnew file mode 100644 index 0000000..08f2493 --- /dev/null +++ b/images/delete.png diff --git a/images/page_white_edit.png b/images/page_white_edit.png Binary files differnew file mode 100644 index 0000000..b93e776 --- /dev/null +++ b/images/page_white_edit.png diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 0000000..6ffde2f --- /dev/null +++ b/nbproject/project.properties @@ -0,0 +1,7 @@ +include.path=${php.global.include.path} +php.version=PHP_5 +source.encoding=UTF-8 +src.dir=. +tags.asp=false +tags.short=true +web.root=. diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 0000000..d694c53 --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://www.netbeans.org/ns/project/1"> + <type>org.netbeans.modules.php.project</type> + <configuration> + <data xmlns="http://www.netbeans.org/ns/php-project/1"> + <name>Alpine package browser</name> + </data> + </configuration> +</project> |