Added syncing (push and pull) and refactored a few functions.

This commit is contained in:
Beanow 2014-08-09 00:46:53 +02:00
commit 1fe9bb9b5b
11 changed files with 732 additions and 155 deletions

View file

@ -28,17 +28,23 @@ function admin_content(&$a) {
$flagged = 'No entries.';
}
//Get the backlog size.
$res = q("SELECT count(*) as `count` FROM `profile` WHERE `updated` < '%s'",
//Get the maintenance backlog size.
$res = q("SELECT count(*) as `count` FROM `profile` WHERE `updated` < '%s'",
dbesc(date('Y-m-d H:i:s', time()-$a->config['maintenance']['min_scrape_delay'])));
$backlog = 'unknown';
if(count($res)){ $backlog = $res[0]['count'].' entries'; }
$maintenance_backlog = 'unknown';
if(count($res)){ $maintenance_backlog = $res[0]['count'].' entries'; }
//Get the pulling backlog size.
$res = q("SELECT count(*) as `count` FROM `sync-pull-queue`");
$pulling_backlog = 'unknown';
if(count($res)){ $pulling_backlog = $res[0]['count'].' entries'; }
$tpl = file_get_contents('view/admin.tpl');
return replace_macros($tpl, array(
'$present' => is_file('.htimport') ? ' (present)' : '',
'$flagged' => $flagged,
'$backlog' => $backlog,
'$maintenance_backlog' => $maintenance_backlog,
'$pulling_backlog' => $pulling_backlog,
'$maintenance_size' => $a->config['maintenance']['max_scrapes'].' items per maintenance call.'
));

View file

@ -1,6 +1,7 @@
<?php
require_once('include/submit.php');
require_once('include/sync.php');
function submit_content(&$a) {
@ -8,10 +9,7 @@ function submit_content(&$a) {
$url = hex2bin(notags(trim($_GET['url'])));
//Currently we simply push RAW URL's to our targets.
//If we support it that is.
if($a->config['syncing']['enable_pushing']){
q("INSERT INTO `sync-queue` (`url`) VALUES ('%s')", dbesc($url));
}
sync_push($url);
//Run the submit sequence.
run_submit($url);

80
mod/sync.php Normal file
View file

@ -0,0 +1,80 @@
<?php
function sync_content(&$a)
{
header('Content-type: application/json; charset=utf-8');
//When no arguments were given, return a json token to show we support this method.
if($a->argc < 2){
echo json_encode(array(
'pulling_enabled'=>!!$a->config['syncing']['enable_pulling'],
'pushing_enabled'=>!!$a->config['syncing']['enable_pushing']
));
exit;
}
//Method switcher here.
else{
switch($a->argv[1]){
case 'pull':
if(!$a->config['syncing']['enable_pulling']){
echo json_encode(array('error'=>'Pulling disabled.')); exit;
}
switch ($a->argv[2]) {
case 'all': echo json_encode(do_pull_all()); exit;
case 'since': echo json_encode(do_pull($a->argv[3])); exit;
}
default: echo json_encode(array('error'=>'Unknown method.')); exit;
}
}
}
function do_pull($since)
{
if(!intval($since)){
return array('error' => 'Must set a since timestamp.');
}
//Recently modified items.
$r = q("SELECT * FROM `sync-timestamps` WHERE `modified` > '%s'", date('Y-m-d H:i:s', intval($since)));
//This removes all duplicates.
$profiles = array();
foreach($r as $row) $profiles[$row['url']] = $row['url'];
//This removes the keys, so it's a flat array.
$results = array_values($profiles);
//Format it nicely.
return array(
'now' => time(),
'count' => count($results),
'results' => $results
);
}
function do_pull_all()
{
//Find all the profiles.
$r = q("SELECT `homepage` FROM `profile`");
//This removes all duplicates.
$profiles = array();
foreach($r as $row) $profiles[$row['homepage']] = $row['homepage'];
//This removes the keys, so it's a flat array.
$results = array_values($profiles);
//Format it nicely.
return array(
'now' => time(),
'count' => count($results),
'results' => $results
);
}