File indexing completed on 2025-01-05 03:53:36
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2011-03-22 0007 * Description : a Iface C++ interface 0008 * 0009 * SPDX-FileCopyrightText: 2011-2024 by Gilles Caulier <caulier dot gilles at gmail dot com> 0010 * SPDX-FileCopyrightText: 2011 by Alexandre Mendes <alex dot mendes1988 at gmail dot com> 0011 * SPDX-FileCopyrightText: 2011 by Vincent Garcia <xavier dot vincent dot garcia at gmail dot com> 0012 * 0013 * SPDX-License-Identifier: GPL-2.0-or-later 0014 * 0015 * ============================================================ */ 0016 0017 #include "mediawiki_parse.h" 0018 0019 // Qt includes 0020 0021 #include <QTimer> 0022 #include <QUrl> 0023 #include <QUrlQuery> 0024 #include <QXmlStreamReader> 0025 #include <QNetworkReply> 0026 #include <QNetworkRequest> 0027 0028 // Local includes 0029 0030 #include "mediawiki_job_p.h" 0031 #include "mediawiki_iface.h" 0032 0033 namespace MediaWiki 0034 { 0035 0036 class Q_DECL_HIDDEN ParsePrivate : public JobPrivate 0037 { 0038 0039 public: 0040 0041 explicit ParsePrivate(Iface& MediaWiki) 0042 : JobPrivate(MediaWiki) 0043 { 0044 } 0045 0046 QMap<QString, QString> requestParameter; 0047 }; 0048 0049 Parse::Parse(Iface& MediaWiki, QObject* const parent) 0050 : Job(*new ParsePrivate(MediaWiki), parent) 0051 { 0052 } 0053 0054 Parse::~Parse() 0055 { 0056 } 0057 0058 void Parse::setText(const QString& param) 0059 { 0060 Q_D(Parse); 0061 d->requestParameter[QStringLiteral("text")] = param; 0062 } 0063 0064 void Parse::setTitle(const QString& param) 0065 { 0066 Q_D(Parse); 0067 d->requestParameter[QStringLiteral("title")] = param; 0068 } 0069 0070 void Parse::setPageName(const QString& param) 0071 { 0072 Q_D(Parse); 0073 d->requestParameter[QStringLiteral("page")] = param; 0074 } 0075 0076 void Parse::setUseLang(const QString& param) 0077 { 0078 Q_D(Parse); 0079 d->requestParameter[QStringLiteral("uselang")] = param; 0080 } 0081 0082 void Parse::start() 0083 { 0084 QTimer::singleShot(0, this, SLOT(doWorkSendRequest())); 0085 } 0086 0087 void Parse::doWorkSendRequest() 0088 { 0089 Q_D(Parse); 0090 0091 QUrl url = d->MediaWiki.url(); 0092 QUrlQuery query; 0093 query.addQueryItem(QStringLiteral("format"), QStringLiteral("xml")); 0094 query.addQueryItem(QStringLiteral("action"), QStringLiteral("parse")); 0095 0096 QMapIterator<QString, QString> i(d->requestParameter); 0097 while (i.hasNext()) 0098 { 0099 i.next(); 0100 query.addQueryItem(i.key(), i.value()); 0101 } 0102 url.setQuery(query); 0103 0104 // Set the request 0105 QNetworkRequest request(url); 0106 request.setRawHeader("User-Agent", d->MediaWiki.userAgent().toUtf8()); 0107 0108 // Send the request 0109 d->reply = d->manager->get(request); 0110 connectReply(); 0111 connect(d->reply, SIGNAL(finished()), 0112 this, SLOT(doWorkProcessReply())); 0113 } 0114 0115 void Parse::doWorkProcessReply() 0116 { 0117 Q_D(Parse); 0118 disconnect(d->reply, SIGNAL(finished()), 0119 this, SLOT(doWorkProcessReply())); 0120 0121 if (d->reply->error() == QNetworkReply::NoError) 0122 { 0123 QXmlStreamReader reader(d->reply); 0124 QString text; 0125 0126 while (!reader.atEnd() && !reader.hasError()) 0127 { 0128 QXmlStreamReader::TokenType token = reader.readNext(); 0129 0130 if (token == QXmlStreamReader::StartElement) 0131 { 0132 if (reader.name() == QLatin1String("text")) 0133 { 0134 text = reader.text().toString(); 0135 setError(Parse::NoError); 0136 } 0137 else if (reader.name() == QLatin1String("error")) 0138 { 0139 if (reader.attributes().value(QStringLiteral("code")).toString() == QLatin1String("params")) 0140 this->setError(this->TooManyParams); 0141 else if (reader.attributes().value(QStringLiteral("code")).toString() == QLatin1String("missingtitle")) 0142 this->setError(this->MissingPage); 0143 0144 d->reply->close(); 0145 d->reply->deleteLater(); 0146 emitResult(); 0147 return; 0148 } 0149 } 0150 } 0151 0152 if (!reader.hasError()) 0153 { 0154 Q_EMIT result(text); 0155 } 0156 else 0157 { 0158 setError(Parse::XmlError); 0159 } 0160 } 0161 else 0162 { 0163 setError(Parse::NetworkError); 0164 } 0165 0166 d->reply->close(); 0167 d->reply->deleteLater(); 0168 emitResult(); 0169 } 0170 0171 } // namespace MediaWiki 0172 0173 #include "moc_mediawiki_parse.cpp"