Skip to content

Commit

Permalink
Fixed for CSRF errors.
Browse files Browse the repository at this point in the history
Updated Gear export to fix errors in Engine and Heatsink defs.
  • Loading branch information
Fulmir committed Jun 1, 2023
1 parent b4dd7b3 commit 9ccf169
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 25 deletions.
4 changes: 2 additions & 2 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ var ExportCmd = &cobra.Command{
logrus.Infof("Skipping BLACKLISTED mech %s", mech.Chassis.Description.Name)
continue
}

filename := fmt.Sprintf("MechDef_%s.wiki", variant)
filename := fmt.Sprintf("MechDef_%s.wiki", strings.Replace(variant, "/", "-", -1))

logrus.Debugf("Writing mech wiki %s", filename)

Expand Down
6 changes: 3 additions & 3 deletions export/chassisdef.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type ChassisDef struct {
type ChassisLocation struct {
Location string
Hardpoints []struct {
WeaponMount string
WeaponMountID string
Omni bool
}
Tonnage float64
Expand All @@ -86,9 +86,9 @@ func (cl ChassisLocation) ToWiki(chassisID string) string {
omniHardpoints := []string{}
for _, hardpoint := range cl.Hardpoints {
if !hardpoint.Omni {
hardpoints = append(hardpoints, hardpoint.WeaponMount)
hardpoints = append(hardpoints, hardpoint.WeaponMountID)
} else {
omniHardpoints = append(omniHardpoints, hardpoint.WeaponMount)
omniHardpoints = append(omniHardpoints, hardpoint.WeaponMountID)
}
}

Expand Down
17 changes: 8 additions & 9 deletions export/gear.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,13 @@ type GearCustom struct {

BonusDescriptions []string `json:",omitempty"`

EngineHeatBlock struct {
HeatSinkCount int `json:",omitempty"`
} `json:",omitempty"`
EngineHeatBlock int `json:",omitempty"`

Cooling struct {
HeatSinkDefId string `json:",omitempty"`
} `json:",omitempty"`

EngineCore struct {
Rating int `json:",omitempty"`
} `json:",omitempty"`
EngineCore string `json:",omitempty"`

ArmActuator struct {
AccuracyBonus int `json:",omitempty"`
Expand Down Expand Up @@ -111,7 +107,7 @@ type Weapon struct {
ShotsWhenFired int
ProjectilesPerShot int
AttackRecoil int
Instability int
Instability float64
WeaponEffectID string
}

Expand Down Expand Up @@ -334,6 +330,9 @@ func (g Gear) ToWiki() string {
return ""
}
c, ok := cat["CategoryID"]
if !ok {
return ""
}
categoryString, ok := c.(string)
if !ok {
return ""
Expand Down Expand Up @@ -385,9 +384,9 @@ func (g Gear) ToWiki() string {
case CoolingWikiTemplate:
wt.AddArg("HeatsinkDefID", g.Custom.Cooling.HeatSinkDefId)
case EngineHeatBlockWikiTemplate:
wt.AddArg("HeatsinkCount", g.Custom.EngineHeatBlock.HeatSinkCount)
wt.AddArg("HeatsinkCount", g.Custom.EngineHeatBlock)
case EngineCoreWikiTemplate:
wt.AddArg("Rating", g.Custom.EngineCore.Rating)
wt.AddArg("Rating", g.Custom.EngineCore)
case EngineShieldWikiTemplate:
wt.AddArg("ReservedSlots", g.Custom.Weights.ReservedSlots)
wt.AddArg("EngineFactor", g.Custom.Weights.EngineFactor)
Expand Down
56 changes: 45 additions & 11 deletions importer/mediawiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// BATCH_SIZE is the number of wiki pages to retrieve at one time.
const BATCH_SIZE = 20
const BATCH_SIZE = 10

func Import(wikidata string, dryrun bool, username, password, url string) error {
w, err := mwclient.New(url, "")
Expand All @@ -26,6 +26,10 @@ func Import(wikidata string, dryrun bool, username, password, url string) error
logrus.Info("doing dry run, will not make alterations")
}

w.Maxlag.On = true
w.Maxlag.Retries = 3
w.Maxlag.Timeout = "30"

err = w.Login(username, password)
if err != nil {
logrus.Warnf("error logging in: %s", err)
Expand Down Expand Up @@ -106,24 +110,54 @@ func Import(wikidata string, dryrun bool, username, password, url string) error
continue
}

if strings.TrimSpace(fileContent) == strings.TrimSpace(content) {
logrus.Debugf("UNCHANGED %s", pageName)
unchanged[pageTitle] = struct{}{}
} else {
if content != "" {
logrus.Infof("UPDATE %s", pageName)
updates[pageTitle] = struct{}{}
}
if !dryrun {
// if strings.TrimSpace(fileContent) == strings.TrimSpace(content) {
// logrus.Debugf("UNCHANGED %s", pageName)
// unchanged[pageTitle] = struct{}{}
// } else {
if content != "" {
logrus.Infof("UPDATING %s", pageName)
updates[pageTitle] = struct{}{}
}
if !dryrun {
errCheck := true
for i := 0; i < 5 && errCheck; i++ {
if err := w.Edit(map[string]string{
"title": pageName,
"text": fileContent,
"summary": "automated page update",
}); err != nil {
logrus.Errorf("Error writing page %s to wiki: %s\n", pageName, err)
logrus.Debugf("Error writing page %s to wiki: %s\n", pageName, err)

errCode := strings.Split(err.Error(), ":")[0]

if errCode == "badtoken" {
logrus.Warnf("CSRF Token auth failed while writing page %s to wiki, retrying after attempt %s: %s\n", pageName, strconv.Itoa(i+1), err)
errCheck = true
delete(w.Tokens, "csrf")
w.Logout()
w.Login(username, password)
} else if strings.Contains(errCode, "invalid") || strings.Contains(errCode, "denied") {
errCheck = false
logrus.Errorf("Error writing page %s to wiki, retrying after attempt %s: %s\n", pageName, strconv.Itoa(i+1), err)
} else if strings.Contains(errCode, "edit successful") {
errCheck = false
logrus.Warnf("Edit successful but contained no changes to page %s to wiki: %s\n", pageName, err)
} else if strings.Contains(errCode, "error occured during HTTP request") {
errCheck = true
logrus.Errorf("Error writing page %s to wiki, retrying after attempt %s: %s\n", pageName, strconv.Itoa(i+1), err)
w.Maxlag.Timeout = "90"
} else {
errCheck = true
logrus.Errorf("Error writing page %s to wiki, retrying after attempt %s: %s\n", pageName, strconv.Itoa(i+1), err)
}
} else {
w.Maxlag.Timeout = "30"
errCheck = false
logrus.Infof("SUCCESSFULLY UPDATED %s", pageName)
}
}
}
//}
}
}

Expand Down

0 comments on commit 9ccf169

Please sign in to comment.