Wilkins has uploaded a new change for review. https://gerrit.wikimedia.org/r/245904
Change subject: Creating <familytree> tag to insert the tree into pages ...................................................................... Creating <familytree> tag to insert the tree into pages Change-Id: I20684accceddcd96b6c6754d4348fba66c1d8f00 --- A FamilyTree.php A FamilyTreeTag.php M SemanticGenealogy.php M SpecialFamilyTree.php 4 files changed, 314 insertions(+), 206 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticGenealogy refs/changes/04/245904/1 diff --git a/FamilyTree.php b/FamilyTree.php new file mode 100644 index 0000000..63ebb72 --- /dev/null +++ b/FamilyTree.php @@ -0,0 +1,234 @@ +<?php + + +/** + * Special page that show a family tree + * + * @file SpecialFamilyTree.php + * @ingroup SemanticGenealogy + * + * @licence GNU GPL v2+ + * @author Thomas Pellissier Tanon < thoma...@hotmail.fr > + */ +class FamilyTree +{ + + private $output; + + + public function setOutput($output) + { + $this->output = $output; + } + + public function getOutput() + { + return $this->output; + } + + /** + * Return the number of people in a generation + * + * @param int $gen The number of the generation (beginning at 0) + * @return int + */ + public static function getNumOfPeopleInGen( $gen ) { + $result = 1; + for($i = 0; $i < $gen; $i++ ) { + $result *= 2; + } + return $result; + } + + public function getAncestors( SMWDIWikiPage $page, $numOfGenerations ) { + $tree = array(); + $tree[0][1] = new PersonPageValues( $page ); + + for($i = 0; $i < $numOfGenerations && $tree[$i] !== null; $i++ ) { + $tree = $this->addGenInTree( $i + 1, $tree ); + } + return $tree; + } + + public function addGenInTree( $gen, array $tree ) { + $empty = true; + $son = self::getNumOfPeopleInGen( $gen - 1 ); + $end = $son * 4; + for( $parent = $son * 2; $parent < $end; true ) { + if( isset( $tree[$gen - 1][$son] ) ) { + $father = $tree[$gen - 1][$son]->father; + if( $father instanceof SMWDIWikiPage ) { + $tree[$gen][$parent] = new PersonPageValues( $father ); + $empty = false; + } else { + $tree[$gen][$parent] = null; + } + $parent++; + + $mother = $tree[$gen - 1][$son]->mother; + if( $mother instanceof SMWDIWikiPage ) { + $tree[$gen][$parent] = new PersonPageValues( $mother ); + $empty = false; + } else { + $tree[$gen][$parent] = null; + } + $parent++; + } else { + $parent += 2; + } + $son++; + } + //Verif s'il n'y a personne dans la génération + if($empty) { + $tree[$gen] = null; + } + return $tree; + } + + public function outputAncestorsTree( array $tree, $numOfGenerations ) { + $output = $this->getOutput(); + $output->addHTML('<table style="text-align:center;">'); + $col = 1; + for( $i = $numOfGenerations - 1; $i >= 0; $i-- ) { + if( isset( $tree[$i] ) ) { + $output->addHTML('<tr>'); + foreach ($tree[$i] as $sosa => $person) { + $output->addHTML('<td colspan="' . $col . '">'); + if($person !== null) { + $output->addHTML($sosa . '<br/>' ); + $output->addWikiText( $person->getDescriptionWikiText( true ) ); + if($sosa != 1) { + if($sosa % 2 == 0) + $output->addHTML( '<br/>\\' ); + else + $output->addHTML( '<br/>/' ); + } + } + $output->addHTML('</td>'); + } + $output->addHTML('</tr>'); + } + $col *= 2; + } + $output->addHTML('</table>'); + } + + public function outputDescendantList( SMWDIWikiPage $main, $numOfGenerations ) { + $output = $this->getOutput(); + $main = new PersonPageValues( $main ); + $output->addWikiText( $main->getDescriptionWikiText( false ) ); + $this->outputDescendantLine( $main, '', $numOfGenerations ); + } + + public function outputDescendantLine( $person, $pellissier, $end ) { + $output = $this->getOutput(); + $children = $person->getChildren(); + $i = 1; + foreach($children as $child) { + $pel = $pellissier . $i . '.'; + $output->addWikiText( $pel . ' ' . $child->getDescriptionWikiText( false ) ); + if( $end > 0 ) + $this->outputDescendantLine( $child, $pel, $end - 1); + $i++; + } + } + + public function getRelation( SMWDIWikiPage $page1, SMWDIWikiPage $page2 ) { + $tree1 = array(); + $tree2 = array(); + $tree1[0][1] = new PersonPageValues( $page1 ); + $tree2[0][1] = new PersonPageValues( $page2 ); + + for($i = 0; $tree1[$i] !== null && $tree2[$i] !== null; $i++ ) { + $tree1 = $this->addGenInTree( $i + 1, $tree1 ); + if($tree1[$i + 1] !== null) { + $result = $this->compareGenWith($tree1[$i + 1], $tree2, $i ); + if($result !== null) { + list($sosa1, $sosa2) = $result; + break; + } + } + + $tree2 = $this->addGenInTree( $i + 1, $tree2 ); + if($tree2[$i + 1] !== null) { + $result = $this->compareGenWith($tree2[$i + 1], $tree1, $i + 1 ); + if($result !== null) { + list($sosa2, $sosa1) = $result; + break; + } + } + } + + if($result !== null) + return array( $this->getListOfAncestors( $sosa1, $tree1 ), $this->getListOfAncestors( $sosa2, $tree2 ) ); + } + + public function compareGenWith( array $gen, array $tree, $max ) { + for( $i = $max; $i >= 0; $i-- ) { + if( isset( $tree[$i] ) ) { + foreach( $tree[$i] as $sosa2 => $person2 ) { + if($person2 !== null) { + foreach( $gen as $sosa1 => $person1 ) { + if($person1 !== null) { + if( $person1->title->equals( $person2->title ) ) { + return array( $sosa1, $sosa2 ); + } + } + } + } + } + } + } + } + + public function getListOfAncestors( $sosa, array $tree ) { + $num = 1; + $temp = 1; + for($i = 0; $num < $sosa; $i++) { + $temp *= 2; + $num += $temp; + } + + $list = array(); + for( $j = $i; $j >= 0; $j-- ) { + $list[] = $tree[$j][$sosa]; + $sosa /= 2; + } + return $list; + } + + public function outputRelationTree( array $trees ) { + $output = $this->getOutput(); + list( $tree1, $tree2 ) = $trees; + + $output->addHTML( '<table style="text-align:center;">' ); + $output->addHTML( '<tr><td colspan="2">' ); + $person = $tree1[0]; + if( $person->fullname instanceof SMWDIBlob ) + $output->addWikiText( $person->getDescriptionWikiText( false ) ); + $output->addHTML( '</td></tr>' ); + + $length = max( count( $tree1 ), count( $tree2 ) ); + for($i = 1; $i < $length; $i++ ) { + $output->addHTML('<tr><td>'); + if( isset( $tree1[$i] ) ) { + $person = $tree1[$i]; + if( $person->fullname instanceof SMWDIBlob ) + $output->addWikiText( '|<br/>' . $person->getDescriptionWikiText( false ) ); + } + $output->addHTML('</td><td>'); + if( isset( $tree2[$i] ) ) { + $person = $tree2[$i]; + if( $person->fullname instanceof SMWDIBlob ) + $output->addWikiText( '|<br/>' . $person->getDescriptionWikiText( false ) ); + } + $output->addHTML('</td></tr>'); + } + $output->addHTML('</table>'); + } + + + + + +} diff --git a/FamilyTreeTag.php b/FamilyTreeTag.php new file mode 100644 index 0000000..2c7dc41 --- /dev/null +++ b/FamilyTreeTag.php @@ -0,0 +1,66 @@ +<?php + + +class FamilyTreeTag +{ + + const TAG = "familytree"; + const ATTR_PERSON = "person"; + const ATTR_PERSON2 = "person2"; + const ATTR_DEPTH = "depth"; + const ATTR_TYPE = "type"; + const ATTR_STYLE = "style"; + + // Hook our callback function into the parser + public static function wfFamilytreeParserInit (Parser $parser) + { + // When the parser sees the <familytree> tag, it executes + // the wfFamilytreeRender function (see below) + $parser->setHook( 'familytree', array('FamilyTreeTag', 'wfFamilytreeRender') ); + return true; + } + + // Execute + public static function wfFamilytreeRender ($input, array $args, Parser $parser, PPFrame $frame) + { + $output = new OutputPage; + $familytree = new FamilyTree(); + $familytree->setOutput($output); + $numOfGenerations = $args[self::ATTR_DEPTH]; + $pageName = $args[self::ATTR_PERSON]; + $pageTitle = Title::newFromText( $pageName ); + $page = SMWDIWikiPage::newFromTitle( $pageTitle ); + + + switch($args[self::ATTR_TYPE]) { + case 'ancestors': + $tree = $familytree->getAncestors( $page, $numOfGenerations ); + $familytree->outputAncestorsTree( $tree, $numOfGenerations ); + break; + case 'descendant': + $familytree->outputDescendantList( $page, $numOfGenerations ); + break; + case 'link': + if($pageName2 == '') { + $output->addWikiText( '<span class="error">' . wfMsg( 'semanticgenealogy-specialfamilytree-error-nosecondpagename' ) . '</span>' ); + return; + } + $pageTitle2 = Title::newFromText( $pageName2 ); + $page2 = SMWDIWikiPage::newFromTitle( $pageTitle2 ); + $tree = $familytree->getRelation( $page, $page2 ); + if( $tree !== null) { + $familytree->outputRelationTree( $tree ); + } else { + $output->addWikiText( '<span class="error">' . wfMsg( 'semanticgenealogy-specialfamilytree-error-nolinkfound', $pageName, $pageName2 ) . '</span>' ); + } + break; + default: + $output->addWikiText( '<span class="error">' . wfMsg( 'semanticgenealogy-specialfamilytree-error-unknowntype', $type ) . '</span>' ); + } + + $output->setArticleBodyOnly(true); + ob_start(); + $output->output(); + return ob_get_clean(); + } +} diff --git a/SemanticGenealogy.php b/SemanticGenealogy.php index 874616c..f30f429 100644 --- a/SemanticGenealogy.php +++ b/SemanticGenealogy.php @@ -70,6 +70,8 @@ $wgAutoloadClasses['SemanticGenealogy'] = $dir . 'SemanticGenealogy.body.php'; $wgAutoloadClasses['PersonPageValues'] = $dir . 'PersonPageValues.php'; +$wgAutoloadClasses['FamilyTreeTag'] = $dir . 'FamilyTreeTag.php'; +$wgAutoloadClasses['FamilyTree'] = $dir . 'FamilyTree.php'; $wgAutoloadClasses['GenealogicalFilePrinter'] = $dir . 'GenealogicalFilePrinter.php'; $wgAutoloadClasses['Gedcom5FilePrinter'] = $dir . 'GenealogicalFilePrinter.php'; @@ -92,3 +94,6 @@ 'messages' => array( ) ); + + +$wgHooks['ParserFirstCallInit'][] = 'FamilyTreeTag::wfFamilytreeParserInit'; diff --git a/SpecialFamilyTree.php b/SpecialFamilyTree.php index 0b5367c..1fb3019 100644 --- a/SpecialFamilyTree.php +++ b/SpecialFamilyTree.php @@ -100,17 +100,20 @@ return; $pageTitle = Title::newFromText( $pageName ); - $page = SMWDIWikiPage::newFromTitle( $pageTitle ); + $page = SMWDIWikiPage::newFromTitle( $pageTitle ); + + $familytree = new FamilyTree(); + $familytree->setOutput($this->getOutput()); if($type == '') $type = 'ancestors'; switch($type) { case 'ancestors': - $tree = $this->getAncestors( $page, $numOfGenerations ); - $this->outputAncestorsTree( $tree, $numOfGenerations ); + $tree = $familytree->getAncestors( $page, $numOfGenerations ); + $familytree->outputAncestorsTree( $tree, $numOfGenerations ); break; case 'descendant': - $this->outputDescendantList( $page, $numOfGenerations ); + $familytree->outputDescendantList( $page, $numOfGenerations ); break; case 'link': if($pageName2 == '') { @@ -119,9 +122,9 @@ } $pageTitle2 = Title::newFromText( $pageName2 ); $page2 = SMWDIWikiPage::newFromTitle( $pageTitle2 ); - $tree = $this->getRelation( $page, $page2 ); + $tree = $familytree->getRelation( $page, $page2 ); if( $tree !== null) { - $this->outputRelationTree( $tree ); + $familytree->outputRelationTree( $tree ); } else { $output->addWikiText( '<span class="error">' . wfMsg( 'semanticgenealogy-specialfamilytree-error-nolinkfound', $pageName, $pageName2 ) . '</span>' ); } @@ -140,206 +143,6 @@ return wfMsg( 'semanticgenealogy-specialfamilytree-title' ); } - /** - * Return the number of people in a generation - * - * @param int $gen The number of the generation (beginning at 0) - * @return int - */ - protected static function getNumOfPeopleInGen( $gen ) { - $result = 1; - for($i = 0; $i < $gen; $i++ ) { - $result *= 2; - } - return $result; - } - - protected function getAncestors( SMWDIWikiPage $page, $numOfGenerations ) { - $tree = array(); - $tree[0][1] = new PersonPageValues( $page ); - - for($i = 0; $i < $numOfGenerations && $tree[$i] !== null; $i++ ) { - $tree = $this->addGenInTree( $i + 1, $tree ); - } - return $tree; - } - - protected function addGenInTree( $gen, array $tree ) { - $empty = true; - $son = self::getNumOfPeopleInGen( $gen - 1 ); - $end = $son * 4; - for( $parent = $son * 2; $parent < $end; true ) { - if( isset( $tree[$gen - 1][$son] ) ) { - $father = $tree[$gen - 1][$son]->father; - if( $father instanceof SMWDIWikiPage ) { - $tree[$gen][$parent] = new PersonPageValues( $father ); - $empty = false; - } else { - $tree[$gen][$parent] = null; - } - $parent++; - - $mother = $tree[$gen - 1][$son]->mother; - if( $mother instanceof SMWDIWikiPage ) { - $tree[$gen][$parent] = new PersonPageValues( $mother ); - $empty = false; - } else { - $tree[$gen][$parent] = null; - } - $parent++; - } else { - $parent += 2; - } - $son++; - } - //Verif s'il n'y a personne dans la génération - if($empty) { - $tree[$gen] = null; - } - return $tree; - } - - protected function outputAncestorsTree( array $tree, $numOfGenerations ) { - $output = $this->getOutput(); - $output->addHTML('<table style="text-align:center;">'); - $col = 1; - for( $i = $numOfGenerations - 1; $i >= 0; $i-- ) { - if( isset( $tree[$i] ) ) { - $output->addHTML('<tr>'); - foreach ($tree[$i] as $sosa => $person) { - $output->addHTML('<td colspan="' . $col . '">'); - if($person !== null) { - $output->addHTML($sosa . '<br/>' ); - $output->addWikiText( $person->getDescriptionWikiText( true ) ); - if($sosa != 1) { - if($sosa % 2 == 0) - $output->addHTML( '<br/>\\' ); - else - $output->addHTML( '<br/>/' ); - } - } - $output->addHTML('</td>'); - } - $output->addHTML('</tr>'); - } - $col *= 2; - } - $output->addHTML('</table>'); - } - - protected function outputDescendantList( SMWDIWikiPage $main, $numOfGenerations ) { - $output = $this->getOutput(); - $main = new PersonPageValues( $main ); - $output->addWikiText( $main->getDescriptionWikiText( false ) ); - $this->outputDescendantLine( $main, '', $numOfGenerations ); - } - - protected function outputDescendantLine( $person, $pellissier, $end ) { - $output = $this->getOutput(); - $children = $person->getChildren(); - $i = 1; - foreach($children as $child) { - $pel = $pellissier . $i . '.'; - $output->addWikiText( $pel . ' ' . $child->getDescriptionWikiText( false ) ); - if( $end > 0 ) - $this->outputDescendantLine( $child, $pel, $end - 1); - $i++; - } - } - - protected function getRelation( SMWDIWikiPage $page1, SMWDIWikiPage $page2 ) { - $tree1 = array(); - $tree2 = array(); - $tree1[0][1] = new PersonPageValues( $page1 ); - $tree2[0][1] = new PersonPageValues( $page2 ); - - for($i = 0; $tree1[$i] !== null && $tree2[$i] !== null; $i++ ) { - $tree1 = $this->addGenInTree( $i + 1, $tree1 ); - if($tree1[$i + 1] !== null) { - $result = $this->compareGenWith($tree1[$i + 1], $tree2, $i ); - if($result !== null) { - list($sosa1, $sosa2) = $result; - break; - } - } - - $tree2 = $this->addGenInTree( $i + 1, $tree2 ); - if($tree2[$i + 1] !== null) { - $result = $this->compareGenWith($tree2[$i + 1], $tree1, $i + 1 ); - if($result !== null) { - list($sosa2, $sosa1) = $result; - break; - } - } - } - - if($result !== null) - return array( $this->getListOfAncestors( $sosa1, $tree1 ), $this->getListOfAncestors( $sosa2, $tree2 ) ); - } - - protected function compareGenWith( array $gen, array $tree, $max ) { - for( $i = $max; $i >= 0; $i-- ) { - if( isset( $tree[$i] ) ) { - foreach( $tree[$i] as $sosa2 => $person2 ) { - if($person2 !== null) { - foreach( $gen as $sosa1 => $person1 ) { - if($person1 !== null) { - if( $person1->title->equals( $person2->title ) ) { - return array( $sosa1, $sosa2 ); - } - } - } - } - } - } - } - } - - protected function getListOfAncestors( $sosa, array $tree ) { - $num = 1; - $temp = 1; - for($i = 0; $num < $sosa; $i++) { - $temp *= 2; - $num += $temp; - } - - $list = array(); - for( $j = $i; $j >= 0; $j-- ) { - $list[] = $tree[$j][$sosa]; - $sosa /= 2; - } - return $list; - } - - protected function outputRelationTree( array $trees ) { - $output = $this->getOutput(); - list( $tree1, $tree2 ) = $trees; - - $output->addHTML( '<table style="text-align:center;">' ); - $output->addHTML( '<tr><td colspan="2">' ); - $person = $tree1[0]; - if( $person->fullname instanceof SMWDIBlob ) - $output->addWikiText( $person->getDescriptionWikiText( false ) ); - $output->addHTML( '</td></tr>' ); - - $length = max( count( $tree1 ), count( $tree2 ) ); - for($i = 1; $i < $length; $i++ ) { - $output->addHTML('<tr><td>'); - if( isset( $tree1[$i] ) ) { - $person = $tree1[$i]; - if( $person->fullname instanceof SMWDIBlob ) - $output->addWikiText( '|<br/>' . $person->getDescriptionWikiText( false ) ); - } - $output->addHTML('</td><td>'); - if( isset( $tree2[$i] ) ) { - $person = $tree2[$i]; - if( $person->fullname instanceof SMWDIBlob ) - $output->addWikiText( '|<br/>' . $person->getDescriptionWikiText( false ) ); - } - $output->addHTML('</td></tr>'); - } - $output->addHTML('</table>'); - } protected function getGroupName() { return 'other'; -- To view, visit https://gerrit.wikimedia.org/r/245904 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I20684accceddcd96b6c6754d4348fba66c1d8f00 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/SemanticGenealogy Gerrit-Branch: master Gerrit-Owner: Wilkins <thiba...@taillandier.name> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits