Skip to content

Commit 1b36063

Browse files
committed
[smarcet] - #13651
* moved db code to php
1 parent a2b9c9b commit 1b36063

3 files changed

Lines changed: 87 additions & 64 deletions

File tree

registration/code/infrastructure/active_records/OpenStackMember.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function onBeforeWrite()
133133

134134
// reset spam check
135135

136-
if(isset($fields['Bio']) && $this->owner->Type != 'Spam')
136+
if(isset($fields['Bio']) && !$this->isSpam())
137137
{
138138
$bio_old = trim($fields['Bio']['before']);
139139
$bio_new = trim($fields['Bio']['after']);
@@ -666,10 +666,25 @@ public function deActivate(){
666666
}
667667

668668
public function resetTypeClassification(){
669+
error_log(sprintf("resetting Member %s TypeClassification", $this->owner->Email).PHP_EOL);
669670
$this->owner->Type = "None";
670671
DB::query(sprintf("DELETE FROM MemberEstimatorFeed WHERE Email = '%s';", $this->owner->Email));
671672
}
672673

674+
/**
675+
* @return bool
676+
*/
677+
public function isHam(){
678+
return $this->owner->Type == 'Ham';
679+
}
680+
681+
/**
682+
* @return bool
683+
*/
684+
public function isSpam(){
685+
return $this->owner->Type == 'Spam';
686+
}
687+
673688
/**
674689
* @return bool
675690
*/

registration/code/model/member_spammer_estimator/MemberSpammerProcessorTask.php

Lines changed: 71 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -27,61 +27,77 @@ final class MemberSpammerProcessorTask extends CronTask
2727
*/
2828
public function run()
2929
{
30-
$command = sprintf( '%1$s/registration/code/model/member_spammer_estimator/member_spammer_estimator_process.sh "%1$s/registration/code/model/member_spammer_estimator" "%1$s"', Director::baseFolder());
31-
$process = new Process($command);
32-
$process->setTimeout(PHP_INT_MAX);
33-
$process->setIdleTimeout(PHP_INT_MAX);
34-
$process->run();
35-
36-
while ($process->isRunning()) {
37-
}
38-
39-
$csv_content = $process->getOutput();
40-
echo $csv_content.PHP_EOL;
41-
42-
if (!$process->isSuccessful()) {
43-
throw new Exception("Process Error!");
44-
}
45-
46-
$rows = CSVReader::load($csv_content);
47-
$output = "<p>Nothing to process</p>";
48-
49-
if(count($rows) > 0)
50-
{ $output = '<ul>';
51-
52-
foreach($rows as $row){
53-
$member_id = intval($row["ID"]);
54-
$member = Member::get()->byID($member_id);
55-
56-
if(!$member) continue;
57-
58-
$action_url = $row["Type"] == "Ham"? "/members-spammers/%s/deactivate": "/members-spammers/%s/activate";
59-
$action_url = Director::absoluteURL(sprintf($action_url, $member->ID));
60-
$action_text = $row["Type"] == "Ham"? "Mark as Spam": "Mark as Ham";
61-
$edit_url = Director::absoluteURL(sprintf("/admin/security/EditForm/field/Members/item/%s/edit", $member->ID));
62-
63-
$output .= sprintf(
64-
"<li>[%s] - %s, %s (%s).<a href='%s'>Edit</a> <a href='%s'>%s</a></li>",
65-
$row["Type"],
66-
$member->FirstName,
67-
$member->Surname,
68-
$member->Email,
69-
$edit_url,
70-
$action_url,
71-
$action_text
72-
);
30+
SapphireTransactionManager::getInstance()->transaction(function(){
31+
$command = sprintf( '%1$s/registration/code/model/member_spammer_estimator/member_spammer_estimator_process.sh "%1$s/registration/code/model/member_spammer_estimator" "%1$s"', Director::baseFolder());
32+
$process = new Process($command);
33+
$process->setTimeout(PHP_INT_MAX);
34+
$process->setIdleTimeout(PHP_INT_MAX);
35+
$process->run();
36+
37+
while ($process->isRunning()) {
38+
}
39+
40+
$csv_content = $process->getOutput();
41+
echo $csv_content.PHP_EOL;
42+
43+
if (!$process->isSuccessful()) {
44+
throw new Exception("Process Error!");
45+
}
46+
47+
$rows = CSVReader::load($csv_content);
48+
$output = "<p>Nothing to process</p>";
49+
50+
if(count($rows) > 0)
51+
{ $output = '<ul>';
52+
53+
foreach($rows as $row){
54+
$member_id = intval($row["ID"]);
55+
$type = $row["Type"];
56+
$member = Member::get()->byID($member_id);
57+
58+
if(!$member) continue;
59+
// member processing
60+
if($type == 'Spam'){
61+
echo sprintf("Marking Member %s as Spam", $member->Email).PHP_EOL;
62+
$member->deActivate();
63+
}
64+
else{
65+
echo sprintf("Marking Member %s as Ham", $member->Email).PHP_EOL;
66+
$member->activate();
67+
}
68+
69+
$member->write();
70+
71+
$action_url = $type == "Ham"? "/members-spammers/%s/deactivate": "/members-spammers/%s/activate";
72+
$action_url = Director::absoluteURL(sprintf($action_url, $member->ID));
73+
$action_text = $type == "Ham"? "Mark as Spam": "Mark as Ham";
74+
$edit_url = Director::absoluteURL(sprintf("/admin/security/EditForm/field/Members/item/%s/edit", $member->ID));
75+
76+
$output .= sprintf(
77+
"<li>[%s] - %s, %s (%s).<a href='%s'>Edit</a> <a href='%s'>%s</a></li>",
78+
$row["Type"],
79+
$member->FirstName,
80+
$member->Surname,
81+
$member->Email,
82+
$edit_url,
83+
$action_url,
84+
$action_text
85+
);
86+
87+
88+
}
89+
$output .= '</ul>';
7390
}
74-
$output .= '</ul>';
75-
}
76-
77-
$email = EmailFactory::getInstance()->buildEmail
78-
(
79-
"noreply@openstack.org",
80-
MEMBER_SPAM_PROCESSOR_TO,
81-
"Member Spammer Processor Task Results",
82-
$output
83-
);
84-
85-
$email->send();
91+
92+
$email = EmailFactory::getInstance()->buildEmail
93+
(
94+
"noreply@openstack.org",
95+
MEMBER_SPAM_PROCESSOR_TO,
96+
"Member Spammer Processor Task Results",
97+
$output
98+
);
99+
100+
$email->send();
101+
});
86102
}
87103
}

registration/code/model/member_spammer_estimator/member_spammer_estimator_process.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ def stripHtmlFromBody(doc):
3535
return s.get_data()
3636

3737
SELECT_QUERY = ("SELECT ID, Email, FirstName, Surname, Bio FROM Member WHERE Type = 'None' AND Bio IS NOT NULL AND Bio <> '';")
38-
UPDATE_MEMBER = ("UPDATE Member SET Type = '%s', Active = %s WHERE ID = %s ;")
39-
SELECT_EXISTS = ("SELECT * FROM MemberEstimatorFeed WHERE Email='%s' AND FirstName ='%s' AND Surname ='%s'")
40-
INSERT_MEMBER_TRAINING_DATA = ("INSERT INTO MemberEstimatorFeed (Email, FirstName, Surname, Bio, Type) VALUES('%s', '%s', '%s', '%s', '%s');")
4138

4239
try:
4340
# Open database connection
@@ -52,13 +49,8 @@ def stripHtmlFromBody(doc):
5249
classifier = pickle.load(open('member_classifier.pickle', 'rb'))
5350
predicted = classifier.predict(X_test.drop(['ID'], axis = 1 ))
5451
for item, type in zip(X_test.to_dict( orient = 'records'), predicted):
55-
cursor.execute(UPDATE_MEMBER % (type, 1 if type == 'Ham' else 0, item['ID']))
5652
if type == 'Spam':
5753
print("%s,%s") % (item['ID'],'Spam')
58-
cursor.execute(SELECT_EXISTS % (item['Email'],item['FirstName'],item['Surname']))
59-
exists = cursor.fetchone();
60-
if exists is None:
61-
cursor.execute(INSERT_MEMBER_TRAINING_DATA % (item['Email'], item['FirstName'], item['Surname'], item['Bio'], type))
6254
else:
6355
print("%s,%s") % (item['ID'],'Ham')
6456
db.commit()

0 commit comments

Comments
 (0)