File indexing completed on 2024-06-02 05:09:30

0001 /***************************************************************************
0002  *   SPDX-License-Identifier: GPL-2.0-or-later
0003  *                                                                         *
0004  *   SPDX-FileCopyrightText: 2004-2019 Thomas Fischer <fischer@unix-ag.uni-kl.de>
0005  *   SPDX-FileCopyrightText: 2013 Yngve I. Levinsen <yngve.inntjore.levinsen@cern.ch>
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or modify  *
0008  *   it under the terms of the GNU General Public License as published by  *
0009  *   the Free Software Foundation; either version 2 of the License, or     *
0010  *   (at your option) any later version.                                   *
0011  *                                                                         *
0012  *   This program is distributed in the hope that it will be useful,       *
0013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0015  *   GNU General Public License for more details.                          *
0016  *                                                                         *
0017  *   You should have received a copy of the GNU General Public License     *
0018  *   along with this program; if not, see <https://www.gnu.org/licenses/>. *
0019  ***************************************************************************/
0020 
0021 #include "onlinesearchcernds.h"
0022 
0023 #include <QUrlQuery>
0024 
0025 #include <KLocalizedString>
0026 
0027 #include "logging_networking.h"
0028 
0029 OnlineSearchCERNDS::OnlineSearchCERNDS(QObject *parent)
0030         : OnlineSearchSimpleBibTeXDownload(parent)
0031 {
0032     /// nothing
0033 }
0034 
0035 QString OnlineSearchCERNDS::label() const
0036 {
0037     return i18n("CERN Document Server");
0038 }
0039 
0040 QUrl OnlineSearchCERNDS::homepage() const
0041 {
0042     return QUrl(QStringLiteral("https://cds.cern.ch/"));
0043 }
0044 
0045 QUrl OnlineSearchCERNDS::buildQueryUrl(const QMap<QueryKey, QString> &query, int numResults)
0046 {
0047     /// Example for a search URL:
0048     /// https://cds.cern.ch/search?action_search=Search&sf=&so=d&rm=&sc=0&of=hx&f=&rg=10&ln=en&as=1&m1=a&p1=stone&f1=title&op1=a&m2=a&p2=smith&f2=author&op2=a&m3=a&p3=&f3=
0049 
0050     /// of=hx  asks for BibTeX results
0051     /// rg=10  asks for 10 results
0052     /// c=CERN+Document+Server or c=Articles+%26+Preprints   to limit scope
0053     /// Search search argument (X={1,2,3,...}):
0054     ///   pX   search text
0055     ///   mX   a=all words; o=any; e=exact phrase; p=partial phrase; r=regular expression
0056     ///   opX  a=AND; o=OR; n=AND NOT
0057     ///   fX   ""=any field; title; author; reportnumber; year; fulltext
0058 
0059     /// Build URL
0060     QUrl url = QUrl(QStringLiteral("https://cds.cern.ch/search?ln=en&action_search=Search&c=Articles+%26+Preprints&as=1&sf=&so=d&rm=&sc=0&of=hx&f="));
0061     QUrlQuery q(url);
0062     /// Set number of expected results
0063     q.addQueryItem(QStringLiteral("rg"), QString::number(numResults));
0064 
0065     /// Number search arguments
0066     int argumentCount = 0;
0067 
0068     /// add words from "free text" field
0069     const QStringList freeTextWords = splitRespectingQuotationMarks(query[QueryKey::FreeText]);
0070     for (const QString &word : freeTextWords) {
0071         ++argumentCount;
0072         q.addQueryItem(QString(QStringLiteral("p%1")).arg(argumentCount), word);
0073         q.addQueryItem(QString(QStringLiteral("m%1")).arg(argumentCount), QStringLiteral("a"));
0074         q.addQueryItem(QString(QStringLiteral("op%1")).arg(argumentCount), QStringLiteral("a"));
0075         q.addQueryItem(QString(QStringLiteral("f%1")).arg(argumentCount), QString());
0076     }
0077 
0078     /// add words from "author" field
0079     const QStringList authorWords = splitRespectingQuotationMarks(query[QueryKey::Author]);
0080     for (const QString &word : authorWords) {
0081         ++argumentCount;
0082         q.addQueryItem(QString(QStringLiteral("p%1")).arg(argumentCount), word);
0083         q.addQueryItem(QString(QStringLiteral("m%1")).arg(argumentCount), QStringLiteral("a"));
0084         q.addQueryItem(QString(QStringLiteral("op%1")).arg(argumentCount), QStringLiteral("a"));
0085         q.addQueryItem(QString(QStringLiteral("f%1")).arg(argumentCount), QStringLiteral("author"));
0086     }
0087 
0088     /// add words from "title" field
0089     const QStringList titleWords = splitRespectingQuotationMarks(query[QueryKey::Title]);
0090     for (const QString &word : titleWords) {
0091         ++argumentCount;
0092         q.addQueryItem(QString(QStringLiteral("p%1")).arg(argumentCount), word);
0093         q.addQueryItem(QString(QStringLiteral("m%1")).arg(argumentCount), QStringLiteral("a"));
0094         q.addQueryItem(QString(QStringLiteral("op%1")).arg(argumentCount), QStringLiteral("a"));
0095         q.addQueryItem(QString(QStringLiteral("f%1")).arg(argumentCount), QStringLiteral("title"));
0096     }
0097 
0098     /// add words from "title" field
0099     const QString year = query[QueryKey::Year];
0100     if (!year.isEmpty()) {
0101         ++argumentCount;
0102         q.addQueryItem(QString(QStringLiteral("p%1")).arg(argumentCount), year);
0103         q.addQueryItem(QString(QStringLiteral("m%1")).arg(argumentCount), QStringLiteral("a"));
0104         q.addQueryItem(QString(QStringLiteral("op%1")).arg(argumentCount), QStringLiteral("a"));
0105         q.addQueryItem(QString(QStringLiteral("f%1")).arg(argumentCount), QStringLiteral("year"));
0106     }
0107 
0108     url.setQuery(q);
0109     return url;
0110 }