Huggle  build:^490^dce1e5c
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
apiquery.cpp
1 //This program is free software: you can redistribute it and/or modify
2 //it under the terms of the GNU General Public License as published by
3 //the Free Software Foundation, either version 3 of the License, or
4 //(at your option) any later version.
5 
6 //This program is distributed in the hope that it will be useful,
7 //but WITHOUT ANY WARRANTY; without even the implied warranty of
8 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 //GNU General Public License for more details.
10 
11 #include "apiquery.hpp"
12 
13 using namespace Huggle;
14 
16 {
17  if (this->ActionPart == "")
18  {
19  throw new Exception("No action provided for api request");
20  }
21 
22  if (OverrideWiki == "")
23  {
24  URL = Core::GetProjectScriptURL(Configuration::Project) + "api.php?action=" + this->ActionPart;
25  }
26  else
27  {
28  URL = Configuration::GetURLProtocolPrefix() + OverrideWiki + "api.php?action=" + this->ActionPart;
29  }
30 
31  if (this->Parameters != "")
32  {
33  URL = URL + "&" + this->Parameters;
34  }
35 
36  switch (this->RequestFormat)
37  {
38  case XML:
39  URL += "&format=xml";
40  break;
41  case JSON:
42  URL += "&format=json";
43  break;
44  case PlainText:
45  case Default:
46  break;
47  }
48 }
49 
50 QString ApiQuery::ConstructParameterLessUrl()
51 {
52  QString url;
53  if (this->ActionPart == "")
54  {
55  throw new Exception("No action provided for api request", "void ApiQuery::ConstructParameterLessUrl()");
56  }
57  if (OverrideWiki == "")
58  {
59  url = Core::GetProjectScriptURL(Configuration::Project) + "api.php?action=" + this->ActionPart;
60  }
61  else
62  {
63  url = Configuration::GetURLProtocolPrefix() + OverrideWiki + "api.php?action=" + this->ActionPart;
64  }
65 
66  switch (this->RequestFormat)
67  {
68  case XML:
69  url += "&format=xml";
70  break;
71  case JSON:
72  url += "&format=json";
73  break;
74  case PlainText:
75  case Default:
76  break;
77  }
78 
79  return url;
80 }
81 
83 {
84  // other formats will be supported later
85  return (this->RequestFormat == XML);
86 }
87 
88 // TODO: move this function to RevertQuery
90 {
91  this->CustomStatus = RevertQuery::GetCustomRevertStatus(this->Result->Data);
92  if (this->CustomStatus != "Reverted")
93  {
94  this->Result->Failed = true;
95  }
96 }
97 
99 {
100  this->RequestFormat = XML;
101  this->URL = "";
102  this->Type = QueryApi;
103  this->ActionPart = "";
104  this->Result = NULL;
105  this->Parameters = "";
106  this->UsingPOST = false;
107  this->Target = "none";
108  this->OverrideWiki = "";
109 }
110 
111 void ApiQuery::Finished()
112 {
113  this->Result->Data += QString(this->reply->readAll());
114  // now we need to check if request was successful or not
115  if (this->reply->error())
116  {
117  this->Result->ErrorMessage = reply->errorString();
118  this->Result->Failed = true;
119  this->reply->deleteLater();
120  this->reply = NULL;
121  this->Status = StatusDone;
122  return;
123  }
124  if (this->ActionPart == "rollback")
125  {
126  FinishRollback();
127  }
128  this->reply->deleteLater();
129  this->reply = NULL;
130  if (!this->HiddenQuery)
131  {
132  Core::DebugLog("Finished request " + URL, 2);
133  }
134  this->Status = StatusDone;
135  this->ProcessCallback();
136 }
137 
139 {
140  if (this->Status == StatusProcessing)
141  {
142  Core::DebugLog("Cowardly refusing to double process the query");
143  return;
144  }
145  this->StartTime = QDateTime::currentDateTime();
146  if (this->URL == "")
147  {
148  this->ConstructUrl();
149  }
150  this->Status = StatusProcessing;
151  this->Result = new QueryResult();
152  QUrl url;
153  if (UsingPOST)
154  {
155  url = QUrl::fromEncoded(this->ConstructParameterLessUrl().toUtf8());
156  } else
157  {
158  url = QUrl::fromEncoded(this->URL.toUtf8());
159  }
160  QNetworkRequest request(url);
161  if (UsingPOST)
162  {
163  request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
164  }
165  if (UsingPOST)
166  {
167  this->reply = Query::NetworkManager.post(request, this->Parameters.toUtf8());
168  } else
169  {
170  this->reply = Query::NetworkManager.get(request);
171  }
172  if (!this->HiddenQuery)
173  {
174  Core::DebugLog("Processing api request " + this->URL, 2);
175  }
176  QObject::connect(this->reply, SIGNAL(finished()), this, SLOT(Finished()));
177  QObject::connect(this->reply, SIGNAL(readyRead()), this, SLOT(ReadData()));
178 }
179 
180 void ApiQuery::ReadData()
181 {
182  this->Result->Data += QString(this->reply->readAll());
183 }
184 
185 void ApiQuery::SetAction(const Action action)
186 {
187  switch (action)
188  {
189  case ActionQuery:
190  this->ActionPart = "query";
191  return;
192  case ActionLogin:
193  this->ActionPart = "login";
194  return;
195  case ActionLogout:
196  this->ActionPart = "logout";
197  return;
198  case ActionTokens:
199  this->ActionPart = "tokens";
200  return;
201  case ActionPurge:
202  this->ActionPart = "purge";
203  return;
204  case ActionRollback:
205  this->ActionPart = "rollback";
206  return;
207  case ActionDelete:
208  this->ActionPart = "delete";
209  return;
210  case ActionUndelete:
211  this->ActionPart = "undelete";
212  return;
213  case ActionBlock:
214  this->ActionPart = "block";
215  return;
216  case ActionProtect:
217  this->ActionPart = "protect";
218  return;
219  case ActionEdit:
220  this->ActionPart = "edit";
221  }
222 }
223 
224 void ApiQuery::SetAction(const QString action)
225 {
226  this->ActionPart = action;
227 }
228 
230 {
231  if (this->reply != NULL)
232  {
233  this->reply->abort();
234  }
235 }
236 
238 {
239  return this->Target;
240 }
241 
243 {
244  return "ApiQuery (" +
245  this->ActionPart + ")";
246 }
static QString GetProjectScriptURL()
Return a script url like http://en.wikipedia.org/w/.
Definition: core.cpp:674
QString Target
This is optional property which contains a label of target this query is for.
Definition: apiquery.hpp:98
bool HiddenQuery
Definition: query.hpp:96
void Kill()
Terminate the query.
Definition: apiquery.cpp:229
bool FormatIsCurrentlySupported()
Check if return format is supported by huggle.
Definition: apiquery.cpp:82
static QString GetURLProtocolPrefix()
Return a prefix for url.
Result of query.
Definition: queryresult.hpp:19
QString QueryTypeToString()
Returns a type of query as a string.
Definition: apiquery.cpp:242
void Process()
Run.
Definition: apiquery.cpp:138
void ProcessCallback()
Definition: query.cpp:126
ApiQuery()
Creates a new instance of this class and set the defaults.
Definition: apiquery.cpp:98
QString OverrideWiki
You can change this to url of different wiki than a project.
Definition: apiquery.hpp:100
QueryType Type
Type of a query.
Definition: query.hpp:80
QNetworkReply * reply
Reply from qnet.
Definition: apiquery.hpp:62
void SetAction(const Action action)
Change the action type.
Definition: apiquery.cpp:185
void ConstructUrl()
Generate api url.
Definition: apiquery.cpp:15
Every exception raised by huggle is defined by this class.
Definition: exception.hpp:20
bool UsingPOST
Whether the query will submit parameters using POST data.
Definition: apiquery.hpp:77
void FinishRollback()
This is only needed when you are using rollback.
Definition: apiquery.cpp:89
static WikiSite Project
currently selected project
static void DebugLog(QString Message, unsigned int Verbosity=1)
This log is only shown if verbosity is same or larger than requested verbosity.
Definition: core.cpp:641
QString ErrorMessage
If query is in error the reason for error is stored here.
Definition: queryresult.hpp:27
QString Parameters
Parameters for action, for example page title.
Definition: apiquery.hpp:84
QString CustomStatus
Custom status.
Definition: query.hpp:76
QString QueryTargetToString()
Get a query target as a string.
Definition: apiquery.cpp:237
Format RequestFormat
This is a requested format in which the result should be written in.
Definition: apiquery.hpp:79
QString Data
Data retrieved by query.
Definition: queryresult.hpp:25
QueryResult * Result
Result of query, see documentation of QueryResult for more.
Definition: query.hpp:68