aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
authorVirtualTam <virtualtam+github@flibidi.net>2016-10-21 11:04:52 +0200
committerGitHub <noreply@github.com>2016-10-21 11:04:52 +0200
commit3d5e0aede31e07f060e3ca470d36374c54d5040c (patch)
tree77d08b9ea6d45946c21a55b5389cb66ba2e887cd /application
parent954dc2446caade6ccad3ffd1173ef139c1f36ad3 (diff)
parent735ed4a94e1da5874195ac47c00612043a193edf (diff)
downloadShaarli-3d5e0aede31e07f060e3ca470d36374c54d5040c.tar.gz
Shaarli-3d5e0aede31e07f060e3ca470d36374c54d5040c.tar.zst
Shaarli-3d5e0aede31e07f060e3ca470d36374c54d5040c.zip
Merge pull request #673 from virtualtam/cleanup/linkdb
LinkDB: code cleanup
Diffstat (limited to 'application')
-rw-r--r--application/LinkDB.php154
-rw-r--r--application/NetscapeBookmarkUtils.php2
-rw-r--r--application/Updater.php2
3 files changed, 79 insertions, 79 deletions
diff --git a/application/LinkDB.php b/application/LinkDB.php
index de9e73b0..c8b162b6 100644
--- a/application/LinkDB.php
+++ b/application/LinkDB.php
@@ -31,7 +31,7 @@
31class LinkDB implements Iterator, Countable, ArrayAccess 31class LinkDB implements Iterator, Countable, ArrayAccess
32{ 32{
33 // Links are stored as a PHP serialized string 33 // Links are stored as a PHP serialized string
34 private $_datastore; 34 private $datastore;
35 35
36 // Link date storage format 36 // Link date storage format
37 const LINK_DATE_FORMAT = 'Ymd_His'; 37 const LINK_DATE_FORMAT = 'Ymd_His';
@@ -45,26 +45,26 @@ class LinkDB implements Iterator, Countable, ArrayAccess
45 // List of links (associative array) 45 // List of links (associative array)
46 // - key: link date (e.g. "20110823_124546"), 46 // - key: link date (e.g. "20110823_124546"),
47 // - value: associative array (keys: title, description...) 47 // - value: associative array (keys: title, description...)
48 private $_links; 48 private $links;
49 49
50 // List of all recorded URLs (key=url, value=linkdate) 50 // List of all recorded URLs (key=url, value=linkdate)
51 // for fast reserve search (url-->linkdate) 51 // for fast reserve search (url-->linkdate)
52 private $_urls; 52 private $urls;
53 53
54 // List of linkdate keys (for the Iterator interface implementation) 54 // List of linkdate keys (for the Iterator interface implementation)
55 private $_keys; 55 private $keys;
56 56
57 // Position in the $this->_keys array (for the Iterator interface) 57 // Position in the $this->keys array (for the Iterator interface)
58 private $_position; 58 private $position;
59 59
60 // Is the user logged in? (used to filter private links) 60 // Is the user logged in? (used to filter private links)
61 private $_loggedIn; 61 private $loggedIn;
62 62
63 // Hide public links 63 // Hide public links
64 private $_hidePublicLinks; 64 private $hidePublicLinks;
65 65
66 // link redirector set in user settings. 66 // link redirector set in user settings.
67 private $_redirector; 67 private $redirector;
68 68
69 /** 69 /**
70 * Set this to `true` to urlencode link behind redirector link, `false` to leave it untouched. 70 * Set this to `true` to urlencode link behind redirector link, `false` to leave it untouched.
@@ -87,7 +87,7 @@ class LinkDB implements Iterator, Countable, ArrayAccess
87 * @param string $redirector link redirector set in user settings. 87 * @param string $redirector link redirector set in user settings.
88 * @param boolean $redirectorEncode Enable urlencode on redirected urls (default: true). 88 * @param boolean $redirectorEncode Enable urlencode on redirected urls (default: true).
89 */ 89 */
90 function __construct( 90 public function __construct(
91 $datastore, 91 $datastore,
92 $isLoggedIn, 92 $isLoggedIn,
93 $hidePublicLinks, 93 $hidePublicLinks,
@@ -95,13 +95,13 @@ class LinkDB implements Iterator, Countable, ArrayAccess
95 $redirectorEncode = true 95 $redirectorEncode = true
96 ) 96 )
97 { 97 {
98 $this->_datastore = $datastore; 98 $this->datastore = $datastore;
99 $this->_loggedIn = $isLoggedIn; 99 $this->loggedIn = $isLoggedIn;
100 $this->_hidePublicLinks = $hidePublicLinks; 100 $this->hidePublicLinks = $hidePublicLinks;
101 $this->_redirector = $redirector; 101 $this->redirector = $redirector;
102 $this->redirectorEncode = $redirectorEncode === true; 102 $this->redirectorEncode = $redirectorEncode === true;
103 $this->_checkDB(); 103 $this->check();
104 $this->_readDB(); 104 $this->read();
105 } 105 }
106 106
107 /** 107 /**
@@ -109,7 +109,7 @@ class LinkDB implements Iterator, Countable, ArrayAccess
109 */ 109 */
110 public function count() 110 public function count()
111 { 111 {
112 return count($this->_links); 112 return count($this->links);
113 } 113 }
114 114
115 /** 115 /**
@@ -118,7 +118,7 @@ class LinkDB implements Iterator, Countable, ArrayAccess
118 public function offsetSet($offset, $value) 118 public function offsetSet($offset, $value)
119 { 119 {
120 // TODO: use exceptions instead of "die" 120 // TODO: use exceptions instead of "die"
121 if (!$this->_loggedIn) { 121 if (!$this->loggedIn) {
122 die('You are not authorized to add a link.'); 122 die('You are not authorized to add a link.');
123 } 123 }
124 if (empty($value['linkdate']) || empty($value['url'])) { 124 if (empty($value['linkdate']) || empty($value['url'])) {
@@ -127,8 +127,8 @@ class LinkDB implements Iterator, Countable, ArrayAccess
127 if (empty($offset)) { 127 if (empty($offset)) {
128 die('You must specify a key.'); 128 die('You must specify a key.');
129 } 129 }
130 $this->_links[$offset] = $value; 130 $this->links[$offset] = $value;
131 $this->_urls[$value['url']]=$offset; 131 $this->urls[$value['url']]=$offset;
132 } 132 }
133 133
134 /** 134 /**
@@ -136,7 +136,7 @@ class LinkDB implements Iterator, Countable, ArrayAccess
136 */ 136 */
137 public function offsetExists($offset) 137 public function offsetExists($offset)
138 { 138 {
139 return array_key_exists($offset, $this->_links); 139 return array_key_exists($offset, $this->links);
140 } 140 }
141 141
142 /** 142 /**
@@ -144,13 +144,13 @@ class LinkDB implements Iterator, Countable, ArrayAccess
144 */ 144 */
145 public function offsetUnset($offset) 145 public function offsetUnset($offset)
146 { 146 {
147 if (!$this->_loggedIn) { 147 if (!$this->loggedIn) {
148 // TODO: raise an exception 148 // TODO: raise an exception
149 die('You are not authorized to delete a link.'); 149 die('You are not authorized to delete a link.');
150 } 150 }
151 $url = $this->_links[$offset]['url']; 151 $url = $this->links[$offset]['url'];
152 unset($this->_urls[$url]); 152 unset($this->urls[$url]);
153 unset($this->_links[$offset]); 153 unset($this->links[$offset]);
154 } 154 }
155 155
156 /** 156 /**
@@ -158,31 +158,31 @@ class LinkDB implements Iterator, Countable, ArrayAccess
158 */ 158 */
159 public function offsetGet($offset) 159 public function offsetGet($offset)
160 { 160 {
161 return isset($this->_links[$offset]) ? $this->_links[$offset] : null; 161 return isset($this->links[$offset]) ? $this->links[$offset] : null;
162 } 162 }
163 163
164 /** 164 /**
165 * Iterator - Returns the current element 165 * Iterator - Returns the current element
166 */ 166 */
167 function current() 167 public function current()
168 { 168 {
169 return $this->_links[$this->_keys[$this->_position]]; 169 return $this->links[$this->keys[$this->position]];
170 } 170 }
171 171
172 /** 172 /**
173 * Iterator - Returns the key of the current element 173 * Iterator - Returns the key of the current element
174 */ 174 */
175 function key() 175 public function key()
176 { 176 {
177 return $this->_keys[$this->_position]; 177 return $this->keys[$this->position];
178 } 178 }
179 179
180 /** 180 /**
181 * Iterator - Moves forward to next element 181 * Iterator - Moves forward to next element
182 */ 182 */
183 function next() 183 public function next()
184 { 184 {
185 ++$this->_position; 185 ++$this->position;
186 } 186 }
187 187
188 /** 188 /**
@@ -190,19 +190,19 @@ class LinkDB implements Iterator, Countable, ArrayAccess
190 * 190 *
191 * Entries are sorted by date (latest first) 191 * Entries are sorted by date (latest first)
192 */ 192 */
193 function rewind() 193 public function rewind()
194 { 194 {
195 $this->_keys = array_keys($this->_links); 195 $this->keys = array_keys($this->links);
196 rsort($this->_keys); 196 rsort($this->keys);
197 $this->_position = 0; 197 $this->position = 0;
198 } 198 }
199 199
200 /** 200 /**
201 * Iterator - Checks if current position is valid 201 * Iterator - Checks if current position is valid
202 */ 202 */
203 function valid() 203 public function valid()
204 { 204 {
205 return isset($this->_keys[$this->_position]); 205 return isset($this->keys[$this->position]);
206 } 206 }
207 207
208 /** 208 /**
@@ -210,14 +210,14 @@ class LinkDB implements Iterator, Countable, ArrayAccess
210 * 210 *
211 * If no DB file is found, creates a dummy DB. 211 * If no DB file is found, creates a dummy DB.
212 */ 212 */
213 private function _checkDB() 213 private function check()
214 { 214 {
215 if (file_exists($this->_datastore)) { 215 if (file_exists($this->datastore)) {
216 return; 216 return;
217 } 217 }
218 218
219 // Create a dummy database for example 219 // Create a dummy database for example
220 $this->_links = array(); 220 $this->links = array();
221 $link = array( 221 $link = array(
222 'title'=>' Shaarli: the personal, minimalist, super-fast, no-database delicious clone', 222 'title'=>' Shaarli: the personal, minimalist, super-fast, no-database delicious clone',
223 'url'=>'https://github.com/shaarli/Shaarli/wiki', 223 'url'=>'https://github.com/shaarli/Shaarli/wiki',
@@ -230,7 +230,7 @@ You use the community supported version of the original Shaarli project, by Seba
230 'linkdate'=> date('Ymd_His'), 230 'linkdate'=> date('Ymd_His'),
231 'tags'=>'opensource software' 231 'tags'=>'opensource software'
232 ); 232 );
233 $this->_links[$link['linkdate']] = $link; 233 $this->links[$link['linkdate']] = $link;
234 234
235 $link = array( 235 $link = array(
236 'title'=>'My secret stuff... - Pastebin.com', 236 'title'=>'My secret stuff... - Pastebin.com',
@@ -240,64 +240,64 @@ You use the community supported version of the original Shaarli project, by Seba
240 'linkdate'=> date('Ymd_His', strtotime('-1 minute')), 240 'linkdate'=> date('Ymd_His', strtotime('-1 minute')),
241 'tags'=>'secretstuff' 241 'tags'=>'secretstuff'
242 ); 242 );
243 $this->_links[$link['linkdate']] = $link; 243 $this->links[$link['linkdate']] = $link;
244 244
245 // Write database to disk 245 // Write database to disk
246 $this->writeDB(); 246 $this->write();
247 } 247 }
248 248
249 /** 249 /**
250 * Reads database from disk to memory 250 * Reads database from disk to memory
251 */ 251 */
252 private function _readDB() 252 private function read()
253 { 253 {
254 254
255 // Public links are hidden and user not logged in => nothing to show 255 // Public links are hidden and user not logged in => nothing to show
256 if ($this->_hidePublicLinks && !$this->_loggedIn) { 256 if ($this->hidePublicLinks && !$this->loggedIn) {
257 $this->_links = array(); 257 $this->links = array();
258 return; 258 return;
259 } 259 }
260 260
261 // Read data 261 // Read data
262 // Note that gzinflate is faster than gzuncompress. 262 // Note that gzinflate is faster than gzuncompress.
263 // See: http://www.php.net/manual/en/function.gzdeflate.php#96439 263 // See: http://www.php.net/manual/en/function.gzdeflate.php#96439
264 $this->_links = array(); 264 $this->links = array();
265 265
266 if (file_exists($this->_datastore)) { 266 if (file_exists($this->datastore)) {
267 $this->_links = unserialize(gzinflate(base64_decode( 267 $this->links = unserialize(gzinflate(base64_decode(
268 substr(file_get_contents($this->_datastore), 268 substr(file_get_contents($this->datastore),
269 strlen(self::$phpPrefix), -strlen(self::$phpSuffix))))); 269 strlen(self::$phpPrefix), -strlen(self::$phpSuffix)))));
270 } 270 }
271 271
272 // If user is not logged in, filter private links. 272 // If user is not logged in, filter private links.
273 if (!$this->_loggedIn) { 273 if (!$this->loggedIn) {
274 $toremove = array(); 274 $toremove = array();
275 foreach ($this->_links as $link) { 275 foreach ($this->links as $link) {
276 if ($link['private'] != 0) { 276 if ($link['private'] != 0) {
277 $toremove[] = $link['linkdate']; 277 $toremove[] = $link['linkdate'];
278 } 278 }
279 } 279 }
280 foreach ($toremove as $linkdate) { 280 foreach ($toremove as $linkdate) {
281 unset($this->_links[$linkdate]); 281 unset($this->links[$linkdate]);
282 } 282 }
283 } 283 }
284 284
285 $this->_urls = array(); 285 $this->urls = array();
286 foreach ($this->_links as &$link) { 286 foreach ($this->links as &$link) {
287 // Keep the list of the mapping URLs-->linkdate up-to-date. 287 // Keep the list of the mapping URLs-->linkdate up-to-date.
288 $this->_urls[$link['url']] = $link['linkdate']; 288 $this->urls[$link['url']] = $link['linkdate'];
289 289
290 // Sanitize data fields. 290 // Sanitize data fields.
291 sanitizeLink($link); 291 sanitizeLink($link);
292 292
293 // Remove private tags if the user is not logged in. 293 // Remove private tags if the user is not logged in.
294 if (! $this->_loggedIn) { 294 if (! $this->loggedIn) {
295 $link['tags'] = preg_replace('/(^|\s+)\.[^($|\s)]+\s*/', ' ', $link['tags']); 295 $link['tags'] = preg_replace('/(^|\s+)\.[^($|\s)]+\s*/', ' ', $link['tags']);
296 } 296 }
297 297
298 // Do not use the redirector for internal links (Shaarli note URL starting with a '?'). 298 // Do not use the redirector for internal links (Shaarli note URL starting with a '?').
299 if (!empty($this->_redirector) && !startsWith($link['url'], '?')) { 299 if (!empty($this->redirector) && !startsWith($link['url'], '?')) {
300 $link['real_url'] = $this->_redirector; 300 $link['real_url'] = $this->redirector;
301 if ($this->redirectorEncode) { 301 if ($this->redirectorEncode) {
302 $link['real_url'] .= urlencode(unescape($link['url'])); 302 $link['real_url'] .= urlencode(unescape($link['url']));
303 } else { 303 } else {
@@ -315,19 +315,19 @@ You use the community supported version of the original Shaarli project, by Seba
315 * 315 *
316 * @throws IOException the datastore is not writable 316 * @throws IOException the datastore is not writable
317 */ 317 */
318 private function writeDB() 318 private function write()
319 { 319 {
320 if (is_file($this->_datastore) && !is_writeable($this->_datastore)) { 320 if (is_file($this->datastore) && !is_writeable($this->datastore)) {
321 // The datastore exists but is not writeable 321 // The datastore exists but is not writeable
322 throw new IOException($this->_datastore); 322 throw new IOException($this->datastore);
323 } else if (!is_file($this->_datastore) && !is_writeable(dirname($this->_datastore))) { 323 } else if (!is_file($this->datastore) && !is_writeable(dirname($this->datastore))) {
324 // The datastore does not exist and its parent directory is not writeable 324 // The datastore does not exist and its parent directory is not writeable
325 throw new IOException(dirname($this->_datastore)); 325 throw new IOException(dirname($this->datastore));
326 } 326 }
327 327
328 file_put_contents( 328 file_put_contents(
329 $this->_datastore, 329 $this->datastore,
330 self::$phpPrefix.base64_encode(gzdeflate(serialize($this->_links))).self::$phpSuffix 330 self::$phpPrefix.base64_encode(gzdeflate(serialize($this->links))).self::$phpSuffix
331 ); 331 );
332 332
333 } 333 }
@@ -337,14 +337,14 @@ You use the community supported version of the original Shaarli project, by Seba
337 * 337 *
338 * @param string $pageCacheDir page cache directory 338 * @param string $pageCacheDir page cache directory
339 */ 339 */
340 public function savedb($pageCacheDir) 340 public function save($pageCacheDir)
341 { 341 {
342 if (!$this->_loggedIn) { 342 if (!$this->loggedIn) {
343 // TODO: raise an Exception instead 343 // TODO: raise an Exception instead
344 die('You are not authorized to change the database.'); 344 die('You are not authorized to change the database.');
345 } 345 }
346 346
347 $this->writeDB(); 347 $this->write();
348 348
349 invalidateCaches($pageCacheDir); 349 invalidateCaches($pageCacheDir);
350 } 350 }
@@ -358,8 +358,8 @@ You use the community supported version of the original Shaarli project, by Seba
358 */ 358 */
359 public function getLinkFromUrl($url) 359 public function getLinkFromUrl($url)
360 { 360 {
361 if (isset($this->_urls[$url])) { 361 if (isset($this->urls[$url])) {
362 return $this->_links[$this->_urls[$url]]; 362 return $this->links[$this->urls[$url]];
363 } 363 }
364 return false; 364 return false;
365 } 365 }
@@ -376,7 +376,7 @@ You use the community supported version of the original Shaarli project, by Seba
376 public function filterHash($request) 376 public function filterHash($request)
377 { 377 {
378 $request = substr($request, 0, 6); 378 $request = substr($request, 0, 6);
379 $linkFilter = new LinkFilter($this->_links); 379 $linkFilter = new LinkFilter($this->links);
380 return $linkFilter->filter(LinkFilter::$FILTER_HASH, $request); 380 return $linkFilter->filter(LinkFilter::$FILTER_HASH, $request);
381 } 381 }
382 382
@@ -388,7 +388,7 @@ You use the community supported version of the original Shaarli project, by Seba
388 * @return array list of shaare found. 388 * @return array list of shaare found.
389 */ 389 */
390 public function filterDay($request) { 390 public function filterDay($request) {
391 $linkFilter = new LinkFilter($this->_links); 391 $linkFilter = new LinkFilter($this->links);
392 return $linkFilter->filter(LinkFilter::$FILTER_DAY, $request); 392 return $linkFilter->filter(LinkFilter::$FILTER_DAY, $request);
393 } 393 }
394 394
@@ -430,7 +430,7 @@ You use the community supported version of the original Shaarli project, by Seba
430 $request = ''; 430 $request = '';
431 } 431 }
432 432
433 $linkFilter = new LinkFilter($this->_links); 433 $linkFilter = new LinkFilter($this->links);
434 return $linkFilter->filter($type, $request, $casesensitive, $privateonly); 434 return $linkFilter->filter($type, $request, $casesensitive, $privateonly);
435 } 435 }
436 436
@@ -442,7 +442,7 @@ You use the community supported version of the original Shaarli project, by Seba
442 { 442 {
443 $tags = array(); 443 $tags = array();
444 $caseMapping = array(); 444 $caseMapping = array();
445 foreach ($this->_links as $link) { 445 foreach ($this->links as $link) {
446 foreach (preg_split('/\s+/', $link['tags'], 0, PREG_SPLIT_NO_EMPTY) as $tag) { 446 foreach (preg_split('/\s+/', $link['tags'], 0, PREG_SPLIT_NO_EMPTY) as $tag) {
447 if (empty($tag)) { 447 if (empty($tag)) {
448 continue; 448 continue;
@@ -467,7 +467,7 @@ You use the community supported version of the original Shaarli project, by Seba
467 public function days() 467 public function days()
468 { 468 {
469 $linkDays = array(); 469 $linkDays = array();
470 foreach (array_keys($this->_links) as $day) { 470 foreach (array_keys($this->links) as $day) {
471 $linkDays[substr($day, 0, 8)] = 0; 471 $linkDays[substr($day, 0, 8)] = 0;
472 } 472 }
473 $linkDays = array_keys($linkDays); 473 $linkDays = array_keys($linkDays);
diff --git a/application/NetscapeBookmarkUtils.php b/application/NetscapeBookmarkUtils.php
index d6840d37..dd21f05b 100644
--- a/application/NetscapeBookmarkUtils.php
+++ b/application/NetscapeBookmarkUtils.php
@@ -183,7 +183,7 @@ class NetscapeBookmarkUtils
183 $importCount++; 183 $importCount++;
184 } 184 }
185 185
186 $linkDb->savedb($pagecache); 186 $linkDb->save($pagecache);
187 return self::importStatus( 187 return self::importStatus(
188 $filename, 188 $filename,
189 $filesize, 189 $filesize,
diff --git a/application/Updater.php b/application/Updater.php
index 90913235..36eddd4f 100644
--- a/application/Updater.php
+++ b/application/Updater.php
@@ -143,7 +143,7 @@ class Updater
143 $link['tags'] = implode(' ', array_unique(LinkFilter::tagsStrToArray($link['tags'], true))); 143 $link['tags'] = implode(' ', array_unique(LinkFilter::tagsStrToArray($link['tags'], true)));
144 $this->linkDB[$link['linkdate']] = $link; 144 $this->linkDB[$link['linkdate']] = $link;
145 } 145 }
146 $this->linkDB->savedb($this->conf->get('resource.page_cache')); 146 $this->linkDB->save($this->conf->get('resource.page_cache'));
147 return true; 147 return true;
148 } 148 }
149 149