Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanediondev committed Aug 21, 2015
1 parent 8a06a3d commit 80697f1
Showing 1 changed file with 55 additions and 15 deletions.
70 changes: 55 additions & 15 deletions application/controllers/Settings.php
Expand Up @@ -168,6 +168,7 @@ public function update($release = false) {
if($release && !file_exists('update/'.$release.'.txt')) {
set_time_limit(120);

//set database type according to configuration
if($this->db->dbdriver == 'mysqli') {
$database_type = 'mysql';
}
Expand All @@ -178,6 +179,10 @@ public function update($release = false) {
$database_type = 'sqlite';
}

$versions_feed = array();
$versions_update = array();

//get release archive and save local
$local_file = 'update/'.$release.'.zip';
file_put_contents($local_file, file_get_contents('https://github.com/readerself/readerself/archive/'.$release.'.zip'));

Expand All @@ -189,45 +194,77 @@ public function update($release = false) {
'application/database/readerself.sqlite',
);

//extract archive
$zip = new ZipArchive;
if($zip->open($local_file) === TRUE) {
$total_files = $zip->numFiles;
//loop and copy all files
for($i=0;$i<$total_files;$i++) {
$file_source = $zip->getNameIndex($i);
$file_destination = str_replace('readerself-'.$release.'/', '', $file_source);
$dirname = dirname($file_destination);
//create directory if missing
if(!is_dir($dirname)) {
mkdir($dirname);
}
//copy if not excluded
if(!in_array($file_destination, $exclude_files)) {
copy('zip://'.$local_file.'#'.$file_source, $file_destination);
}
}
$zip->close();

$reverse_releases = array();
//populate versions array from feed
foreach($data['entries']->entry as $entry) {
$reverse_releases[] = $entry->title;
$versions_feed[] = $entry->title;
}
$reverse_releases = array_reverse($reverse_releases);

foreach($reverse_releases as $release_history) {
$filename = 'application/database/update-'.$database_type.'-'.$release_history.'.sql';
if(file_exists($filename) && !file_exists('update/'.$release_history.'.txt')) {
$queries = explode(';', trim(file_get_contents($filename)));
foreach($queries as $query) {
if($query != '') {
$this->db->query($query);
}

//remove local archive
unlink($local_file);
}

$dir = 'application/database';
if(is_dir($dir)) {
if($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
$test_start_with = 'update-'.$database_type.'-';
if(substr($file, -4) == '.sql' && substr($file, 0, strlen($test_start_with)) == $test_start_with) {
$versions_update[] = substr($file, strlen($test_start_with), -4);
}
}
closedir($dh);
}
}
usort($versions_update, 'versionSort');

$fp = fopen('update/'.$release_history.'.txt', 'w');
fclose($fp);
//update database from exiting update scripts
$versions_update = array_reverse($versions_update);
foreach($versions_update as $version) {
//execute script if exists
$filename = 'application/database/update-'.$database_type.'-'.$version.'.sql';
if(file_exists($filename) && !file_exists('update/'.$version.'.txt')) {
$queries = explode(';', trim(file_get_contents($filename)));
foreach($queries as $query) {
if($query != '') {
$this->db->query($query);
}
}
}

unlink($local_file);
//lock version
$fp = fopen('update/'.$version.'.txt', 'w');
fclose($fp);
}

$versions_feed = array_reverse($versions_feed);
foreach($versions_feed as $version) {
if(!file_exists('update/'.$version.'.txt')) {
//lock version
$fp = fopen('update/'.$version.'.txt', 'w');
fclose($fp);
}
}

redirect(base_url().'settings/update');
}

Expand All @@ -242,3 +279,6 @@ public function is_example() {
return TRUE;
}
}
function versionSort($a, $b) {
return -1 * version_compare($a, $b);
}

0 comments on commit 80697f1

Please sign in to comment.