Samwilson has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/398622 )

Change subject: Move graph-creating and checking to the Tree class
......................................................................

Move graph-creating and checking to the Tree class

Also give a better error when GraphViz isn't installed.

Change-Id: I8289afe5d73a77e2ec92ff64ec8d5b1604b1861e
---
M i18n/en.json
M i18n/qqq.json
M src/Hooks.php
M src/Tree.php
4 files changed, 46 insertions(+), 14 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Genealogy 
refs/changes/22/398622/1

diff --git a/i18n/en.json b/i18n/en.json
index d1a9141..9e9e3d1 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -18,5 +18,6 @@
        "genealogy-parser-function-not-found": "Genealogy parser function type 
not recognised: $1",
        "genealogy-existing-partners": "This person already has the following 
{{PLURAL:$1|partner|partners}}: ",
        "genealogy-invalid-parent-title": "Error: invalid parent page title: 
'$1'",
-       "genealogy-invalid-partner-title": "Error: invalid partner page title: 
'$1'"
+       "genealogy-invalid-partner-title": "Error: invalid partner page title: 
'$1'",
+       "genealogy-no-graphviz": "Graphic tree generation requires the GraphViz 
extension to be installed"
 }
diff --git a/i18n/qqq.json b/i18n/qqq.json
index a8783e8..9ae4f09 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -20,5 +20,6 @@
        "genealogy-parser-function-not-found": "The error message shown when 
the parser function is passed something it doesn't recognise.",
        "genealogy-existing-partners": "The message that is shown above the 
page-editing form, alerting the user to the fact that the person they're 
editing has already got a defined partner from another page.",
        "genealogy-invalid-parent-title": "The error message that is shown when 
a user-supplied parent name is not a valid page title.",
-       "genealogy-invalid-partner-title": "The error message that is shown 
when a user-supplied partner name is not a valid page title."
+       "genealogy-invalid-partner-title": "The error message that is shown 
when a user-supplied partner name is not a valid page title.",
+       "genealogy-no-graphviz": "An error message displayed when the GraphViz 
extension isn't installed."
 }
diff --git a/src/Hooks.php b/src/Hooks.php
index 1e2c3f3..83e8d68 100644
--- a/src/Hooks.php
+++ b/src/Hooks.php
@@ -131,11 +131,7 @@
                                if ( isset( $params['descendant depth'] ) ) {
                                        $tree->setDescendantDepth( 
$params['descendant depth'] );
                                }
-                               if ( $tree->hasAncestorsOrDescendants() ) {
-                                       $graphviz = $tree->getGraphviz();
-                                       $out .= $parser->recursiveTagParse( 
"<graphviz>\n$graphviz\n</graphviz>" );
-                                       // $out .= $parser->recursiveTagParse( 
"<pre>$graphviz</pre>" );
-                               }
+                               $out = $tree->getWikitext( $parser );
                                break;
                        default:
                                $msg = wfMessage( 
'genealogy-parser-function-not-found', [ $type ] );
diff --git a/src/Tree.php b/src/Tree.php
index e8316f4..0a0cc73 100644
--- a/src/Tree.php
+++ b/src/Tree.php
@@ -2,6 +2,8 @@
 
 namespace MediaWiki\Extensions\Genealogy;
 
+use Html;
+use Parser;
 use Title;
 
 class Tree {
@@ -77,15 +79,41 @@
        }
 
        /**
+        * Get the wikitext for the tree, containing the <graphviz> element and 
dot-formatted contents.
+        * @param Parser $parser The parser.
+        * @return string Unsafe half-parsed HTML, as returned by 
Parser::recursiveTagParse().
+        */
+       public function getWikitext( Parser $parser ) {
+               // If there's nothing to render, give up.
+               if ( !$this->hasAncestorsOrDescendants() ) {
+                       return '';
+               }
+
+               // See if GraphViz is installed.
+               if ( !class_exists( '\MediaWiki\Extension\GraphViz\GraphViz' ) 
) {
+                       $err = wfMessage( 'genealogy-no-graphviz' );
+                       return Html::element( 'p', [ 'class' => 'error' ], $err 
);
+               }
+
+               // Make sure the current user (even anonymous IP users) can 
upload the image file.
+               // @todo Move to GraphViz
+               // $parser->getUser()->mRights[] = 'upload';
+
+               // Get the GraphViz source and run it through the GraphViz 
extension.
+               $graphSource = $this->getGraphvizSource();
+               $out = $parser->recursiveTagParse( 
"<graphviz>\n$graphSource\n</graphviz>" );
+
+               // Debugging.
+               // $out .= $parser->recursiveTagParse( 
"<pre>$graphSource</pre>" );
+
+               return $out;
+       }
+
+       /**
         * Get the Dot source code for the graph of this tree.
         * @return string
         */
-       public function getGraphviz() {
-               $treeName = md5( join( '', $this->ancestors ) . join( '', 
$this->descendants ) );
-               $this->out( 'top', 'start', "digraph GenealogyTree_$treeName {" 
);
-               $this->out( 'top', 'graph-attrs', 'graph [rankdir=LR]' );
-               $this->out( 'top', 'edge-attrs', 'edge [arrowhead=none]' );
-
+       public function getGraphvizSource() {
                $traverser = new Traverser();
                $traverser->register( [ $this, 'visit' ] );
 
@@ -102,9 +130,15 @@
                        return '<span class="error">No people found</span>';
                }
 
+               // Start the tree.
+               $treeName = md5( join( '', $this->ancestors ) . join( '', 
$this->descendants ) );
+               $this->out( 'top', 'start', "digraph GenealogyTree_$treeName {" 
);
+               $this->out( 'top', 'graph-attrs', 'graph [rankdir=LR, 
ranksep=0.55]' );
+               $this->out( 'top', 'edge-attrs', 'edge [arrowhead=none, 
headport=w]' );
+               $this->out( 'top', 'node-attrs', 'node [shape=plaintext, 
fontsize=12]' );
+
                // Combine all parts of the graph output.
                $out = join( "\n", $this->graph_source_code['top'] ) . "\n\n"
-                       . "node [ shape=plaintext ]\n"
                        . join( "\n", $this->graph_source_code['person'] ) . 
"\n\n";
                if ( isset( $this->graph_source_code['partner'] ) ) {
                        $out .= join( "\n", $this->graph_source_code['partner'] 
) . "\n\n";

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8289afe5d73a77e2ec92ff64ec8d5b1604b1861e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Genealogy
Gerrit-Branch: master
Gerrit-Owner: Samwilson <s...@samwilson.id.au>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to