/**
  * Add a file to the search index.
  * @param $articleId int
  * @param $type int
  * @param $fileId int
  */
 function updateFileIndex($articleId, $type, $fileId)
 {
     import('classes.file.ArticleFileManager');
     $fileMgr = new ArticleFileManager($articleId);
     $file =& $fileMgr->getFile($fileId);
     if (isset($file)) {
         $parser =& SearchFileParser::fromFile($file);
     }
     if (isset($parser)) {
         if ($parser->open()) {
             $searchDao =& DAORegistry::getDAO('ArticleSearchDAO');
             $objectId = $searchDao->insertObject($articleId, $type, $fileId);
             $position = 0;
             while (($text = $parser->read()) !== false) {
                 ArticleSearchIndex::indexObjectKeywords($objectId, $text, $position);
             }
             $parser->close();
         }
     }
 }
 function &generateArticleDom(&$doc, &$journal, &$issue, &$article, &$galley)
 {
     $unavailableString = Locale::translate('plugins.importexport.erudit.unavailable');
     $root =& XMLCustomWriter::createElement($doc, 'article');
     XMLCustomWriter::setAttribute($root, 'idprop', $journal->getId() . '-' . $issue->getId() . '-' . $article->getId() . '-' . $galley->getId(), false);
     XMLCustomWriter::setAttribute($root, 'arttype', 'article');
     $lang = $article->getLanguage();
     XMLCustomWriter::setAttribute($root, 'lang', isset($lang) ? $lang : 'en');
     XMLCustomWriter::setAttribute($root, 'processing', 'cart');
     /* --- admin --- */
     $adminNode =& XMLCustomWriter::createElement($doc, 'admin');
     XMLCustomWriter::appendChild($root, $adminNode);
     /* --- articleinfo --- */
     $articleInfoNode =& XMLCustomWriter::createElement($doc, 'articleinfo');
     XMLCustomWriter::appendChild($adminNode, $articleInfoNode);
     // The first public ID should be a full URL to the article.
     $urlIdNode =& XMLCustomWriter::createChildWithText($doc, $articleInfoNode, 'idpublic', Request::url($journal->getPath(), 'article', 'view', array($article->getId(), $galley->getId())));
     XMLCustomWriter::setAttribute($urlIdNode, 'scheme', 'sici');
     /* --- journal --- */
     $journalNode =& XMLCustomWriter::createElement($doc, 'journal');
     XMLCustomWriter::appendChild($adminNode, $journalNode);
     XMLCustomWriter::setAttribute($journalNode, 'id', 'ojs-' . $journal->getPath());
     XMLCustomWriter::createChildWithText($doc, $journalNode, 'jtitle', $journal->getLocalizedTitle());
     XMLCustomWriter::createChildWithText($doc, $journalNode, 'jshorttitle', $journal->getLocalizedSetting('initials'), false);
     if (!($printIssn = $journal->getSetting('printIssn'))) {
         $printIssn = $unavailableString;
     }
     XMLCustomWriter::createChildWithText($doc, $journalNode, 'idissn', $printIssn);
     if (!($onlineIssn = $journal->getSetting('onlineIssn'))) {
         $onlineIssn = $unavailableString;
     }
     XMLCustomWriter::createChildWithText($doc, $journalNode, 'iddigissn', $onlineIssn);
     /* --- issue --- */
     $issueNode =& XMLCustomWriter::createElement($doc, 'issue');
     XMLCustomWriter::appendChild($adminNode, $issueNode);
     XMLCustomWriter::setAttribute($issueNode, 'id', 'ojs-' . $issue->getBestIssueId());
     XMLCustomWriter::createChildWithText($doc, $issueNode, 'volume', $issue->getVolume(), false);
     XMLCustomWriter::createChildWithText($doc, $issueNode, 'issueno', $issue->getNumber(), false);
     $pubNode =& XMLCustomWriter::createElement($doc, 'pub');
     XMLCustomWriter::appendChild($issueNode, $pubNode);
     XMLCustomWriter::createChildWithText($doc, $pubNode, 'year', $issue->getYear());
     $digPubNode =& XMLCustomWriter::createElement($doc, 'digpub');
     XMLCustomWriter::appendChild($issueNode, $digPubNode);
     if ($issue->getDatePublished()) {
         XMLCustomWriter::createChildWithText($doc, $digPubNode, 'date', EruditExportDom::formatDate($issue->getDatePublished()));
     }
     /* --- Publisher & DTD --- */
     $publisherInstitution =& $journal->getSetting('publisherInstitution');
     $publisherNode =& XMLCustomWriter::createElement($doc, 'publisher');
     XMLCustomWriter::setAttribute($publisherNode, 'id', 'ojs-' . $journal->getId() . '-' . $issue->getId() . '-' . $article->getId());
     XMLCustomWriter::appendChild($adminNode, $publisherNode);
     $publisherInstitution = $unavailableString;
     if (empty($publisherInstitution)) {
         $publisherInstitution = $unavailableString;
     }
     XMLCustomWriter::createChildWithText($doc, $publisherNode, 'orgname', $publisherInstitution);
     $digprodNode =& XMLCustomWriter::createElement($doc, 'digprod');
     XMLCustomWriter::createChildWithText($doc, $digprodNode, 'orgname', $publisherInstitution);
     XMLCustomWriter::setAttribute($digprodNode, 'id', 'ojs-prod-' . $journal->getId() . '-' . $issue->getId() . '-' . $article->getId());
     XMLCustomWriter::appendChild($adminNode, $digprodNode);
     $digdistNode =& XMLCustomWriter::createElement($doc, 'digdist');
     XMLCustomWriter::createChildWithText($doc, $digdistNode, 'orgname', $publisherInstitution);
     XMLCustomWriter::setAttribute($digdistNode, 'id', 'ojs-dist-' . $journal->getId() . '-' . $issue->getId() . '-' . $article->getId());
     XMLCustomWriter::appendChild($adminNode, $digdistNode);
     $dtdNode =& XMLCustomWriter::createElement($doc, 'dtd');
     XMLCustomWriter::appendChild($adminNode, $dtdNode);
     XMLCustomWriter::setAttribute($dtdNode, 'name', 'Erudit Article');
     XMLCustomWriter::setAttribute($dtdNode, 'version', '3.0.0');
     /* --- copyright --- */
     $copyright = $journal->getLocalizedSetting('copyrightNotice');
     XMLCustomWriter::createChildWithText($doc, $adminNode, 'copyright', empty($copyright) ? $unavailableString : $copyright);
     /* --- frontmatter --- */
     $frontMatterNode =& XMLCustomWriter::createElement($doc, 'frontmatter');
     XMLCustomWriter::appendChild($root, $frontMatterNode);
     $titleGroupNode =& XMLCustomWriter::createElement($doc, 'titlegr');
     XMLCustomWriter::appendChild($frontMatterNode, $titleGroupNode);
     XMLCustomWriter::createChildWithText($doc, $titleGroupNode, 'title', strip_tags($article->getLocalizedTitle()));
     /* --- authorgr --- */
     $authorGroupNode =& XMLCustomWriter::createElement($doc, 'authorgr');
     XMLCustomWriter::appendChild($frontMatterNode, $authorGroupNode);
     $authorNum = 1;
     foreach ($article->getAuthors() as $author) {
         $authorNode =& XMLCustomWriter::createElement($doc, 'author');
         XMLCustomWriter::appendChild($authorGroupNode, $authorNode);
         XMLCustomWriter::setAttribute($authorNode, 'id', 'ojs-' . $journal->getId() . '-' . $issue->getId() . '-' . $article->getId() . '-' . $galley->getId() . '-' . $authorNum);
         $persNameNode =& XMLCustomWriter::createElement($doc, 'persname');
         XMLCustomWriter::appendChild($authorNode, $persNameNode);
         XMLCustomWriter::createChildWithText($doc, $persNameNode, 'firstname', $author->getFirstName());
         XMLCustomWriter::createChildWithText($doc, $persNameNode, 'middlename', $author->getMiddleName(), false);
         XMLCustomWriter::createChildWithText($doc, $persNameNode, 'familyname', $author->getLastName());
         if ($author->getLocalizedAffiliation() != '') {
             $affiliationNode =& XMLCustomWriter::createElement($doc, 'affiliation');
             XMLCustomWriter::appendChild($authorNode, $affiliationNode);
             XMLCustomWriter::createChildWithText($doc, $affiliationNode, 'blocktext', $author->getLocalizedAffiliation(), false);
         }
         $authorNum++;
     }
     /* --- abstract and keywords --- */
     foreach ((array) $article->getAbstract(null) as $locale => $abstract) {
         $abstract = strip_tags($abstract);
         $abstractNode =& XMLCustomWriter::createElement($doc, 'abstract');
         XMLCustomWriter::setAttribute($abstractNode, 'lang', $locale);
         XMLCustomWriter::appendChild($frontMatterNode, $abstractNode);
         XMLCustomWriter::createChildWithText($doc, $abstractNode, 'blocktext', $abstract);
         unset($abstractNode);
     }
     if ($keywords = $article->getLocalizedSubject()) {
         $keywordGroupNode =& XMLCustomWriter::createElement($doc, 'keywordgr');
         XMLCustomWriter::setAttribute($keywordGroupNode, 'lang', ($language = $article->getLanguage()) ? $language : 'en');
         foreach (explode(';', $keywords) as $keyword) {
             XMLCustomWriter::createChildWithText($doc, $keywordGroupNode, 'keyword', trim($keyword), false);
         }
         XMLCustomWriter::appendChild($frontMatterNode, $keywordGroupNode);
     }
     /* --- body --- */
     $bodyNode =& XMLCustomWriter::createElement($doc, 'body');
     XMLCustomWriter::appendChild($root, $bodyNode);
     import('classes.file.ArticleFileManager');
     $articleFileManager = new ArticleFileManager($article->getId());
     $file =& $articleFileManager->getFile($galley->getFileId());
     $parser =& SearchFileParser::fromFile($file);
     if (isset($parser)) {
         if ($parser->open()) {
             // File supports text indexing.
             $textNode =& XMLCustomWriter::createElement($doc, 'text');
             XMLCustomWriter::appendChild($bodyNode, $textNode);
             while (($line = $parser->read()) !== false) {
                 $line = trim($line);
                 if ($line != '') {
                     XMLCustomWriter::createChildWithText($doc, $textNode, 'blocktext', $line, false);
                 }
             }
             $parser->close();
         }
     }
     return $root;
 }
 /**
  * Signal to the indexing back-end that an article file changed.
  *
  * @see ArticleSearchIndex::articleMetadataChanged() above for more
  * comments.
  *
  * @param $articleId int
  * @param $type int
  * @param $fileId int
  */
 function articleFileChanged($articleId, $type, $fileId)
 {
     // Check whether a search plug-in jumps in.
     $hookResult =& HookRegistry::call('ArticleSearchIndex::articleFileChanged', array($articleId, $type, $fileId));
     // If no search plug-in is activated then fall back to the
     // default database search implementation.
     if ($hookResult === false || is_null($hookResult)) {
         import('classes.file.ArticleFileManager');
         $fileManager = new ArticleFileManager($articleId);
         $file =& $fileManager->getFile($fileId);
         if (isset($file)) {
             $parser =& SearchFileParser::fromFile($file);
         }
         if (isset($parser)) {
             if ($parser->open()) {
                 $searchDao =& DAORegistry::getDAO('ArticleSearchDAO');
                 $objectId = $searchDao->insertObject($articleId, $type, $fileId);
                 $position = 0;
                 while (($text = $parser->read()) !== false) {
                     $this->_indexObjectKeywords($objectId, $text, $position);
                 }
                 $parser->close();
             }
         }
     }
 }
 /**
  * Save settings.
  */
 function execute()
 {
     $articleDao =& DAORegistry::getDAO('ArticleDAO');
     $signoffDao =& DAORegistry::getDAO('SignoffDAO');
     $sectionEditorSubmissionDao =& DAORegistry::getDAO('SectionEditorSubmissionDAO');
     $application =& PKPApplication::getApplication();
     $request =& $application->getRequest();
     $user =& $request->getUser();
     $router =& $request->getRouter();
     $journal =& $router->getContext($request);
     $article = new Article();
     $article->setLocale($journal->getPrimaryLocale());
     // FIXME in bug #5543
     $article->setUserId($user->getId());
     $article->setJournalId($journal->getId());
     $article->setSectionId($this->getData('sectionId'));
     $article->setLanguage(String::substr($journal->getPrimaryLocale(), 0, 2));
     $article->setTitle($this->getData('title'), null);
     // Localized
     $article->setAbstract($this->getData('abstract'), null);
     // Localized
     $article->setDiscipline($this->getData('discipline'), null);
     // Localized
     $article->setSubjectClass($this->getData('subjectClass'), null);
     // Localized
     $article->setSubject($this->getData('subject'), null);
     // Localized
     $article->setCoverageGeo($this->getData('coverageGeo'), null);
     // Localized
     $article->setCoverageChron($this->getData('coverageChron'), null);
     // Localized
     $article->setCoverageSample($this->getData('coverageSample'), null);
     // Localized
     $article->setType($this->getData('type'), null);
     // Localized
     $article->setSponsor($this->getData('sponsor'), null);
     // Localized
     $article->setCitations($this->getData('citations'));
     $article->setPages($this->getData('pages'));
     // Set some default values so the ArticleDAO doesn't complain when adding this article
     $article->setDateSubmitted(Core::getCurrentDate());
     $article->setStatus(STATUS_PUBLISHED);
     $article->setSubmissionProgress(0);
     $article->stampStatusModified();
     $article->setCurrentRound(1);
     $article->setFastTracked(1);
     $article->setHideAuthor(0);
     $article->setCommentsStatus(0);
     // Insert the article to get it's ID
     $articleDao->insertArticle($article);
     $articleId = $article->getId();
     // Add authors
     $authors = $this->getData('authors');
     for ($i = 0, $count = count($authors); $i < $count; $i++) {
         if ($authors[$i]['authorId'] > 0) {
             // Update an existing author
             $author =& $article->getAuthor($authors[$i]['authorId']);
             $isExistingAuthor = true;
         } else {
             // Create a new author
             $author = new Author();
             $isExistingAuthor = false;
         }
         if ($author != null) {
             $author->setSubmissionId($articleId);
             $author->setFirstName($authors[$i]['firstName']);
             $author->setMiddleName($authors[$i]['middleName']);
             $author->setLastName($authors[$i]['lastName']);
             if (array_key_exists('affiliation', $authors[$i])) {
                 $author->setAffiliation($authors[$i]['affiliation'], null);
             }
             $author->setCountry($authors[$i]['country']);
             $author->setEmail($authors[$i]['email']);
             $author->setUrl($authors[$i]['url']);
             if (array_key_exists('competingInterests', $authors[$i])) {
                 $author->setCompetingInterests($authors[$i]['competingInterests'], null);
             }
             $author->setBiography($authors[$i]['biography'], null);
             $author->setPrimaryContact($this->getData('primaryContact') == $i ? 1 : 0);
             $author->setSequence($authors[$i]['seq']);
             if ($isExistingAuthor == false) {
                 $article->addAuthor($author);
             }
         }
     }
     // Add the submission files as galleys
     import('classes.file.TemporaryFileManager');
     import('classes.file.ArticleFileManager');
     $tempFileIds = $this->getData('tempFileId');
     $temporaryFileManager = new TemporaryFileManager();
     $articleFileManager = new ArticleFileManager($articleId);
     foreach (array_keys($tempFileIds) as $locale) {
         $temporaryFile = $temporaryFileManager->getFile($tempFileIds[$locale], $user->getId());
         $fileId = null;
         if ($temporaryFile) {
             $fileId = $articleFileManager->temporaryFileToArticleFile($temporaryFile, ARTICLE_FILE_SUBMISSION);
             $fileType = $temporaryFile->getFileType();
             if (strstr($fileType, 'html')) {
                 import('classes.article.ArticleHTMLGalley');
                 $galley = new ArticleHTMLGalley();
             } else {
                 import('classes.article.ArticleGalley');
                 $galley = new ArticleGalley();
             }
             $galley->setArticleId($articleId);
             $galley->setFileId($fileId);
             $galley->setLocale($locale);
             if ($galley->isHTMLGalley()) {
                 $galley->setLabel('HTML');
             } else {
                 if (strstr($fileType, 'pdf')) {
                     $galley->setLabel('PDF');
                 } else {
                     if (strstr($fileType, 'postscript')) {
                         $galley->setLabel('Postscript');
                     } else {
                         if (strstr($fileType, 'xml')) {
                             $galley->setLabel('XML');
                         } else {
                             $galley->setLabel(__('common.untitled'));
                         }
                     }
                 }
             }
             $galleyDao =& DAORegistry::getDAO('ArticleGalleyDAO');
             $galleyDao->insertGalley($galley);
         }
         if ($locale == $journal->getPrimaryLocale()) {
             $article->setSubmissionFileId($fileId);
             $article->SetReviewFileId($fileId);
         }
         // Update file search index
         import('classes.search.ArticleSearchIndex');
         if (isset($galley)) {
             ArticleSearchIndex::updateFileIndex($galley->getArticleId(), ARTICLE_SEARCH_GALLEY_FILE, $galley->getFileId());
         }
     }
     // Designate this as the review version by default.
     $authorSubmissionDao =& DAORegistry::getDAO('AuthorSubmissionDAO');
     $authorSubmission =& $authorSubmissionDao->getAuthorSubmission($articleId);
     import('classes.submission.author.AuthorAction');
     AuthorAction::designateReviewVersion($authorSubmission, true);
     // Accept the submission
     $sectionEditorSubmission =& $sectionEditorSubmissionDao->getSectionEditorSubmission($articleId);
     $articleFileManager = new ArticleFileManager($articleId);
     $sectionEditorSubmission->setReviewFile($articleFileManager->getFile($article->getSubmissionFileId()));
     import('classes.submission.sectionEditor.SectionEditorAction');
     SectionEditorAction::recordDecision($sectionEditorSubmission, SUBMISSION_EDITOR_DECISION_ACCEPT);
     // Create signoff infrastructure
     $copyeditInitialSignoff = $signoffDao->build('SIGNOFF_COPYEDITING_INITIAL', ASSOC_TYPE_ARTICLE, $articleId);
     $copyeditAuthorSignoff = $signoffDao->build('SIGNOFF_COPYEDITING_AUTHOR', ASSOC_TYPE_ARTICLE, $articleId);
     $copyeditFinalSignoff = $signoffDao->build('SIGNOFF_COPYEDITING_FINAL', ASSOC_TYPE_ARTICLE, $articleId);
     $copyeditInitialSignoff->setUserId(0);
     $copyeditAuthorSignoff->setUserId($user->getId());
     $copyeditFinalSignoff->setUserId(0);
     $signoffDao->updateObject($copyeditInitialSignoff);
     $signoffDao->updateObject($copyeditAuthorSignoff);
     $signoffDao->updateObject($copyeditFinalSignoff);
     $layoutSignoff = $signoffDao->build('SIGNOFF_LAYOUT', ASSOC_TYPE_ARTICLE, $articleId);
     $layoutSignoff->setUserId(0);
     $signoffDao->updateObject($layoutSignoff);
     $proofAuthorSignoff = $signoffDao->build('SIGNOFF_PROOFREADING_AUTHOR', ASSOC_TYPE_ARTICLE, $articleId);
     $proofProofreaderSignoff = $signoffDao->build('SIGNOFF_PROOFREADING_PROOFREADER', ASSOC_TYPE_ARTICLE, $articleId);
     $proofLayoutEditorSignoff = $signoffDao->build('SIGNOFF_PROOFREADING_LAYOUT', ASSOC_TYPE_ARTICLE, $articleId);
     $proofAuthorSignoff->setUserId($user->getId());
     $proofProofreaderSignoff->setUserId(0);
     $proofLayoutEditorSignoff->setUserId(0);
     $signoffDao->updateObject($proofAuthorSignoff);
     $signoffDao->updateObject($proofProofreaderSignoff);
     $signoffDao->updateObject($proofLayoutEditorSignoff);
     import('classes.author.form.submit.AuthorSubmitForm');
     AuthorSubmitForm::assignEditors($article);
     $articleDao->updateArticle($article);
     // Add to end of editing queue
     import('classes.submission.editor.EditorAction');
     if (isset($galley)) {
         EditorAction::expediteSubmission($article);
     }
     if ($this->getData('destination') == "issue") {
         // Add to an existing issue
         $issueId = $this->getData('issueId');
         $this->scheduleForPublication($articleId, $issueId);
     }
     // Index article.
     import('classes.search.ArticleSearchIndex');
     ArticleSearchIndex::indexArticleMetadata($article);
     // Import the references list.
     $citationDao =& DAORegistry::getDAO('CitationDAO');
     $rawCitationList = $article->getCitations();
     $citationDao->importCitations($request, ASSOC_TYPE_ARTICLE, $articleId, $rawCitationList);
 }