Skip to content

Commit

Permalink
Merge branch 'includeGenresImportExport-761' into 'main'
Browse files Browse the repository at this point in the history
Adds journal genres import/export

See merge request softwares-pkp/plugins_ojs/fullJournalTransfer!44
  • Loading branch information
thiagolepidus committed Feb 28, 2024
2 parents c23d9da + a2c6c75 commit 96ca439
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 50 deletions.
3 changes: 2 additions & 1 deletion FullJournalImportExportPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ public function exportJournal($journal, $archivePath, $opts)

libxml_use_internal_errors(true);
$journalXml = $filter->execute($journal);
$xml = $journalXml->saveXml();

$errors = array_filter(libxml_get_errors(), function ($error) {
return $error->level == LIBXML_ERR_ERROR || $error->level == LIBXML_ERR_FATAL;
Expand All @@ -233,6 +232,8 @@ public function exportJournal($journal, $archivePath, $opts)
return false;
}

$xml = $journalXml->saveXml();

if (empty($xml)) {
return false;
}
Expand Down
37 changes: 37 additions & 0 deletions filter/export/JournalNativeXmlFilter.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public function createJournalNode($doc, $journal)
$this->addNavigationMenuItems($doc, $journalNode, $journal);
$this->addNavigationMenus($doc, $journalNode, $journal);
$this->addUsers($doc, $journalNode, $journal);
$this->addGenres($doc, $journalNode, $journal);
$this->addSections($doc, $journalNode, $journal);
$this->addIssues($doc, $journalNode, $journal);
$this->addArticles($doc, $journalNode, $journal);
Expand Down Expand Up @@ -300,6 +301,42 @@ public function addUsers($doc, $journalNode, $journal)
}
}

public function addGenres($doc, $journalNode, $journal)
{
$genreDAO = DAORegistry::getDAO('GenreDAO');
$genres = $genreDAO->getByContextId($journal->getId())->toArray();
$deployment = $this->getDeployment();

if (!count($genres)) {
return;
}

$genresNode = $doc->createElementNS($deployment->getNamespace(), 'genres');
foreach ($genres as $genre) {
$genreNode = $doc->createElementNS($deployment->getNamespace(), 'genre');

$genreNode->appendChild($node = $doc->createElementNS(
$deployment->getNamespace(),
'id',
$genre->getId()
));
$node->setAttribute('type', 'internal');
$node->setAttribute('advice', 'ignore');

$genreNode->setAttribute('key', $genre->getKey());
$genreNode->setAttribute('category', (int) $genre->getCategory());
$genreNode->setAttribute('dependent', (int) $genre->getDependent());
$genreNode->setAttribute('supplementary', (int) $genre->getSupplementary());
$genreNode->setAttribute('seq', $genre->getSequence());
$genreNode->setAttribute('enabled', $genre->getEnabled());

$this->createLocalizedNodes($doc, $genreNode, 'name', $genre->getName(null));

$genresNode->appendChild($genreNode);
}

$journalNode->appendChild($genresNode);
}

public function addSections($doc, $journalNode, $journal)
{
Expand Down
50 changes: 41 additions & 9 deletions filter/import/NativeXmlJournalFilter.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ public function handleElement($node)
for ($n = $node->firstChild; $n !== null; $n = $n->nextSibling) {
if (is_a($n, 'DOMElement')) {
$this->handleChildElement($n, $journal);
if ($n->tagName === 'supported_locales') {
$this->installDefaultGenres($journal);
}
}
}
$contextDAO->updateObject($journal);
Expand Down Expand Up @@ -95,12 +92,6 @@ public function createJournalDirs($journal, $deployment)
}
}

public function installDefaultGenres($journal)
{
$genreDao = \DAORegistry::getDAO('GenreDAO');
$genreDao->installDefaults($journal->getId(), $journal->getData('supportedLocales'));
}

public function handleChildElement($node, $journal)
{
$deployment = $this->getDeployment();
Expand Down Expand Up @@ -141,6 +132,9 @@ public function handleChildElement($node, $journal)
case 'PKPUsers':
$this->parseUsers($node, $journal);
break;
case 'genres':
$this->parseGenres($node, $journal);
break;
case 'sections':
$this->parseSections($node, $journal);
break;
Expand Down Expand Up @@ -253,6 +247,44 @@ public function parseUsers($node, $journal)
return $filter->execute($usersXml);
}

public function parseGenres($node, $journal)
{
$deployment = $this->getDeployment();

for ($n = $node->firstChild; $n !== null; $n = $n->nextSibling) {
if (is_a($n, 'DOMElement') && $n->tagName === 'genre') {
$this->parseGenre($n, $journal);
}
}
}

