1
0
mirror of https://github.com/Chouchen/shorturl.git synced 2018-06-07 06:34:38 +02:00
shorturl/class/ShortURL.php

47 lines
1.4 KiB
PHP
Raw Normal View History

2010-12-23 12:26:25 +01:00
<?
include 'XMLSQL.php';
class ShortURL extends XMLSQL{
const DATABASE = "db/database.xml";
2011-01-04 12:26:21 +01:00
const STATE_ALREADY_EXIST = "This shortcut already exists. ";
const STATE_FIELD_MISSING = "Don't leave any field blank ! ";
const STATE_ERROR = "Error. ";
const STATE_CREATED = "Shortcut created ";
2010-12-23 12:26:25 +01:00
public $_debug = false;
public function __construct($path = ''){
parent::__construct($path.self::DATABASE);
}
public function shortThisUrl($longUrl, $shortName){
if($this->pkAlreadyExists($shortName, 'url')){
return self::STATE_ALREADY_EXIST;
}else{
2011-01-04 11:57:00 +01:00
return $this->insert(array('url'=>$longUrl,'hit'=>'0'), rawurlencode($shortName))->into('url')->query();
2010-12-23 12:26:25 +01:00
}
}
public function findThisUrl($shortName){
if($this->pkAlreadyExists(rawurlencode($shortName), 'url')){
2011-01-04 11:57:00 +01:00
$this->_incrementStatFor($shortName);
2010-12-23 12:26:25 +01:00
return $this->select(array('url'))->from('url')->where(rawurlencode($shortName))->query();
}else{
return;
}
}
public function extractEverything(){
return $this->select()->from('url')->query();
}
2011-01-04 11:57:00 +01:00
/**
* Considering the table with $shortname already exist
*/
private function _incrementStatFor($shortName){
$currentHit = $this->select(array('hit'))->from('url')->where(rawurlencode($shortName))->query();
$currentHit = $currentHit[0];
return $this->update('url')->set(array('hit'=>$currentHit+1))->where(rawurlencode($shortName))->query();
}
2010-12-23 12:26:25 +01:00
}