aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2017-05-07 16:50:20 +0200
committerArthurHoaro <arthur@hoa.ro>2017-05-07 17:11:22 +0200
commit813849e5216cb87121e0f778a734575be6a36052 (patch)
tree45605ad43e3f751eba51d19f8c6955c3824e8d32 /application
parenta4af59f47103a3f9c903eeddb1e30ab9cb7344f0 (diff)
downloadShaarli-813849e5216cb87121e0f778a734575be6a36052.tar.gz
Shaarli-813849e5216cb87121e0f778a734575be6a36052.tar.zst
Shaarli-813849e5216cb87121e0f778a734575be6a36052.zip
Add history entries for API endpoint
CHANGED: datetime is now store as an object in history store file
Diffstat (limited to 'application')
-rw-r--r--application/FileUtils.php2
-rw-r--r--application/History.php4
-rw-r--r--application/api/ApiMiddleware.php5
-rw-r--r--application/api/controllers/ApiController.php9
-rw-r--r--application/api/controllers/History.php2
-rw-r--r--application/api/controllers/Links.php5
6 files changed, 19 insertions, 8 deletions
diff --git a/application/FileUtils.php b/application/FileUtils.php
index b8ad8970..a167f642 100644
--- a/application/FileUtils.php
+++ b/application/FileUtils.php
@@ -26,7 +26,7 @@ class FileUtils
26 * The file will be created if it doesn't exist. 26 * The file will be created if it doesn't exist.
27 * 27 *
28 * @param string $file File path. 28 * @param string $file File path.
29 * @param string $content Content to write. 29 * @param mixed $content Content to write.
30 * 30 *
31 * @return int|bool Number of bytes written or false if it fails. 31 * @return int|bool Number of bytes written or false if it fails.
32 * 32 *
diff --git a/application/History.php b/application/History.php
index f93b0356..116b9264 100644
--- a/application/History.php
+++ b/application/History.php
@@ -135,7 +135,7 @@ class History
135 135
136 $item = [ 136 $item = [
137 'event' => $status, 137 'event' => $status,
138 'datetime' => (new DateTime())->format(DateTime::ATOM), 138 'datetime' => new DateTime(),
139 'id' => $id !== null ? $id : '', 139 'id' => $id !== null ? $id : '',
140 ]; 140 ];
141 $this->history = array_merge([$item], $this->history); 141 $this->history = array_merge([$item], $this->history);
@@ -177,7 +177,7 @@ class History
177 { 177 {
178 $comparaison = new DateTime('-'. $this->retentionTime . ' seconds'); 178 $comparaison = new DateTime('-'. $this->retentionTime . ' seconds');
179 foreach ($this->history as $key => $value) { 179 foreach ($this->history as $key => $value) {
180 if (DateTime::createFromFormat(DateTime::ATOM, $value['datetime']) < $comparaison) { 180 if ($value['datetime'] < $comparaison) {
181 unset($this->history[$key]); 181 unset($this->history[$key]);
182 } 182 }
183 } 183 }
diff --git a/application/api/ApiMiddleware.php b/application/api/ApiMiddleware.php
index 4120f7a9..ff209393 100644
--- a/application/api/ApiMiddleware.php
+++ b/application/api/ApiMiddleware.php
@@ -4,6 +4,7 @@ namespace Shaarli\Api;
4use Shaarli\Api\Exceptions\ApiException; 4use Shaarli\Api\Exceptions\ApiException;
5use Shaarli\Api\Exceptions\ApiAuthorizationException; 5use Shaarli\Api\Exceptions\ApiAuthorizationException;
6 6
7use Shaarli\Config\ConfigManager;
7use Slim\Container; 8use Slim\Container;
8use Slim\Http\Request; 9use Slim\Http\Request;
9use Slim\Http\Response; 10use Slim\Http\Response;
@@ -31,7 +32,7 @@ class ApiMiddleware
31 protected $container; 32 protected $container;
32 33
33 /** 34 /**
34 * @var \ConfigManager instance. 35 * @var ConfigManager instance.
35 */ 36 */
36 protected $conf; 37 protected $conf;
37 38
@@ -121,7 +122,7 @@ class ApiMiddleware
121 * 122 *
122 * FIXME! LinkDB could use a refactoring to avoid this trick. 123 * FIXME! LinkDB could use a refactoring to avoid this trick.
123 * 124 *
124 * @param \ConfigManager $conf instance. 125 * @param ConfigManager $conf instance.
125 */ 126 */
126 protected function setLinkDb($conf) 127 protected function setLinkDb($conf)
127 { 128 {
diff --git a/application/api/controllers/ApiController.php b/application/api/controllers/ApiController.php
index f35b923a..3be85b98 100644
--- a/application/api/controllers/ApiController.php
+++ b/application/api/controllers/ApiController.php
@@ -2,6 +2,7 @@
2 2
3namespace Shaarli\Api\Controllers; 3namespace Shaarli\Api\Controllers;
4 4
5use Shaarli\Config\ConfigManager;
5use \Slim\Container; 6use \Slim\Container;
6 7
7/** 8/**
@@ -19,7 +20,7 @@ abstract class ApiController
19 protected $ci; 20 protected $ci;
20 21
21 /** 22 /**
22 * @var \ConfigManager 23 * @var ConfigManager
23 */ 24 */
24 protected $conf; 25 protected $conf;
25 26
@@ -29,6 +30,11 @@ abstract class ApiController
29 protected $linkDb; 30 protected $linkDb;
30 31
31 /** 32 /**
33 * @var \History
34 */
35 protected $history;
36
37 /**
32 * @var int|null JSON style option. 38 * @var int|null JSON style option.
33 */ 39 */
34 protected $jsonStyle; 40 protected $jsonStyle;
@@ -45,6 +51,7 @@ abstract class ApiController
45 $this->ci = $ci; 51 $this->ci = $ci;
46 $this->conf = $ci->get('conf'); 52 $this->conf = $ci->get('conf');
47 $this->linkDb = $ci->get('db'); 53 $this->linkDb = $ci->get('db');
54 $this->history = $ci->get('history');
48 if ($this->conf->get('dev.debug', false)) { 55 if ($this->conf->get('dev.debug', false)) {
49 $this->jsonStyle = JSON_PRETTY_PRINT; 56 $this->jsonStyle = JSON_PRETTY_PRINT;
50 } else { 57 } else {
diff --git a/application/api/controllers/History.php b/application/api/controllers/History.php
index c4ff3e5d..da034eb2 100644
--- a/application/api/controllers/History.php
+++ b/application/api/controllers/History.php
@@ -28,7 +28,7 @@ class History extends ApiController
28 */ 28 */
29 public function getHistory($request, $response) 29 public function getHistory($request, $response)
30 { 30 {
31 $history = (new \History($this->conf->get('resource.history')))->getHistory(); 31 $history = $this->history->getHistory();
32 $history = array_reverse($history); 32 $history = array_reverse($history);
33 33
34 // Return history operations from the {offset}th, starting from {since}. 34 // Return history operations from the {offset}th, starting from {since}.
diff --git a/application/api/controllers/Links.php b/application/api/controllers/Links.php
index a40e974d..eb78dd26 100644
--- a/application/api/controllers/Links.php
+++ b/application/api/controllers/Links.php
@@ -141,6 +141,7 @@ class Links extends ApiController
141 141
142 $this->linkDb[$link['id']] = $link; 142 $this->linkDb[$link['id']] = $link;
143 $this->linkDb->save($this->conf->get('resource.page_cache')); 143 $this->linkDb->save($this->conf->get('resource.page_cache'));
144 $this->history->addLink($link);
144 $out = ApiUtils::formatLink($link, index_url($this->ci['environment'])); 145 $out = ApiUtils::formatLink($link, index_url($this->ci['environment']));
145 $redirect = $this->ci->router->relativePathFor('getLink', ['id' => $link['id']]); 146 $redirect = $this->ci->router->relativePathFor('getLink', ['id' => $link['id']]);
146 return $response->withAddedHeader('Location', $redirect) 147 return $response->withAddedHeader('Location', $redirect)
@@ -184,6 +185,7 @@ class Links extends ApiController
184 $responseLink = ApiUtils::updateLink($responseLink, $requestLink); 185 $responseLink = ApiUtils::updateLink($responseLink, $requestLink);
185 $this->linkDb[$responseLink['id']] = $responseLink; 186 $this->linkDb[$responseLink['id']] = $responseLink;
186 $this->linkDb->save($this->conf->get('resource.page_cache')); 187 $this->linkDb->save($this->conf->get('resource.page_cache'));
188 $this->history->updateLink($responseLink);
187 189
188 $out = ApiUtils::formatLink($responseLink, $index); 190 $out = ApiUtils::formatLink($responseLink, $index);
189 return $response->withJson($out, 200, $this->jsonStyle); 191 return $response->withJson($out, 200, $this->jsonStyle);
@@ -205,9 +207,10 @@ class Links extends ApiController
205 if (! isset($this->linkDb[$args['id']])) { 207 if (! isset($this->linkDb[$args['id']])) {
206 throw new ApiLinkNotFoundException(); 208 throw new ApiLinkNotFoundException();
207 } 209 }
208 210 $link = $this->linkDb[$args['id']];
209 unset($this->linkDb[(int) $args['id']]); 211 unset($this->linkDb[(int) $args['id']]);
210 $this->linkDb->save($this->conf->get('resource.page_cache')); 212 $this->linkDb->save($this->conf->get('resource.page_cache'));
213 $this->history->deleteLink($link);
211 214
212 return $response->withStatus(204); 215 return $response->withStatus(204);
213 } 216 }