diff options
author | ArthurHoaro <arthur@hoa.ro> | 2017-09-02 15:10:44 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2017-09-02 15:10:44 +0200 |
commit | 9ec0a61156192484ca90a8dc88b7c23b26129755 (patch) | |
tree | a3b6690d3a0dde8ea858f3b8c27a9454e997d005 /tests/utils/ReferenceLinkDB.php | |
parent | 96a1c79456b27892b9221707803f29585565b9dc (diff) | |
download | Shaarli-9ec0a61156192484ca90a8dc88b7c23b26129755.tar.gz Shaarli-9ec0a61156192484ca90a8dc88b7c23b26129755.tar.zst Shaarli-9ec0a61156192484ca90a8dc88b7c23b26129755.zip |
Performances: reorder links when they're written instead of read
relates to #891
Diffstat (limited to 'tests/utils/ReferenceLinkDB.php')
-rw-r--r-- | tests/utils/ReferenceLinkDB.php | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/tests/utils/ReferenceLinkDB.php b/tests/utils/ReferenceLinkDB.php index f09eebc1..e887aa78 100644 --- a/tests/utils/ReferenceLinkDB.php +++ b/tests/utils/ReferenceLinkDB.php | |||
@@ -141,6 +141,7 @@ class ReferenceLinkDB | |||
141 | */ | 141 | */ |
142 | public function write($filename) | 142 | public function write($filename) |
143 | { | 143 | { |
144 | $this->reorder(); | ||
144 | file_put_contents( | 145 | file_put_contents( |
145 | $filename, | 146 | $filename, |
146 | '<?php /* '.base64_encode(gzdeflate(serialize($this->_links))).' */ ?>' | 147 | '<?php /* '.base64_encode(gzdeflate(serialize($this->_links))).' */ ?>' |
@@ -148,6 +149,27 @@ class ReferenceLinkDB | |||
148 | } | 149 | } |
149 | 150 | ||
150 | /** | 151 | /** |
152 | * Reorder links by creation date (newest first). | ||
153 | * | ||
154 | * Also update the urls and ids mapping arrays. | ||
155 | * | ||
156 | * @param string $order ASC|DESC | ||
157 | */ | ||
158 | public function reorder($order = 'DESC') | ||
159 | { | ||
160 | // backward compatibility: ignore reorder if the the `created` field doesn't exist | ||
161 | if (! isset(array_values($this->_links)[0]['created'])) { | ||
162 | return; | ||
163 | } | ||
164 | |||
165 | $order = $order === 'ASC' ? -1 : 1; | ||
166 | // Reorder array by dates. | ||
167 | usort($this->_links, function($a, $b) use ($order) { | ||
168 | return $a['created'] < $b['created'] ? 1 * $order : -1 * $order; | ||
169 | }); | ||
170 | } | ||
171 | |||
172 | /** | ||
151 | * Returns the number of links in the reference data | 173 | * Returns the number of links in the reference data |
152 | */ | 174 | */ |
153 | public function countLinks() | 175 | public function countLinks() |
@@ -187,6 +209,7 @@ class ReferenceLinkDB | |||
187 | 209 | ||
188 | public function getLinks() | 210 | public function getLinks() |
189 | { | 211 | { |
212 | $this->reorder(); | ||
190 | return $this->_links; | 213 | return $this->_links; |
191 | } | 214 | } |
192 | 215 | ||