Samwilson has submitted this change and it was merged.

Change subject: Move core functions to their own class, add Composer support, 
and fix dates and links.
......................................................................


Move core functions to their own class, add Composer support, and fix dates and 
links.

Change-Id: I2b43f32028d76bbaa2a75158553d495588b5e0e7
---
A Core.php
M Genealogy.i18n.php
M Genealogy.php
M Person.php
A composer.json
5 files changed, 190 insertions(+), 151 deletions(-)

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



diff --git a/Core.php b/Core.php
new file mode 100644
index 0000000..ba5b80d
--- /dev/null
+++ b/Core.php
@@ -0,0 +1,110 @@
+<?php
+
+class GenealogyCore {
+
+       static function SetupParserFunction(Parser &$parser) {
+               $parser->setFunctionHook('genealogy', 
'GenealogyCore::RenderParserFunction');
+               return true;
+       }
+
+       /**
+        * Render the output of the parser function.
+        * The input parameters are wikitext with templates expanded.
+        * The output should be wikitext too.
+        *
+        * @param Parser $parser
+        * @param string $type
+        * @param string $param2
+        * @param string $param3
+        * @return string The wikitext with which to replace the parser 
function call.
+        */
+       static function RenderParserFunction(Parser $parser) {
+               $params = array();
+               $args = func_get_args();
+               array_shift($args); // Remove $parser
+               $type = array_shift($args); // Get param 1, the function type
+               foreach ($args as $arg) { // Everything that's left must be 
named
+                       $pair = explode('=', $arg, 2);
+                       if (count($pair) == 2) {
+                               $name = trim($pair[0]);
+                               $value = trim($pair[1]);
+                               $params[$name] = $value;
+                       } else {
+                               $params[] = $arg;
+                       }
+               }
+               $out = ''; //"<pre>".print_r($params, true)."</pre>";
+               switch ($type) {
+                       case 'person':
+                               if (isset($params['birth date'])) {
+                                       $out .= 'b.&nbsp;' . $params['birth 
date'];
+                                       self::SaveProp($parser, 'birth date', 
$params['birth date'], false);
+                               }
+                               if (isset($params['death date'])) {
+                                       $out .= 'd.&nbsp;' . $params['death 
date'];
+                                       self::SaveProp($parser, 'death date', 
$params['death date'], false);
+                               }
+                               break;
+                       case 'parent':
+                               $parentTitle = Title::newFromText($params[0]);
+                               if ($parentTitle->exists()) {
+                                       $person = new 
GenealogyPerson($parentTitle);
+                                       $out .= $person->getWikiLink();
+                               } else {
+                                       $out .= "[[" . $params[0] . "]]";
+                               }
+                               self::SaveProp($parser, 'parent', $params[0]);
+                               break;
+                       case 'siblings':
+                               $person = new 
GenealogyPerson($parser->getTitle());
+                               $out .= 
self::PeopleList($person->getSiblings());
+                               break;
+                       case 'partner':
+                               //$out .= "[[".$params[0]."]]";
+                               self::SaveProp($parser, 'partner', $params[0]);
+                               break;
+                       case 'partners':
+                               $person = new 
GenealogyPerson($parser->getTitle());
+                               $out .= 
self::PeopleList($person->getPartners());
+                               break;
+                       case 'children':
+                               $person = new 
GenealogyPerson($parser->getTitle());
+                               $out .= 
self::PeopleList($person->getChildren());
+                               break;
+                       default:
+                               $out .= '<span class="error">'
+                                       . 'Genealogy parser function type not 
recognised: "' . $type . '".'
+                                       . '</span>';
+                               break;
+               }
+               return $out;
+       }
+
+       static function SaveProp($parser, $prop, $val, $multi = true) {
+               if ($multi) {
+                       $propNum = 1;
+                       while ($par = 
$parser->getOutput()->getProperty("genealogy $prop $propNum")
+                                       and $par != $val) {
+                               $propNum++;
+                       }
+                       $parser->getOutput()->setProperty("genealogy $prop 
$propNum", $val);
+               } else {
+                       $parser->getOutput()->setProperty("genealogy $prop", 
$val);
+               }
+       }
+
+       /**
+        * Get a wikitext list of people.
+        * @todo Replace with a proper templating system.
+        * @param array|GenealogyPerson $people
+        * @return string
+        */
+       static function PeopleList($people) {
+               $out = '';
+               foreach ($people as $person) {
+                       $out .= "* " . $person->getWikiLink() . "\n";
+               }
+               return $out;
+       }
+
+}
diff --git a/Genealogy.i18n.php b/Genealogy.i18n.php
index ce9bf78..07784f9 100644
--- a/Genealogy.i18n.php
+++ b/Genealogy.i18n.php
@@ -14,6 +14,5 @@
  */
 $messages['en'] = array(
        'genealogy' => "Genealogy",
-       'genealogy-desc' => "Adds a parser function for reducing redundancy '"
-                                       ."when linking between genealogical 
records",
+       'genealogy-desc' => "Adds a parser function for easier linking between 
genealogical records",
 );
diff --git a/Genealogy.php b/Genealogy.php
index 5dfaae5..49e8308 100644
--- a/Genealogy.php
+++ b/Genealogy.php
@@ -6,6 +6,15 @@
 if (!defined('MEDIAWIKI')) die(1);
 
 /**
+ * Explicit global declarations, for when this is autoloaded by Composer
+ */
+global $wgExtensionCredits,
+               $wgExtensionMessagesFiles,
+               $wgAutoloadClasses,
+               $wgSpecialPages,
+               $wgHooks;
+
+/**
  * Extension metadata
  */
 $wgExtensionCredits['parserhook'][] = array(
@@ -30,103 +39,10 @@
 $wgAutoloadClasses['Genealogy'] = __FILE__;
 $wgAutoloadClasses['GenealogyPerson'] = __DIR__ . '/Person.php';
 $wgAutoloadClasses['GenealogySpecial'] = __DIR__ . '/Special.php';
+$wgAutoloadClasses['GenealogyCore'] = __DIR__ . '/Core.php';
 $wgSpecialPages['Genealogy'] = 'GenealogySpecial';
 
 /**
  * Parser function
  */
-$wgHooks['ParserFirstCallInit'][] = 'GenealogySetupParserFunction';
-
-function GenealogySetupParserFunction(Parser &$parser) {
-       $parser->setFunctionHook('genealogy', 'GenealogyRenderParserFunction');
-       return true;
-}
-
-/**
- * Render the output of the parser function.
- * The input parameters are wikitext with templates expanded.
- * The output should be wikitext too.
- *
- * @param Parser $parser
- * @param string $type
- * @param string $param2
- * @param string $param3
- * @return string The wikitext with which to replace the parser function call.
- */
-function GenealogyRenderParserFunction(Parser $parser) {
-       $params = array();
-       $args = func_get_args();
-       array_shift($args); // Remove $parser
-       $type = array_shift($args); // Get param 1, the function type
-       foreach ( $args as $arg ) { // Everything that's left must be named
-               $pair = explode('=', $arg, 2);
-               if ( count( $pair ) == 2 ) {
-                       $name = trim($pair[0]);
-                       $value = trim($pair[1]);
-                       $params[$name] = $value;
-               } else {
-                       $params[] = $arg;
-               }
-       }
-       $out = ''; //"<pre>".print_r($params, true)."</pre>";
-       switch ($type) {
-               case 'person':
-                       $out .= 'b.&nbsp;'.$params['birth date'];
-                       GenealogySaveProp($parser, 'birth date', $params['birth 
date'], FALSE);
-                       break;
-               case 'parent':
-                       $out .= "[[".$params[0]."]]";
-                       GenealogySaveProp($parser, 'parent', $params[0]);
-                       break;
-               case 'siblings':
-                       $person = new GenealogyPerson($parser->getTitle());
-                       $out .= GenealogyPeopleList($person->getSiblings());
-                       break;
-               case 'partner':
-                       //$out .= "[[".$params[0]."]]";
-                       GenealogySaveProp($parser, 'partner', $params[0]);
-                       break;
-               case 'partners':
-                       $person = new GenealogyPerson($parser->getTitle());
-                       $out .= GenealogyPeopleList($person->getPartners());
-                       break;
-               case 'children':
-                       $person = new GenealogyPerson($parser->getTitle());
-                       $out .= GenealogyPeopleList($person->getChildren());
-                       break;
-               default:
-                       $out .= '<span class="error">'
-                               .'Genealogy parser function type not 
recognised: "'.$type.'".'
-                               .'</span>';
-                       break;
-       }
-       return $out;
-}
-
-function GenealogySaveProp($parser, $prop, $val, $multi = TRUE) {
-       if ($multi) {
-               $propNum = 1;
-               while ($par = $parser->getOutput()->getProperty("genealogy 
$prop $propNum")
-                               AND $par != $val) {
-                       $propNum++;
-               }
-               $parser->getOutput()->setProperty("genealogy $prop $propNum", 
$val);
-       } else {
-               $parser->getOutput()->setProperty("genealogy $prop", $val);
-       }
-}
-
-/**
- * Get a wikitext list of people.
- * @todo Replace with a proper templating system.
- * @param array|GenealogyPerson $people
- * @return string
- */
-function GenealogyPeopleList($people) {
-       $out = '';
-       foreach ($people as $person) {
-               $date = ($person->hasDates()) ? " 
(".$person->getBirthDate().")" : "";
-               $out .= "* 
[[".$person->getTitle()->getPrefixedText()."]]$date\n";
-       }
-       return $out;
-}
+$wgHooks['ParserFirstCallInit'][] = 'GenealogyCore::SetupParserFunction';
diff --git a/Person.php b/Person.php
index c8bfb81..33ebabe 100644
--- a/Person.php
+++ b/Person.php
@@ -27,27 +27,55 @@
                return $this->title;
        }
 
+       public function getWikiLink() {
+               $birthYear = $this->getBirthDate('Y');
+               $deathYear = $this->getDeathDate('Y');
+               $date = ($this->hasDates()) ? " ($birthYear&ndash;$deathYear)" 
: "";
+               return "[[" . $this->getTitle()->getPrefixedText() . "]]$date";
+       }
+
        /**
         * Whether or not this person has a birth or death date.
         * @return boolean
         */
        public function hasDates() {
-               return $this->getBirthDate() !== FALSE;
+               return $this->getBirthDate() !== false;
        }
 
        /**
-        * Get the birth date of this person, or false if it's not defined. If 
strtotime recognises the
-        * format, the date will be converted to the standard wiki date format; 
if it doesn't, the
-        * value defined in the page will be returned.
+        * Get the birth date of this person. 
+        * @uses GenealogyPerson::getDate()
         * @return string
         */
-       public function getBirthDate() {
-               $birthDate = $this->getPropSingle('birth date');
-               $time = strtotime($birthDate);
-               if ($time !== FALSE) {
-                       return date('j F Y', $time);
+       public function getBirthDate($format = 'j F Y') {
+               return $this->getDate('birth', $format);
+       }
+
+       /**
+        * Get the death date of this person.
+        * @uses GenealogyPerson::getDate()
+        * @return string
+        */
+       public function getDeathDate($format = 'j F Y') {
+               return $this->getDate('death', $format);
+       }
+
+       /**
+        * Get birth or death date.
+        *
+        * If strtotime recognises the format, the date will be converted to 
the standard wiki date
+        * format; if it doesn't, the value defined in the page will be 
returned.
+        *
+        * @param string $type Either 'birth' or 'death'.
+        * @return string
+        */
+       public function getDate($type, $format) {
+               $date = $this->getPropSingle("$type date");
+               $time = strtotime($date);
+               if ($time !== false) {
+                       return date($format, $time);
                } else {
-                       return $birthDate;
+                       return $date;
                }
        }
 
@@ -182,7 +210,10 @@
                return $dbr->selectField(
                        'page_props', // table to use
                        'pp_value', // Field to select
-                       array( 'pp_page' => $this->title->getArticleID(), 
'pp_propname' => "genealogy $prop" ), // where conditions
+                       array( // where conditions
+                               'pp_page' => $this->title->getArticleID(),
+                               'pp_propname' => "genealogy $prop"
+                       ),
                        __METHOD__
                );
        }
@@ -205,48 +236,5 @@
                }
                return $out;
        }
-
-//     /**
-//      * Get an array of GenealogyPerson objects built from pages that link 
to this one with the
-//      * relationship of $as.
-//      * @param string $as The relationship type, either 'parent' or 
'partner'.
-//      */
-//     private function whatLinksHere($as) {
-//             $out = array();
-//             $prefexedTitle = $this->title->getPrefixedDBkey();
-//             $dbr = wfGetDB(DB_SLAVE);
-//             $res = $dbr->select(
-//                     array('pl' => 'pagelinks', 'p' => 'page'),
-//                     array('page_namespace', 'page_title'), // columns
-//                     array(// conditions
-//                             'pl_title' => $prefexedTitle,
-//                             'pl_from = page_id',
-//                             'pl_namespace = page_namespace'
-//                     ),
-//                     __METHOD__,
-//                     array(),
-//                     array('page' => array())
-//             );
-//             foreach ($res as $row) {
-//                     $linkTitle = Title::makeTitle($row->page_namespace, 
$row->page_title);
-//                     $possibleLink = new WikiPage($linkTitle);
-//                     $text = 
ContentHandler::getContentText($possibleLink->getContent());
-//                     $pattern = 
'/{{\#'.$this->magicRegex.':\s*'.$as.'\s*\|\s*'.$prefexedTitle.'/';
-//                     if(preg_match($pattern, $text)===1) {
-//                             $out[$linkTitle->getPrefixedDBkey()] = new 
GenealogyPerson($linkTitle);
-//                     }
-//             }
-//             return $out;
-//     }
-//
-//     /**
-//      * Get the text of this page.
-//      * @return string
-//      */
-//     private function getText() {
-//             $selfPage = new WikiPage($this->title);
-//             $text = ContentHandler::getContentText($selfPage->getContent());
-//             return $text;
-//     }
 
 }
diff --git a/composer.json b/composer.json
new file mode 100644
index 0000000..8eab5b5
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,26 @@
+{
+       "name": "samwilson/genealogy",
+       "type": "mediawiki-extension",
+       "license": "GPL-3.0+",
+       "keywords": ["genealogy", "family history", "MediaWiki"],
+       "authors": [
+               {
+                       "name": "Sam Wilson",
+                       "email": "s...@samwilson.id.au",
+                       "homepage": "http://samwilson.id.au";,
+                       "role": "developer"
+               }
+       ],
+       "support": {
+               "issues": "https://github.com/samwilson/Genealogy/issues";,
+               "irc": "irc://irc.freenode.net/mediawiki",
+               "source": "https://github.com/samwilson/Genealogy";
+       },
+       "require": {
+       },
+       "autoload": {
+               "files" : [
+                       "Genealogy.php"
+               ]
+       }
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I2b43f32028d76bbaa2a75158553d495588b5e0e7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Genealogy
Gerrit-Branch: master
Gerrit-Owner: Samwilson <s...@samwilson.id.au>
Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com>
Gerrit-Reviewer: Samwilson <s...@samwilson.id.au>
Gerrit-Reviewer: Siebrand <siebr...@kitano.nl>

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

Reply via email to