Skip to content

Commit

Permalink
[DB MIGRATION REQ'D] Preserve intermediary short links, closes ThinkU…
Browse files Browse the repository at this point in the history
  • Loading branch information
ginatrapani committed Feb 25, 2012
1 parent 7b50b7a commit 55001ab
Show file tree
Hide file tree
Showing 16 changed files with 292 additions and 50 deletions.
15 changes: 9 additions & 6 deletions tests/TestOfDAOFactory.php
Expand Up @@ -43,10 +43,10 @@ public function setUp() {

protected function buildData() {
$builders = array();

// test table for our test dao
$test_table_sql = 'CREATE TABLE tu_test_table(' .
'id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,' .
'id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,' .
'test_name varchar(20),' .
'test_id int(11),' .
'unique key test_id_idx (test_id)' .
Expand Down Expand Up @@ -364,14 +364,17 @@ public function testGetGroupMembershipDAO() {
$this->assertIsA($dao, 'GroupMembershipCountMySQLDAO');
}

/**
* Test get TableStatsDAO
*/
public function testGetTableStatsDAO() {
$dao = DAOFactory::getDAO('TableStatsDAO');
$this->assertNotNull($dao);
$this->assertIsA($dao, 'TableStatsMySQLDAO');
}

public function testGetShortLinkDAO() {
$dao = DAOFactory::getDAO('ShortLinkDAO');
$this->assertNotNull($dao);
$this->assertIsA($dao, 'ShortLinkMySQLDAO');
}
/**
* Test get InstallerDAO without a config file, override with array of config values
*/
Expand All @@ -384,7 +387,7 @@ public function testGetInstallerDAONoConfigFile(){
$this->assertTrue(isset($dao));
$this->assertIsA($dao, 'InstallerMySQLDAO');
$result = $dao->getTables();
$this->assertEqual(sizeof($result), 28);
$this->assertEqual(sizeof($result), 29);
$this->assertEqual($result[0], $cfg_values["table_prefix"].'encoded_locations');
$this->restoreConfigFile();
}
Expand Down
7 changes: 6 additions & 1 deletion tests/TestOfInstaller.php
Expand Up @@ -137,6 +137,7 @@ public function testInstallerShowTables() {
$config->getValue('table_prefix')."instances_twitter, ".
$config->getValue('table_prefix')."invites," .
//$config->getValue('table_prefix')."links," .
$config->getValue('table_prefix')."links_short," .
$config->getValue('table_prefix')."mentions," .
$config->getValue('table_prefix')."mentions_posts, " .
$config->getValue('table_prefix')."owner_instances, ".
Expand Down Expand Up @@ -211,6 +212,7 @@ public function testInstallerCheckTable() {
$config->getValue('table_prefix')."instances, ".
$config->getValue('table_prefix')."instances_twitter, ".
$config->getValue('table_prefix')."invites," .
$config->getValue('table_prefix')."links_short," .
$config->getValue('table_prefix')."mentions," .
$config->getValue('table_prefix')."mentions_posts, " .
$config->getValue('table_prefix')."owner_instances, ".
Expand Down Expand Up @@ -401,6 +403,7 @@ public function testInstallerPopulateTables() {
$config->getValue('table_prefix')."instances_twitter, ".
$config->getValue('table_prefix')."invites," .
$config->getValue('table_prefix')."links," .
$config->getValue('table_prefix')."links_short," .
$config->getValue('table_prefix')."mentions," .
$config->getValue('table_prefix')."mentions_posts, " .
$config->getValue('table_prefix')."owner_instances, ".
Expand Down Expand Up @@ -483,6 +486,7 @@ public function testInstallerPopulateTables() {
$config->getValue('table_prefix')."instances_twitter, ".
$config->getValue('table_prefix')."invites," .
$config->getValue('table_prefix')."links," .
$config->getValue('table_prefix')."links_short," .
$config->getValue('table_prefix')."mentions," .
$config->getValue('table_prefix')."mentions_posts, " .
$config->getValue('table_prefix')."owner_instances, ".
Expand Down Expand Up @@ -545,9 +549,10 @@ public function testInstallerRepairTables() {
public function testGetTablesToInstall(){
$installer = Installer::getInstance();
$tables = $installer->getTablesToInstall();

$expected_tables = array('encoded_locations', 'favorites', 'follower_count', 'follows', 'group_member_count',
'group_members', 'groups', 'hashtags', 'hashtags_posts',
'instances', 'instances_twitter', 'invites', 'links', 'mentions', 'mentions_posts', 'options',
'instances', 'instances_twitter', 'invites', 'links', 'links_short', 'mentions', 'mentions_posts', 'options',
'owner_instances', 'owners', 'places','places_posts',
'plugins', 'post_errors', 'posts', 'stream_data', 'stream_procs', 'user_errors', 'users');
$this->assertIdentical($tables, $expected_tables);
Expand Down
2 changes: 1 addition & 1 deletion tests/TestOfInstallerMySQLDAO.php
Expand Up @@ -82,7 +82,7 @@ public function testGetTables() {
$config_array = $config->getValuesArray();
$dao = new InstallerMySQLDAO($config_array);
$result = $dao->getTables();
$this->assertEqual(sizeof($result), 27);
$this->assertEqual(sizeof($result), 28);
$this->assertEqual($result[0], $config_array["table_prefix"].'encoded_locations');
}
public function testCheckTable() {
Expand Down
16 changes: 8 additions & 8 deletions tests/TestOfLinkMySQLDAO.php
Expand Up @@ -67,7 +67,7 @@ protected function buildData() {
$post_key = $counter + 80;
$builders[] = FixtureBuilder::build('links', array('url'=>'http://flic.kr/p/'.$counter.'e',
'title'=>'Link '.$counter, 'clicks'=>0, 'post_key'=>$post_key,
'error'=>'Generic test error message, Photo not found', 'image_src'=>'http://flic.kr/thumbnail.png',
'error'=>'Generic test error message, Photo not found', 'image_src'=>'http://flic.kr/thumbnail.png',
'expanded_url'=>'', 'error'=>''));
$counter++;
}
Expand Down Expand Up @@ -130,7 +130,7 @@ public function tearDown() {

public function testInsert(){
$link = new Link(array('url'=>'http://example.com/test', 'image_src'=>'',
'expanded_url'=>'http://very.long.domain.that.nobody.would.bother.to.type.com/index.php',
'expanded_url'=>'http://very.long.domain.that.nobody.would.bother.to.type.com/index.php',
'title'=>'Very Long URL', 'post_key'=>1234));

$result = $this->DAO->insert($link);
Expand All @@ -151,8 +151,8 @@ public function testInsert(){

//test another with new fields set
$link = new Link(array('url'=>'http://example.com/test2', 'image_src'=>'',
'expanded_url'=>'http://very.long.domain.that.nobody.would.bother.to.type.com/index.php',
'title'=>'Very Long URL', 'post_key'=>1234567,
'expanded_url'=>'http://very.long.domain.that.nobody.would.bother.to.type.com/index.php',
'title'=>'Very Long URL', 'post_key'=>1234567,
'image_src'=>'http://example.com/thumbnail.png', 'description'=>'My hot link', 'caption'=>"Hot, huh?"));

$result = $this->DAO->insert($link);
Expand Down Expand Up @@ -181,7 +181,7 @@ public function testSaveExpandedUrl() {
$this->assertTrue(sizeof($links_to_expand)>0);

//Just expanded URL
$link = $links_to_expand[0];
$link = $links_to_expand[0]->url;
$this->DAO->saveExpandedUrl($link, "http://expandedurl.com");
$updated_link = $this->DAO->getLinkByUrl($link);
$this->assertEqual($updated_link->expanded_url, "http://expandedurl.com");
Expand Down Expand Up @@ -223,7 +223,7 @@ public function testSaveExpansionError() {

public function testUpdate(){
$link = new Link(array('url'=>'http://example.com/test', 'image_src'=>'',
'expanded_url'=>'http://very.long.domain.that.nobody.would.bother.to.type.com/index.php',
'expanded_url'=>'http://very.long.domain.that.nobody.would.bother.to.type.com/index.php',
'title'=>'Very Long URL', 'post_key'=>15000));

$result = $this->DAO->insert($link);
Expand Down Expand Up @@ -443,10 +443,10 @@ public function testUniqueConstraint2() {
$builder1 = $builder2 = null;
try {
$builder1 = FixtureBuilder::build('links', array('url'=>'http://example.com/'.$counter,
'title'=>'Link '.$counter, 'clicks'=>0, 'post_key'=>$counter,
'title'=>'Link '.$counter, 'clicks'=>0, 'post_key'=>$counter,
'expanded_url'=>'', 'error'=>'', 'image_src'=>''));
$builder2 = FixtureBuilder::build('links', array('url'=>'http://example.com/'.$counter,
'title'=>'Link '.$counter, 'clicks'=>0, 'post_key'=>$counter,
'title'=>'Link '.$counter, 'clicks'=>0, 'post_key'=>$counter,
'expanded_url'=>'', 'error'=>'', 'image_src'=>''));
} catch(PDOException $e) {
$this->assertPattern('/Integrity constraint violation/', $e->getMessage());
Expand Down
60 changes: 60 additions & 0 deletions tests/TestOfShortLinkMySQLDAO.php
@@ -0,0 +1,60 @@
<?php
/**
*
* ThinkUp/tests/TestOfShortLinkMySQLDAO.php
*
* Copyright (c) 2012 Gina Trapani
*
* LICENSE:
*
* This file is part of ThinkUp (http://thinkupapp.com).
*
* ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
* later version.
*
* ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
* <http://www.gnu.org/licenses/>.
*
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2012 Gina Trapani
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
*
*/

require_once dirname(__FILE__).'/init.tests.php';
require_once THINKUP_WEBAPP_PATH.'_lib/extlib/simpletest/autorun.php';
require_once THINKUP_WEBAPP_PATH.'config.inc.php';

class TestOfShortLinkMySQLDAO extends ThinkUpUnitTestCase {

public function testCreateNewShortLinkDAO() {
$dao = DAOFactory::getDAO('ShortLinkDAO');
$this->assertTrue(isset($dao));
$this->assertIsA($dao, 'ShortLinkMySQLDAO');
}

public function testInsert() {
$dao = DAOFactory::getDAO('ShortLinkDAO');
$result = $dao->insert(12, 'http://t.co/12');
$this->assertEqual($result, 1);

$sql = "SELECT * FROM " . $this->table_prefix . 'links_short';
$stmt = ShortLinkMySQLDAO::$PDO->query($sql);
$data = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
array_push($data, $row);
}
$stmt->closeCursor();
$this->assertEqual(count($data), 1);
$data = $data[0];
$this->assertEqual($data['id'], 1);
$this->assertEqual($data['link_id'], 12);
$this->assertEqual($data['short_url'], 'http://t.co/12');
$this->assertEqual($data['click_count'], 0);
}
}
1 change: 1 addition & 0 deletions tests/all_model_tests.php
Expand Up @@ -85,6 +85,7 @@
$model_tests->add(new TestOfPDODAO());
$model_tests->add(new TestOfURLProcessor());
$model_tests->add(new TestOfTableStatsMySQLDAO());
$model_tests->add(new TestOfShortLinkMySQLDAO());

$tr = new TextReporter();
list($usec, $sec) = explode(" ", microtime());
Expand Down
12 changes: 8 additions & 4 deletions webapp/_lib/model/class.DAOFactory.php
Expand Up @@ -46,7 +46,7 @@ class DAOFactory {
*/
static $dao_mapping = array (
//Test DAO
'TestDAO' => array(
'TestDAO' => array(
//MySQL Version
'mysql' => 'TestMySQLDAO',
//faux Version
Expand Down Expand Up @@ -115,7 +115,7 @@ class DAOFactory {
//StreamData DAO
'StreamDataDAO' => array(
//MySQL Version
'mysql' => 'StreamDataMySQLDAO' ),
'mysql' => 'StreamDataMySQLDAO' ),
//StreamProc DAO
'StreamProcDAO' => array(
//MySQL Version
Expand Down Expand Up @@ -147,7 +147,7 @@ class DAOFactory {
//Option MySQL DAO
'OptionDAO' => array (
//MySQL Version
'mysql' => 'OptionMySQLDAO'),
'mysql' => 'OptionMySQLDAO'),
//Backup MySQL DAO
'BackupDAO' => array (
//MySQL Version
Expand Down Expand Up @@ -175,7 +175,11 @@ class DAOFactory {
//TableStats MySQL DAO
'TableStatsDAO' => array (
//MySQL Version
'mysql' => 'TableStatsMySQLDAO')
'mysql' => 'TableStatsMySQLDAO'),
//ShortLink MySQL DAO
'ShortLinkDAO' => array (
//MySQL Version
'mysql' => 'ShortLinkMySQLDAO')
);

/*
Expand Down
11 changes: 3 additions & 8 deletions webapp/_lib/model/class.LinkMySQLDAO.php
Expand Up @@ -226,9 +226,9 @@ public function getPhotosByFriends($user_id, $network, $count = 15, $page = 1, $
}

public function getLinksToExpand($limit = 1500) {
$q = "SELECT l1.url AS url ";
$q = "SELECT * ";
$q .= "FROM ( ";
$q .= " SELECT l.url, l.post_key ";
$q .= " SELECT * ";
$q .= " FROM #prefix#links AS l ";
$q .= " WHERE l.expanded_url = '' and l.error = '' ";
$q .= " ORDER BY id DESC LIMIT :limit ";
Expand All @@ -240,12 +240,7 @@ public function getLinksToExpand($limit = 1500) {
if ($this->profiler_enabled) Profiler::setDAOMethod(__METHOD__);
$ps = $this->execute($q, $vars);

$rows = $this->getDataRowsAsArrays($ps);
$urls = array();
foreach($rows as $row){
$urls[] = $row['url'];
}
return $urls;
return $this->getDataRowsAsObjects($ps, 'Link');
}

public function getLinksToExpandByURL($url, $limit = 0) {
Expand Down
63 changes: 63 additions & 0 deletions webapp/_lib/model/class.ShortLink.php
@@ -0,0 +1,63 @@
<?php
/**
*
* ThinkUp/webapp/_lib/model/class.ShortLink.php
*
* Copyright (c) 2012 Gina Trapani
*
* LICENSE:
*
* This file is part of ThinkUp (http://thinkupapp.com).
*
* ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
* later version.
*
* ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
* <http://www.gnu.org/licenses/>.
*
* ShortLink
*
* Shortened URLs, potentially many per link object.
*
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2012 Gina Trapani
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
*
*/

class ShortLink {
/**
* @var int Internal unique ID.
*/
var $id;
/**
* @var int Expanded link ID in links table.
*/
var $link_id;
/**
* @var str Shortened URL.
*/
var $short_url;
/**
* @var int Total number of clicks as reported by shortening service.
*/
var $click_count;
/**
* @var str Last time the click count was updated from the shortening service.
*/
var $first_seen;
public function __construct($row = false) {
if ($row) {
$this->id = $row['id'];
$this->link_id = $row['link_id'];
$this->short_url = $row['short_url'];
$this->click_count = $row['click_count'];
$this->first_seen = $row['first_seen'];
}
}
}
41 changes: 41 additions & 0 deletions webapp/_lib/model/class.ShortLinkMySQLDAO.php
@@ -0,0 +1,41 @@
<?php
/**
*
* ThinkUp/webapp/_lib/model/class.ShortLinkMySQLDAO.php
*
* Copyright (c) 2012 Gina Trapani
*
* LICENSE:
*
* This file is part of ThinkUp (http://thinkupapp.com).
*
* ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
* later version.
*
* ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
* <http://www.gnu.org/licenses/>.
*
* Short Link Data Access Object interface
*
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2012 Gina Trapani
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
*/
class ShortLinkMySQLDAO extends PDODAO {
public function insert($link_id, $short_url) {
$q = "INSERT INTO #prefix#links_short ";
$q .= "(link_id, short_url) ";
$q .= "VALUES (:link_id , :short_url) ";
$vars = array(
':link_id'=>(int)$link_id,
':short_url'=>$short_url
);
$ps = $this->execute($q, $vars);
return $this->getInsertId($ps);
}
}

0 comments on commit 55001ab

Please sign in to comment.