[prev in list] [next in list] [prev in thread] [next in thread] 

List:       midgard-dev
Subject:    [midgard-dev] mgd_walk_xxx_tree() function set
From:       Alexander Bokovoy <ab () stimorol ! com ! by>
Date:       2000-01-28 22:53:18
[Download RAW message or body]

Hello Midgardians,

  Below  is  my report and sketch of documentation for new set of core
  Midgard/PHP  functions mgd_walk_xxx_tree() based on David Guerizec's
  suggestions.  My  English is not good for writing manual but I think
  that Ron, Emile, and Jamie will have enough words for translating my
  poor report into manual entry :-)

  Today  I  added  set  of  functions  for traversing trees of topics,
  pages,  styles, and reply articles to the core of Midgard/PHP 1.2.6.
  These  functions  all  have  the  same syntax and differ only in the
  field of application.

  Template:
    int mgd_walk_xxx_tree(string func, int root, int maxlevel,
                          mixed &xparam[, boolean order]);

  Actual functions for walking through topics, pages, styles,
  and reply articles:
    mgd_walk_topic_tree, mgd_walk_page_tree, mgd_walk_style_tree,
    mgd_walk_article_tree

    parameters:
      func  -  name of user function which will be called for each
               element of tree. This function should be defined as
               
               void func(int id, int level, mixed &xparam);

               where  <id>  is an ID of tree element, <level> is index
               of its level under the <root>. <root> itself has index 0
               first sub-tree indexed as 1, sub-sub-tree - as 2, etc.,
               no matter where actually <root> exists in whole
               database tree.

      root  -  ID of root of the tree
      maxlevel - max deep level of affected sub-trees. Set 0 for
               traversing whole tree, give negative number for
               traversing only elements deeper <maxlevel> (see
               example 4 for more info).
      xparam - user-given variable which will be passed to
               user-defined function on each call. Use it for own
               needs.
      order  - optional parameter which could be used to specify
               traversing order. If <order> is true (default) then
               user-defined function will be called for each element
               before processing its sub-elements. If <order> is false
               each element will be processed _after_ all its childs.
               <order> = true could be used for copying structure,
               while <order> = false - for deleting structure.
  
  Examples:
    The following script is used to test mgd_walk_xxx_tree()
    functions (added to midgard-root.php3 before templates):
-------------------8<------------------------------------8<-------
   $TESTER_ADDRESS = "ab"; /* Address of tester */
   
   function addname($id, $level, &$xparam) {
       $page = mgd_get_page($id);
       for($i=0; $i<$level; $i++) $xparam .= "   ";
       $xparam .= $page->name." [$id, $page->up]\n";
       return true;
   }

   $str1  = "Ex.1. - Imitate copying:\n";
   mgd_walk_page_tree('addname', 0, 0, &$str1);
   
   $str1 .= "\nEx.2. - Imitate deleting:\n";
   mgd_walk_page_tree('addname', 0, 0, &$str1, false);

   $str1 .= "\nEx.3. - Imitate copying no deeper second level after root:\n";
   mgd_walk_page_tree('addname', 0, 2, &$str1);

   $str1 .= "\nEx.4. - Imitate deletion of all elements deeper second level:\n";
   mgd_walk_page_tree('addname', 0, -2, &$str1, false);
   
   /* Send results to tester */
   mgd_create_mail("root",$TESTER_ADDRESS,"Test",$str1);
-------------------8<------------------------------------8<-------
  Results:
  
-------------------8<------------------------------------8<-------
Ex.1. - Imitate copying:
Admin Root [1, 0]
   about [33, 1]
   topic [7, 1]
      article [29, 7]
   style [5, 1]
      element [25, 5]
   host [4, 1]
      page [17, 4]
         element [20, 17]
   group [3, 1]
      person [11, 3]
VMUC Root [34, 0]
   contact [40, 34]
   events [39, 34]
   products [38, 34]
   news [37, 34]
   about [35, 34]
      site [36, 35]

-------------------8<------------------------------------8<-------

Ex.2. - Imitate deleting:
   about [33, 1]
      article [29, 7]
   topic [7, 1]
      element [25, 5]
   style [5, 1]
         element [20, 17]
      page [17, 4]
   host [4, 1]
      person [11, 3]
   group [3, 1]
Admin Root [1, 0]
   contact [40, 34]
   events [39, 34]
   products [38, 34]
   news [37, 34]
      site [36, 35]
   about [35, 34]
VMUC Root [34, 0]

-------------------8<------------------------------------8<-------

Ex.3. - Imitate copying no deeper second level after root:
Admin Root [1, 0]
   about [33, 1]
   topic [7, 1]
      article [29, 7]
   style [5, 1]
      element [25, 5]
   host [4, 1]
      page [17, 4]
VMUC Root [34, 0]
   contact [40, 34]
   events [39, 34]
   products [38, 34]
   news [37, 34]
   about [35, 34]
      site [36, 35]

-------------------8<------------------------------------8<-------

Ex.4. - Imitate deletion of all elements deeper second level:
      article [29, 7]
      element [25, 5]
         element [20, 17]
      page [17, 4]
      person [11, 3]
      site [36, 35]

-------------------8<------------------------------------8<-------

As  for  actual copy/delete/move trees for Admin site, these functions
will  be  implemented  at  the  Midgard-lib level and will do copying,
deletion, moving entirely in C to avoid request delays.

Best regards,
 Alexander Bokovoy.

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic