Huggle  build:^490^dce1e5c
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
editquery.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 "editquery.hpp"
12 
13 using namespace Huggle;
14 
15 EditQuery::EditQuery()
16 {
17  this->summary = "";
18  this->Result = NULL;
19  this->qEdit = NULL;
20  this->Minor = false;
21  this->page = "";
22  this->qToken = NULL;
23  this->text = "";
24  this->Type = QueryEdit;
25 }
26 
27 EditQuery::~EditQuery()
28 {
29  if (qToken != NULL)
30  {
31  qToken->UnregisterConsumer(HUGGLECONSUMER_EDITQUERY);
32  }
33 }
34 
36 {
37  this->Status = StatusProcessing;
38  this->StartTime = QDateTime::currentDateTime();
39  qToken = new ApiQuery();
40  qToken->SetAction(ActionQuery);
41  qToken->Parameters = "prop=info&intoken=edit&titles=" + QUrl::toPercentEncoding(page);
42  /// \todo LOCALIZE ME
43  qToken->Target = "Retrieving token to edit " + page;
44  qToken->RegisterConsumer(HUGGLECONSUMER_EDITQUERY);
45  Core::AppendQuery(qToken);
46  qToken->Process();
47 }
48 
50 {
51  if (this->Result != NULL)
52  {
53  return true;
54  }
55  if (qToken != NULL)
56  {
57  if (!qToken->Processed())
58  {
59  return false;
60  }
61  if (qToken->Result->Failed)
62  {
63  this->Result = new QueryResult();
64  this->Result->Failed = true;
65  /// \todo LOCALIZE ME
66  this->Result->ErrorMessage = "Unable to retrieve edit token, error was: " + qToken->Result->ErrorMessage;
67  this->qToken->UnregisterConsumer(HUGGLECONSUMER_EDITQUERY);
68  this->qToken = NULL;
69  return true;
70  }
71  QDomDocument d;
72  d.setContent(qToken->Result->Data);
73  QDomNodeList l = d.elementsByTagName("page");
74  if (l.count() == 0)
75  {
76  this->Result = new QueryResult();
77  this->Result->Failed = true;
78  /// \todo LOCALIZE ME
79  this->Result->ErrorMessage = "Unable to retrieve edit token";
80  Core::DebugLog("Debug message for edit: " + qToken->Result->Data);
81  this->qToken->UnregisterConsumer(HUGGLECONSUMER_EDITQUERY);
82  this->qToken = NULL;
83  return true;
84  }
85  QDomElement element = l.at(0).toElement();
86  if (!element.attributes().contains("edittoken"))
87  {
88  this->Result = new QueryResult();
89  this->Result->Failed = true;
90  /// \todo LOCALIZE ME
91  this->Result->ErrorMessage = "Unable to retrieve edit token";
92  Core::DebugLog("Debug message for edit: " + qToken->Result->Data);
93  this->qToken->UnregisterConsumer(HUGGLECONSUMER_EDITQUERY);
94  this->qToken = NULL;
95  return true;
96  }
97  _Token = element.attribute("edittoken");
98  qToken->Lock();
99  qToken->UnregisterConsumer(HUGGLECONSUMER_EDITQUERY);
100  qToken = NULL;
101  qEdit = new ApiQuery();
102  qEdit->Target = "Writing " + page;
103  qEdit->UsingPOST = true;
104  qEdit->RegisterConsumer(HUGGLECONSUMER_EDITQUERY);
105  qEdit->SetAction(ActionEdit);
106  qEdit->Parameters = "title=" + QUrl::toPercentEncoding(page) + "&text=" + QUrl::toPercentEncoding(text) +
107  "&summary=" + QUrl::toPercentEncoding(this->summary) + "&token=" + QUrl::toPercentEncoding(_Token);
109  qEdit->Process();
110  return false;
111  }
112  if (qEdit != NULL)
113  {
114  if (!qEdit->Processed())
115  {
116  return false;
117  }
118  QDomDocument d;
119  d.setContent(qEdit->Result->Data);
120  QDomNodeList l = d.elementsByTagName("edit");
121  if (l.count() > 0)
122  {
123  QDomElement element = l.at(0).toElement();
124  if (element.attributes().contains("result"))
125  {
126  if (element.attribute("result") == "Success")
127  {
128  if (Core::Main != NULL)
129  {
130  HistoryItem item;
131  item.Result = "Successful";
132  item.Type = HistoryEdit;
133  item.Target = this->page;
134  Core::Main->_History->Prepend(item);
135  }
136  /// \todo LOCALIZE ME
137  Core::Log("Successfuly edit " + page);
138  }
139  }
140  }
141  Result = new QueryResult();
142  qEdit->UnregisterConsumer(HUGGLECONSUMER_EDITQUERY);
143  qEdit = NULL;
144  }
145  return true;
146 }
static void Log(QString Message)
Write text to terminal as well as ring log.
Definition: core.cpp:563
History * _History
Pointer to history.
Definition: mainwindow.hpp:142
QString Target
This is optional property which contains a label of target this query is for.
Definition: apiquery.hpp:98
void Process()
Execute query.
Definition: editquery.cpp:35
QString summary
Edit summary.
Definition: editquery.hpp:37
virtual bool Processed()
Returns true in case that query is processed.
Definition: query.cpp:45
void UnregisterConsumer(const int consumer)
This function will remove a string which prevent the object from being removed.
Definition: collectable.cpp:68
bool Minor
Whether the edit is minor or not.
Definition: editquery.hpp:39
Result of query.
Definition: queryresult.hpp:19
void RegisterConsumer(const int consumer)
Registers a consumer.
Definition: collectable.cpp:57
QString page
Page that is going to be edited.
Definition: editquery.hpp:33
static void AppendQuery(Query *item)
Insert a query to internal list of running queries, so that they can be watched This will insert it t...
Definition: core.cpp:557
void Process()
Run.
Definition: apiquery.cpp:138
ApiQuery * qEdit
Api query to edit page.
Definition: editquery.hpp:43
QueryType Type
Type of a query.
Definition: query.hpp:80
void Prepend(HistoryItem item)
Insert a new item to top of list.
Definition: history.cpp:37
void SetAction(const Action action)
Change the action type.
Definition: apiquery.cpp:185
QString text
Text a page will be replaced with.
Definition: editquery.hpp:35
bool UsingPOST
Whether the query will submit parameters using POST data.
Definition: apiquery.hpp:77
bool Processed()
Returns true in case that query is processed.
Definition: editquery.cpp:49
HistoryType Type
Type of item.
Definition: history.hpp:46
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
History consist of these items.
Definition: history.hpp:35
void Lock()
Lock this object so that other threads can't change consumers or modify its properties.
static MainWindow * Main
Pointer to main.
Definition: core.hpp:111
This class can be used to execute any kind of api query on any wiki.
Definition: apiquery.hpp:55
QString _Token
Edit token, will be retrieved during request.
Definition: editquery.hpp:45
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