function &createTextNode(&$doc, $value) { $value = Core::cleanVar($value); if (is_callable(array($doc, 'createTextNode'))) { $element =& $doc->createTextNode($value); } else { $element = new XMLNode(); $element->setValue($value); } return $element; }
/** * Add the metadata XML of a single article to an * XML article list. * * @param $articleDoc DOMDocument * @param $article PublishedArticle * @param $journal Journal * @param $markToDelete boolean If true the returned XML * will only contain a deletion marker. */ function _addArticleXml(&$articleDoc, &$article, &$journal, $markToDelete = false) { assert(is_a($article, 'Article')); // Get the root node of the list. assert(is_a($articleDoc, 'DOMDocument')); $articleList =& $articleDoc->documentElement; // Create a new article node. $articleNode =& XMLCustomWriter::createElement($articleDoc, 'article'); // Add ID information. XMLCustomWriter::setAttribute($articleNode, 'id', $article->getId()); XMLCustomWriter::setAttribute($articleNode, 'sectionId', $article->getSectionId()); XMLCustomWriter::setAttribute($articleNode, 'journalId', $article->getJournalId()); XMLCustomWriter::setAttribute($articleNode, 'instId', $this->_instId); // Set the load action. $loadAction = $markToDelete ? 'delete' : 'replace'; XMLCustomWriter::setAttribute($articleNode, 'loadAction', $loadAction); XMLCustomWriter::appendChild($articleList, $articleNode); // The XML for an article marked to be deleted contains no metadata. if ($markToDelete) { return; } assert(is_a($article, 'PublishedArticle')); // Add authors. $authors = $article->getAuthors(); if (!empty($authors)) { $authorList =& XMLCustomWriter::createElement($articleDoc, 'authorList'); foreach ($authors as $author) { /* @var $author Author */ XMLCustomWriter::createChildWithText($articleDoc, $authorList, 'author', $author->getFullName(true)); } XMLCustomWriter::appendChild($articleNode, $authorList); } // We need the request to retrieve locales and build URLs. $request = PKPApplication::getRequest(); // Get all supported locales. $site = $request->getSite(); $supportedLocales = $site->getSupportedLocales() + array_keys($journal->getSupportedLocaleNames()); assert(!empty($supportedLocales)); // Add titles. $titleList =& XMLCustomWriter::createElement($articleDoc, 'titleList'); // Titles are used for sorting, we therefore need // them in all supported locales. assert(!empty($supportedLocales)); foreach ($supportedLocales as $locale) { $localizedTitle = $article->getLocalizedTitle($locale); if (!is_null($localizedTitle)) { // Add the localized title. $titleNode =& XMLCustomWriter::createChildWithText($articleDoc, $titleList, 'title', $localizedTitle); XMLCustomWriter::setAttribute($titleNode, 'locale', $locale); // If the title does not exist in the given locale // then use the localized title for sorting only. $title = $article->getTitle($locale); $sortOnly = empty($title) ? 'true' : 'false'; XMLCustomWriter::setAttribute($titleNode, 'sortOnly', $sortOnly); } } XMLCustomWriter::appendChild($articleNode, $titleList); // Add abstracts. $abstracts = $article->getAbstract(null); // return all locales if (!empty($abstracts)) { $abstractList =& XMLCustomWriter::createElement($articleDoc, 'abstractList'); foreach ($abstracts as $locale => $abstract) { $abstractNode =& XMLCustomWriter::createChildWithText($articleDoc, $abstractList, 'abstract', $abstract); XMLCustomWriter::setAttribute($abstractNode, 'locale', $locale); } XMLCustomWriter::appendChild($articleNode, $abstractList); } // Add discipline. $disciplines = $article->getDiscipline(null); // return all locales if (!empty($disciplines)) { $disciplineList =& XMLCustomWriter::createElement($articleDoc, 'disciplineList'); foreach ($disciplines as $locale => $discipline) { $disciplineNode =& XMLCustomWriter::createChildWithText($articleDoc, $disciplineList, 'discipline', $discipline); XMLCustomWriter::setAttribute($disciplineNode, 'locale', $locale); } XMLCustomWriter::appendChild($articleNode, $disciplineList); } // Add subjects and subject classes. $subjectClasses = $article->getSubjectClass(null); $subjects = $article->getSubject(null); if (!empty($subjectClasses) || !empty($subjects)) { $subjectList =& XMLCustomWriter::createElement($articleDoc, 'subjectList'); if (!is_array($subjectClasses)) { $subjectClasses = array(); } if (!is_array($subjects)) { $subjects = array(); } $locales = array_unique(array_merge(array_keys($subjectClasses), array_keys($subjects))); foreach ($locales as $locale) { $subject = ''; if (isset($subjectClasses[$locale])) { $subject .= $subjectClasses[$locale]; } if (isset($subjects[$locale])) { if (!empty($subject)) { $subject .= ' '; } $subject .= $subjects[$locale]; } $subjectNode =& XMLCustomWriter::createChildWithText($articleDoc, $subjectList, 'subject', $subject); XMLCustomWriter::setAttribute($subjectNode, 'locale', $locale); } XMLCustomWriter::appendChild($articleNode, $subjectList); } // Add type. $types = $article->getType(null); // return all locales if (!empty($types)) { $typeList =& XMLCustomWriter::createElement($articleDoc, 'typeList'); foreach ($types as $locale => $type) { $typeNode =& XMLCustomWriter::createChildWithText($articleDoc, $typeList, 'type', $type); XMLCustomWriter::setAttribute($typeNode, 'locale', $locale); } XMLCustomWriter::appendChild($articleNode, $typeList); } // Add coverage. $coverageGeo = $article->getCoverageGeo(null); $coverageChron = $article->getCoverageChron(null); $coverageSample = $article->getCoverageSample(null); if (!empty($coverageGeo) || !empty($coverageChron) || !empty($coverageSample)) { $coverageList =& XMLCustomWriter::createElement($articleDoc, 'coverageList'); if (!is_array($coverageGeo)) { $coverageGeo = array(); } if (!is_array($coverageChron)) { $coverageChron = array(); } if (!is_array($coverageSample)) { $coverageSample = array(); } $locales = array_unique(array_merge(array_keys($coverageGeo), array_keys($coverageChron), array_keys($coverageSample))); foreach ($locales as $locale) { $coverage = ''; if (isset($coverageGeo[$locale])) { $coverage .= $coverageGeo[$locale]; } if (isset($coverageChron[$locale])) { if (!empty($coverage)) { $coverage .= '; '; } $coverage .= $coverageChron[$locale]; } if (isset($coverageSample[$locale])) { if (!empty($coverage)) { $coverage .= '; '; } $coverage .= $coverageSample[$locale]; } $coverageNode =& XMLCustomWriter::createChildWithText($articleDoc, $coverageList, 'coverage', $coverage); XMLCustomWriter::setAttribute($coverageNode, 'locale', $locale); } XMLCustomWriter::appendChild($articleNode, $coverageList); } // Add journal titles. $journalTitleList =& XMLCustomWriter::createElement($articleDoc, 'journalTitleList'); // Journal titles are used for sorting, we therefore need // them in all supported locales. foreach ($supportedLocales as $locale) { $localizedTitle = $journal->getName($locale); $sortOnly = false; if (is_null($localizedTitle)) { // If the title does not exist in the given locale // then use the localized title for sorting only. $journalTitle = $journal->getLocalizedName(); $sortOnly = true; } else { $journalTitle = $localizedTitle; } $journalTitleNode =& XMLCustomWriter::createChildWithText($articleDoc, $journalTitleList, 'journalTitle', $journalTitle); XMLCustomWriter::setAttribute($journalTitleNode, 'locale', $locale); $sortOnly = $sortOnly ? 'true' : 'false'; XMLCustomWriter::setAttribute($journalTitleNode, 'sortOnly', $sortOnly); } XMLCustomWriter::appendChild($articleNode, $journalTitleList); // Add publication dates. $publicationDate = $article->getDatePublished(); if (!empty($publicationDate)) { // Transform and store article publication date. $publicationDate = $this->_convertDate($publicationDate); $dateNode =& XMLCustomWriter::createChildWithText($articleDoc, $articleNode, 'publicationDate', $publicationDate); } $issueId = $article->getIssueId(); if (is_numeric($issueId)) { $issueDao = DAORegistry::getDAO('IssueDAO'); /* @var $issueDao IssueDAO */ $issue = $issueDao->getById($issueId); if (is_a($issue, 'Issue')) { $issuePublicationDate = $issue->getDatePublished(); if (!empty($issuePublicationDate)) { // Transform and store issue publication date. $issuePublicationDate = $this->_convertDate($issuePublicationDate); $dateNode =& XMLCustomWriter::createChildWithText($articleDoc, $articleNode, 'issuePublicationDate', $issuePublicationDate); } } } // We need the router to build file URLs. $router = $request->getRouter(); /* @var $router PageRouter */ // Add galley files $articleGalleyDao = DAORegistry::getDAO('ArticleGalleyDAO'); $galleys = $articleGalleyDao->getBySubmissionId($article->getId()); $galleyList = null; while ($galley = $galleys->next()) { /* @var $galley ArticleGalley */ $locale = $galley->getLocale(); $galleyUrl = $router->url($request, $journal->getPath(), 'article', 'download', array(intval($article->getId()), intval($galley->getId()))); if (!empty($locale) && !empty($galleyUrl)) { if (is_null($galleyList)) { $galleyList =& XMLCustomWriter::createElement($articleDoc, 'galleyList'); } $galleyNode =& XMLCustomWriter::createElement($articleDoc, 'galley'); XMLCustomWriter::setAttribute($galleyNode, 'locale', $locale); XMLCustomWriter::setAttribute($galleyNode, 'fileName', $galleyUrl); XMLCustomWriter::appendChild($galleyList, $galleyNode); } } // Wrap the galley XML as CDATA. if (!is_null($galleyList)) { if (is_callable(array($articleDoc, 'saveXml'))) { $galleyXml = $articleDoc->saveXml($galleyList); } else { $galleyXml = $galleyList->toXml(); } $galleyOuterNode =& XMLCustomWriter::createElement($articleDoc, 'galley-xml'); if (is_callable(array($articleDoc, 'createCDATASection'))) { $cdataNode = $articleDoc->createCDATASection($galleyXml); } else { $cdataNode = new XMLNode(); $cdataNode->setValue('<![CDATA[' . $galleyXml . ']]>'); } XMLCustomWriter::appendChild($galleyOuterNode, $cdataNode); XMLCustomWriter::appendChild($articleNode, $galleyOuterNode); } }