1: <?php
2:
3: namespace Budabot\User\Modules;
4:
5: use stdClass;
6:
7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
21: class FindOrgController {
22:
23: 24: 25: 26:
27: public $moduleName;
28:
29:
30: public $db;
31:
32:
33: public $chatBot;
34:
35:
36: public $text;
37:
38:
39: public $util;
40:
41:
42: public $http;
43:
44:
45: public $logger;
46:
47: private $searches = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'others');
48:
49:
50: public function setup() {
51: $this->db->loadSQLFile($this->moduleName, "organizations");
52: }
53:
54: 55: 56: 57:
58: public function findOrgCommand($message, $channel, $sender, $sendto, $args) {
59: $search = $args[1];
60:
61: $orgs = $this->lookupOrg($search);
62: $count = count($orgs);
63:
64: if ($count > 0) {
65: $blob = $this->formatResults($orgs);
66: $msg = $this->text->makeBlob("Org Search Results for '{$search}' ($count)", $blob);
67: } else {
68: $msg = "No matches found.";
69: }
70: $sendto->reply($msg);
71: }
72:
73: public function lookupOrg($search, $limit = 50) {
74: $tmp = explode(" ", $search);
75: list($query, $params) = $this->util->generateQueryFromParams($tmp, 'name');
76:
77: $sql = "SELECT id, name, faction, num_members FROM organizations WHERE $query LIMIT 50";
78:
79: $orgs = $this->db->query($sql, $params);
80:
81: return $orgs;
82: }
83:
84: public function formatResults($orgs) {
85: $blob = '';
86: forEach ($orgs as $row) {
87: $whoisorg = $this->text->makeChatcmd('Whoisorg', "/tell <myname> whoisorg {$row->id}");
88: $orglist = $this->text->makeChatcmd('Orglist', "/tell <myname> orglist {$row->id}");
89: $orgmembers = $this->text->makeChatcmd('Orgmembers', "/tell <myname> orgmembers {$row->id}");
90: $blob .= "<{$row->faction}>{$row->name}<end> ({$row->id}) - {$row->num_members} members [$orglist] [$whoisorg] [$orgmembers]\n\n";
91: }
92: return $blob;
93: }
94:
95: 96: 97: 98:
99: public function parseAllOrgsEvent($eventObj) {
100: $url = "http://people.anarchy-online.com/people/lookup/orgs.html";
101:
102: $this->logger->log("DEBUG", "Downloading all orgs from '$url'");
103: try {
104: $this->db->beginTransaction();
105: $this->db->exec("DELETE FROM organizations");
106: forEach ($this->searches as $search) {
107: $response = $this->http->get($url)->withQueryParams(array('l' => $search))->waitAndReturnResponse();
108:
109: $pattern = '@<tr>\s*<td align="left">\s*<a href="http://people.anarchy-online.com/org/stats/d/(\d+)/name/(\d+)">\s*([^<]+)</a></td>\s*<td align="right">(\d+)</td>\s*<td align="right">(\d+)</td>\s*<td align="left">([^<]+)</td>\s*<td align="left">([^<]+)</td>\s*<td align="left" class="dim">RK5</td>\s*</tr>@s';
110:
111: preg_match_all($pattern, $response->body, $arr, PREG_SET_ORDER);
112: forEach ($arr as $match) {
113: $obj = new stdClass;
114:
115: $obj->id = $match[2];
116: $obj->name = trim($match[3]);
117: $obj->num_members = $match[4];
118: $obj->faction = $match[6];
119:
120:
121: $this->db->exec("INSERT INTO organizations (id, name, faction, num_members) VALUES (?, ?, ?, ?)", $obj->id, $obj->name, $obj->faction, $obj->num_members);
122: }
123: }
124: $this->db->commit();
125: } catch (Exception $e) {
126: $this->logger->log("ERROR", "Error downloading orgs");
127: $this->db->rollback();
128: }
129: $this->logger->log("DEBUG", "Finished downloading orgs");
130: }
131: }
132:
133: