Skip to content

Commit

Permalink
Fixes #2247 Publication BibTeX Import (#2358)
Browse files Browse the repository at this point in the history
  • Loading branch information
tadean authored Jun 16, 2023
1 parent 24d828e commit eb414e0
Show file tree
Hide file tree
Showing 20 changed files with 1,171 additions and 0 deletions.
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
"npm-asset/blazy": "1.8.2",
"npm-asset/jquery-ui-touch-punch": "0.2.3",
"npm-asset/slick-carousel": "1.8.0",
"renanbr/bibtex-parser": "2.1.2",
"seboettg/citeproc-php": "2.6.0"
},
"require-dev": {
Expand Down Expand Up @@ -188,6 +189,9 @@
},
"drupal/viewsreference": {
"Add Label field formatter (2938433)": "https://www.drupal.org/files/issues/2021-01-27/add_label_formatter-2938433-7.patch"
},
"renanbr/bibtex-parser": {
"Parser performance issue": "https://gist.githubusercontent.com/tadean/39b90d00cd3edfff7c5653f907e15721/raw/8feb59cb4447b0f667fe5f4936a25c4d30211e1b/renanbr_bibtex_parser_performance.patch"
}
},
"drupal-scaffold": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: 'Quickstart Publication BibTeX Import'
type: module
description: 'Allows BibTeX import of publications'
core_version_requirement: ^9 || ^10
package: 'The University of Arizona - Experimental'
dependencies:
- 'az_core:az_core'
- 'az_publication:az_publication'
- 'az_publication_import:az_publication_import'
- migrate
- migrate_plus
- migrate_tools
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
az_publication_bibtex.import_bibtex:
title: 'Import BibTeX Publication'
description: 'Import a Quickstart publication from a BibTeX document.'
route_name: az_publication_bibtex.az_publication_bibtex_import_form
parent: az_publication_import.admin.content
weight: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
az_publication_bibtex.az_publication_bibtex_import_form:
path: '/admin/content/az-publication/import/bibtex'
defaults:
_form: '\Drupal\az_publication_bibtex\Form\AZPublicationBibtexForm'
_title: 'BibTeX Import'
requirements:
_permission: 'administer quickstart configuration'
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
id: az_publication_bibtex_import
label: AZ Quickstart Publication BibTeX Import
migration_tags:
- Publications
source:
plugin: url
data_fetcher_plugin: file
data_parser_plugin: az_bibtex
urls: []
ids:
citation_key:
type: string
fields:
-
name: _type
selector: _type
-
name: citation_key
selector: citation-key
-
name: title
selector: title
-
name: author
selector: author
-
name: publisher
selector: publisher
-
name: institution
selector: institution
-
name: school
selector: school
-
name: location
selector: location
-
name: volume
selector: volume
-
name: pages
selector: pages
-
name: number
selector: number
-
name: booktitle
selector: booktitle
-
name: journaltitle
selector: journaltitle
-
name: journal
selector: journal
-
name: annotation
selector: annotation
-
name: abstract
selector: abstract
-
name: url
selector: url
-
name: doi
selector: doi
-
name: version
selector: version
-
name: datetype
selector: _date_type
-
name: date
selector: _date

process:
type:
plugin: default_value
default_value: az_publication
field_az_publication_type:
plugin: static_map
source: _type
map:
article: article-journal
book: book
booklet: pamphlet
collection: collection
conference: paper-conference
inbook: book
incollection: entry
inproceedings: paper-conference
manual: pamphlet
mastersthesis: thesis
misc: post
online: webpage
patent: patent
periodical: periodical
phdthesis: thesis
proceedings: paper-conference
report: report
techreport: report
thesis: thesis
unpublished: manuscript
default_value: article-journal
title: title
field_az_publication_date_type: datetype
field_az_publication_date:
plugin: format_date
from_format: 'Y-m-d'
to_format: 'Y-m-d'
from_timezone: 'UTC'
source: date
field_az_publication_container:
plugin: null_coalesce
source:
- booktitle
- journaltitle
- journal
field_az_publication_abstract/value:
plugin: null_coalesce
source:
- abstract
- annotation
field_az_publication_abstract/format:
plugin: default_value
default_value: az_standard
field_az_publication_location: location
field_az_publication_publisher:
plugin: null_coalesce
source:
- publisher
- institution
- school
field_az_publication_page: pages
'field_az_publication_link/uri': url
field_az_publication_doi: doi
field_az_publication_volume: volume
field_az_publication_issue: number
field_az_publication_version: version
field_az_authors:
plugin: sub_process
source: author
process:
fullname:
-
plugin: callback
callable: array_filter
source:
- first
- von
- last
- jr
-
plugin: concat
delimiter: ' '
target_id:
plugin: entity_generate
source: '@fullname'
value_key: name
ignore_case: true
bundle: 1
bundle_key: status
entity_type: az_author
values:
field_az_author_fname: first
field_az_author_nondrop_particle: von
field_az_author_lname: last
field_az_author_suffix: jr

destination:
plugin: entity:node
bundle: az_publication

dependencies:
enforced:
module:
- az_publication
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace Drupal\az_publication_bibtex\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\file\Entity\File;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate_tools\MigrateBatchExecutable;
use Drupal\migrate\MigrateMessage;

/**
* BibTeX import form.
*/
class AZPublicationBibtexForm extends FormBase {

/**
* The migration plugin manager.
*
* @var \Drupal\migrate\Plugin\MigrationPluginManager
*/
protected $pluginManagerMigration;

/**
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->fileSystem = $container->get('file_system');
$instance->pluginManagerMigration = $container->get('plugin.manager.migration');
return $instance;
}

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'az_publication_bibtex_import_form';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {

$form['bibtex'] = [
'#type' => 'managed_file',
'#title' => $this->t('Upload a BibTeX document'),
'#upload_location' => 'temporary://bibtex/',
'#upload_validators' => [
'file_validate_extensions' => ['bib'],
],
'#required' => TRUE,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Import'),
];

return $form;
}

/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {

parent::validateForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValues();
if (!empty($values['bibtex'])) {
$fid = reset($values['bibtex']);
$file = File::load($fid);
if (!empty($file)) {
$uri = $file->getFileUri();
$path = $this->fileSystem->realpath($uri);
$migration_id = 'az_publication_bibtex_import';
/** @var \Drupal\migrate\Plugin\Migration $migration */
$migration = $this->pluginManagerMigration->createInstance($migration_id);
// Reset status.
$status = $migration->getStatus();
if ($status !== MigrationInterface::STATUS_IDLE) {
$migration->setStatus(MigrationInterface::STATUS_IDLE);
}
$options = [
'limit' => 0,
'update' => 1,
'force' => 0,
'configuration' => [
'source' => [
'urls' => $path,
],
],
];
$executable = new MigrateBatchExecutable($migration, new MigrateMessage(), $options);
$executable->batchImport();
}
}
}

}
Loading

0 comments on commit eb414e0

Please sign in to comment.