/** * Split a string into a clean array of keywords * @param $text string * @param $allowWildcards boolean * @return array of keywords */ static function filterKeywords($text, $allowWildcards = false) { $minLength = Config::getVar('search', 'min_word_length'); $stopwords = self::_loadStopwords(); // Join multiple lines into a single string if (is_array($text)) { $text = join("\n", $text); } $cleanText = Core::cleanVar($text); // Remove punctuation $cleanText = String::regexp_replace('/[!"\\#\\$%\'\\(\\)\\.\\?@\\[\\]\\^`\\{\\}~]/', '', $cleanText); $cleanText = String::regexp_replace('/[\\+,:;&\\/<=>\\|\\\\]/', ' ', $cleanText); $cleanText = String::regexp_replace('/[\\*]/', $allowWildcards ? '%' : ' ', $cleanText); $cleanText = String::strtolower($cleanText); // Split into words $words = String::regexp_split('/\\s+/', $cleanText); // FIXME Do not perform further filtering for some fields, e.g., author names? // Remove stopwords $keywords = array(); foreach ($words as $k) { if (!isset($stopwords[$k]) && String::strlen($k) >= $minLength && !is_numeric($k)) { $keywords[] = String::substr($k, 0, SEARCH_KEYWORD_MAX_LENGTH); } } return $keywords; }
/** * Display user login form. * Redirect to user index page if user is already validated. */ function index($args, &$request) { $this->validate(); $this->setupTemplate($request); if (Validation::isLoggedIn()) { $request->redirect(null, 'user'); } if (Config::getVar('security', 'force_login_ssl') && $request->getProtocol() != 'https') { // Force SSL connections for login $request->redirectSSL(); } $sessionManager =& SessionManager::getManager(); $session =& $sessionManager->getUserSession(); $templateMgr =& TemplateManager::getManager(); // If the user wasn't expecting a login page, i.e. if they're new to the // site and want to submit a paper, it helps to explain why they need to // register. if ($request->getUserVar('loginMessage')) { $templateMgr->assign('loginMessage', $request->getUserVar('loginMessage')); } $templateMgr->assign('username', $session->getSessionVar('username')); $templateMgr->assign('remember', $request->getUserVar('remember')); $templateMgr->assign('source', $request->getUserVar('source')); $templateMgr->assign('showRemember', Config::getVar('general', 'session_lifetime') > 0); // For force_login_ssl with base_url[...]: make sure SSL used for login form $loginUrl = $this->_getLoginUrl($request); if (Config::getVar('security', 'force_login_ssl')) { $loginUrl = String::regexp_replace('/^http:/', 'https:', $loginUrl); } $templateMgr->assign('loginUrl', $loginUrl); $templateMgr->display('user/login.tpl'); }
/** * @see Filter::process() * @param $citationString string * @return MetadataDescription */ function &process($citationString) { $nullVar = null; $queryParams = array('demo' => '3', 'textlines' => $citationString); // Parscit web form - the result is (mal-formed) HTML if (is_null($result = $this->callWebService(PARSCIT_WEBSERVICE, $queryParams, XSL_TRANSFORMER_DOCTYPE_STRING, 'POST'))) { return $nullVar; } $result = html_entity_decode($result); // Detect errors. if (!String::regexp_match('/.*<algorithm[^>]+>.*<\\/algorithm>.*/s', $result)) { $translationParams = array('filterName' => $this->getDisplayName()); $this->addError(Locale::translate('submission.citations.filter.webserviceResultTransformationError', $translationParams)); return $nullVar; } // Screen-scrape the tagged portion and turn it into XML. $xmlResult = String::regexp_replace('/.*<algorithm[^>]+>(.*)<\\/algorithm>.*/s', '\\1', $result); $xmlResult = String::regexp_replace('/&/', '&', $xmlResult); // Transform the result into an array of meta-data. if (is_null($metadata = $this->transformWebServiceResults($xmlResult, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'parscit.xsl'))) { return $nullVar; } // Extract a publisher from the place string if possible. $metadata =& $this->fixPublisherNameAndLocation($metadata); return $this->getNlm30CitationDescriptionFromMetadataArray($metadata); }
/** * @see Filter::process() * @param $isbn string * @return MetadataDescription a looked up citation description * or null if the filter fails */ function &process($isbn) { $nullVar = null; // Instantiate the web service request $lookupParams = array('access_key' => $this->getApiKey(), 'index1' => 'isbn', 'results' => 'details,authors', 'value1' => $isbn); // Call the web service if (is_null($resultDOM =& $this->callWebService(ISBNDB_WEBSERVICE_URL, $lookupParams))) { return $nullVar; } // Transform and pre-process the web service result if (is_null($metadata =& $this->transformWebServiceResults($resultDOM, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'isbndb.xsl'))) { return $nullVar; } // Extract place and publisher from the combined entry. $metadata['publisher-loc'] = String::trimPunctuation(String::regexp_replace('/^(.+):.*/', '\\1', $metadata['place-publisher'])); $metadata['publisher-name'] = String::trimPunctuation(String::regexp_replace('/.*:([^,]+),?.*/', '\\1', $metadata['place-publisher'])); unset($metadata['place-publisher']); // Reformat the publication date $metadata['date'] = String::regexp_replace('/^[^\\d{4}]+(\\d{4}).*/', '\\1', $metadata['date']); // Clean non-numerics from ISBN $metadata['isbn'] = String::regexp_replace('/[^\\dX]*/', '', $isbn); // Set the publicationType $metadata['[@publication-type]'] = NLM_PUBLICATION_TYPE_BOOK; return $this->getNlmCitationDescriptionFromMetadataArray($metadata); }
/** * Format XML for single DC element. * @param $propertyName string * @param $value array * @param $multilingual boolean optional */ function formatElement($propertyName, $values, $multilingual = false) { if (!is_array($values)) { $values = array($values); } // Translate the property name to XML syntax. $openingElement = str_replace(array('[@', ']'), array(' ', ''), $propertyName); $closingElement = String::regexp_replace('/\\[@.*/', '', $propertyName); // Create the actual XML entry. $response = ''; foreach ($values as $key => $value) { if ($multilingual) { $key = str_replace('_', '-', $key); assert(is_array($value)); foreach ($value as $subValue) { if ($key == METADATA_DESCRIPTION_UNKNOWN_LOCALE) { $response .= "\t<{$openingElement}>" . OAIUtils::prepOutput($subValue) . "</{$closingElement}>\n"; } else { $response .= "\t<{$openingElement} xml:lang=\"{$key}\">" . OAIUtils::prepOutput($subValue) . "</{$closingElement}>\n"; } } } else { assert(is_scalar($value)); $response .= "\t<{$openingElement}>" . OAIUtils::prepOutput($value) . "</{$closingElement}>\n"; } } return $response; }
/** * @see Filter::process() * @param $citationDescription MetadataDescription * @return MetadataDescription */ function &process(&$citationDescription) { $nullVar = null; $searchParams = array('pid' => $this->getEmail(), 'noredirect' => 'true', 'format' => 'unixref'); $doi = $citationDescription->getStatement('pub-id[@pub-id-type="doi"]'); if (!empty($doi)) { // Directly look up the DOI with OpenURL 0.1 $searchParams['id'] = 'doi:' . $doi; } else { // Use OpenURL meta-data to search for the entry if (is_null($openUrlMetadata = $this->_prepareOpenUrlSearch($citationDescription))) { return $nullVar; } $searchParams += $openUrlMetadata; } // Call the CrossRef web service if (is_null($resultXml =& $this->callWebService(CROSSREF_WEBSERVICE_URL, $searchParams, XSL_TRANSFORMER_DOCTYPE_STRING))) { return $nullVar; } // Remove default name spaces from XML as CrossRef doesn't // set them reliably and element names are unique anyway. $resultXml = String::regexp_replace('/ xmlns="[^"]+"/', '', $resultXml); // Transform and process the web service result if (is_null($metadata =& $this->transformWebServiceResults($resultXml, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'crossref.xsl'))) { return $nullVar; } return $this->addMetadataArrayToNlmCitationDescription($metadata, $citationDescription); }
/** * @see Filter::process() * @param $input string * @return mixed array */ function &process(&$input) { // The default implementation assumes that raw citations are // separated with line endings. // 1) Remove empty lines $input = String::regexp_replace('/[\\r\\n]+/s', "\n", $input); // 2) Break up at line endings $output = explode("\n", $input); // TODO: Implement more complex treatment, e.g. filtering of // number strings at the beginning of each string, etc. return $output; }
/** * Get the HTML contents for this block. * @param $templateMgr object * @param $request PKPRequest * @return $string */ function getContents(&$templateMgr, $request = null) { if (!defined('SESSION_DISABLE_INIT')) { $session =& $request->getSession(); $templateMgr->assign_by_ref('userSession', $session); $templateMgr->assign('loggedInUsername', $session->getSessionVar('username')); $loginUrl = $request->url(null, null, 'login', 'signIn'); if (Config::getVar('security', 'force_login_ssl')) { $loginUrl = String::regexp_replace('/^http:/', 'https:', $loginUrl); } $templateMgr->assign('userBlockLoginUrl', $loginUrl); } return parent::getContents($templateMgr, $request); }
function getContents(&$templateMgr) { if (!defined('SESSION_DISABLE_INIT')) { $session =& Request::getSession(); $templateMgr->assign_by_ref('userSession', $session); $templateMgr->assign('loggedInUsername', $session->getSessionVar('username')); $loginUrl = Request::url(null, 'login', 'signIn'); $templateMgr->assign('paperPackageUpPlugin', "/index.php/mr2/PaperPackageUpPlugin/view/"); if (Config::getVar('security', 'force_login_ssl')) { $loginUrl = String::regexp_replace('/^http:/', 'https:', $loginUrl); } $templateMgr->assign('userBlockLoginUrl', $loginUrl); } return parent::getContents($templateMgr); }
/** * @see Filter::process() * @param $citationString string * @return MetadataDescription */ function &process($citationString) { $nullVar = null; $queryParams = array('textlines' => $citationString); // Parscit web form - the result is (mal-formed) HTML if (is_null($result = $this->callWebService(PARSCIT_WEBSERVICE, $queryParams, XSL_TRANSFORMER_DOCTYPE_STRING))) { return $nullVar; } // Screen-scrape the tagged portion and turn it into XML $xmlResult = String::regexp_replace('/.*<algorithm[^>]+>(.*)<\\/algorithm>.*/s', '\\1', html_entity_decode($result)); $xmlResult = String::regexp_replace('/&/', '&', $xmlResult); // Transform the result into an array of meta-data if (is_null($metadata = $this->transformWebServiceResults($xmlResult, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'parscit.xsl'))) { return $nullVar; } // Extract a publisher from the place string if possible $metadata =& $this->fixPublisherNameAndLocation($metadata); return $this->addMetadataArrayToNlmCitationDescription($metadata); }
/** * @see Filter::process() * @param $input string * @return mixed array */ function &process(&$input) { // The default implementation assumes that raw citations are // separated with line endings. // 1) Remove empty lines and normalize line endings. $input = String::regexp_replace('/[\\r\\n]+/s', "\n", $input); // 2) Remove trailing/leading line breaks. $input = trim($input, "\n"); // 3) Break up at line endings. if (empty($input)) { $citations = array(); } else { $citations = explode("\n", $input); } // 4) Remove numbers from the beginning of each citation. foreach ($citations as $index => $citation) { $citations[$index] = String::regexp_replace('/^\\s*[\\[#]?[0-9]+[.)\\]]?\\s*/', '', $citation); } return $citations; }
function getContents(&$templateMgr) { if (!defined('SESSION_DISABLE_INIT')) { $session =& Request::getSession(); $templateMgr->assign_by_ref('userSession', $session); $templateMgr->assign('loggedInUsername', $session->getSessionVar('username')); $loginUrl = Request::url(null, 'login', 'signIn'); // if the page is not ssl enabled, and force_login_ssl is set, this flag will present a link instead of the form $forceSSL = false; if (Config::getVar('security', 'force_login_ssl')) { if (Request::getProtocol() != 'https') { $loginUrl = Request::url(null, 'login'); $forceSSL = true; } $loginUrl = String::regexp_replace('/^http:/', 'https:', $loginUrl); } $templateMgr->assign('userBlockLoginSSL', $forceSSL); $templateMgr->assign('userBlockLoginUrl', $loginUrl); } return parent::getContents($templateMgr); }
/** * @see PubIdPlugin::getPubId() */ function getPubId($pubObject, $preview = false) { // Determine the type of the publishing object. $pubObjectType = $this->getPubObjectType($pubObject); // Initialize variables for publication objects. $publicationFormat = $pubObjectType == 'PublicationFormat' ? $pubObject : null; $monograph = $pubObjectType == 'Monograph' ? $pubObject : null; // Get the press id of the object. if (in_array($pubObjectType, array('PublicationFormat', 'Monograph'))) { $pressId = $pubObject->getContextId(); } else { return null; } $press = $this->_getPress($pressId); if (!$press) { return null; } $pressId = $press->getId(); // If we already have an assigned DOI, use it. $storedDOI = $pubObject->getStoredPubId('doi'); if ($storedDOI) { return $storedDOI; } // Retrieve the DOI prefix. $doiPrefix = $this->getSetting($pressId, 'doiPrefix'); if (empty($doiPrefix)) { return null; } // Generate the DOI suffix. $doiSuffixGenerationStrategy = $this->getSetting($pressId, 'doiSuffix'); switch ($doiSuffixGenerationStrategy) { case 'customId': $doiSuffix = $pubObject->getData('doiSuffix'); break; case 'pattern': $doiSuffix = $this->getSetting($pressId, "doi{$pubObjectType}SuffixPattern"); // %p - press initials $doiSuffix = String::regexp_replace('/%p/', String::strtolower($press->getPath()), $doiSuffix); if ($publicationFormat) { // %m - monograph id, %f - publication format id $doiSuffix = String::regexp_replace('/%m/', $publicationFormat->getMonographId(), $doiSuffix); $doiSuffix = String::regexp_replace('/%f/', $publicationFormat->getId(), $doiSuffix); } if ($monograph) { // %m - monograph id $doiSuffix = String::regexp_replace('/%m/', $monograph->getId(), $doiSuffix); } break; default: $doiSuffix = String::strtolower($press->getPath()); if ($publicationFormat) { $doiSuffix .= '.' . $publicationFormat->getMonographId(); $doiSuffix .= '.' . $publicationFormat->getId(); } if ($monograph) { $doiSuffix .= '.' . $monograph->getId(); } } if (empty($doiSuffix)) { return null; } // Join prefix and suffix. $doi = $doiPrefix . '/' . $doiSuffix; if (!$preview) { // Save the generated DOI. $this->setStoredPubId($pubObject, $pubObjectType, $doi); } return $doi; }
/** * Converts a string with a single person * to an NLM name description. * * TODO: add initials from all given names to initials * element * * @param $personString string * @param $title boolean true to parse for title * @param $degrees boolean true to parse for degrees * @return MetadataDescription an NLM name description or null * if the string could not be converted */ function &_parsePersonString($personString, $title, $degrees) { // Expressions to parse person strings, ported from CiteULike person // plugin, see http://svn.citeulike.org/svn/plugins/person.tcl static $personRegex = array('title' => '(?:His (?:Excellency|Honou?r)\\s+|Her (?:Excellency|Honou?r)\\s+|The Right Honou?rable\\s+|The Honou?rable\\s+|Right Honou?rable\\s+|The Rt\\.? Hon\\.?\\s+|The Hon\\.?\\s+|Rt\\.? Hon\\.?\\s+|Mr\\.?\\s+|Ms\\.?\\s+|M\\/s\\.?\\s+|Mrs\\.?\\s+|Miss\\.?\\s+|Dr\\.?\\s+|Sir\\s+|Dame\\s+|Prof\\.?\\s+|Professor\\s+|Doctor\\s+|Mister\\s+|Mme\\.?\\s+|Mast(?:\\.|er)?\\s+|Lord\\s+|Lady\\s+|Madam(?:e)?\\s+|Priv\\.-Doz\\.\\s+)+', 'degrees' => '(,\\s+(?:[A-Z\\.]+))+', 'initials' => '(?:(?:[A-Z]\\.){1,3}[A-Z]\\.?)|(?:(?:[A-Z]\\.\\s){1,3}[A-Z]\\.?)|(?:[A-Z]{1,4})|(?:(?:[A-Z]\\.-?){1,4})|(?:(?:[A-Z]\\.-?){1,3}[A-Z]\\.?)|(?:(?:[A-Z]-){1,3}[A-Z])|(?:(?:[A-Z]\\s){1,3}[A-Z]\\.?)|(?:(?:[A-Z]-){1,3}[A-Z]\\.?)', 'prefix' => 'Dell(?:[a|e])?(?:\\s|$)|Dalle(?:\\s|$)|D[a|e]ll\'(?:\\s|$)|Dela(?:\\s|$)|Del(?:\\s|$)|[Dd]e(?:\\s|$)(?:La(?:\\s|$)|Los(?:\\s|$))?|[Dd]e(?:\\s|$)|[Dd][a|i|u](?:\\s|$)|L[a|e|o](?:\\s|$)|[D|L|O]\'|St\\.?(?:\\s|$)|San(?:\\s|$)|[Dd]en(?:\\s|$)|[Vv]on(?:\\s|$)(?:[Dd]er(?:\\s|$))?|(?:[Ll][ea](?:\\s|$))?[Vv]an(?:\\s|$)(?:[Dd]e(?:n|r)?(?:\\s|$))?', 'givenName' => '(?:[^ \\t\\n\\r\\f\\v,.;()]{2,}|[^ \\t\\n\\r\\f\\v,.;()]{2,}\\-[^ \\t\\n\\r\\f\\v,.;()]{2,})'); // The expressions for given name, suffix and surname are the same $personRegex['surname'] = $personRegex['suffix'] = $personRegex['givenName']; $personRegex['double-surname'] = "(?:" . $personRegex['surname'] . "\\s)*" . $personRegex['surname']; // Shortcut for prefixed surname $personRegexPrefixedSurname = "(?P<prefix>(?:" . $personRegex['prefix'] . ")?)(?P<surname>" . $personRegex['surname'] . ")"; $personRegexPrefixedDoubleSurname = "(?P<prefix>(?:" . $personRegex['prefix'] . ")?)(?P<surname>" . $personRegex['double-surname'] . ")"; // Instantiate the target person description $personDescription = new MetadataDescription('lib.pkp.plugins.metadata.nlm30.schema.Nlm30NameSchema', $this->_assocType); // Clean the person string $personString = trim($personString); // 1. Extract title and degree from the person string and use this as suffix $suffixString = ''; $results = array(); if ($title && String::regexp_match_get('/^(' . $personRegex['title'] . ')/i', $personString, $results)) { $suffixString = trim($results[1], ',:; '); $personString = String::regexp_replace('/^(' . $personRegex['title'] . ')/i', '', $personString); } if ($degrees && String::regexp_match_get('/(' . $personRegex['degrees'] . ')$/i', $personString, $results)) { $degreesArray = explode(',', trim($results[1], ',')); foreach ($degreesArray as $key => $degree) { $degreesArray[$key] = String::trimPunctuation($degree); } $suffixString .= ' - ' . implode('; ', $degreesArray); $personString = String::regexp_replace('/(' . $personRegex['degrees'] . ')$/i', '', $personString); } if (!empty($suffixString)) { $personDescription->addStatement('suffix', $suffixString); } // Space initials when followed by a given name or last name. $personString = String::regexp_replace('/([A-Z])\\.([A-Z][a-z])/', '\\1. \\2', $personString); // 2. Extract names and initials from the person string // The parser expressions are ordered by specificity. The most specific expressions // come first. Only if these specific expressions don't work will we turn to less // specific ones. This avoids parsing errors. It also explains why we don't use the // ?-quantifier for optional elements like initials or middle name where they could // be misinterpreted. $personExpressions = array('/^' . $personRegexPrefixedSurname . '$/i', '/^(?P<initials>' . $personRegex['initials'] . ')\\s' . $personRegexPrefixedSurname . '$/', '/^' . $personRegexPrefixedSurname . ',?\\s(?P<initials>' . $personRegex['initials'] . ')$/', '/^' . $personRegexPrefixedDoubleSurname . ',\\s(?P<givenName>' . $personRegex['givenName'] . ')\\s(?P<initials>' . $personRegex['initials'] . ')$/', '/^(?P<givenName>' . $personRegex['givenName'] . ')\\s(?P<initials>' . $personRegex['initials'] . ')\\s' . $personRegexPrefixedSurname . '$/', '/^' . $personRegexPrefixedDoubleSurname . ',\\s(?P<givenName>(?:' . $personRegex['givenName'] . '\\s)+)(?P<initials>' . $personRegex['initials'] . ')$/', '/^(?P<givenName>(?:' . $personRegex['givenName'] . '\\s)+)(?P<initials>' . $personRegex['initials'] . ')\\s' . $personRegexPrefixedSurname . '$/', '/^' . $personRegexPrefixedDoubleSurname . ',(?P<givenName>(?:\\s' . $personRegex['givenName'] . ')+)$/', '/^(?P<givenName>(?:' . $personRegex['givenName'] . '\\s)+)' . $personRegexPrefixedSurname . '$/', '/^\\s*(?P<surname>' . $personRegex['surname'] . ')(?P<suffix>(?:\\s+' . $personRegex['suffix'] . ')?)\\s*,\\s*(?P<initials>(?:' . $personRegex['initials'] . ')?)\\s*\\((?P<givenName>(?:\\s*' . $personRegex['givenName'] . ')+)\\s*\\)\\s*(?P<prefix>(?:' . $personRegex['prefix'] . ')?)$/', '/^(?P<givenName>' . $personRegex['givenName'] . ')\\.(?P<surname>' . $personRegex['double-surname'] . ')$/', '/^(?P<surname>.*)$/'); $results = array(); foreach ($personExpressions as $expressionId => $personExpression) { if ($nameFound = String::regexp_match_get($personExpression, $personString, $results)) { // Given names if (!empty($results['givenName'])) { // Split given names $givenNames = explode(' ', trim($results['givenName'])); foreach ($givenNames as $givenName) { $personDescription->addStatement('given-names', $givenName); unset($givenName); } } // Initials (will also be saved as given names) if (!empty($results['initials'])) { $results['initials'] = str_replace(array('.', '-', ' '), array('', '', ''), $results['initials']); for ($initialNum = 0; $initialNum < String::strlen($results['initials']); $initialNum++) { $initial = $results['initials'][$initialNum]; $personDescription->addStatement('given-names', $initial); unset($initial); } } // Surname if (!empty($results['surname'])) { // Correct all-upper surname if (strtoupper($results['surname']) == $results['surname']) { $results['surname'] = ucwords(strtolower($results['surname'])); } $personDescription->addStatement('surname', $results['surname']); } // Prefix/Suffix foreach (array('prefix', 'suffix') as $propertyName) { if (!empty($results[$propertyName])) { $results[$propertyName] = trim($results[$propertyName]); $personDescription->addStatement($propertyName, $results[$propertyName]); } } break; } } return $personDescription; }
/** * @see Form::fetch() */ function fetch($request) { $templateMgr = TemplateManager::getManager($request); // The form description depends on the current state // of the selection process: do we select a filter template // or configure the settings of a selected template? $filter =& $this->getFilter(); if (is_a($filter, 'Filter')) { $displayName = $filter->getDisplayName(); $templateMgr->assign('filterDisplayName', $displayName); if (count($filter->getSettings())) { // We need a filter specific translation key so that we // can explain the filter's configuration options. // We use the display name to generate such a key as this // is probably easiest for translators to understand. // This also has the advantage that we can explain // composite filters individually. // FIXME: When we start to translate display names then // please make sure that you use the en-US key for this // processing. Alternatively we might want to introduce // an alphanumeric "filter key" to the filters table. $filterKey = String::regexp_replace('/[^a-zA-Z0-9]/', '', $displayName); $filterKey = strtolower(substr($filterKey, 0, 1)) . substr($filterKey, 1); $formDescriptionKey = $this->getDescription() . '.' . $filterKey; } else { $formDescriptionKey = $this->getDescription() . 'Confirm'; } } else { $templateMgr->assign('filterDisplayName', ''); $formDescriptionKey = $this->getDescription() . 'Template'; } $templateMgr->assign('formTitle', $this->getTitle()); $templateMgr->assign('formDescription', $formDescriptionKey); return parent::fetch($request); }
/** * @see PubIdPlugin::getPubId() */ function getPubId($pubObject, $preview = false) { $urn = $pubObject->getStoredPubId($this->getPubIdType()); if (!$urn) { // Determine the type of the publishing object $pubObjectType = $this->getPubObjectType($pubObject); // Initialize variables for publication objects $issue = $pubObjectType == 'Issue' ? $pubObject : null; $article = $pubObjectType == 'Article' ? $pubObject : null; $galley = $pubObjectType == 'Galley' ? $pubObject : null; // Get the journal id of the object if (in_array($pubObjectType, array('Issue', 'Article'))) { $journalId = $pubObject->getJournalId(); } else { // Retrieve the published article assert(is_a($pubObject, 'SubmissionFile')); $articleDao = DAORegistry::getDAO('ArticleDAO'); $article = $articleDao->getById($pubObject->getArticleId(), null, true); if (!$article) { return null; } // Now we can identify the journal $journalId = $article->getJournalId(); } // get the journal $journal = $this->getJournal($journalId); if (!$journal) { return null; } $journalId = $journal->getId(); // Check whether URNs are enabled for the given object type $urnEnabled = $this->getSetting($journalId, "enable{$pubObjectType}URN") == '1'; if (!$urnEnabled) { return null; } // Retrieve the issue if (!is_a($pubObject, 'Issue')) { assert(!is_null($article)); $issueDao = DAORegistry::getDAO('IssueDAO'); $issue = $issueDao->getIssueByArticleId($article->getId(), $journal->getId(), true); } // Retrieve the URN prefix $urnPrefix = $this->getSetting($journal->getId(), 'urnPrefix'); if (empty($urnPrefix)) { return null; } // Generate the URN suffix $urnSuffixSetting = $this->getSetting($journal->getId(), 'urnSuffix'); switch ($urnSuffixSetting) { case 'publisherId': $urnSuffix = (string) call_user_func_array(array($pubObject, "getBest{$pubObjectType}Id"), array(&$journal)); // When the suffix equals the object's ID then // require an object-specific prefix to be sure that the suffix is unique if ($pubObjectType != 'Article' && $urnSuffix === (string) $pubObject->getId()) { $urnSuffix = strtolower_codesafe($pubObjectType[0]) . $urnSuffix; } if (!empty($urnSuffix)) { $urn = $urnPrefix . $urnSuffix; if ($this->getSetting($journal->getId(), 'checkNo')) { $urn .= $this->_calculateCheckNo($urn); } } break; case 'customIdentifier': $urnSuffix = $pubObject->getData('urnSuffix'); if (!empty($urnSuffix)) { $urn = $urnPrefix . $urnSuffix; } break; case 'pattern': $urnSuffix = $this->getSetting($journal->getId(), "urn{$pubObjectType}SuffixPattern"); // %j - journal initials $urnSuffix = String::regexp_replace('/%j/', String::strtolower($journal->getAcronym($journal->getPrimaryLocale())), $urnSuffix); // %x - custom identifier if ($pubObject->getStoredPubId('publisher-id')) { $urnSuffix = String::regexp_replace('/%x/', $pubObject->getStoredPubId('publisher-id'), $urnSuffix); } if ($issue) { // %v - volume number $urnSuffix = String::regexp_replace('/%v/', $issue->getVolume(), $urnSuffix); // %i - issue number $urnSuffix = String::regexp_replace('/%i/', $issue->getNumber(), $urnSuffix); // %Y - year $urnSuffix = String::regexp_replace('/%Y/', $issue->getYear(), $urnSuffix); } if ($article) { // %a - article id $urnSuffix = String::regexp_replace('/%a/', $article->getId(), $urnSuffix); // %p - page number if ($article->getPages()) { $urnSuffix = String::regexp_replace('/%p/', $article->getPages(), $urnSuffix); } } if ($galley) { // %g - galley id $urnSuffix = String::regexp_replace('/%g/', $galley->getId(), $urnSuffix); } if (!empty($urnSuffix)) { $urn = $urnPrefix . $urnSuffix; if ($this->getSetting($journal->getId(), 'checkNo')) { $urn .= $this->_calculateCheckNo($urn); } } break; default: $urnSuffix = String::strtolower($journal->getAcronym($journal->getPrimaryLocale())); if ($issue) { $urnSuffix .= '.v' . $issue->getVolume() . 'i' . $issue->getNumber(); } else { $urnSuffix .= '.v%vi%i'; } if ($article) { $urnSuffix .= '.' . $article->getId(); } if ($galley) { $urnSuffix .= '.g' . $galley->getId(); } $urn = $urnPrefix . $urnSuffix; if ($this->getSetting($journal->getId(), 'checkNo')) { $urn .= $this->_calculateCheckNo($urn); } } if ($urn && !$preview) { $this->setStoredPubId($pubObject, $pubObjectType, $urn); } } return $urn; }
/** * @see PubIdPlugin::getPubId() */ function getPubId(&$pubObject, $preview = false) { $doi = null; if (!$this->isExcluded($pubObject)) { // Determine the type of the publishing object. $pubObjectType = $this->getPubObjectType($pubObject); // Initialize variables for publication objects. $issue = $pubObjectType == 'Issue' ? $pubObject : null; $article = $pubObjectType == 'Article' ? $pubObject : null; $galley = $pubObjectType == 'Galley' ? $pubObject : null; $suppFile = $pubObjectType == 'SuppFile' ? $pubObject : null; // Get the journal id of the object. if (in_array($pubObjectType, array('Issue', 'Article'))) { $journalId = $pubObject->getJournalId(); } else { // Retrieve the published article. assert(is_a($pubObject, 'ArticleFile')); $articleDao =& DAORegistry::getDAO('PublishedArticleDAO'); /* @var $articleDao PublishedArticleDAO */ $article =& $articleDao->getPublishedArticleByArticleId($pubObject->getArticleId(), null, true); if (!$article) { return null; } // Now we can identify the journal. $journalId = $article->getJournalId(); } $journal =& $this->getJournal($journalId); if (!$journal) { return null; } $journalId = $journal->getId(); // Check whether DOIs are enabled for the given object type. $doiEnabled = $this->getSetting($journalId, "enable{$pubObjectType}Doi") == '1'; if (!$doiEnabled) { return null; } // If we already have an assigned DOI, use it. $storedDOI = $pubObject->getStoredPubId('doi'); if ($storedDOI) { return $storedDOI; } // Retrieve the issue. if (!is_a($pubObject, 'Issue')) { assert(!is_null($article)); $issueDao =& DAORegistry::getDAO('IssueDAO'); /* @var $issueDao IssueDAO */ $issue =& $issueDao->getIssueByArticleId($article->getId(), $journal->getId(), true); } if ($issue && $journalId != $issue->getJournalId()) { return null; } // Retrieve the DOI prefix. $doiPrefix = $this->getSetting($journalId, 'doiPrefix'); if (empty($doiPrefix)) { return null; } // Generate the DOI suffix. $doiSuffixGenerationStrategy = $this->getSetting($journalId, 'doiSuffix'); switch ($doiSuffixGenerationStrategy) { case 'publisherId': switch ($pubObjectType) { case 'Issue': $doiSuffix = (string) $pubObject->getBestIssueId($journal); break; case 'Article': $doiSuffix = (string) $pubObject->getBestArticleId($journal); break; case 'Galley': $doiSuffix = (string) $pubObject->getBestGalleyId($journal); break; case 'SuppFile': $doiSuffix = (string) $pubObject->getBestSuppFileId($journal); break; default: assert(false); } // When the suffix equals the object's ID then // require an object-specific prefix to be sure that // the suffix is unique. if ($pubObjectType != 'Article' && $doiSuffix === (string) $pubObject->getId()) { $doiSuffix = strtolower_codesafe($pubObjectType[0]) . $doiSuffix; } break; case 'customId': $doiSuffix = $pubObject->getData('doiSuffix'); break; case 'pattern': $doiSuffix = $this->getSetting($journalId, "doi{$pubObjectType}SuffixPattern"); // %j - journal initials $doiSuffix = String::regexp_replace('/%j/', String::strtolower($journal->getLocalizedSetting('initials', $journal->getPrimaryLocale())), $doiSuffix); // %x - custom identifier if ($pubObject->getStoredPubId('publisher-id')) { $doiSuffix = String::regexp_replace('/%x/', $pubObject->getStoredPubId('publisher-id'), $doiSuffix); } if ($issue) { // %v - volume number $doiSuffix = String::regexp_replace('/%v/', $issue->getVolume(), $doiSuffix); // %i - issue number $doiSuffix = String::regexp_replace('/%i/', $issue->getNumber(), $doiSuffix); // %Y - year $doiSuffix = String::regexp_replace('/%Y/', $issue->getYear(), $doiSuffix); } if ($article) { // %a - article id $doiSuffix = String::regexp_replace('/%a/', $article->getId(), $doiSuffix); // %p - page number if ($article->getPages()) { $doiSuffix = String::regexp_replace('/%p/', $article->getPages(), $doiSuffix); } } if ($galley) { // %g - galley id $doiSuffix = String::regexp_replace('/%g/', $galley->getId(), $doiSuffix); } if ($suppFile) { // %s - supp file id $doiSuffix = String::regexp_replace('/%s/', $suppFile->getId(), $doiSuffix); } break; default: $doiSuffix = String::strtolower($journal->getLocalizedSetting('initials', $journal->getPrimaryLocale())); if ($issue) { $doiSuffix .= '.v' . $issue->getVolume() . 'i' . $issue->getNumber(); } else { $doiSuffix .= '.v%vi%i'; } if ($article) { $doiSuffix .= '.' . $article->getId(); } if ($galley) { $doiSuffix .= '.g' . $galley->getId(); } if ($suppFile) { $doiSuffix .= '.s' . $suppFile->getId(); } } if (empty($doiSuffix)) { return null; } // Join prefix and suffix. $doi = $doiPrefix . '/' . $doiSuffix; if (!$preview) { // Save the generated DOI. $this->setStoredPubId($pubObject, $pubObjectType, $doi); } } return $doi; }
/** * Convert limited HTML into a string. * @param $html string * @return string */ static function html2text($html) { $html = String::regexp_replace('/<[\\/]?p>/', "\n", $html); $html = String::regexp_replace('/<li>/', '• ', $html); $html = String::regexp_replace('/<\\/li>/', "\n", $html); $html = String::regexp_replace('/<br[ ]?[\\/]?>/', "\n", $html); $html = html_entity_decode(strip_tags($html), ENT_COMPAT, 'UTF-8'); return $html; }
/** * Suggest a username given the first and last names. * @return string */ function suggestUsername($firstName, $lastName) { $initial = String::substr($firstName, 0, 1); $suggestion = String::regexp_replace('/[^a-zA-Z0-9_-]/', '', String::strtolower($initial . $lastName)); $userDao =& DAORegistry::getDAO('UserDAO'); for ($i = ''; $userDao->userExistsByUsername($suggestion . $i); $i++) { } return $suggestion . $i; }
function cleanFileName($var) { $var = String::regexp_replace('/[^\\w\\-\\.]/', '', $var); if (!FilesHandler::fileNameFilter($var)) { $var = time() . ''; } return $var; }
/** * Create an identifier element with the object's DOI. * @param $object Issue|PublishedArticle|ArticleGalley|SuppFile * @param $relationType string One of the DATACITE_RELTYPE_* constants. * @return XMLNode|DOMImplementation|null Can be null if the given ID Type * has not been assigned to the given object. */ function &_relatedIdentifierElement(&$object, $relationType) { $id = $object->getPubId('doi'); if (empty($id)) { return $nullVar; $nullVar = null; } if ($this->getTestMode()) { $id = String::regexp_replace('#^[^/]+/#', DATACITE_API_TESTPREFIX . '/', $id); } return $this->createElementWithText('relatedIdentifier', $id, array('relatedIdentifierType' => DATACITE_IDTYPE_DOI, 'relationType' => $relationType)); }
/** * Generate a unique username for a user based on the user's name. * @param $user ImportedUser the user to be modified by this function */ function generateUsername(&$user) { $userDao =& DAORegistry::getDAO('UserDAO'); $baseUsername = String::regexp_replace('/[^A-Z0-9]/i', '', $user->getLastName()); if (empty($baseUsername)) { $baseUsername = String::regexp_replace('/[^A-Z0-9]/i', '', $user->getFirstName()); } if (empty($username)) { // Default username if we can't use the user's last or first name $baseUsername = '******'; } for ($username = $baseUsername, $i = 1; $userDao->userExistsByUsername($username, true); $i++) { $username = $baseUsername . $i; } $user->setUsername($username); }
/** * @see FunctionalDoiExportTest::cleanXml() */ protected function cleanXml($xml) { // Fix URLs. $xml = String::regexp_replace('#http://[^\\s]+/index.php/(test|index)#', 'http://example.com/index.php/test', $xml); // Fix sent date. $xml = String::regexp_replace('/<SentDate>[0-9]{12}<\\/SentDate>/', '<SentDate>201111082218</SentDate>', $xml); // Fix version. $xml = String::regexp_replace('/(<MessageNote>[^<]*)([0-9]\\.){4}(<\\/MessageNote>)/', '\\1x.x.x.x.\\3', $xml); return parent::cleanXml($xml); }
/** * Translate query keywords. * @param $searchPhrase string * @return The translated search phrase. */ function _translateSearchPhrase($searchPhrase, $backwards = false) { static $queryKeywords; if (is_null($queryKeywords)) { // Query keywords. $queryKeywords = array(String::strtoupper(__('search.operator.not')) => 'NOT', String::strtoupper(__('search.operator.and')) => 'AND', String::strtoupper(__('search.operator.or')) => 'OR'); } if ($backwards) { $translationTable = array_flip($queryKeywords); } else { $translationTable = $queryKeywords; } // Translate the search phrase. foreach ($translationTable as $translateFrom => $translateTo) { $searchPhrase = String::regexp_replace("/(^|\\s){$translateFrom}(\\s|\$)/i", "\\1{$translateTo}\\2", $searchPhrase); } return $searchPhrase; }
/** * Build the test file UI as understood by Selenium. * @param string $fileFormat The test file format, e.g. "pdf". * @return string */ private function _getTestFileUri($fileFormat) { $testFile = 'tests/functional/pages/editor/test-files/test-article.' . $fileFormat; self::assertTrue(file_exists($testFile)); $testFile = realpath($testFile); if (Core::isWindows()) { $testFile = str_replace(DIRECTORY_SEPARATOR, '/', $testFile); $testFile = String::regexp_replace('%^[A-Z]:/%', '/', $testFile); } $testFile = 'file://' . $testFile; return $testFile; }
/** * Mark an object as "registered" * by saving it's DOI to the object's * "registeredDoi" setting. * We prefix the setting with the plug-in's * id so that we do not get name clashes * when several DOI registration plug-ins * are active at the same time. * @parem $request Request * @param $object Issue|PublishedArticle|ArticleGalley * @parem $testPrefix string */ function markRegistered($request, $object, $testPrefix) { $registeredDoi = $object->getPubId('doi'); assert(!empty($registeredDoi)); if ($this->isTestMode($request)) { $registeredDoi = String::regexp_replace('#^[^/]+/#', $testPrefix . '/', $registeredDoi); } $this->saveRegisteredDoi($object, $registeredDoi); }
/** * Retrieve the DOI of an object. The DOI will be * patched if we are in test mode. * @param $object Issue|PublishedArticle|ArticleGalley * @return string */ function _getDoi(&$object) { $doi = $object->getPubId('doi'); if (!empty($doi) && $this->getTestMode()) { $doi = String::regexp_replace('#^[^/]+/#', MEDRA_WS_TESTPREFIX . '/', $doi); } return $doi; }
function _cleanFileName($var) { $var = String::regexp_replace('/[^\\w\\-\\.]/', '', $var); if (!$this->_fileNameFilter($var)) { $var = time() . ''; } return $var; }
/** * Override the built-in smarty truncate modifier to support mbstring and HTML tags * text properly, if possible. */ function smartyTruncate($string, $length = 80, $etc = '...', $break_words = false, $middle = false, $skip_tags = true) { if ($length == 0) { return ''; } if (String::strlen($string) > $length) { $originalLength = String::strlen($string); if ($skip_tags) { if ($middle) { $tagsReverse = array(); $this->_removeTags($string, $tagsReverse, true, $length); } $tags = array(); $string = $this->_removeTags($string, $tags, false, $length); } $length -= min($length, String::strlen($etc)); if (!$middle) { if (!$break_words) { $string = String::regexp_replace('/\\s+?(\\S+)?$/', '', String::substr($string, 0, $length + 1)); } else { $string = String::substr($string, 0, $length + 1); } if ($skip_tags) { $string = $this->_reinsertTags($string, $tags); } return $this->_closeTags($string) . $etc; } else { $firstHalf = String::substr($string, 0, $length / 2); $secondHalf = String::substr($string, -$length / 2); if ($break_words) { if ($skip_tags) { $firstHalf = $this->_reinsertTags($firstHalf, $tags); $secondHalf = $this->reinsertTags($secondHalf, $tagsReverse, true); return $this->_closeTags($firstHalf) . $etc . $this->_closeTags($secondHalf, true); } else { return $firstHalf . $etc . $secondHalf; } } else { for ($i = $length / 2; $string[$i] != ' '; $i++) { $firstHalf = String::substr($string, 0, $i + 1); } for ($i = $length / 2; String::substr($string, -$i, 1) != ' '; $i++) { $secondHalf = String::substr($string, -$i - 1); } if ($skip_tags) { $firstHalf = $this->_reinsertTags($firstHalf, $tags); $secondHalf = $this->reinsertTags($secondHalf, $tagsReverse, strlen($string)); return $this->_closeTags($firstHalf) . $etc . $this->_closeTags($secondHalf, true); } else { return $firstHalf . $etc . $secondHalf; } } } } else { return $string; } }
/** * Check whether the given DOI appears on the object's page. * @param $objectType string * @param $expectedDoi string/boolean the expected DOI or false if * no DOI should be present. */ private function checkDoiDisplay($objectType, $expectedDoi) { $url = $this->getUrl($objectType, 1); $this->verifyAndOpen($url); try { if ($expectedDoi === false) { $visibleElement = $this->pages[$objectType]['visible']; if (strpos($objectType, 'citations') !== false) { $this->assertNotText($visibleElement, 'doi'); } else { $this->assertElementNotPresent($visibleElement); } foreach (array('DC-meta', 'Google-meta') as $doiMetaAttribute) { if (isset($this->pages[$objectType][$doiMetaAttribute])) { $doiMetaElement = String::regexp_replace('/@[^@]+$/', '', $this->pages[$objectType][$doiMetaAttribute]); $this->assertElementNotPresent($doiMetaElement); } } } else { $expectedDoiPattern = "(^|.* ){$expectedDoi}(\$| .*)"; $doiText = $this->getText($this->pages[$objectType]['visible']); if ($expectedDoi == '10.1234/t.v1i1.1') { $fata = 'morgana'; } $this->assertText($this->pages[$objectType]['visible'], $expectedDoiPattern); foreach (array('DC-meta', 'Google-meta') as $doiMetaAttribute) { if (isset($this->pages[$objectType][$doiMetaAttribute])) { $this->assertAttribute($this->pages[$objectType][$doiMetaAttribute], $expectedDoiPattern); } } } } catch (Exception $e) { throw $this->improveException($e, $objectType); } }