function category_tree($leaf = false, $level = 1)
 {
     if (!$leaf) {
         $leaf = MsCategoryFactory::get_root_category();
     }
     $indent = str_repeat('#', $level);
     $name = $leaf->get('name');
     if (!$name) {
         $name = "''no name set!''";
     }
     $id = $leaf->id;
     $r = "{$indent} '''[[MediaWiki:ms-{$id}-category|{$id}]]''' " . ($leaf->exists() ? '' : "'''DOES NOT EXIST'''") . "\n";
     $r .= "{$indent}* ''MSGS'': [[MediaWiki:ms-{$id}-record|record]], [[MediaWiki:ms-{$id}-category-input|input]], [[MediaWiki:ms-{$id}-presearch-box|presearch]], [[MediaWiki:ms-{$id}-postsearch-box|postsearch]]\n";
     $r .= "{$indent}* ''DATABASES'': ";
     foreach ($leaf->get_databases() as $db) {
         $r .= "[[MediaWiki:ms-{$db}-database|{$db}]], ";
         $msg = MsDatabase::get_conf_msg_name($db);
         if (wfMsgExists($msg)) {
             $this->list_of_msgs[] = $msg;
         }
     }
     $r .= "\n";
     foreach ($leaf->get_conf_array() as $k => $v) {
         $r .= "{$indent}* ''{$k}'': {$v}\n";
     }
     #if(wfMsgExists($msg)) $this->list_of_msgs += $msg;
     $this->list_of_msgs = array_merge($this->list_of_msgs, $leaf->get_messages());
     $this->list_of_dbs = array_merge($this->list_of_dbs, $leaf->get_databases());
     foreach ($leaf->get_sub_categories(MsCategory::AS_OBJECTS) as $subcat) {
         $r .= $this->category_tree($subcat, $level + 1);
     }
     return $r;
 }
 /**
  * check stack for inheritance consistency, repair the stack internally -- 
  * that is, set $this->array to the top to at least the errorous position
  **/
 function clean()
 {
     // trivial case.
     if (empty($this->array)) {
         // add a root
         $this->array[] = MsCategoryFactory::get_root_category();
         return;
     }
     // check root
     if ($this->array[0]->id != self::$root_category_name) {
         // the root was bad.
         array_unshift($this->array[0], MsCategoryFactory::get_root_category());
     }
     // walk down stack from the TOP until almost-root
     for ($x = count($this->array) - 1; $x >= 1; $x--) {
         // if the top element is not a child from the one below...
         if (!$this->array[$x - 1]->has_sub_category($this->array[$x])) {
             // ... then kill it.
             array_pop($this->array);
         }
     }
     // stack is clean.
 }