Kji has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/182820

Change subject: Implemented the new parser function called hierarchySubtree.
......................................................................

Implemented the new parser function called hierarchySubtree.

This parser function is used to retrieve and display the subtree within a
hierarchy that is rooted at a specified row.

Implemented the basics of a subhierarchy parser function.

Change-Id: I121b8cc7dc532da8e8292ccf9cf5638014a46b3b

Implemented the format parameter for the hierarchySubtree parser function.

Change-Id: I7db594e763ba69cb2dbcb4209c6e10620f2d1f15

Updated DOxygen documentation relating to the subhierarchy functionality.

Change-Id: I7f06909a1a547b64930893139ab61ea8075f5a1f

Updated DOxygen documentation regarding the hierarchySubtree parser function to 
include an additional sample invokation.

Change-Id: Ia3733ba3afaee718a226700df2736f5357fd1f04
---
M HierarchyBuilder.class.php
M HierarchyBuilder.i18n.magic.php
M HierarchyBuilder.php
3 files changed, 136 insertions(+), 1 deletion(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/HierarchyBuilder 
refs/changes/20/182820/1

diff --git a/HierarchyBuilder.class.php b/HierarchyBuilder.class.php
index 2abfe86..d2f6641 100644
--- a/HierarchyBuilder.class.php
+++ b/HierarchyBuilder.class.php
@@ -37,6 +37,7 @@
        const INTROTEMPLATE = 'introtemplate';
        const OUTROTEMPLATE = 'outrotemplate';
        const LINK = 'link';
+       const FORMAT = 'format';
 
        /**
         * This function gives the section number for a target page within a
@@ -723,4 +724,78 @@
 
                return $rootAndChildren;
        }
+
+       /**
+        * This function returns the subhierarchy defined by its root node 
within
+        * a specific hierarchy on a given page.
+        *
+        * The pagename and propertyname arguments define the hierarchy from 
which
+        * to extract the subhierarchy. The root argument defines the root node 
of
+        * the subhierarchy within the overall hierarchy to extract.
+        *
+        * @param string $root: The node within the hierarchy which is the root 
of
+        *  the subhierarchy to be returned.
+        * @param string $pagename: The name of the page which contains the 
hierarchy.
+        * @param string $propertyname: The name of the property that contains 
the
+        *  hierarchy data.
+        *
+        * @return string: The depth corrected wikitext representation of the 
+        *  subhierarchy within the overall hierarchy who's root is $root. 
Otherwise,
+        *  if no such subhierarchy exists, the empty string is returned 
instead.
+        */
+       public static function getSubhierarchy( $root, $pagename, $propertyname 
) {
+               $hierarchy = self::getPropertyFromPage( $pagename, 
$propertyname );
+
+               return HierarchyBuilder::getSubhierarchyHelper( $root, 
"[[Hierarchy_Root]]\n" . $hierarchy, '');
+       }
+
+       /**
+        * This is single step look ahead recursive helper for finding a 
subhierarchy
+        * with matching root.
+        *
+        * The root of the wikitextHierarchy passed in is the immediate parent 
of the
+        * subhierarchies which are actually being tested for matching the 
targeted
+        * root. Once we find the correct subhierarchy, we need to update it 
before
+        * it is returned so that the depths are corrected so that it appears 
to be
+        * a standalone hierarchy. That is, if the root of the subhierarchy is 
at a
+        * depth of 4 and it's children are at a depth of 5, we must correct 
those
+        * depths such that the root will have a depth of 1 and the children 
have a
+        * depth of 2. 
+        *
+        * @param string $root: The target root that we are searching for.
+        * @param string $wikitextHierarchy: The string wikitext representation 
of
+        *  the current hierarchy who's immediate subhieraries are being tested.
+        * @param string $depth: The depth of the current hierarchy who's
+        *  subhierarchies are being tested.
+        *
+        * @return string: The depth corrected wikitext representaion of the 
+        *  subhierarchy who's root is $root if such a subhierarchy exists 
within
+        *  the hierarchy $wikitextHierarchy. Otherwise, the empty string is 
returned.
+        */
+       private static function getSubhierarchyHelper( $root, 
$wikitextHierarchy, $depth ) {
+               $curRootAndSubHierarchies = HierarchyBuilder::splitHierarchy( 
$wikitextHierarchy, $depth);
+               $subHierarchies = array_slice( $curRootAndSubHierarchies, 1 );
+
+               foreach ($subHierarchies as $subHierarchy) {
+                       $subHierarchyRows = preg_split( '/\n/', $subHierarchy);
+                       $subHierarchyRoot = 
HierarchyBuilder::getPageNameFromHierarchyRow( $subHierarchyRows[0] );
+                       if ($subHierarchyRoot == $root) {
+                               $subHierarchyRows[0] = str_repeat( '*', strlen( 
$depth ) + 1) . $subHierarchyRows[0]; // put the stars on the root row to start
+                               $result = array_reduce( $subHierarchyRows, 
+                                       function( $carry, $item ) use ( $depth 
) {
+                                               $carry .= "\n" . substr( $item, 
strlen($depth));
+                                               return $carry;
+                                       }
+                               );
+                               return $result;
+                       } else {
+                               $subHierarchyCandidate = 
HierarchyBuilder::getSubhierarchyHelper( $root, $subHierarchy, $depth . '*' );
+                               if ($subHierarchyCandidate != '') {
+                                       return $subHierarchyCandidate;
+                               }
+                       }
+               }
+
+               return '';
+       }
 }
