Piotras' blog: Archive
2005-07-01 - 2005-07-31
BloGTK in action
Posted on 2005-07-12 16:02:55 UTC.
The worst blog application? BloGTK. Why? Because it is impossible to run this program if you make any mistake in account configuration. Well, if one may say about account configuration ;) All data are always saved to 'Default' account while pressing "save" for other accounts delete anything you wrote ;)
New MgdSchema methods
Posted on 2005-07-12 17:34:02 UTC.
June was fast , very fast month. My family had a short vacation and midgard developers released beta1 and rc1 release during this month.
The funny thing with 1.7 releases is that only few people pay attention at new file system based midcom , while plenty of users and developers focus on new experimental Midgard2 preview technology :)
I have been thinking about simple and fast list method , which could list "child" objects in tree. It was quite fast to implement midgard_object_list function and $object->list() method for midgard-php. Ah! Too fast. Someone could try to set order using such method. So, what could be the best choice? Midgard Query Builder :)
There are already two similiar methods for new midgard-php objects registered with MgdSchema.
- list() List all objects of the same type which have upfield set to current object's primary field.
- list_childs() List all objects of "child" type which have parentfield defined.
Both method returns new MidgardQueryBuilder object instance with parent or up property already being added ( transparently in core ) as constraint to Midgard Query Builder. Here's some simple example from the code I used for tetsing:$sn = new NewMidgardSnippetdir();
$sn->get_by_id(186);
$qb = $sn->list();
$objects = $qb->execute();
$parent = $objects[0]->get_parent();
Example looks a bit difficult and seems to be too much complicated for such trivial thing which was always made using mgd_list_xxxx functions. Yes, it is , but it is quite good ( perfect ? ;) way to write additional wrappers and make any API which let's you limit objects retrieved from database.
get_parent method returns "parent" ( in tree ) object which is defined with parent and parenfield attributes in midgard schema.
I also added ( interesting IMO ) get method. This is simple method which returns only one object and searches for the object comparing property values directly. Few months ago I wrote about find() method which could be used as a replacement for mgd_get_xxx_by_name, but get is even better.
$article = new NewMidgardArticle();
$article->topic = 1;
$article->name = "index";
$article->get();
No fetchables , no arrays in a place when you expect only one object.
What is also interesting is get_by_path method , which "returns" object if found.
$article = new NewMidgardArticle();
$article->get_by_path(""/192-168-0-17-pptest-root-topic/test/img_0016");
I didn't make many tests , but this method should work without any problems for trees like this: /topic/topic/article/article/article.
I wanted to make get_by_path method asap , cause tarjei is writing new midgard webdav class. Using desktop apps with midgard will be much more than cool pleasure :)
$object->parent
Posted on 2005-07-13 17:02:20 UTC.
I even didn't know that using gnome application with midgard can be so easy and flexible :)
This time I try drivel , it's looks much better than BloGTK , thank you Smallone!.
Following tariej's suggestion I wrote simple parent method today.
What it really does? Just returns class name of object's parent type. Pay attention. Not parent class name which was extended but parent class name which is parent in midgard's tree.
Here's brief definition from schema:
type name="NewMidgardArticle" parent="NewMidgardTopic"
$article = new NewMidgardArticle();
$parent = $article->parent()
print $parent;
NewMidgardTopic
Something you should know about metadata.
Posted on 2005-07-20 16:07:27 UTC.
Metadata definition for a good start.
It's good ( and even very good ) that new MgdSchema objects have new methods and midgard-php have new functions which describe classes' and objects' data ( like property type ) , and internal classes' data like: "which property keeps data ( id or guid ) which points directly to object's parent object in midgard tree". Writing code without such knowledge is sometimes like writing code with closed eyes.
I was a bit confused when I decided to use exactly the same enum type data in core and in midgard-php. Why? Because making iteration for enum is "easy as hell". At last I found solution in Alexander's repligard code , which let me create simple GSList and use its position as php integer value and its data as name to register constant.
So , new midgard-php constants are already usable.
MGD_META_PROPERTY_PARENT
( use it when you want to know which property ( it's name ) points to parent object in midgard tree )
Schema definition: <property name="topic" type="integer" parentfield="topic"/>
MGD_META_PROPERTY_UP
( use it when you want to know which property ( it's name ) points to up object in midgard tree, usually object of the same type )
Schema definition: <property name="up" type="integer" upfield="up"/>
MGD_META_PROPERTY_PRIMARY
( use it when you want to know which property ( it's name ) is used as primary field ( id or guid ))
Schema definition: <property name="id" type="integer" primaryfield="id"/>
MGD_META_TREE_PARENT
( use it when you want to know the name of parent object's in midgard tree )
Schema definition: <type name="NewMidgardArticle" parent="NewMidgardTopic">
MGD_META_TREE_CHILDS
( use it when you want to know how many child ( in midgard tree ) objects ( and their names ) may be used for object )
There is no schema definition for this. Child types are created automagically when schema is loaded.
OK , let's look at some sample code. These constants may be used as function's or object method's parameters.
mgd_get_class_data( int metatype, string class name);
returns property name or array if found or false on failure
Yes, I am not happy with function which returns string or array , but you always know what you want to get while
writing code.
$parent = mgd_get_class_data(MGD_META_PROPERTY_PARENT , "NewMidgardArticle");
/* or, if you would like to use object :
$art = new NewMidgardArticle();
$parent = $art->get_data(MGD_META_PROPERTY_PARENT);
*/
print $parent;
output:
topic
Another example:
$topic = new NewMidgardTopic();output :
$childs = $topic->get_data (MGD_META_TREE_CHILDS);
print_r($childs);
Array
(
[NewMidgardArticle] =>
[NewMidgardArt] =>
[PPNewMidgardArt] =>
[org_openpsa_document] =>
)
OK. What about properties metadata?
mgd_get_class_var(string property name, string class name);
returns array with property name as key
$name = mgd_get_class_var("name", "NewMidgardTopic");
/* or
$topic = new NewMidgardTopic();
$name = $topic->get_var("name");
*/
print_r($name);output:
Array
(
[name] => Array
(
[type] => string
)
)
mgd_get_class_property is an alias to mgd_get_class_var.
mgd_get_class_vars(string class name);
returns array with all class default properties and their types
$props = mgd_get_class_vars("NewMidgardTopic");output:[...]etc etc etc
[revision] => Array
(
[type] => int
)
[created] => Array
(
[type] => string
)
[revisor] => Array
(
[type] => int
)
mgd_get_class_properties function is an alias to mgd_get_class_vars.
is element loaded?
Posted on 2005-07-20 18:13:27 UTC.
Quite relaxing to write mgd_is_element_loaded function after metadata initial implementation was done ;)
bool mgd_is_element_loaded(string element name);
returns true if element is already loaded , false if not
This function doesn't care if you think about style or page element.
Example?
$a = mgd_is_element_loaded("code-init");
var_dump($a);
$b = mgd_is_element_loaded("amerigard");
var_dump($b);
output:
bool(true)
bool(false)
Midgard Query Builder multilanged more
Posted on 2005-07-30 15:20:23 UTC.
It was very frustrating experience to try to make mgd_list_xxx functions really easily working with multilanged objects. Well, it appears that such functions can not be used with ML'ed objects in real life as what you can get ( and fetch ) are objects with some lang set and additionally "the same" objects dulicated with lang 0.
I used to use ML Midgard feature only with mgd_get_xxx functions , so midgard ML'ed fetchables became something weird.
Luckily I was able to "fix" this missed feature with Midgard Query Builder. So now , using QB all objects returned by execute method have no duplicated ( lang 0 ) ones. Additionaly you are able to limit objects and get only these ones which use particular language.Follow midgard documentation to see how set_lang method works.
Additionally QB has count method which returns number of objects returned by query.
In theory this "should be" '$qb->count()' times faster than count($qb->execute).