adding scripts for toolbox import and setting up vagrant
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 9 Jul 2014 20:30:36 +0000 (16:30 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 9 Jul 2014 20:30:36 +0000 (16:30 -0400)
add GLM_HOST_ID config for vagrant to use devdb database
script for the toolbox import

Toolkit/Scripts/Toolbox/Files.php [new file with mode: 0644]
Toolkit/Scripts/Toolbox/Pages.php [new file with mode: 0644]
Toolkit/Scripts/Toolbox/Paragraphs.php [new file with mode: 0644]
Toolkit/Scripts/Toolbox/Update.php [new file with mode: 0644]
Toolkit/Scripts/Toolbox/index.php [new file with mode: 0644]
Toolkit/Scripts/Update.php [new file with mode: 0644]
Toolkit/Scripts/index.php [new file with mode: 0644]
config/server.ini

diff --git a/Toolkit/Scripts/Toolbox/Files.php b/Toolkit/Scripts/Toolbox/Files.php
new file mode 100644 (file)
index 0000000..79263fa
--- /dev/null
@@ -0,0 +1,189 @@
+<?php
+
+class Toolkit_Scripts_Toolbox_Files
+{
+    protected $id;
+    protected $filename;
+    protected $bytes;
+    protected $type;
+    protected $urltext;
+    protected $paragraph;
+    protected $pos;
+
+    public function getId() {
+        return $this->id;
+    }
+
+    public function setId($id) {
+        if (!is_numeric($id)) {
+            throw new Exception('id must be a number');
+        }
+        if (!$this->id) {
+            $this->id = $id;
+        }
+        return $this;
+    }
+
+    public function getFilename()
+    {
+        return $this->filename;
+    }
+
+    public function setFilename($filename)
+    {
+        $this->filename = $filename;//$this->_convertToUTF8($filename);
+        return $this;
+    }
+
+    public function getBytes()
+    {
+        return $this->bytes;
+    }
+
+    public function setBytes($bytes)
+    {
+        $this->bytes = $bytes;
+        return $this;
+    }
+
+    public function getType()
+    {
+        return $this->type;
+    }
+
+    public function setType($type)
+    {
+        $this->type = $type;
+        return $this;
+    }
+
+    public function getUrltext()
+    {
+        return $this->urltext;
+    }
+
+    public function setUrltext($urltext)
+    {
+        $this->urltext = $this->_convertToUTF8($urltext);
+        return $this;
+    }
+
+    public function getParagraph()
+    {
+        return $this->paragraph;
+    }
+
+    public function setParagraph($paragraph)
+    {
+        $this->paragraph = $this->_convertToUTF8($paragraph);
+        return $this;
+    }
+
+    public function getPos()
+    {
+        return $this->pos;
+    }
+
+    public function setPos($pos)
+    {
+        $this->pos = $pos;
+        return $this;
+    }
+
+    public function importFile(PDO $dbh, Toolkit_FileServer_FileAdapter $fs)
+    {
+        static $update;
+        if (!$update) {
+            $sql = "
+            UPDATE files
+               SET filename = :filename
+             WHERE id = :id";
+            $update = $dbh->prepare($sql);
+        }
+        $filename = $this->getFilename();
+        if (!$filename) {
+            return false;
+        }
+        $fileUrl
+            = FILE_URL_ORG
+            . urlencode($this->getFilename());
+        try {
+            $res = $fs->upload($fileUrl);
+        } catch (Toolkit_FileServer_Exception $e) {
+            Toolkit_Logger::logException('File Server', $e);
+            return;
+        }
+        $this->setFilename($res['name']);
+        if ($res['name']) {
+            try {
+                $update->bindParam(
+                    ':filename',
+                    $res['name'],
+                    PDO::PARAM_STR
+                );
+                $update->bindParam(
+                    ':id',
+                    $this->getId(),
+                    PDO::PARAM_INT
+                );
+                $update->execute();
+            } catch(PDOException $e) {
+                Toolkit_Common::handleError($e);
+            }
+        }
+    }
+
+    public function update(PDO $dbh)
+    {
+        try {
+            $tablename = 'files';
+            $values = get_object_vars($this);
+            $sql = Toolkit_Common::createSQLUpdate(
+                $tablename,
+                array_keys($values),
+                array('id = :id')
+            );
+
+            $stmt = Toolkit_Common::prepareQuery(
+                $dbh,
+                $tablename,
+                $sql,
+                $values
+            );
+            $stmt->execute();
+            return $this;
+        } catch(PDOException $e) {
+            Toolkit_Common::handleError($e);
+        }
+    }
+
+    public function save(PDO $dbh)
+    {
+        try {
+            $tablename = 'files';
+            $values = get_object_vars($this);
+            unset($values['id']);
+            $sql = Toolkit_Common::createSQLInsert(
+                $tablename,
+                array_keys($values)
+            );
+            $sql .= ' RETURNING id';
+            $stmt = Toolkit_Common::prepareQuery(
+                $dbh,
+                $tablename,
+                $sql,
+                $values
+            );
+            $stmt->execute();
+            $this->setId($stmt->fetchColumn());
+            return $this;
+        } catch(PDOException $e) {
+            Toolkit_Common::handleError($e);
+        }
+    }
+
+    private function _convertToUTF8($text)
+    {
+        return iconv("ISO-8859-1", "UTF-8//IGNORE", $text);
+    }
+}
diff --git a/Toolkit/Scripts/Toolbox/Pages.php b/Toolkit/Scripts/Toolbox/Pages.php
new file mode 100644 (file)
index 0000000..ca8ddb7
--- /dev/null
@@ -0,0 +1,157 @@
+<?php
+
+class Toolkit_Scripts_Toolbox_Pages
+{
+
+    protected $id;
+    protected $active;
+    protected $navigation_name;
+    protected $keyword;
+    protected $meta_title;
+    protected $meta_description;
+    protected $template;
+    protected $parent;
+    protected $pos;
+
+    public function getId() {
+        return $this->id;
+    }
+
+    public function setId($id) {
+        if (!is_numeric($id)) {
+            throw new Exception('id must be a number');
+        }
+        if (!$this->id) {
+            $this->id = $id;
+        }
+        return $this;
+    }
+
+    public function getActive() {
+        return $this->active;
+    }
+
+    public function setActive($active) {
+        $this->active = ($active) ? true: false;
+        return $this;
+    }
+
+    public function getNavigation_name() {
+        return $this->navigation_name;
+    }
+
+    public function setNavigation_name($navigation_name) {
+        $this->navigation_name = $this->_convertToUTF8($navigation_name);
+        return $this;
+    }
+
+    public function getKeyword() {
+        return $this->keyword;
+    }
+
+    public function setKeyword($keyword) {
+        $this->keyword = $this->_convertToUTF8($keyword);
+        return $this;
+    }
+
+    public function getMeta_title() {
+        return $this->meta_title;
+    }
+
+    public function setMeta_title($meta_title) {
+        $this->meta_title = $this->_convertToUTF8($meta_title);
+        return $this;
+    }
+
+    public function getMeta_description() {
+        return $this->meta_description;
+    }
+
+    public function setMeta_description($meta_description) {
+        $this->meta_description = $this->_convertToUTF8($meta_description);
+        return $this;
+    }
+
+    public function getTemplate() {
+        return $this->template;
+    }
+
+    public function setTemplate($template) {
+        $this->template = $template;
+        return $this;
+    }
+
+    public function getParent() {
+        return $this->parent;
+    }
+
+    public function setParent($parent) {
+        $this->parent = $parent;
+        return $this;
+    }
+
+    public function getPos() {
+        return $this->pos;
+    }
+
+    public function setPos($pos) {
+        $this->pos = $pos;
+        return $this;
+    }
+
+    public function insert(PDO $dbh)
+    {
+        try {
+            $tablename = 'pages';
+            $values = get_object_vars($this);
+
+            $sql = Toolkit_Common::createSQLInsert(
+                $tablename,
+                array_keys($values)
+            );
+            $sql .= ' RETURNING id';
+
+            $stmt = Toolkit_Common::prepareQuery(
+                $dbh,
+                $tablename,
+                $sql,
+                $values
+            );
+
+            $stmt->execute();
+        } catch(PDOException $e) {
+            Toolkit_Common::handleError($e);
+        }
+    }
+
+    public function update(PDO $dbh)
+    {
+        try {
+            $tablename = 'pages';
+            $values = get_object_vars($this);
+
+            $sql = Toolkit_Common::createSQLUpdate(
+                $tablename,
+                array_keys($values),
+                array('id = :id')
+            );
+
+            $stmt = Toolkit_Common::prepareQuery(
+                $dbh,
+                $tablename,
+                $sql,
+                $values
+            );
+
+            $stmt->execute();
+        } catch(PDOException $e) {
+            Toolkit_Common::handleError($e);
+        }
+    }
+
+    private function _convertToUTF8($text)
+    {
+        return $text;
+        return iconv("ISO-8859-1", "UTF-8//IGNORE", $text);
+    }
+}
diff --git a/Toolkit/Scripts/Toolbox/Paragraphs.php b/Toolkit/Scripts/Toolbox/Paragraphs.php
new file mode 100644 (file)
index 0000000..30cbc31
--- /dev/null
@@ -0,0 +1,189 @@
+<?php
+
+class Toolkit_Scripts_Toolbox_Paragraphs
+{
+
+    protected $id;
+    protected $title;
+    protected $page;
+    protected $pos;
+    protected $active;
+    protected $description;
+    protected $caption;
+    protected $image;
+
+    public function getId() {
+        return $this->id;
+    }
+
+    public function setId($id) {
+        if (!is_numeric($id)) {
+            throw new Exception('id must be a number');
+        }
+        if (!$this->id) {
+            $this->id = $id;
+        }
+        return $this;
+    }
+
+    public function getTitle() {
+        return $this->title;
+    }
+
+    public function setTitle($title) {
+        $this->title = $this->_convertToUTF8($title);
+        return $this;
+    }
+
+    public function getPage() {
+        return $this->page;
+    }
+
+    public function setPage($page) {
+        $this->page = $page;
+        return $this;
+    }
+
+    public function getPos() {
+        return $this->pos;
+    }
+
+    public function setPos($pos) {
+        $this->pos = $pos;
+        return $this;
+    }
+
+    public function getActive() {
+        return $this->active;
+    }
+
+    public function setActive($active) {
+        $this->active = $active;
+        return $this;
+    }
+
+    public function getDescription() {
+        return $this->description;
+    }
+
+    public function setDescription($description) {
+        $this->description = $this->_convertToUTF8($description);
+        return $this;
+    }
+
+    public function getCaption() {
+        return $this->caption;
+    }
+
+    public function setCaption($caption) {
+        $this->caption = $this->_convertToUTF8($caption);
+        return $this;
+    }
+
+    public function getImage() {
+        return $this->image;
+    }
+
+    public function setImage($image) {
+        if ($image != 'ERROR') {
+            $this->image = $image;
+        }
+        return $this;
+    }
+
+    public function importImage(PDO $dbh, Toolkit_Image_Server $is)
+    {
+        static $update;
+        if (!$update) {
+            $sql = "
+            UPDATE paragraphs
+               SET image = :image
+             WHERE id = :id";
+            $update = $dbh->prepare($sql);
+        }
+        $img = null;
+        if ($this->getImage() != '') {
+            $imageUrl
+                = IMG_URL_ORG
+                . urlencode($this->getImage());
+            try {
+                $img = $is->imageUpload($imageUrl);
+                var_dump($img);
+                $this->setImage($img);
+            } catch(PEAR_Exception $e) {
+                Toolkit_Common::handleError($e);
+            }
+        }
+        if ($img) {
+            try {
+                $update->bindParam(
+                    ':image',
+                    $img,
+                    PDO::PARAM_STR
+                );
+                $update->bindParam(
+                    ':id',
+                    $this->getId(),
+                    PDO::PARAM_INT
+                );
+                $update->execute();
+            } catch(PDOException $e) {
+                Toolkit_Common::handleError($e);
+            }
+        }
+    }
+
+    public function insert(PDO $dbh)
+    {
+        try {
+            $tablename = 'paragraphs';
+            $values = get_object_vars($this);
+            $sql = Toolkit_Common::createSQLInsert(
+                $tablename,
+                array_keys($values)
+            );
+            $sql .= ' RETURNING id';
+            $stmt = Toolkit_Common::prepareQuery(
+                $dbh,
+                $tablename,
+                $sql,
+                $values
+            );
+            $stmt->execute();
+            $this->setId($stmt->fetchColumn());
+            return $this;
+        } catch(PDOException $e) {
+            Toolkit_Common::handleError($e);
+        }
+    }
+
+    public function update(PDO $dbh)
+    {
+        try {
+            $tablename = 'paragraphs';
+            $values = get_object_vars($this);
+            $sql = Toolkit_Common::createSQLUpdate(
+                $tablename,
+                array_keys($values),
+                array('id = :id')
+            );
+            $stmt = Toolkit_Common::prepareQuery(
+                $dbh,
+                $tablename,
+                $sql,
+                $values
+            );
+            $stmt->execute();
+            return $this;
+        } catch(PDOException $e) {
+            Toolkit_Common::handleError($e);
+        }
+    }
+
+    private function _convertToUTF8($text)
+    {
+        return $text;
+        return iconv("ISO-8859-1", "UTF-8//IGNORE", $text);
+    }
+
+}
diff --git a/Toolkit/Scripts/Toolbox/Update.php b/Toolkit/Scripts/Toolbox/Update.php
new file mode 100644 (file)
index 0000000..f14885d
--- /dev/null
@@ -0,0 +1,570 @@
+<?php
+
+class Toolkit_Scripts_Toolbox_Update
+    extends Toolkit_Scripts_Update
+{
+
+    protected $pages      = array();
+    protected $paragraphs = array();
+    protected $files      = array();
+    protected $htImages   = array();
+    protected $htImageFolders = array();
+    protected $newHtImages = array();
+
+    const POSITION_OFFSET = 2;
+
+    public function __construct()
+    {
+    }
+
+    public function fetchShortUrls()
+    {
+        $sql = "
+        SELECT id,short_url
+          FROM bus_category
+         WHERE short_url != ''
+           AND short_url IS NOT NULL";
+        $stmt = $this->_remoteDbh->query($sql);
+        $format = 'RewriteRule ^%s$ index.php?catid=%d [L]<br>';
+        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
+            //var_dump($row);
+            echo sprintf($format, $row['short_url'], $row['id']);
+        }
+    }
+
+    private function _getFileObj($fileName, $fileUrlText, $paragraphId, $pos)
+    {
+        if (!is_file(UP_BASE . $fileName)) {
+            return false;
+        }
+        $storedSize = filesize(UP_BASE . $fileName);
+        $mediaType  = filetype(UP_BASE . $fileName);
+        $extension  = end(explode('.', $fileName));
+        $file = new Toolkit_Scripts_Toolbox_Files();
+        $file
+            ->setFilename($fileName)
+            ->setBytes($storedSize)
+            ->setType($mediaType)
+            ->setUrltext($fileUrlText)
+            ->setParagraph($paragraphId)
+            ->setPos($pos);
+        return $file;
+    }
+
+    public function deleteOldContent()
+    {
+        $this->_localDbh->query("DELETE FROM toolbox.paragraphs");
+        $this->_localDbh->query("DELETE FROM toolbox.pages");
+        $this->_localDbh->query("DELETE FROM toolbox.files");
+    }
+
+    public function getOldPages()
+    {
+        $mainParaStartCounter = 5000;
+        $sql = "
+          SELECT b.*,bcb.pos
+            FROM bus b, bus_category_bus bcb
+           WHERE bcb.busid = b.id
+             AND bcb.catid = :catid
+        ORDER BY bcb.pos";
+        $getParas = $this->_remoteDbh->prepare($sql);
+        try {
+            $sql = "
+              SELECT *
+                FROM bus_category
+            ORDER BY parent,pos";
+            $stmt = $this->_remoteDbh->query($sql);
+            $stmt->execute();
+
+            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
+                // first paragraph actually comes from the old bus_category table
+                $paragraph = new Toolkit_Scripts_Toolbox_Paragraphs();
+                $paragraph
+                    ->setId(++$mainParaStartCounter)
+                    ->setTitle($row['intro'])
+                    ->setPage($row['id'])
+                    ->setPos(1)
+                    ->setActive(true)
+                    ->setDescription($row['description'])
+                    ->setCaption($row['imagename'])
+                    ->setImage($row['image']);
+                $this->paragraphs[] = $paragraph;
+                // now let's get the paragraphs from bus_category_bus and bus
+                // remember that we need to increment the pos by one
+                // because we already have one paragraph
+                $getParas->bindValue(
+                    ":catid",
+                    $row['id'],
+                    PDO::PARAM_INT
+                );
+                $getParas->execute();
+                while ($row2 = $getParas->fetch(PDO::FETCH_ASSOC)) {
+                    $paragraph = new Toolkit_Scripts_Toolbox_Paragraphs();
+                    $paragraph
+                        ->setId($row2['id'])
+                        ->setTitle($row2['name'])
+                        ->setPage($row['id'])
+                        ->setPos($row2['pos'] + self::POSITION_OFFSET)
+                        ->setActive(true)
+                        ->setDescription($row2['description'])
+                        ->setCaption($row2['imagename'])
+                        ->setImage($row2['image']);
+                    $this->paragraphs[] = $paragraph;
+                    // get the files for this paragraph
+                    for ($i = 1; $i <= 5; ++$i) {
+                        switch ($i) {
+                            case 1:
+                                $fileName    = 'file';
+                                $fileUrlText = 'filename';
+                                break;
+                            default:
+                                $fileName    = 'file' . $i;
+                                $fileUrlText = 'file' . $i . 'name';
+                                break;
+                        }
+                        if ($row2[$fileName]) {
+                            $file = $this->_getFileObj(
+                                $row2[$fileName],
+                                $row2[$fileUrlText],
+                                $row2['id'],
+                                $i
+                            );
+                            if ($file) {
+                                $this->files[] = $file;
+                            }
+                        }
+                    }
+                }
+                $page = new Toolkit_Scripts_Toolbox_Pages();
+                $page
+                    ->setId($row['id'])
+                    ->setActive($row['active'])
+                    ->setMeta_title($row['title'])
+                    ->setMeta_description($row['meta_descr'])
+                    ->setNavigation_name($row['category'])
+                    ->setPos($row['pos'])
+                    ->setParent($row['parent'])
+                    ->setKeyword($row['keyword'])
+                    ->setTemplate($row['template']);
+
+                // set the page to pages array
+                $this->pages[$page->getParent()][$page->getId()] = $page;
+            }
+//            echo '<pre>'.print_r($this->pages, true).'</pre>';
+        } catch(PDOException $e) {
+            Toolkit_Common::handleError($e);
+        }
+    }
+
+    public function updatePages()
+    {
+        if (!is_array($this->pages)) {
+            die('no pages');
+        }
+        //var_dump($this->pages);
+        //exit;
+        $found = array();
+        $notFound = array();
+        foreach ($this->pages as $parent => $pages) {
+            foreach ($pages as $page) {
+                // see if there's a page match for local db
+                $sql = "
+                SELECT id,navigation_name
+                  FROM toolbox.pages
+                 WHERE id = :id";
+                $stmt = $this->_localDbh->prepare($sql);
+                $stmt->bindParam(':id', $page->getId());
+                $stmt->execute();
+                if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
+                    $found[] = $row;
+                    $page->update($this->_localDbh);
+                } else {
+                    $notFound[] = $page;
+                    $page->insert($this->_localDbh);
+                }
+            }
+        }
+        $this->resetSequences();
+        var_dump($found);
+        var_dump($notFound);
+    }
+
+    public function resetSequences()
+    {
+        $sql = "SELECT setval('toolbox.pages_id_seq', (SELECT max(id) FROM toolbox.pages))";
+        $this->_localDbh->query($sql);
+        $sql = "SELECT setval('toolbox.paragraphs_id_seq', (SELECT max(id) FROM toolbox.paragraphs))";
+        $this->_localDbh->query($sql);
+        $sql = "SELECT setval('toolbox.files_id_seq', (SELECT max(id) FROM toolbox.files));";
+        $this->_localDbh->query($sql);
+    }
+
+    public function updateParagraphs()
+    {
+        if (!is_array($this->paragraphs)) {
+            return false;
+        }
+        $this->_localDbh->query("DELETE FROM toolbox.paragraphs");
+        $found    = array();
+        $notFound = array();
+        foreach ($this->paragraphs as $paragraph) {
+            // see if there's a paragraph in local db
+            $sql = "
+            SELECT id,title
+              FROM toolbox.paragraphs
+             WHERE id = :id";
+            $stmt = $this->_localDbh->prepare($sql);
+            $stmt->bindParam(':id', $paragraph->getId());
+            $stmt->execute();
+            if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
+                $found[] = $row;
+                $paragraph->update($this->_localDbh);
+            } else {
+                $notFound[] = $paragraph;
+                $paragraph->insert($this->_localDbh);
+            }
+        }
+        var_dump($found);
+        var_dump($notFound);
+    }
+
+    public function getNewParagraphs()
+    {
+        $this->paragraphs = array();
+        $sql = "
+        SELECT *
+          FROM paragraphs
+         WHERE image != ''
+           AND image not like 'is%'";
+        $this->paragraphs = array();
+        try {
+            $stmt = $this->_localDbh->query($sql);
+            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
+                $paragraph = new Toolkit_Scripts_Toolbox_Paragraphs();
+                $paragraph
+                    ->setId($row['id'])
+                    ->setImage($row['image']);
+                $this->paragraphs[] = $paragraph;
+            }
+            var_dump($this->paragraphs);
+        } catch(PDOException $e) {
+            Toolkit_Common::handleError($e);
+        }
+    }
+
+    public function getNewParagraphsWithDescr()
+    {
+        $this->paragraphs     = array();
+        $this->htImages       = array();
+        $this->htImageFolders = array();
+        $sql = "
+        SELECT *
+          FROM paragraphs
+         WHERE description != ''
+           AND strpos(description, 'ht_images') > 0";
+        $this->paragraphs = array();
+        try {
+            $stmt = $this->_localDbh->query($sql);
+            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
+                $paragraph = new Toolkit_Scripts_Toolbox_Paragraphs();
+                $paragraph
+                    ->setId($row['id'])
+                    ->setDescription($row['description']);
+                preg_match_all('/< *img[^>]*src *= *["\']?([^"\']*)/i', $row['description'], $matches);
+                if ($matches) {
+                    foreach ($matches[1] as $match) {
+                        $this->htImages[$match] = true;
+                    }
+                }
+                $this->paragraphs[] = $paragraph;
+            }
+            echo '<pre>'.print_r($this->htImages, true).'</pre>';
+        } catch(PDOException $e) {
+            Toolkit_Common::handleError($e);
+        }
+    }
+
+    public function addFiles()
+    {
+        $this->_localDbh->query("DELETE FROM toolbox.files");
+        if (!is_array($this->files)) {
+            return false;
+        }
+        foreach ($this->files as $file) {
+            $file->save($this->_localDbh);
+        }
+    }
+
+    public function getOldFiles()
+    {
+        $this->files = array();
+        $sql = "
+        SELECT *
+          FROM files";
+        $stmt = $this->_remoteDbh->query($sql);
+        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
+            $file = $this->_getFileObj(
+                $row['filename'],
+                $row['urltext'],
+                $row['bus_id'],
+                $row['pos']
+            );
+            if ($file) {
+                $this->files[] = $file;
+            }
+        }
+        var_dump($this->files);
+    }
+
+    public function importFiles(Toolkit_FileServer_FileAdapter $fs)
+    {
+        try {
+            $sql = "
+            SELECT *
+              FROM toolbox.files
+             WHERE filename NOT LIKE 'fs%'";
+            $stmt = $this->_localDbh->query($sql);
+            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
+                var_dump($row);
+                $file = $this->_getFileObj(
+                    $row['filename'],
+                    $row['urltext'],
+                    $row['paragraph'],
+                    $row['pos']
+                );
+                if ($file) {
+                    $file->setId($row['id']);
+                    $file->importFile(
+                        $this->_localDbh,
+                        $fs
+                    );
+                    $file->update($this->_localDbh);
+                }
+            }
+        } catch (PDOException $e) {
+            Toolkit_Common::handleError($e);
+        }
+        exit;
+    }
+
+    public function addFolder($folderName, $parentId)
+    {
+        if (!is_numeric($parentId)) {
+            throw new InvalidArgumentException('parentId must be numeric');
+        }
+        if ($folderName == '') {
+            throw new InvalidArgumentException('folderName must not be empty');
+        }
+        try {
+            // check to see if the folder is already there
+            $sql = "
+            SELECT id
+              FROM ckeditor_folders
+             WHERE name = :name";
+            $stmt2 = $this->_localDbh->prepare($sql);
+            $stmt2->bindParam(':name', $folderName);
+            $stmt2->execute();
+            $folderId = $stmt2->fetchColumn();
+            if ($folderId) {
+                return $folderId;
+            } else {
+                $sql = "
+                INSERT INTO ckeditor_folders
+                (name, parent)
+                VALUES
+                (:name, :parent)";
+                $stmt = $this->_localDbh->prepare($sql);
+                $stmt->bindParam(':name', $folderName, PDO::PARAM_STR);
+                $stmt->bindParam(':parent', $parentId, PDO::PARAM_INT);
+                $stmt->execute();
+                return $this->_localDbh->lastInsertId('ckeditor_folders_id_seq');
+            }
+        } catch (PDOException $e) {
+            Toolkit_Common::handleError($e);
+        }
+    }
+
+    public function addImage(Toolkit_FileServer_ImageAdapter $ia, $folder, $image)
+    {
+        if ($image == '') {
+            throw new InvalidArgumentException('Image must not be empty');
+        }
+        if (!is_numeric($folder['id'])) {
+            throw new InvalidArgumentException('Folder array must contain id');
+        }
+        // check to see if the folder is already there
+        $sql = "
+        SELECT name_on_disk
+          FROM ckeditor_images
+         WHERE file_name = :file_name";
+        $stmt2 = $this->_localDbh->prepare($sql);
+        $stmt2->bindParam(':file_name', $image);
+        $stmt2->execute();
+        $imageName = $stmt2->fetchColumn();
+        if ($imageName) {
+            return $imageName;
+        }
+        if ($folder['path'] == '../../images/ht_images/') {
+            $imageUrl
+                = FILEMANAGER_URL_ORG
+                . $image;
+        } else {
+            $imageUrl
+                = FILEMANAGER_URL_ORG
+                . $folder['path']
+                . $image;
+        }
+        if ($image != '') {
+            try {
+                $img = $ia->upload($imageUrl);
+            } catch(PEAR_Exception $e) {
+                Toolkit_Common::handleError($e);
+            } catch(Toolkit_FileServer_Exception $e) {
+                return false;
+            }
+        }
+        $dimensions = $ia->getImageSize(CKIMAGE . $img['name']);
+        $sql = "
+            INSERT INTO ckeditor_images (file_name, name_on_disk, folder,
+            original_width, original_height)
+            VALUES (:fname, :nod, :folder, :width, :height)";
+
+        $stmt = $this->_localDbh->prepare($sql);
+        $stmt->bindParam(
+            ':fname',
+            $image,
+            PDO::PARAM_STR
+        );
+        $stmt->bindParam(':nod', $img['name'], PDO::PARAM_STR);
+        $stmt->bindValue(':folder', $folder['id'], PDO::PARAM_INT);
+        $stmt->bindValue(':width', $dimensions[0], PDO::PARAM_INT);
+        $stmt->bindValue(':height', $dimensions[1], PDO::PARAM_INT);
+
+        $stmt->execute();
+        return $img['name'];
+    }
+
+    public function importImages(Toolkit_Image_Server $is)
+    {
+        $this->getNewParagraphs();
+        if (is_array($this->paragraphs)) {
+            foreach ($this->paragraphs as $paragraph) {
+                $img = $paragraph->getImage();
+                $pattern = '/^is/';
+                if ($img != '' && !preg_match($pattern, $img)) {
+                    $paragraph->importImage($this->_localDbh, $is);
+                }
+            }
+        }
+    }
+
+    public function importParagraphImages(Toolkit_FileServer_ImageAdapter $ia)
+    {
+        if (is_array($this->htImages)) {
+            foreach ($this->htImages as $href => $found) {
+                // parse out the url to find the ending
+                $dirPart = str_replace(FILEMANAGER_URL_ORG, '', $href);
+                if ($dirPart != '') {
+                    $items = explode('/', $dirPart);
+                    $itemCount = count($items);
+                    switch ($itemCount) {
+                    case 3 :
+                        $this->htImageFolders[$items[0]][$items[1]][] = array(
+                            'image' => $items[2],
+                            'path'  => $matches[0]
+                        );
+                        break;
+                    case 2 :
+                        $this->htImageFolders[$items[0]][] = array(
+                            'image' => $items[1],
+                            'path'  => $matches[0]
+                        );
+                        break;
+                    case 1 :
+                        $this->htImageFolders[] = array(
+                            'image' => $items[0],
+                            'path'  => $matches[0]
+                        );
+                        break;
+                    }
+                }
+
+            }
+//            echo '<pre>'.print_r($this->htImageFolders, true).'</pre>';
+//            exit;
+            if (is_array($this->htImageFolders)) {
+                foreach ($this->htImageFolders as $folderName => $images) {
+                    if (!is_numeric($folderName)) {
+                        var_dump($folderName);
+                        $folderId = $this->addFolder($folderName, 0);
+                        $folder = array(
+                            'id'   => $folderId,
+                            'path' => $folderName . '/'
+                        );
+                        foreach ($images as $image) {
+                            if (!$img = $this->addImage($ia, $folder, $image['image'])) {
+//                                echo 'not found!';
+                            } else {
+                                $this->newHtImages[] = array(
+                                    'oldPath' => FILEMANAGER_URL_ORG . $image['path']
+                                        . $folderName . '/' . $image['image'],
+                                    'newPath' => CKIMAGE_ORIGINAL
+                                        . $img
+                                );
+                            }
+                        }
+
+                    } else {
+                        $folder = array(
+                            'id' => 1,
+                            'path' => ''
+                        );
+                        if (!$img = $this->addImage($ia, $folder, $images['image'])) {
+//                            echo 'not found!';
+                        } else {
+                            $this->newHtImages[] = array(
+                                    'oldPath' => FILEMANAGER_URL_ORG . $images['path']
+                                        . $images['image'],
+                                    'newPath' => CKIMAGE_ORIGINAL
+                                        . $img
+                                );
+                        }
+                    }
+                }
+            }
+//            echo '<pre>'.print_r($this->newHtImages, true).'</pre>';
+
+        }
+    }
+
+    public function updateParagraphsWithImageNames()
+    {
+        echo '<pre>'.print_r($this->htImageFolders, true).'</pre>';
+        echo '<pre>'.print_r($this->newHtImages, true).'</pre>';
+
+        try {
+            $sql = "
+            UPDATE paragraphs
+               SET description = replace(description, :oldImageName, :newImageName)";
+            $updateParagraphs = $this->_localDbh->prepare($sql);
+            if (is_array($this->newHtImages)) {
+                foreach ($this->newHtImages as $image) {
+                    $updateParagraphs->bindParam(
+                        ':oldImageName',
+                        $image['oldPath'],
+                        PDO::PARAM_STR
+                    );
+                    $updateParagraphs->bindParam(
+                        ':newImageName',
+                        $image['newPath'],
+                        PDO::PARAM_STR
+                    );
+                    $updateParagraphs->execute();
+                }
+            }
+        } catch(PDOException $e) {
+            Toolkit_Common::handleError($e);
+        }
+    }
+
+}
diff --git a/Toolkit/Scripts/Toolbox/index.php b/Toolkit/Scripts/Toolbox/index.php
new file mode 100644 (file)
index 0000000..7f1735f
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+$toolboxUpdates = new Toolkit_Scripts_Toolbox_Update();
+$toolboxUpdates->setLocalDbh($dbhLocal);
+$toolboxUpdates->setRemoteDbh($dbhRemote);
+
+//$toolboxUpdates->fetchShortUrls();
+// FIRST PART
+//$toolboxUpdates->deleteOldContent();
+//$toolboxUpdates->getOldPages();
+//$toolboxUpdates->updatePages();
+//$toolboxUpdates->updateParagraphs();
+
+// SECOND PART
+//$toolboxUpdates->importImages($is);
+//$toolboxUpdates->getNewParagraphsWithDescr();
+//$toolboxUpdates->importParagraphImages($ia);
+//$toolboxUpdates->updateParagraphsWithImageNames();
+
+// THIRD PART
+//$toolboxUpdates->getOldFiles();
+//$toolboxUpdates->addfiles();
+//$toolboxUpdates->importFiles($fa);
diff --git a/Toolkit/Scripts/Update.php b/Toolkit/Scripts/Update.php
new file mode 100644 (file)
index 0000000..2ed696a
--- /dev/null
@@ -0,0 +1,209 @@
+<?php
+/**
+ * Toolkit_Scripts_Update
+ *
+ * @package Webdav
+ * @version //autogen//
+ * @copyright Copyright (c) 2010 All rights reserved.
+ * @author
+ * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
+ */
+class Toolkit_Scripts_Update
+{
+    protected $_remoteDbh;
+    protected $_remoteSchoolDbh;
+    protected $_localDbh;
+    protected $_fileServer;
+    protected $_imageServer;
+
+    /* public __construct() {{{ */
+    /**
+     * __construct
+     *
+     * @access public
+     * @return void
+     */
+    public function __construct()
+    {
+
+    }
+    /* }}} */
+
+    /* public setRemoteDbh(PDO $dbh) {{{ */
+    /**
+     * setRemoteDbh
+     *
+     * @param PDO $dbh
+     *
+     * @access public
+     * @return void
+     */
+    public function setRemoteDbh(PDO $dbh)
+    {
+        $this->_remoteDbh = $dbh;
+    }
+    /* }}} */
+
+    /* public setLocalDbh(PDO $dbh) {{{ */
+    /**
+     * setLocalDbh
+     *
+     * @param PDO $dbh
+     *
+     * @access public
+     * @return void
+     */
+    public function setLocalDbh(PDO $dbh)
+    {
+        $this->_localDbh = $dbh;
+    }
+    /* }}} */
+
+    public function setRemoteSchoolDbh($schoolDbh)
+    {
+        $this->_remoteSchoolDbh = $schoolDbh;
+    }
+    /* public setFileServer(Toolkit_FileServer_FileAdapterOld $fs) {{{ */
+    /**
+     * setFileServer
+     *
+     * @param Toolkit_FileServer_FileAdapterOld $fs
+     *
+     * @access public
+     * @return void
+     */
+    public function setFileServer(Toolkit_FileServer_FileAdapterOld $fs)
+    {
+        $this->_fileServer = $fs;
+    }
+    /* }}} */
+
+    /* public setImageServer(Toolkit_FileServer_ImageAdapter $is) {{{ */
+    /**
+     * setImageServer
+     *
+     * @param Toolkit_FileServer_ImageAdapter $is
+     *
+     * @access public
+     * @return void
+     */
+    public function setImageServer(Toolkit_FileServer_ImageAdapter $is)
+    {
+        $this->_imageServer = $is;
+    }
+    /* }}} */
+
+    /* public addImage(Toolkit_FileServer_ImageAdapter $ia, $folder, $image) {{{ */
+    /**
+     * addImage
+     *
+     * @param Toolkit_FileServer_ImageAdapter $ia
+     * @param mixed $folder
+     * @param mixed $image
+     *
+     * @access public
+     * @return void
+     */
+    public function addImage(Toolkit_FileServer_ImageAdapter $ia, $folder, $image)
+    {
+        if ($image == '') {
+            throw new InvalidArgumentException('Image must not be empty');
+        }
+        if (!is_numeric($folder['id'])) {
+            throw new InvalidArgumentException('Folder array must contain id');
+        }
+        // check to see if the folder is already there
+        $sql = "
+        SELECT name_on_disk
+          FROM ckeditor_images
+         WHERE file_name = :file_name";
+        $stmt2 = $this->_localDbh->prepare($sql);
+        $stmt2->bindParam(':file_name', $image);
+        $stmt2->execute();
+        $imageName = $stmt2->fetchColumn();
+        if ($imageName) {
+            return $imageName;
+        }
+        $imageUrl
+            = FILEMANAGER_URL_ORG
+            . $folder['path']
+            . urlencode($image);
+        if ($image != '') {
+            try {
+                $img = $ia->upload($imageUrl);
+            } catch(PEAR_Exception $e) {
+                Toolkit_Common::handleError($e);
+            } catch(Toolkit_FileServer_Exception $e) {
+                return false;
+            }
+        }
+        $dimensions = $ia->getImageSize(CKIMAGE . $img['name']);
+        $sql = "
+            INSERT INTO ckeditor_images (file_name, name_on_disk, folder,
+            original_width, original_height)
+            VALUES (:fname, :nod, :folder, :width, :height)";
+
+        $stmt = $this->_localDbh->prepare($sql);
+        $stmt->bindParam(
+            ':fname',
+            $image,
+            PDO::PARAM_STR
+        );
+        $stmt->bindParam(':nod', $img['name'], PDO::PARAM_STR);
+        $stmt->bindValue(':folder', $folder['id'], PDO::PARAM_INT);
+        $stmt->bindValue(':width', $dimensions[0], PDO::PARAM_INT);
+        $stmt->bindValue(':height', $dimensions[1], PDO::PARAM_INT);
+
+        $stmt->execute();
+        return $img['name'];
+    }
+    /* }}} */
+
+    /* public addFolder($folderName, $parentId) {{{ */
+    /**
+     * addFolder
+     *
+     * @param mixed $folderName
+     * @param mixed $parentId
+     *
+     * @access public
+     * @return void
+     */
+    public function addFolder($folderName, $parentId)
+    {
+        if (!is_numeric($parentId)) {
+            throw new InvalidArgumentException('parentId must be numeric');
+        }
+        if ($folderName == '') {
+            throw new InvalidArgumentException('folderName must not be empty');
+        }
+        try {
+            // check to see if the folder is already there
+            $sql = "
+            SELECT id
+              FROM ckeditor_folders
+             WHERE name = :name";
+            $stmt2 = $this->_localDbh->prepare($sql);
+            $stmt2->bindParam(':name', $folderName);
+            $stmt2->execute();
+            $folderId = $stmt2->fetchColumn();
+            if ($folderId) {
+                return $folderId;
+            } else {
+                $sql = "
+                INSERT INTO ckeditor_folders
+                (name, parent)
+                VALUES
+                (:name, :parent)";
+                $stmt = $this->_localDbh->prepare($sql);
+                $stmt->bindParam(':name', $folderName, PDO::PARAM_STR);
+                $stmt->bindParam(':parent', $parentId, PDO::PARAM_INT);
+                $stmt->execute();
+                return $this->_localDbh->lastInsertId('ckeditor_folders_id_seq');
+            }
+        } catch (PDOException $e) {
+            Toolkit_Common::handleError($e);
+        }
+    }
+    /* }}} */
+}
diff --git a/Toolkit/Scripts/index.php b/Toolkit/Scripts/index.php
new file mode 100644 (file)
index 0000000..02b78e6
--- /dev/null
@@ -0,0 +1,26 @@
+<?php
+require_once '../../setup.phtml';
+define('IMG_URL_ORG', 'http://www.bigfore.com/images/original/');
+define('FILEMANAGER_URL_ORG', 'http://www.bigfore.com/images/ht_images/');
+define('FILE_URL_ORG', 'http://www.bigfore.com/uploads/');
+define('FILE_PATH_ORG', BASE . 'uploads/');
+
+$driverOptions = array(
+    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_BOTH,
+);
+$dbhLocal  = Toolkit_Database::getInstance();
+$dbhRemote = new PDO(
+    'pgsql: host=ds3 dbname=bigfore user=nobody',
+    null,
+    null,
+    $driverOptions
+);
+$dbhRemote->setAttribute(
+    PDO::ATTR_ERRMODE,
+    PDO::ERRMODE_EXCEPTION
+);
+$ia = new Toolkit_FileServer_ImageAdapter();
+$fa = new Toolkit_FileServer_FileAdapter();
+$is = new Toolkit_Image_Server();
+
+include './Toolbox/index.php';
index 4a2a49b..0f179d3 100644 (file)
@@ -288,7 +288,7 @@ google.maps.key   = "ABQIAAAAFEik3hTZkksVQYtPm0OFmRT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQ
 app.base_url = "http://localhost:8080/app.gaslightmedia.com/"
 app.base_secure_url = "https://localhost:8080/app.gaslightmedia.com/"
 
-database.params.host = "ds3"
+database.params.host = "devdb"
 
 email.owner                                 = "vagrant+owner@localhost"
 email.contact                               = "vagrant+contact@localhost"