aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/utils
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2017-10-28 12:44:44 +0200
committerGitHub <noreply@github.com>2017-10-28 12:44:44 +0200
commit0926d263902c184bd4f4c2036cb8ee90f81c5060 (patch)
treef326e180aee99881725e5e2abf5a4c13e323e964 /tests/utils
parent88d38cb290aad669ad1406e2362d85c81e46d4f6 (diff)
parent9ec0a61156192484ca90a8dc88b7c23b26129755 (diff)
downloadShaarli-0926d263902c184bd4f4c2036cb8ee90f81c5060.tar.gz
Shaarli-0926d263902c184bd4f4c2036cb8ee90f81c5060.tar.zst
Shaarli-0926d263902c184bd4f4c2036cb8ee90f81c5060.zip
Merge pull request #962 from ArthurHoaro/feature/perfs2
Performances: reorder links when they're written instead of read
Diffstat (limited to 'tests/utils')
-rw-r--r--tests/utils/ReferenceLinkDB.php23
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