public function parseGenre($node, $journal)
{
$deployment = $this->getDeployment();

$genreDAO = DAORegistry::getDAO('GenreDAO');
$genre = $genreDAO->newDataObject();
$genre->setContextId($journal->getId());
$genre->setKey($node->getAttribute('key'));
$genre->setCategory($node->getAttribute('category'));
$genre->setDependent($node->getAttribute('dependent'));
$genre->setSupplementary($node->getAttribute('supplementary'));
$genre->setSequence($node->getAttribute('seq'));
$genre->setEnabled($node->getAttribute('enabled'));

for ($n = $node->firstChild; $n !== null; $n = $n->nextSibling) {
if (is_a($n, 'DOMElement') && $n->tagName === 'name') {
list($locale, $value) = $this->parseLocalizedContent($n);
if (empty($locale)) {
$locale = $context->getPrimaryLocale();
}
$genre->setName($value, $locale);
}
}

$genreId = $genreDAO->insertObject($genre);
}

public function parseSections($node, $journal)
{
$deployment = $this->getDeployment();
Expand Down
23 changes: 23 additions & 0 deletions fullJournal.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<element ref="pkp:navigation_menu_items" minOccurs="0" maxOccurs="unbounded" />
<element ref="pkp:navigation_menus" minOccurs="0" maxOccurs="unbounded" />
<element ref="pkp:PKPUsers" minOccurs="0" maxOccurs="unbounded" />
<element ref="pkp:genres" minOccurs="0" maxOccurs="unbounded" />
<element ref="pkp:sections" minOccurs="0" maxOccurs="unbounded" />
<element ref="pkp:extended_issue" minOccurs="0" maxOccurs="unbounded" />
<element ref="pkp:extended_issues" minOccurs="0" maxOccurs="unbounded" />
Expand Down Expand Up @@ -208,6 +209,27 @@
<attribute name="can_change_metadata" type="int" />
</complexType>

<element name="genres">
<complexType>
<sequence>
<element ref="pkp:genre" minOccurs="0" maxOccurs="unbounded" />
</sequence>
</complexType>
</element>

<complexType name="genre">
<sequence>
<element ref="pkp:id" minOccurs="0" maxOccurs="unbounded" />
<element name="name" type="pkp:localizedNode" minOccurs="0" maxOccurs="unbounded" />
</sequence>
<attribute name="key" type="string" />
<attribute name="category" type="int" />
<attribute name="dependent" type="int" />
<attribute name="supplementary" type="int" />
<attribute name="seq" type="int" />
<attribute name="enabled" type="int" />
</complexType>

<element name="review_rounds">
<complexType>
<sequence>
Expand Down Expand Up @@ -311,6 +333,7 @@
<element name="navigation_menu_item" type="pkp:navigation_menu_item" />
<element name="navigation_menu" type="pkp:navigation_menu" />
<element name="announcement" type="pkp:announcement" />
<element name="genre" type="pkp:genre" />
<element name="review_round" type="pkp:review_round" />
<element name="stage_assignment" type="pkp:stage_assignment" />
<element name="review_assignment" type="pkp:review_assignment" />
Expand Down
38 changes: 0 additions & 38 deletions tests/import/NativeXmlJournalFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,44 +107,6 @@ private function setJournalChecklistNodeData($journal)
$journal->setData('submissionChecklist', $defaultJournal->getData('submissionChecklist'));
}

public function testInstallDefaultGenres()
{
$journalImportFilter = $this->getNativeImportExportFilter();
$journal = new Journal();
$journal->setId(rand());
$journal->setData('supportedLocales', ['en_US']);

$expectedGenreNames = [
"##default.genres.article##",
"##default.genres.researchInstrument##",
"##default.genres.researchMaterials##",
"##default.genres.researchResults##",
"##default.genres.transcripts##",
"##default.genres.dataAnalysis##",
"##default.genres.dataSet##",
"##default.genres.sourceTexts##",
"##default.genres.multimedia##",
"##default.genres.image##",
"##default.genres.styleSheet##",
"##default.genres.other##"
];

AppLocale::requireComponents(LOCALE_COMPONENT_APP_DEFAULT);

$journalImportFilter->installDefaultGenres($journal);

$genreDao = DAORegistry::getDAO('GenreDAO');
$genres = $genreDao->getByContextId($journal->getId());
$genreNames = [];
while ($genre = $genres->next()) {
foreach ($genre->getName(null) as $locale => $name) {
$genreNames[] = $name;
}
}

$this->assertEquals($expectedGenreNames, $genreNames);
}

public function testJournalFilterParseSubmissionChecklist()
{
$journalImportFilter = $this->getNativeImportExportFilter();
Expand Down
4 changes: 2 additions & 2 deletions version.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<version>
<application>fullJournalTransfer</application>
<type>plugins.importexport</type>
<release>2.0.6.1</release>
<date>2024-02-27</date>
<release>2.0.7.0</release>
<date>2024-02-28</date>
<class>FullJournalImportExportPlugin</class>
</version>

0 comments on commit 96ca439

Please sign in to comment.