MakeProgress + mysql + PHP

ТЗ: Хранить задания со статусом выполнения на серваке и отдавать их плазмойду мейкпрогресс.

Реализация: Так как набросать надо было быстро — обошлось мне в 100 строк на пыхыпэ, для хранения используется mysql

Лицензия: WTFPL

Исходник под катом, выслал Никите Мельниченко, глядишь включит в виде бонуса к плазмойду.


Файл берем отсюда

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/* This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details. */ 
 
//This script looks more lika a basic example of how such a system can be implemented. 
//All of it was written in some 5 minutes so some things you might expect may be missing.
//The basic usage is:
//You set this thing up on your server, then you can do the queries that will update the progress of the tasks via http get
//just navigate to http://localhost_or_whatever_your_host_is/tsk.php?set=1&amp;idkey=some_log_string_used_as_id&amp;task=myTask&amp;m=0&amp;M=100&amp;v=10
//The same thing in plain words:
//idkey is a unique string that is used to auth the user. Say you have a whole family using one db for tasks - each member can have a task called myTask. 
//Without knowing the idkey one cannot change or query task status
//task is the name of the task, be creative =)
//m stands for minimum value, In the example this is zero
//M stands for maximum value, In this example 100
//v stands for actual value. 
//Now, once you've set your scripts to update their progress, either with this script or directly to db, time to setup makeProgress plasmoid. 
//Nikita promissed that it will soon be able to fetch the progress via an url by itself, but while it cannot, do the following
//Create a new 'poll' progress and tell it to poll the following app: wget -O - http://localhost_or_whatever_your_host_is/tsk.php?idkey=some_log_string_used_as_id&amp;task=myTask
//That's all and should be self-explanatory.
//To setup the database do the following, apart from uploading this script to your host
//Run this shit in php admin, adjust the name of the table if needed
/*
 CREATE TABLE `tasks` (
`idkey` VARCHAR( 255 ) NOT NULL ,
`task` VARCHAR( 255 ) NOT NULL ,
`min` INT NOT NULL ,
`max` INT NOT NULL ,
`value` INT NOT NULL ,
INDEX ( `idkey` , `task` )
) ENGINE = MYISAM 
*/
 
//Adjust these to fit your config
$login = "login";
$pass = "pass";
$host = "host";
$database = "database";
//And adjust this if you altered table name
$table = 'tasks';
//No more modifications below this line
function send_task($minimum,$maximum,$value)
{
echo "{\"minimum\":$minimum,\"maximum\":$maximum,\"value\":$value}\n";
}
 
function database_connect()
{
global $host,$login,$pass,$database;
$conn = mysql_connect($host, $login, $pass);
if ($conn == FALSE) die("shit happens");
mysql_select_db($database);
}
 
function get_task_data($task, $idkey)
{
global $table;
$res = mysql_query("SELECT * FROM `$table` WHERE `idkey`='". mysql_real_escape_string($idkey)."' AND `task` = '". mysql_real_escape_string($task)."';");
if (mysql_affected_rows() &gt; 0)
 {
 return mysql_fetch_assoc($res);
 }
 else
 return 0;
}
 
function set_task_data($task, $idkey, $min, $max, $val)
{
global $table;
if (is_numeric($min) &amp;&amp; is_numeric($max) &amp;&amp; is_numeric($val))
 {
 $res = mysql_query("SELECT * FROM `$table` WHERE `idkey`='". mysql_real_escape_string($idkey)."' AND `task` = '". mysql_real_escape_string($task)."';");
 if (mysql_affected_rows() &gt; 0)
 {
 //need to update the table
 mysql_query("UPDATE `$table` SET `min` = '$min', `max` = '$max', `value` = '$val' WHERE `idkey` = '". mysql_real_escape_string($idkey)."' AND `task` = '". mysql_real_escape_string($task)."'");
 }else
 {
 mysql_query("INSERT INTO `$table`  (`idkey` , `task` ,`min` , `max` ,`value` )VALUES ('".mysql_real_escape_string($idkey)."', '".mysql_real_escape_string($task)."', '$min', '$max', '$val' );
 ");
 }
 }
}
 
database_connect();
if (isset($_GET['task']))
 if (isset($_GET['set']))
 {
 set_task_data($_GET['task'],$_GET['idkey'], $_GET['m'],$_GET['M'],$_GET['v']);
 echo "ok";
 }else
 {
 $data = get_task_data($_GET['task'],$_GET['idkey']);
 send_task($data['min'],$data['max'],$data['value']);
 }
//That's all in 100 lines exactly =)
>
Print Friendly

MakeProgress + mysql + PHP: 1 комментарий

  1. По-моему, логичнее было использовать SQLite, он хотя бы по дефолту во многих дистрибутивах используется. Либо проще — хранить plaint текст в файле, но у которого хитро-высчитаное имя в виде какого-нибудь хэша. Тогда это не составит труда использовать в том числе и на локальной машине.

Добавить комментарий