Ver Fonte

added journal mode options to org

CauseFX há 4 anos atrás
pai
commit
705e6b0275
4 ficheiros alterados com 101 adições e 10 exclusões
  1. 54 8
      api/classes/organizr.class.php
  2. 0 1
      api/config/default.php
  3. 21 0
      api/v2/routes/database.php
  4. 26 1
      js/functions.js

+ 54 - 8
api/classes/organizr.class.php

@@ -156,13 +156,6 @@ class Organizr
 					'driver' => 'sqlite3',
 					'database' => $this->config['dbLocation'] . $this->config['dbName'],
 				];
-				/*
-				if ($this->config['enableWAL']) {
-					$connect = array_merge([
-						'onConnect' => ['PRAGMA journal_mode = \'WAL\';']
-					], $connect);
-				}
-				*/
 				$this->db = new Connection($connect);
 			} catch (Dibi\Exception $e) {
 				//$this->debug($e->getMessage());
@@ -1970,7 +1963,10 @@ class Organizr
 				),
 			),
 			'Database' => [
-				$this->settingsOption('enable', 'enableWAL', ['label' => 'Enable WAL Mode', 'help' => 'Experimental!  Use at own risk']),
+				$this->settingsOption('html', '', ['label' => 'Journal Mode Status', 'html' => '<script>getJournalMode();</script><h4 class="journal-mode font-bold text-uppercase"><i class="fa fa-spin fa-circle-o-notch"></i></h4>']),
+				$this->settingsOption('blank'),
+				$this->settingsOption('button', '', ['label' => 'Set DELETE Mode (Default)', 'icon' => 'icon-notebook', 'text' => 'Set', 'attr' => 'onclick="setJournalMode(\'DELETE\')"']),
+				$this->settingsOption('button', '', ['label' => 'Set WAL Mode', 'icon' => 'icon-notebook', 'text' => 'Set', 'attr' => 'onclick="setJournalMode(\'WAL\')"']),
 			],
 			'Github' => array(
 				array(
@@ -6922,6 +6918,56 @@ class Organizr
 		return $goodIcons;
 	}
 	
+	public function getJournalMode()
+	{
+		$response = [
+			array(
+				'function' => 'fetch',
+				'query' => 'PRAGMA journal_mode',
+			),
+		];
+		$query = $this->processQueries($response);
+		if ($query) {
+			if ($query['journal_mode']) {
+				$this->setResponse(200, null, $query);
+			} else {
+				$this->setResponse(500, 'Error getting Journal Mode');
+			}
+		} else {
+			$this->setResponse(404, 'Journal Mode not found');
+		}
+		return $query;
+	}
+	
+	public function setJournalMode($option = 'WAL')
+	{
+		$option = strtoupper($option);
+		switch ($option) {
+			case 'WAL':
+			case 'DELETE':
+				break;
+			default:
+				return false;
+		}
+		$response = [
+			array(
+				'function' => 'fetch',
+				'query' => 'PRAGMA journal_mode = \'' . $option . '\';',
+			),
+		];
+		$query = $this->processQueries($response);
+		if ($query) {
+			if ($query['journal_mode']) {
+				$this->setResponse(200, 'Journal Mode updated to: ' . $option, $query);
+			} else {
+				$this->setResponse(500, 'Error getting Journal Mode');
+			}
+		} else {
+			$this->setResponse(404, 'Journal Mode not found');
+		}
+		return $query;
+	}
+	
 	protected function processQueries(array $request, $migration = false)
 	{
 		$results = array();

+ 0 - 1
api/config/default.php

@@ -1,7 +1,6 @@
 <?php
 return [
 	'branch' => 'v2-master',
-	'enableWAL' => false,
 	'authType' => 'internal',
 	'authBackend' => '',
 	'authBackendHost' => '',

+ 21 - 0
api/v2/routes/database.php

@@ -0,0 +1,21 @@
+<?php
+$app->get('/database/journal', function ($request, $response, $args) {
+	$Organizr = ($request->getAttribute('Organizr')) ?? new Organizr();
+	if ($Organizr->qualifyRequest(1, true)) {
+		$Organizr->getJournalMode();
+	}
+	$response->getBody()->write(jsonE($GLOBALS['api']));
+	return $response
+		->withHeader('Content-Type', 'application/json;charset=UTF-8')
+		->withStatus($GLOBALS['responseCode']);
+});
+$app->put('/database/journal/{option}', function ($request, $response, $args) {
+	$Organizr = ($request->getAttribute('Organizr')) ?? new Organizr();
+	if ($Organizr->qualifyRequest(1, true)) {
+		$Organizr->setJournalMode($args['option']);
+	}
+	$response->getBody()->write(jsonE($GLOBALS['api']));
+	return $response
+		->withHeader('Content-Type', 'application/json;charset=UTF-8')
+		->withStatus($GLOBALS['responseCode']);
+});

+ 26 - 1
js/functions.js

@@ -10872,7 +10872,32 @@ function shortcut(selectors = ''){
 		}, timeout);
 	});
 }
-
+function getJournalMode(){
+	organizrAPI2('GET','api/v2/database/journal').success(function(data) {
+		try {
+			let response = data.response;
+			$('.journal-mode').html(response.data.journal_mode);
+		}catch(e) {
+			organizrCatchError(e,data);
+		}
+	}).fail(function(xhr) {
+		OrganizrApiError(xhr);
+	});
+}
+function setJournalMode(mode){
+	messageSingle('Setting New Journal Mode','',activeInfo.settings.notifications.position,"#FFF","info","1500");
+	organizrAPI2('PUT','api/v2/database/journal/' + mode, {}).success(function(data) {
+		try {
+			getJournalMode();
+			let response = data.response;
+			message('Set New Journal Mode',response.data.journal_mode,activeInfo.settings.notifications.position,"#FFF","success","5000");
+		}catch(e) {
+			organizrCatchError(e,data);
+		}
+	}).fail(function(xhr) {
+		OrganizrApiError(xhr);
+	});
+}
 function launch(){
 	console.info('https://docs.organizr.app/help/faq/migration-guide#version-2-0-greater-than-version-2-1');
 	organizrConsole('API V2 API','If you see a 404 Error for api/v2/launch below this line, you have not setup the new location block... See URL above this line', 'error');