11 #include "revertquery.hpp"
13 using namespace Huggle;
15 RevertQuery::RevertQuery()
20 this->PreflightFinished =
false;
21 this->RollingBack =
false;
25 this->qRetrieve = NULL;
27 this->MinorEdit =
false;
28 this->EditQuerySoftwareRollback = NULL;
29 this->qPreflight = NULL;
33 RevertQuery::RevertQuery(
WikiEdit *Edit)
39 this->PreflightFinished =
false;
40 this->RollingBack =
false;
42 this->qRetrieve = NULL;
45 this->EditQuerySoftwareRollback = NULL;
47 this->MinorEdit =
false;
49 this->qPreflight = NULL;
55 if (this->Status == StatusProcessing)
60 this->Status = StatusProcessing;
65 this->StartTime = QDateTime::currentDateTime();
66 this->timer =
new QTimer(
this);
67 connect(this->timer, SIGNAL(timeout()),
this, SLOT(OnTick()));
68 this->timer->start(800);
76 if (PreflightFinished && this->qRevert != NULL)
78 this->qRevert->
Kill();
79 }
else if (this->qPreflight != NULL)
81 this->qPreflight->
Kill();
83 this->Status = StatusInError;
88 this->
Result->Failed =
true;
90 if (this->qRetrieve != NULL)
94 this->qRetrieve = NULL;
98 RevertQuery::~RevertQuery()
100 if (this->edit != NULL)
115 if (!this->PreflightFinished)
120 if (this->Status != StatusDone)
124 this->Status = StatusDone;
133 void RevertQuery::OnTick()
135 if (this->Status != StatusDone)
137 if (!PreflightFinished)
157 QString RevertQuery::GetCustomRevertStatus(QString RevertData)
160 d.setContent(RevertData);
161 QDomNodeList l = d.elementsByTagName(
"error");
164 if (l.at(0).toElement().attributes().contains(
"code"))
167 Error = l.at(0).toElement().attribute(
"code");
169 if (Error ==
"alreadyrolled")
171 return "Edit was reverted by someone else - skipping";
174 if (Error ==
"onlyauthor")
176 return "ERROR: Cannot rollback - page only has one author";
178 return "In error (" + Error +
")";
184 void RevertQuery::Preflight()
189 bool MadeBySameUser =
true;
200 if (w->Time > this->edit->Time)
204 MadeBySameUser =
false;
221 text = (
"There are newer edits to " + this->edit->
Page->
PageName +
", are you sure you want to revert them?");
224 text = (
"There are new edits made to " + this->edit->
Page->
PageName +
" by a different user, are you sure you want to revert them all? (it will likely fail anyway because of old token)");
226 QMessageBox::StandardButton re;
227 re = QMessageBox::question(
Core::Main,
"Preflight check", text, QMessageBox::Yes|QMessageBox::No);
228 if (re == QMessageBox::No)
238 this->qPreflight->
SetAction(ActionQuery);
239 this->qPreflight->
Parameters =
"prop=revisions&rvprop=ids%7Cflags%7Ctimestamp%7Cuser%7Cuserid%7Csize%7Csha1%7Ccomment&rvlimit=20&titles="
244 void RevertQuery::CheckPreflight()
248 this->PreflightFinished =
true;
251 if (this->qPreflight == NULL)
259 if (this->qPreflight->
Result->Failed)
263 this->Status = StatusDone;
265 this->
Result->Failed =
true;
270 QDomNodeList l = d.elementsByTagName(
"rev");
272 bool MadeBySameUser =
true;
274 while (x < l.count())
276 QDomElement e = l.at(x).toElement();
277 if (e.attributes().contains(
"revid"))
279 if (edit->
RevID == e.attribute(
"revid").toInt())
289 if (this->edit->
RevID != WIKI_UNKNOWN_REVID && e.attribute(
"revid").toInt() > edit->
RevID)
301 text = (
"There are newer edits to " + this->edit->
Page->
PageName +
", are you sure you want to revert them");
304 text = (
"There are new edits made to " + this->edit->
Page->
PageName +
" by a different user, are you sure you want to revert them all? (it will likely fail anyway because of old token)");
311 QMessageBox::StandardButton re;
312 re = QMessageBox::question(
Core::Main,
"Preflight check", text, QMessageBox::Yes|QMessageBox::No);
313 if (re == QMessageBox::No)
319 this->
Result->Failed =
true;
321 this->Status = StatusDone;
322 this->PreflightFinished =
true;
327 this->PreflightFinished =
true;
330 bool RevertQuery::CheckRevert()
336 if (this->qRevert == NULL)
347 Core::Log(
"Unable to revert " + this->qRevert->
Target +
": " + this->CustomStatus);
348 qRevert->
Result->Failed =
true;
352 this->
Result->Failed =
true;
358 item.Target = this->qRevert->
Target;
359 item.
Type = HistoryRollback;
360 item.Result =
"Success";
367 this->qRevert = NULL;
371 void RevertQuery::Cancel()
376 this->
Result->Failed =
true;
378 this->Status = StatusDone;
379 this->PreflightFinished =
true;
384 if (this->EditQuerySoftwareRollback != NULL)
386 if (EditQuerySoftwareRollback->
Processed() ==
false)
394 if (this->qPreflight == NULL)
399 if (this->qPreflight->
Processed() !=
true)
404 if (this->qPreflight->
Result->Failed)
408 this->Status = StatusDone;
411 this->
Result->Failed =
true;
416 QDomNodeList l = d.elementsByTagName(
"rev");
423 Core::Log(
"Failed to retrieve a list of edits made to this page, query returned no data");
425 this->Status = StatusDone;
427 this->
Result->
ErrorMessage =
"Failed to retrieve a list of edits made to this page, query returned no data";
428 this->
Result->Failed =
true;
431 QDomElement latest = l.at(0).toElement();
436 while (x < l.count())
438 QDomElement e = l.at(x).toElement();
439 if (e.attributes().contains(
"revid"))
441 if (edit->
RevID == e.attribute(
"revid").toInt())
451 if (this->edit->
RevID != WIKI_UNKNOWN_REVID && e.attribute(
"revid").toInt() > edit->
RevID)
459 Core::Log(
"Unable to revert the page " + this->edit->
Page->
PageName +
" because it was edited meanwhile");
461 this->Status = StatusDone;
464 this->
Result->Failed =
true;
470 int RevID = WIKI_UNKNOWN_REVID;
471 QString content =
"";
474 while (x < l.count())
476 QDomElement e = l.at(x).toElement();
477 if (!e.attributes().contains(
"revid") || !e.attributes().contains(
"user"))
480 Core::Log(
"Unable to revert the page " + this->edit->
Page->
PageName +
" because mediawiki returned some non-sense");
482 this->Status = StatusDone;
485 this->
Result->Failed =
true;
492 RevID = e.attribute(
"revid").toInt();
493 target = e.attribute(
"user");
505 Core::Log(
"Unable to revert the page " + this->edit->
Page->
PageName +
" because it was edited meanwhile");
507 this->Status = StatusDone;
510 this->
Result->Failed =
true;
516 QString summary = Configuration::LocalConfig_SoftwareRevertDefaultSummary;
517 summary = summary.replace(
"$1", this->edit->
User->
Username)
518 .replace(
"$2", target)
519 .replace(
"$3", QString::number(depth))
520 .replace(
"$4", QString::number(RevID));
521 EditQuerySoftwareRollback = Core::EditPage(this->edit->
Page, content, summary, MinorEdit);
522 this->EditQuerySoftwareRollback->
RegisterConsumer(HUGGLECONSUMER_REVERTQUERY);
530 if (this->RollingBack)
536 this->RollingBack =
true;
538 if (this->Summary ==
"")
543 if (this->Summary.contains(
"$1"))
545 this->Summary = this->Summary.replace(
"$1", edit->
User->
Username);
556 if (!Configuration::Rights.contains(
"rollback"))
559 Core::Log(
"You don't have rollback rights, fallback to software rollback");
565 if (this->Token ==
"")
567 this->Token = this->edit->RollbackToken;
570 if (this->Token ==
"")
573 Core::Log(
"ERROR, unable to rollback, because the rollback token was empty: " + this->edit->
Page->
PageName);
575 this->
Result->Failed =
true;
578 this->Status = StatusDone;
583 this->qRevert->
SetAction(ActionRollback);
584 QString token = this->Token;
585 if (token.endsWith(
"+\\"))
587 token = QUrl::toPercentEncoding(token);
591 +
"&user=" + QUrl::toPercentEncoding(edit->
User->
Username)
592 +
"&summary=" + QUrl::toPercentEncoding(this->Summary);
606 void RevertQuery::Revert()
610 this->qPreflight->
SetAction(ActionQuery);
611 this->qPreflight->
Parameters =
"prop=revisions&rvprop=" + QUrl::toPercentEncoding(
"ids|flags|timestamp|user|userid|content|size|sha1|comment")
612 +
"&rvlimit=20&titles=" + QUrl::toPercentEncoding(this->edit->
Page->
PageName);
616 void RevertQuery::Exit()
618 if (this->timer != NULL)
622 if (EditQuerySoftwareRollback != NULL)
625 EditQuerySoftwareRollback = NULL;
628 if (this->qRevert != NULL)
static void Log(QString Message)
Write text to terminal as well as ring log.
History * _History
Pointer to history.
QString Target
This is optional property which contains a label of target this query is for.
void Kill()
Terminate the query.
bool UsingSR
Whether software rollback should be used instead of regular rollback.
virtual bool Processed()
Returns true in case that query is processed.
void UnregisterConsumer(const int consumer)
This function will remove a string which prevent the object from being removed.
void RegisterConsumer(const int consumer)
Registers a consumer.
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...
QString Username
Username.
long getBadnessScore(bool _resync=true)
Retrieve a badness score for current user, see WikiUser::BadnessScore for more.
void Kill()
Terminates a query.
static QList< WikiEdit * > EditList
This list contains reference to all existing edits in memory.
QueryType Type
Type of a query.
QString QueryTargetToString()
Return a target of a query.
bool Processed()
Returns true in case that query is processed.
void Prepend(HistoryItem item)
Insert a new item to top of list.
void SetAction(const Action action)
Change the action type.
static unsigned int Verbosity
Verbosity for debugging to terminal etc, can be switched with parameter –verbosity.
bool UsingPOST
Whether the query will submit parameters using POST data.
static int WriteTimeout
Timeout for write / update queries.
bool Processed()
Returns true in case that query is processed.
HistoryType Type
Type of item.
void Process()
Execute query.
WikiUser * User
User who changed the page.
static void DebugLog(QString Message, unsigned int Verbosity=1)
This log is only shown if verbosity is same or larger than requested verbosity.
static bool AutomaticallyResolveConflicts
Resolve edit conflict without asking user.
bool IgnorePreflightCheck
Rollback with no check if it's a good idea or not (revert even whitelisted users, sysops etc) ...
QString ErrorMessage
If query is in error the reason for error is stored here.
QString Parameters
Parameters for action, for example page title.
History consist of these items.
QString CustomStatus
Custom status.
static MainWindow * Main
Pointer to main.
QString PageName
Name of page.
This class can be used to execute any kind of api query on any wiki.
static QString GetDefaultRevertSummary(QString source)
GetDefaultRevertSummary Retrieve default summary.
WikiPage * Page
Page that was changed by edit.
static void UpdateUser(WikiUser *us)
Update a list of problematic users.
QString Data
Data retrieved by query.
QueryResult * Result
Result of query, see documentation of QueryResult for more.