Kji has submitted this change and it was merged.

Change subject: Updated hierarchyParent and hierarchyChildren parser functions.
......................................................................


Updated hierarchyParent and hierarchyChildren parser functions.

The version number is now 1.4.0.

Updated hierarchyParent parser function to support duplicate target instances 
within a single hierarchy.

Change-Id: Id6abad8f95a914da62ec74e6ab9c9ad995536142

Removed some useless comments

Change-Id: I7aae16b46dd25c6e154d317a714ce28c1a50bc52

Updated hierarchyChildren so the default behavior is to return all root nodes 
when the target is empty.

Change-Id: I1c9c5ef423b3c0785ac5c6d88f19afede867104c
---
M HierarchyBuilder.class.php
M HierarchyBuilder.php
2 files changed, 105 insertions(+), 22 deletions(-)

Approvals:
  Kji: Verified; Looks good to me, approved



diff --git a/HierarchyBuilder.class.php b/HierarchyBuilder.class.php
index 6083e04..371900c 100644
--- a/HierarchyBuilder.class.php
+++ b/HierarchyBuilder.class.php
@@ -78,12 +78,19 @@
         * down to find the target page, and then looping further to identify 
the
         * immediate children. Once the loop to find immediate children finds a 
row
         * which has depth less than or equal to the target then the search is
-        * complete. If there are no immediate children in the hierarchy for the
-        * specified target page, or if the target page is not included in the
-        * hierarchy at all, then this function returns an empty array.
+        * complete. 
+        *
+        * If the target page is empty, then target page is assumed to be the
+        * invisible/imaginary super root node "Hierarchy Root" and all the root
+        * level nodes in the hierarchy will be returned. 
+        *
+        * If there are no immediate children in the hierarchy for the
+        * specified target page, then this function returns an empty array.
         *
         * @param string $targetPageName: is the name of the target page for 
which
-        *  we want the list of immediate children.
+        *  we want the list of immediate children. If this is empty, then we 
will
+        *  assume the target is the invisible super root node "Hierarchy Root" 
and
+        *  return all of the root level nodes from the hierarchy.
         * @param string $hierarchyPageName: is the name of the page containing 
the
         *  hierarchy from which to retrieve the list of children.
         * @param string $hierarchyPropertyName: is the name of the property on 
the
@@ -95,6 +102,19 @@
        public static function getPageChildren( $targetPageName, 
$hierarchyPageName,
                $hierarchyPropertyName
        ) {
+               // handle the strange empty target case first
+               if ( $targetPageName == '' ) {
+                       $rootRows = self::getHierarchyRowsByDepth( '*', 
$hierarchyPageName, $hierarchyPropertyName );
+
+                       $children = array();
+                       foreach( $rootRows as $rootRow ) {
+                               array_push( $children, 
self::getPageNameFromHierarchyRow( $rootRow ) );
+                       }
+
+                       return $children;
+               }
+
+               // handle the normal case where a target page is given
                $hierarchy = self::getPropertyFromPage( $hierarchyPageName, 
$hierarchyPropertyName );
                $hierarchyRows = preg_split( '/\n/', $hierarchy );
 
@@ -183,7 +203,8 @@
         * @param string $hierarchyPropertyName: The name of the property on 
the 
         *  hierarchy page which contains the hierarhcy data. (ex: Hierarchy 
Data)
         *
-        * @return string: The hierarchical parent of target page within the 
hiearchy.
+        * @return array: The hierarchical parents of target page instances 
within
+        *  the hierarchy.
         */
 
        public static function getPageParent( $targetPageName, 
$hierarchyPageName,
@@ -191,6 +212,9 @@
        ) {
                $hierarchy = self::getPropertyFromPage( $hierarchyPageName, 
$hierarchyPropertyName );
                $hierarchyRows = preg_split( '/\n/', $hierarchy );
+
+               // array to store the parents of the target instances
+               $parents = array();
 
                $currentPagePattern = '/\[\[' . $targetPageName . '\]\]/';
                // loop through the hierarchyRows looking for the row 
containing the currentPage
@@ -204,11 +228,12 @@
                                // Note that if there is no hierarchical 
parent, then the parent
                                // will be empty.
                                $parent = self::getParent( $hierarchyRows, 
$row, $i );
-                               return $parent;
+                               array_push( $parents, $parent);
+                               //return $parent;
                        }
                }
 
-               return '';
+               return $parents;
        }
 
        /**
@@ -345,6 +370,38 @@
        }
 
        /**
+        * Return all the rows of a specified depth from within the given 
hierarchy.
+        *
+        * @param string $depth: The depth of the rows which should be returned
+        * @param string $hierarchyPageName: Name of the page that contains the 
hierarchy
+        *  to which $currentPage belongs.
+        * @param string $hierarchyPropertyName: Name of the property on 
$hierarchyPage
+        *  that actually stores the wikitext formatted hierarchy.
+        *
+        * @return array: The list of rows that are at the specified depth 
within
+        *  the given hierarchy. Note that the returned list is a list of 
complete
+        *  rows from the hierarchy and not a list of page names extracted from 
those
+        *  rows.
+        */
+       private function getHierarchyRowsByDepth( $depth, $hierarchyPageName, 
$hierarchyPropertyName ) {
+               $hierarchy = self::getPropertyFromPage( $hierarchyPageName, 
$hierarchyPropertyName );
+               $hierarchyRows = preg_split( '/\n/', $hierarchy );
+
+               $rowsOfDepth = array();
+               $hierarchyRowsSize = count( $hierarchyRows );
+               for ( $i = 0; $i < $hierarchyRowsSize; $i++ ) {
+                       $row = $hierarchyRows[$i];
+                       $rowDepth = self::getDepthOfHierarchyRow( $row );
+
+                       if ( $rowDepth == strlen($depth) ) {
+                               array_push( $rowsOfDepth, $row );
+                       }
+               }
+
+               return $rowsOfDepth;
+       }
+
+       /**
         * Return the wikitext formatted breadcrumb using the given information.
         * 
         * This function gives formatted wikitext for rendering a breadcrumb 
trail
diff --git a/HierarchyBuilder.php b/HierarchyBuilder.php
index d279cc3..c81d0fd 100644
--- a/HierarchyBuilder.php
+++ b/HierarchyBuilder.php
@@ -46,7 +46,7 @@
 $wgExtensionCredits['parserhook'][] = array (
        'path' => __FILE__,
        'name' => 'HierarchyBuilder',
-       'version' => '1.3.1',
+       'version' => '1.4.0',
        'author' => array(
                '[https://www.mediawiki.org/wiki/User:Cindy.cicalese Cindy 
Cicalese]',
                '[https://www.mediawiki.org/wiki/User:Kevin.ji Kevin Ji]'
@@ -288,25 +288,54 @@
                } else {
                        $link = '';
                }
+               // look for the delimiter parameter
+               if ( isset( $optionalParams[HierarchyBuilder::SEPARATOR] ) ) {
+                       $delimiter = 
$optionalParams[HierarchyBuilder::SEPARATOR];
+               } else {
+                       if ( $template != '' ) {
+                               $delimiter = '';
+                       } else {
+                               $delimiter = ',';
+                       }
+               }
 
-               // find the parent
-               $parent = HierarchyBuilder::getPageParent( $pageName, 
$hierarchyPageName,
+               // find the parents
+               $parents = HierarchyBuilder::getPageParent( $pageName, 
$hierarchyPageName,
                        $hierarchyPropertyName );
 
-               // format the parent for return according to the optional arg
+               // format the parents for return according to the optional arg
+               // this code is the same as below for children
                $output = '';
-               if ( $parent != '' ) {
+               if ( count( $parents ) > 0 ) {
                        if ( $template != '' ) {
                                $intro = $introTemplate != '' ? 
"{{{$introTemplate}}}\n" : '';
                                $outro = $outroTemplate != '' ? 
"\n{{{$outroTemplate}}}" : '';
-                               if ( $link == 'none' ) {
-                                       $parent =  "{{" . $template . 
"|$parent}}";
-                               } else {
-                                       $parent =  "{{" . $template . 
"|[[$parent]]}}";
-                               }
-                               $output = $intro . $parent . $outro;
+                               $templateParentString = implode(
+                                       array_map(
+                                               function( $parent ) use ( 
$template, $link ) {
+                                                       if ( $link == 'none' ) {
+                                                               return "{{" . 
$template . "|$parent}}";
+                                                       } else {
+                                                               return "{{" . 
$template . "|[[$parent]]}}";
+                                                       }
+                                               },
+                                               $parents
+                                       ),
+                                       "$delimiter\n"
+                               );
+                               $output = $intro . $templateParentString . 
$outro;
                        } else {
-                               $output = $link == 'none' ? $parent : 
"[[$parent]]";
+                               $parentString = implode(
+                                       array_map(
+                                               function( $parent ) use ( $link 
) {
+                                                       return $link == 'none' 
? $parent : "[[$parent]]";
+                                               },
+                                               $parents
+                                       ),
+                                       $delimiter
+                               );
+
+                               $output = $parentString;
                        }
                }
                $output = $parser->recursiveTagParse( $output );
@@ -414,9 +443,6 @@
                                                        } else {
                                                                return "{{" . 
$template . "|[[$child]]}}";
                                                        }
-                                                       // return $link == 
"none"
-                                                       //      ? "{{" . 
$template . "|$child}}"
-                                                       //      : "{{" . 
$template . "|[[$child]]}}";
                                                },
                                                $children
                                        ),

-- 
To view, visit https://gerrit.wikimedia.org/r/186235
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I1c9c5ef423b3c0785ac5c6d88f19afede867104c
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/HierarchyBuilder
Gerrit-Branch: master
Gerrit-Owner: Kji <[email protected]>
Gerrit-Reviewer: Kji <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to