aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNicolas LÅ“uillet <nicolas.loeuillet@gmail.com>2014-02-28 21:49:38 +0100
committerNicolas LÅ“uillet <nicolas.loeuillet@gmail.com>2014-02-28 21:49:38 +0100
commit53e3158dfe697ea59da1fa0e401e8da75ae13030 (patch)
tree60cac2249e5ebedc7b87f33025048f3b66490ab3
parent31a10069a52c2fd2aca3a835a7bdc1accae197f5 (diff)
downloadwallabag-53e3158dfe697ea59da1fa0e401e8da75ae13030.tar.gz
wallabag-53e3158dfe697ea59da1fa0e401e8da75ae13030.tar.zst
wallabag-53e3158dfe697ea59da1fa0e401e8da75ae13030.zip
[add] cron to fetch content on imported entries
-rw-r--r--cron.php53
-rwxr-xr-xinc/poche/Database.class.php67
-rwxr-xr-xinc/poche/Poche.class.php86
-rw-r--r--inc/poche/Tools.class.php56
-rw-r--r--themes/baggy/config.twig8
5 files changed, 181 insertions, 89 deletions
diff --git a/cron.php b/cron.php
new file mode 100644
index 00000000..cc137ca2
--- /dev/null
+++ b/cron.php
@@ -0,0 +1,53 @@
1<?php
2
3include_once 'inc/poche/global.inc.php';
4include_once 'inc/poche/config.inc.php';
5
6if (php_sapi_name() === 'cli') {
7 $options_cli = getopt('', array(
8 'limit::',
9 'user-id::',
10 'token::',
11 ));
12}
13else {
14 $options_cli = $_GET;
15}
16
17$limit = ! empty($options_cli['limit']) && ctype_digit($options_cli['limit']) ? (int) $options_cli['limit'] : 10;
18$user_id = ! empty($options_cli['user-id']) && ctype_digit($options_cli['user-id']) ? (int) $options_cli['user-id'] : null;
19$token = ! empty($options_cli['token']) ? $options_cli['token'] : null;
20
21if (is_null($user_id)) {
22 die('You must give a user id');
23}
24
25if (is_null($token)) {
26 die('You must give a token');
27}
28
29$store = new Database();
30$config = $store->getConfigUser($user_id);
31
32if ($token != $config['token']) {
33 die(_('Uh, there is a problem with the cron.'));
34}
35
36$items = $store->retrieveUnfetchedEntries($user_id, $limit);
37
38foreach ($items as $item) {
39 $url = new Url(base64_encode($item['url']));
40 $content = Tools::getPageContent($url);
41
42 $title = ($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled');
43 $body = $content['rss']['channel']['item']['description'];
44
45 // // clean content from prevent xss attack
46 // $config = HTMLPurifier_Config::createDefault();
47 // $purifier = new HTMLPurifier($config);
48 // $title = $purifier->purify($title);
49 // $body = $purifier->purify($body);
50
51
52 $store->updateContentAndTitle($item['id'], $title, $body, $user_id);
53} \ No newline at end of file
diff --git a/inc/poche/Database.class.php b/inc/poche/Database.class.php
index c998fe14..edc775f5 100755
--- a/inc/poche/Database.class.php
+++ b/inc/poche/Database.class.php
@@ -230,8 +230,30 @@ class Database {
230 } 230 }
231 } 231 }
232 232
233 public function updateContentAndTitle($id, $title, $body, $user_id) {
234 $sql_action = 'UPDATE entries SET content = ?, title = ? WHERE id=? AND user_id=?';
235 $params_action = array($body, $title, $id, $user_id);
236 $query = $this->executeQuery($sql_action, $params_action);
237
238 return $query;
239 }
240
241 public function retrieveUnfetchedEntries($user_id, $limit) {
242
243 $sql_limit = "LIMIT 0,".$limit;
244 if (STORAGE == 'postgres') {
245 $sql_limit = "LIMIT ".$limit." OFFSET 0";
246 }
247
248 $sql = "SELECT * FROM entries WHERE (content = '' OR content IS NULL) AND user_id=? ORDER BY id " . $sql_limit;
249 $query = $this->executeQuery($sql, array($user_id));
250 $entries = $query->fetchAll();
251
252 return $entries;
253 }
254
233 public function retrieveAll($user_id) { 255 public function retrieveAll($user_id) {
234 $sql = "SELECT * FROM entries WHERE user_id=? ORDER BY id"; 256 $sql = "SELECT * FROM entries WHERE content <> '' AND user_id=? ORDER BY id";
235 $query = $this->executeQuery($sql, array($user_id)); 257 $query = $this->executeQuery($sql, array($user_id));
236 $entries = $query->fetchAll(); 258 $entries = $query->fetchAll();
237 259
@@ -250,7 +272,7 @@ class Database {
250 272
251 public function retrieveOneByURL($url, $user_id) { 273 public function retrieveOneByURL($url, $user_id) {
252 $entry = NULL; 274 $entry = NULL;
253 $sql = "SELECT * FROM entries WHERE url=? AND user_id=?"; 275 $sql = "SELECT * FROM entries WHERE content <> '' AND url=? AND user_id=?";
254 $params = array($url, $user_id); 276 $params = array($url, $user_id);
255 $query = $this->executeQuery($sql, $params); 277 $query = $this->executeQuery($sql, $params);
256 $entry = $query->fetchAll(); 278 $entry = $query->fetchAll();
@@ -267,21 +289,22 @@ class Database {
267 public function getEntriesByView($view, $user_id, $limit = '', $tag_id = 0) { 289 public function getEntriesByView($view, $user_id, $limit = '', $tag_id = 0) {
268 switch ($view) { 290 switch ($view) {
269 case 'archive': 291 case 'archive':
270 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? "; 292 $sql = "SELECT * FROM entries WHERE content <> '' AND user_id=? AND is_read=? ";
271 $params = array($user_id, 1); 293 $params = array($user_id, 1);
272 break; 294 break;
273 case 'fav' : 295 case 'fav' :
274 $sql = "SELECT * FROM entries WHERE user_id=? AND is_fav=? "; 296 $sql = "SELECT * FROM entries WHERE content <> '' AND user_id=? AND is_fav=? ";
275 $params = array($user_id, 1); 297 $params = array($user_id, 1);
276 break; 298 break;
277 case 'tag' : 299 case 'tag' :
278 $sql = "SELECT entries.* FROM entries 300 $sql = "SELECT entries.* FROM entries
279 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id 301 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
280 WHERE entries.user_id=? AND tags_entries.tag_id = ? "; 302 WHERE entries.content <> '' AND
303 entries.user_id=? AND tags_entries.tag_id = ? ";
281 $params = array($user_id, $tag_id); 304 $params = array($user_id, $tag_id);
282 break; 305 break;
283 default: 306 default:
284 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? "; 307 $sql = "SELECT * FROM entries WHERE content <> '' AND user_id=? AND is_read=? ";
285 $params = array($user_id, 0); 308 $params = array($user_id, 0);
286 break; 309 break;
287 } 310 }
@@ -294,24 +317,25 @@ class Database {
294 return $entries; 317 return $entries;
295 } 318 }
296 319
297 public function getEntriesByViewCount($view, $user_id, $tag_id = 0) { 320 public function getEntriesByViewCount($view, $user_id, $tag_id = 0) {
298 switch ($view) { 321 switch ($view) {
299 case 'archive': 322 case 'archive':
300 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? "; 323 $sql = "SELECT count(*) FROM entries WHERE content <> '' AND user_id=? AND is_read=? ";
301 $params = array($user_id, 1); 324 $params = array($user_id, 1);
302 break; 325 break;
303 case 'fav' : 326 case 'fav' :
304 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_fav=? "; 327 $sql = "SELECT count(*) FROM entries WHERE content <> '' AND user_id=? AND is_fav=? ";
305 $params = array($user_id, 1); 328 $params = array($user_id, 1);
306 break; 329 break;
307 case 'tag' : 330 case 'tag' :
308 $sql = "SELECT count(*) FROM entries 331 $sql = "SELECT count(*) FROM entries
309 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id 332 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
310 WHERE entries.user_id=? AND tags_entries.tag_id = ? "; 333 WHERE entries.content <> '' AND
311 $params = array($user_id, $tag_id); 334 entries.user_id=? AND tags_entries.tag_id = ? ";
312 break; 335 $params = array($user_id, $tag_id);
336 break;
313 default: 337 default:
314 $sql = "SELECT count(*) FROM entries WHERE user_id=? AND is_read=? "; 338 $sql = "SELECT count(*) FROM entries WHERE content <> '' AND user_id=? AND is_read=? ";
315 $params = array($user_id, 0); 339 $params = array($user_id, 0);
316 break; 340 break;
317 } 341 }
@@ -319,7 +343,7 @@ class Database {
319 $query = $this->executeQuery($sql, $params); 343 $query = $this->executeQuery($sql, $params);
320 list($count) = $query->fetch(); 344 list($count) = $query->fetch();
321 345
322 return $count; 346 return $count;
323 } 347 }
324 348
325 public function updateContent($id, $content, $user_id) { 349 public function updateContent($id, $content, $user_id) {
@@ -369,7 +393,7 @@ class Database {
369 $sql = "SELECT DISTINCT tags.* FROM tags 393 $sql = "SELECT DISTINCT tags.* FROM tags
370 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id 394 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
371 LEFT JOIN entries ON tags_entries.entry_id=entries.id 395 LEFT JOIN entries ON tags_entries.entry_id=entries.id
372 WHERE entries.user_id=?"; 396 WHERE entries.content <> '' AND entries.user_id=?";
373 $query = $this->executeQuery($sql, array($user_id)); 397 $query = $this->executeQuery($sql, array($user_id));
374 $tags = $query->fetchAll(); 398 $tags = $query->fetchAll();
375 399
@@ -381,7 +405,7 @@ class Database {
381 $sql = "SELECT DISTINCT tags.* FROM tags 405 $sql = "SELECT DISTINCT tags.* FROM tags
382 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id 406 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
383 LEFT JOIN entries ON tags_entries.entry_id=entries.id 407 LEFT JOIN entries ON tags_entries.entry_id=entries.id
384 WHERE tags.id=? AND entries.user_id=?"; 408 WHERE entries.content <> '' AND tags.id=? AND entries.user_id=?";
385 $params = array(intval($id), $user_id); 409 $params = array(intval($id), $user_id);
386 $query = $this->executeQuery($sql, $params); 410 $query = $this->executeQuery($sql, $params);
387 $tag = $query->fetchAll(); 411 $tag = $query->fetchAll();
@@ -393,7 +417,8 @@ class Database {
393 $sql = 417 $sql =
394 "SELECT entries.* FROM entries 418 "SELECT entries.* FROM entries
395 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id 419 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
396 WHERE tags_entries.tag_id = ? AND entries.user_id=?"; 420 WHERE entries.content <> '' AND
421 tags_entries.tag_id = ? AND entries.user_id=?";
397 $query = $this->executeQuery($sql, array($tag_id, $user_id)); 422 $query = $this->executeQuery($sql, array($tag_id, $user_id));
398 $entries = $query->fetchAll(); 423 $entries = $query->fetchAll();
399 424
diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php
index 026b0b4e..5a89a8d2 100755
--- a/inc/poche/Poche.class.php
+++ b/inc/poche/Poche.class.php
@@ -362,60 +362,6 @@ class Poche
362 ); 362 );
363 } 363 }
364 364
365 protected function getPageContent(Url $url)
366 {
367 // Saving and clearing context
368 $REAL = array();
369 foreach( $GLOBALS as $key => $value ) {
370 if( $key != 'GLOBALS' && $key != '_SESSION' && $key != 'HTTP_SESSION_VARS' ) {
371 $GLOBALS[$key] = array();
372 $REAL[$key] = $value;
373 }
374 }
375 // Saving and clearing session
376 $REAL_SESSION = array();
377 foreach( $_SESSION as $key => $value ) {
378 $REAL_SESSION[$key] = $value;
379 unset($_SESSION[$key]);
380 }
381
382 // Running code in different context
383 $scope = function() {
384 extract( func_get_arg(1) );
385 $_GET = $_REQUEST = array(
386 "url" => $url->getUrl(),
387 "max" => 5,
388 "links" => "preserve",
389 "exc" => "",
390 "format" => "json",
391 "submit" => "Create Feed"
392 );
393 ob_start();
394 require func_get_arg(0);
395 $json = ob_get_flush();
396 return $json;
397 };
398 $json = $scope( "inc/3rdparty/makefulltextfeed.php", array("url" => $url) );
399
400 // Clearing and restoring context
401 foreach( $GLOBALS as $key => $value ) {
402 if( $key != "GLOBALS" && $key != "_SESSION" ) {
403 unset($GLOBALS[$key]);
404 }
405 }
406 foreach( $REAL as $key => $value ) {
407 $GLOBALS[$key] = $value;
408 }
409 // Clearing and restoring session
410 foreach( $_SESSION as $key => $value ) {
411 unset($_SESSION[$key]);
412 }
413 foreach( $REAL_SESSION as $key => $value ) {
414 $_SESSION[$key] = $value;
415 }
416 return json_decode($json, true);
417 }
418
419 /** 365 /**
420 * Call action (mark as fav, archive, delete, etc.) 366 * Call action (mark as fav, archive, delete, etc.)
421 */ 367 */
@@ -424,15 +370,21 @@ class Poche
424 switch ($action) 370 switch ($action)
425 { 371 {
426 case 'add': 372 case 'add':
427 $content = $this->getPageContent($url); 373 if (!$import) {
428 $title = ($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled'); 374 $content = Tools::getPageContent($url);
429 $body = $content['rss']['channel']['item']['description']; 375 $title = ($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled');
430 376 $body = $content['rss']['channel']['item']['description'];
431 // clean content from prevent xss attack 377
432 $config = HTMLPurifier_Config::createDefault(); 378 // clean content from prevent xss attack
433 $purifier = new HTMLPurifier($config); 379 $config = HTMLPurifier_Config::createDefault();
434 $title = $purifier->purify($title); 380 $purifier = new HTMLPurifier($config);
435 $body = $purifier->purify($body); 381 $title = $purifier->purify($title);
382 $body = $purifier->purify($body);
383 }
384 else {
385 $title = '';
386 $body = '';
387 }
436 388
437 //search for possible duplicate if not in import mode 389 //search for possible duplicate if not in import mode
438 if (!$import) { 390 if (!$import) {
@@ -897,7 +849,7 @@ class Poche
897 # the second <ol> is for read links 849 # the second <ol> is for read links
898 $read = 1; 850 $read = 1;
899 } 851 }
900 $this->messages->add('s', _('import from instapaper completed')); 852 $this->messages->add('s', _('import from instapaper completed. You have to execute the cron to fetch content.'));
901 Tools::logm('import from instapaper completed'); 853 Tools::logm('import from instapaper completed');
902 Tools::redirect(); 854 Tools::redirect();
903 } 855 }
@@ -941,7 +893,7 @@ class Poche
941 # the second <ul> is for read links 893 # the second <ul> is for read links
942 $read = 1; 894 $read = 1;
943 } 895 }
944 $this->messages->add('s', _('import from pocket completed')); 896 $this->messages->add('s', _('import from pocket completed. You have to execute the cron to fetch content.'));
945 Tools::logm('import from pocket completed'); 897 Tools::logm('import from pocket completed');
946 Tools::redirect(); 898 Tools::redirect();
947 } 899 }
@@ -997,7 +949,7 @@ class Poche
997 } 949 }
998 } 950 }
999 } 951 }
1000 $this->messages->add('s', _('import from Readability completed. ' . $count . ' new links.')); 952 $this->messages->add('s', _('import from Readability completed. You have to execute the cron to fetch content.'));
1001 Tools::logm('import from Readability completed'); 953 Tools::logm('import from Readability completed');
1002 Tools::redirect(); 954 Tools::redirect();
1003 } 955 }
@@ -1043,7 +995,7 @@ class Poche
1043 } 995 }
1044 996
1045 } 997 }
1046 $this->messages->add('s', _('import from Poche completed. ' . $count . ' new links.')); 998 $this->messages->add('s', _('import from Poche completed. You have to execute the cron to fetch content.'));
1047 Tools::logm('import from Poche completed'); 999 Tools::logm('import from Poche completed');
1048 Tools::redirect(); 1000 Tools::redirect();
1049 } 1001 }
diff --git a/inc/poche/Tools.class.php b/inc/poche/Tools.class.php
index eed7afbd..393a415d 100644
--- a/inc/poche/Tools.class.php
+++ b/inc/poche/Tools.class.php
@@ -193,7 +193,7 @@ class Tools
193 193
194 public static function logm($message) 194 public static function logm($message)
195 { 195 {
196 if (DEBUG_POCHE) { 196 if (DEBUG_POCHE && php_sapi_name() != 'cli') {
197 $t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n"; 197 $t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n";
198 file_put_contents(CACHE . '/log.txt', $t, FILE_APPEND); 198 file_put_contents(CACHE . '/log.txt', $t, FILE_APPEND);
199 error_log('DEBUG POCHE : ' . $message); 199 error_log('DEBUG POCHE : ' . $message);
@@ -251,4 +251,58 @@ class Tools
251 251
252 exit; 252 exit;
253 } 253 }
254
255 public static function getPageContent(Url $url)
256 {
257 // Saving and clearing context
258 $REAL = array();
259 foreach( $GLOBALS as $key => $value ) {
260 if( $key != 'GLOBALS' && $key != '_SESSION' && $key != 'HTTP_SESSION_VARS' ) {
261 $GLOBALS[$key] = array();
262 $REAL[$key] = $value;
263 }
264 }
265 // Saving and clearing session
266 $REAL_SESSION = array();
267 foreach( $_SESSION as $key => $value ) {
268 $REAL_SESSION[$key] = $value;
269 unset($_SESSION[$key]);
270 }
271
272 // Running code in different context
273 $scope = function() {
274 extract( func_get_arg(1) );
275 $_GET = $_REQUEST = array(
276 "url" => $url->getUrl(),
277 "max" => 5,
278 "links" => "preserve",
279 "exc" => "",
280 "format" => "json",
281 "submit" => "Create Feed"
282 );
283 ob_start();
284 require func_get_arg(0);
285 $json = ob_get_flush();
286 return $json;
287 };
288 $json = $scope( "inc/3rdparty/makefulltextfeed.php", array("url" => $url) );
289
290 // Clearing and restoring context
291 foreach( $GLOBALS as $key => $value ) {
292 if( $key != "GLOBALS" && $key != "_SESSION" ) {
293 unset($GLOBALS[$key]);
294 }
295 }
296 foreach( $REAL as $key => $value ) {
297 $GLOBALS[$key] = $value;
298 }
299 // Clearing and restoring session
300 foreach( $_SESSION as $key => $value ) {
301 unset($_SESSION[$key]);
302 }
303 foreach( $REAL_SESSION as $key => $value ) {
304 $_SESSION[$key] = $value;
305 }
306 return json_decode($json, true);
307 }
254} 308}
diff --git a/themes/baggy/config.twig b/themes/baggy/config.twig
index 4026bf28..8f35d797 100644
--- a/themes/baggy/config.twig
+++ b/themes/baggy/config.twig
@@ -125,6 +125,14 @@
125 <li><a href="./?import&amp;from=poche">{% trans "Import from wallabag" %}</a> {{ '(after uploaded %s file)'|trans|format(constant('POCHE_FILE')) }}</li> 125 <li><a href="./?import&amp;from=poche">{% trans "Import from wallabag" %}</a> {{ '(after uploaded %s file)'|trans|format(constant('POCHE_FILE')) }}</li>
126 </ul> 126 </ul>
127 127
128 {% if token == '' %}
129 <p>{% trans "3. Your feed token is currently empty and must first be generated to fetch content. Click <a href='?feed&amp;action=generate'>here to generate it</a>." %}</p>
130 {% else %}
131 <p>3. {% trans "You can fetch content for imported items." %} <a href="cron.php?limit=10&amp;user-id={{ user_id }}&amp;token={{token}}" target="_blank">Click here</a> to fetch content for 10 articles.</p>
132 <p>{% trans "You can also create a cron task:" %}</p>
133 <pre><code>0 */4 * * * cd /path/to/wallabag && php cron.php --limit=10 --user-id={{user_id}} --token={{token}} >/dev/null 2>&1</code></pre>
134 {% endif %}
135
128 <h2>{% trans "Export your wallabag data" %}</h2> 136 <h2>{% trans "Export your wallabag data" %}</h2>
129 {% if constant('STORAGE') == 'sqlite' %} 137 {% if constant('STORAGE') == 'sqlite' %}
130 <p><a href="?download" target="_blank">{% trans "Click here" %}</a> {% trans "to download your database." %}</p>{% endif %} 138 <p><a href="?download" target="_blank">{% trans "Click here" %}</a> {% trans "to download your database." %}</p>{% endif %}