This repository has been archived by the owner on Jul 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConclurerGenderCache.module
124 lines (98 loc) · 4.66 KB
/
ConclurerGenderCache.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
class ConclurerGenderCache extends WireData implements Module {
const dbTableName = 'conclurer_gender_cache';
protected $api = null;
protected $tempCache = null;
public function init() {
$this->api = $this->modules->get("ConclurerGenderApi");
foreach (array('ConclurerGenderCacheResult') as $x) {
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.$x.'.php';
}
$this->addHookBefore("ConclurerGenderApiRequest::fetch", $this, "hookBeforeGenderFetch");
$this->addHookAfter("ConclurerGenderApiRequest::fetch", $this, "hookAfterGenderFetch");
}
public function ___install() {
$table = self::dbTableName;
$statement = $this->database->prepare("CREATE TABLE `$table` (`id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `locale` varchar(2) DEFAULT NULL, `gender` varchar(1) DEFAULT NULL, `accuracy` int(3) DEFAULT NULL, PRIMARY KEY (`id`), KEY `name_locale` (`name`,`locale`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
$statement->execute();
$this->message("Database Table '$table' was created");
}
public function ___uninstall() {
$table = self::dbTableName;
$statement = $this->database->prepare("DROP TABLE `$table`");
$statement->execute();
$this->message("Database Table '$table' was deleted");
}
public function hookBeforeGenderFetch (HookEvent $event) {
$request = $event->object;
// Cancel if request includes IP-based or language-based filtering
if ($request->localizationType == 'ip' || $request->localizationType == 'language') return;
// Cancel if request is based on mail address
if ($request->emailAddress != null) return;
$locale = $request->localizationType == 'none' ? '*' : $request->localizationValue;
$this->tempCache = new WireArray();
$notFound = array();
foreach ($request->names as $name) {
$query = $this->lookupName($name, $locale);
if ($query == null) {
$notFound[] = $name;
}
else {
$this->tempCache->push($query);
}
}
if (count($notFound) == 0) {
$event->replace = true;
if ($this->tempCache->count() == 1) $event->return = $this->tempCache->first();
else $event->return = $this->tempCache;
$this->tempCache = null;
return;
}
// Overwrite names with names that haven't been found
$request->names = $notFound;
}
public function hookAfterGenderFetch (HookEvent $event) {
$result = $event->return;
if ($result instanceof WireArray) {
foreach ($result as $element) $this->cacheResult($element);
}
else {
$this->cacheResult($result);
}
// Cancel if no cached elements are available
if ($this->tempCache == null) return;
$event->replace = true;
$z = new WireArray();
foreach ($this->tempCache as $item) $z->push($item);
if ($result instanceof WireArray) {
foreach ($result as $item) $z->push($item);
}
else $z->push($result);
if ($z->count() == 1) $event->return = $z->first();
else $event->return = $z;
}
public function lookupName ($name, $locale='*') {
$table = self::dbTableName;
$statement = $this->database->prepare("SELECT * FROM `$table` WHERE `name` = ? AND `locale` = ? LIMIT 1");
$statement->execute(array(strtolower($name), strtolower($locale)));
$result = $statement->fetch(PDO::FETCH_ASSOC);
if (!$result) return null;
$object = new ConclurerGenderCacheResult();
switch ($result['gender']) {
case 'm': $result['gender'] = 'male'; break;
case 'f': $result['gender'] = 'female'; break;
case 'u': $result['gender'] = 'unknown'; break;
}
$result['country'] = strtoupper($result['locale']);
$object->data = $result;
return $object;
}
protected function cacheResult (WireData $object) {
// Cancel if result is already cached or caused an error
if ($object instanceof ConclurerGenderCacheResult) return;
if ($object instanceof ConclurerGenderApiErrorResult) return;
$table = self::dbTableName;
$statement = $this->database->prepare("INSERT INTO `$table` (`name`, `locale`, `gender`, `accuracy`) VALUES (?, ?, ?, ?)");
$statement->execute(array(strtolower($object->name), (isset($object->country) ? strtolower($object->country) : '*'), substr($object->gender, 0, 1), $object->accuracy));
}
}