File indexing completed on 2024-03-24 16:13:24

0001 /** ===========================================================
0002  * @file
0003  *
0004  * This file is a part of KDE project
0005  * <a href="https://commits.kde.org/libmediawiki">libmediawiki</a>
0006  *
0007  * @date   2011-03-22
0008  * @brief  a MediaWiki C++ interface for KDE
0009  *
0010  * @author Copyright (C) 2011-2012 by Gilles Caulier
0011  *         <a href="mailto:caulier dot gilles at gmail dot com">caulier dot gilles at gmail dot com</a>
0012  * @author Copyright (C) 2011 by Manuel Campomanes
0013  *         <a href="mailto:campomanes dot manuel at gmail dot com">campomanes dot manuel at gmail dot com</a>
0014  * @author Copyright (C) 2010 by Vincent Garcia
0015  *         <a href="mailto:xavier dot vincent dot garcia at gmail dot com">xavier dot vincent dot garcia at gmail dot com</a>
0016  *
0017  * This program is free software; you can redistribute it
0018  * and/or modify it under the terms of the GNU General
0019  * Public License as published by the Free Software Foundation;
0020  * either version 2, or (at your option)
0021  * any later version.
0022  *
0023  * This program is distributed in the hope that it will be useful,
0024  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0025  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0026  * GNU General Public License for more details.
0027  *
0028  * ============================================================ */
0029 
0030 #include "parse.h"
0031 
0032 
0033 // Qt includes
0034 
0035 #include <QTimer>
0036 #include <QUrl>
0037 #include <QUrlQuery>
0038 #include <QXmlStreamReader>
0039 
0040 #include <QNetworkReply>
0041 #include <QNetworkRequest>
0042 
0043 // Local includes
0044 
0045 #include "job_p.h"
0046 #include "mediawiki.h"
0047 
0048 namespace mediawiki
0049 {
0050 
0051 class ParsePrivate : public JobPrivate
0052 {
0053 
0054 public:
0055 
0056     ParsePrivate(MediaWiki& mediawiki)
0057         : JobPrivate(mediawiki)
0058     {
0059     }
0060 
0061     QMap<QString, QString> requestParameter;
0062 };
0063 
0064 Parse::Parse(MediaWiki& mediawiki, QObject* const parent)
0065     : Job(*new ParsePrivate(mediawiki), parent)
0066 {
0067 }
0068 
0069 Parse::~Parse()
0070 {
0071 }
0072 
0073 void Parse::setText(const QString& param)
0074 {
0075     Q_D(Parse);
0076     d->requestParameter[QStringLiteral("text")] = param;
0077 }
0078 
0079 void Parse::setTitle(const QString& param)
0080 {
0081     Q_D(Parse);
0082     d->requestParameter[QStringLiteral("title")] = param;
0083 }
0084 
0085 void Parse::setPageName(const QString& param)
0086 {
0087     Q_D(Parse);
0088     d->requestParameter[QStringLiteral("page")] = param;
0089 }
0090 
0091 void Parse::setUseLang(const QString& param)
0092 {
0093     Q_D(Parse);
0094     d->requestParameter[QStringLiteral("uselang")] = param;
0095 }
0096 
0097 void Parse::start()
0098 {
0099     QTimer::singleShot(0, this, SLOT(doWorkSendRequest()));
0100 }
0101 
0102 void Parse::doWorkSendRequest()
0103 {
0104     Q_D(Parse);
0105 
0106     QUrl url = d->mediawiki.url();
0107     QUrlQuery query;
0108     query.addQueryItem(QStringLiteral("format"), QStringLiteral("xml"));
0109     query.addQueryItem(QStringLiteral("action"), QStringLiteral("parse"));
0110 
0111     QMapIterator<QString, QString> i(d->requestParameter);
0112     while (i.hasNext())
0113     {
0114         i.next();
0115         query.addQueryItem(i.key(), i.value());
0116     }
0117     url.setQuery(query);
0118 
0119     // Set the request
0120     QNetworkRequest request(url);
0121     request.setRawHeader("User-Agent", d->mediawiki.userAgent().toUtf8());
0122 
0123     // Send the request
0124     d->reply = d->manager->get(request);
0125     connectReply();
0126     connect(d->reply, SIGNAL(finished()),
0127             this, SLOT(doWorkProcessReply()));
0128 }
0129 
0130 void Parse::doWorkProcessReply()
0131 {
0132     Q_D(Parse);
0133     disconnect(d->reply, SIGNAL(finished()),
0134                this, SLOT(doWorkProcessReply()));
0135 
0136     if (d->reply->error() == QNetworkReply::NoError)
0137     {
0138         QXmlStreamReader reader(d->reply);
0139         QString text;
0140 
0141         while (!reader.atEnd() && !reader.hasError())
0142         {
0143             QXmlStreamReader::TokenType token = reader.readNext();
0144 
0145             if (token == QXmlStreamReader::StartElement)
0146             {
0147                 if (reader.name() == QLatin1String("text"))
0148                 {
0149                     text = reader.text().toString();
0150                     setError(Parse::NoError);
0151                 }
0152                 else if (reader.name() == QLatin1String("error"))
0153                 {
0154                     if (reader.attributes().value(QStringLiteral("code")).toString() == QLatin1String("params"))
0155                         this->setError(this->TooManyParams);
0156                     else if (reader.attributes().value(QStringLiteral("code")).toString() == QLatin1String("missingtitle"))
0157                         this->setError(this->MissingPage);
0158 
0159                     d->reply->close();
0160                     d->reply->deleteLater();
0161                     emitResult();
0162                     return;
0163                 }
0164             }
0165         }
0166 
0167         if (!reader.hasError())
0168         {
0169             emit result(text);
0170         }
0171         else
0172         {
0173             setError(Parse::XmlError);
0174         }
0175     }
0176     else
0177     {
0178         setError(Parse::NetworkError);
0179     }
0180 
0181     d->reply->close();
0182     d->reply->deleteLater();
0183     emitResult();
0184 }
0185 
0186 } // namespace mediawiki