1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soleo\UrlShortener; |
4
|
|
|
|
5
|
|
|
use \MongoClient; |
6
|
|
|
|
7
|
|
|
class MongoConnection implements ConnectionInterface |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
private $client; |
11
|
|
|
private $DB; |
12
|
|
|
public function __construct($config) |
13
|
|
|
{ |
14
|
|
|
$options = array("connectTimeoutMS" => 30000); |
15
|
|
|
$this->client = new MongoClient($config, $options); |
16
|
|
|
$this->DB = $this->client->selectDB("url_shortener"); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function lookup($slug, $update = false) |
20
|
|
|
{ |
21
|
|
|
$c_db = $this->DB; |
22
|
|
|
$c_table = $c_db->selectCollection("url_table"); |
23
|
|
|
$c_table->ensureIndex(array('shorturl' => 1 ), array('unique' => true)); |
24
|
|
|
$urls = $c_table->findOne(array('shorturl' => $slug), array('longurl', '_id')); |
25
|
|
|
if ($urls) { |
26
|
|
|
if ($update) { |
27
|
|
|
$c_table->ensureIndex(array('hits' => 1 )); |
28
|
|
|
$c_table->update(array("_id" => $urls['_id']), array('$inc' => array("hits" => 1))); |
29
|
|
|
$days = floor(time()/24/3600); |
30
|
|
|
$c_table->update(array("_id" => $urls['_id']), array('$inc' => array("hits_d.{$days}" => 1))); |
31
|
|
|
} |
32
|
|
|
return $urls['longurl']; |
33
|
|
|
} else { |
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function reverseLookup($longUrl) |
39
|
|
|
{ |
40
|
|
|
// If stored, return the slug, otherwise, return false |
|
|
|
|
41
|
|
|
/* |
42
|
|
|
* mongodb |
43
|
|
|
* { |
44
|
|
|
* shorturl |
45
|
|
|
* longurl |
46
|
|
|
* } |
47
|
|
|
*/ |
48
|
|
|
|
49
|
|
|
$c_db = $this->DB; |
50
|
|
|
|
51
|
|
|
// check if url already exists |
52
|
|
|
$c_table = $c_db->selectCollection("url_table"); |
53
|
|
|
$c_table->ensureIndex(array('longurl' => 1 ), array('unique' => true)); |
54
|
|
|
$c_table->ensureIndex(array('shorturl' => 1 ), array('unique' => true)); |
55
|
|
|
$shorturl = $c_table->findOne(array('longurl' => $longUrl), array('shorturl')); |
56
|
|
|
if ($shorturl && $shorturl['shorturl']) { |
57
|
|
|
return $shorturl['shorturl']; |
58
|
|
|
} else { |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function addNewRecord($longUrl, $slug) |
64
|
|
|
{ |
65
|
|
|
$c_db = $this->DB; |
66
|
|
|
$c_table = $c_db->selectCollection("url_table"); |
67
|
|
|
|
68
|
|
|
$c_table->insert(array("shorturl" => $slug, "longurl" => $longUrl)); |
69
|
|
|
return $slug; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getIncrementUid() |
73
|
|
|
{ |
74
|
|
|
// generate increment unique id |
75
|
|
|
$collection_name = 'increment_id'; |
76
|
|
|
$unique_field = 'section'; |
77
|
|
|
$data_field = 'url_id'; |
78
|
|
|
|
79
|
|
|
$c_inc = $this->DB->selectCollection($collection_name); |
80
|
|
|
$c_inc->ensureIndex(array($unique_field => 1), array('unique' => true)); |
81
|
|
|
$ret = $this->DB->command(array("findandmodify" => "increment_id", "query" => array($unique_field => $data_field), |
82
|
|
|
"update" => array('$inc' => array($data_field => 1)), "upsert" => true )); |
83
|
|
|
if ($ret && $ret['value'] && $ret['value'][$data_field]) { |
|
|
|
|
84
|
|
|
return $ret['value'][$data_field]; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.