diff --git a/HierarchyBuilder.i18n.magic.php b/HierarchyBuilder.i18n.magic.php
index e7366ba..bb927b7 100644
--- a/HierarchyBuilder.i18n.magic.php
+++ b/HierarchyBuilder.i18n.magic.php
@@ -28,5 +28,6 @@
        'hierarchyBreadcrumb' => array(0, 'hierarchyBreadcrumb'),
        'hierarchySectionNumber' => array(0, 'hierarchySectionNumber'),
        'hierarchyParent' => array(0, 'hierarchyParent'),
-       'hierarchyChildren' => array(0, 'hierarchyChildren')
+       'hierarchyChildren' => array(0, 'hierarchyChildren'),
+       'hierarchySubtree' => array(0, 'hierarchySubtree')
 );
diff --git a/HierarchyBuilder.php b/HierarchyBuilder.php
index 06e483f..da32820 100644
--- a/HierarchyBuilder.php
+++ b/HierarchyBuilder.php
@@ -114,6 +114,8 @@
        $parser->setFunctionHook( 'hierarchySectionNumber', 'sectionNumber' );
        $parser->setFunctionHook( 'hierarchyParent', 'parent' );
        $parser->setFunctionHook( 'hierarchyChildren', 'children' );
+       $parser->setFunctionHook( 'hierarchySubtree', 'subhierarchy' );
+
        $parser->setHook( 'hierarchy', 'renderHierarchy' );
        global $sfgFormPrinter;
        $sfgFormPrinter->registerInputType( 'HierarchyFormInput' );
@@ -122,6 +124,63 @@
 }
 
 /**
+ * This parser function will return the subhierarchy that is rooted at the 
specified
+ * node within a hierarchy.
+ *
+ * The three required arguments are (in order):
+ *   - The root node of the subhierarchy within the overall hierarchy
+ *   - Full page name of the page containing the hierarchy
+ *   - Property name of the property containing the hierarchy data
+ *
+ * The optional argument is:
+ *   - Format to specify if the results should be returned as a bulleted list 
as
+ *     opposed to the default striped format.
+ *
+ * Example invokation:
+ * @code
+ * {{#hierarchySubtree:Hierarchy Builder|Main Page|Hierarchy Data}}
+ * {{#hierarchySubtree:Hierarchy Builder|Main Page|Hierarchy Data|format=ul}}
+ * @endcode
+ *
+ * @param $parser: Parser
+ *
+ * @return string: The string containing the specified subhierarchy as though
+ *  it were a standalone hierarchy.
+ */
+function subhierarchy( $parser ) {
+       // wikiLog('', 'getPageNumbering', "started");
+       $params = func_get_args();
+       if ( count( $params ) < 4 ) {
+               $output = '';
+       } else {
+               $rootNode = $params[1];
+               $hierarchyPageName = $params[2];
+               $hierarchyPropertyName = $params[3];
+
+               $optionalParams = array_slice( $params, 4 );
+               $optionalParams = parseParams( $optionalParams );
+               if ( isset( $optionalParams[HierarchyBuilder::FORMAT] ) ) {
+                       $format = $optionalParams[HierarchyBuilder::FORMAT];
+               }
+
+               $output = HierarchyBuilder::getSubhierarchy(
+                       $rootNode,
+                       $hierarchyPageName,
+                       $hierarchyPropertyName
+               );
+
+               // this is the bullet output
+               if ($format != 'ul') {
+                       $output = "<hierarchy>$output</hierarchy>";
+               }
+               // otherwise it's the default format and we don't modify output.
+
+               $output = $parser->recursiveTagParse( $output );
+       }
+       return $parser->insertStripItem( $output, $parser->mStripState );
+}
+
+/**
  * This parser function will give the section number of a page in a hierarchy.
  *
  * The three required arguments are (in order):

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia3733ba3afaee718a226700df2736f5357fd1f04
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/HierarchyBuilder
Gerrit-Branch: master
Gerrit-Owner: Kji <[email protected]>

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

Reply via email to