Samwilson has uploaded a new change for review.

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

Change subject: A start on person  metadata, but it's not working well yet.
......................................................................

A start on person  metadata, but it's not working well yet.

Change-Id: I2062c1a2e215ad2c0c9d0586c626795d93da3a0c
---
M Genealogy.php
M Person.php
M README.md
3 files changed, 89 insertions(+), 23 deletions(-)


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

diff --git a/Genealogy.php b/Genealogy.php
index 2ba4348..b85f2e6 100644
--- a/Genealogy.php
+++ b/Genealogy.php
@@ -53,28 +53,46 @@
  * @param string $param3
  * @return string The wikitext with which to replace the parser function call.
  */
-function GenealogyRenderParserFunction(Parser $parser, $type = '', $one = '', 
$two = '') {
+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'];
+                       break;
                case 'parent':
-                       $out = "[[$one]]";
+                       $out .= "[[".$params[0]."]]";
                        break;
                case 'siblings':
                        $person = new GenealogyPerson($parser->getTitle());
-                       $out = GenealogyPeopleList($person->getSiblings());
+                       $out .= GenealogyPeopleList($person->getSiblings());
                        break;
                case 'partner':
-                       $out = "[[$one]]";
+                       $out .= "[[".$params[0]."]]";
                        break;
                case 'partners':
                        $person = new GenealogyPerson($parser->getTitle());
-                       $out = GenealogyPeopleList($person->getPartners());
+                       $out .= GenealogyPeopleList($person->getPartners());
                        break;
                case 'children':
                        $person = new GenealogyPerson($parser->getTitle());
-                       $out = GenealogyPeopleList($person->getChildren());
+                       $out .= GenealogyPeopleList($person->getChildren());
                        break;
                default:
-                       $out = '<span class="error">'
+                       $out .= '<span class="error">'
                                .'Genealogy parser function type not 
recognised: "'.$type.'".'
                                .'</span>';
                        break;
@@ -91,7 +109,8 @@
 function GenealogyPeopleList($people) {
        $out = '';
        foreach ($people as $person) {
-               $out .= "* [[".$person->getTitle()->getPrefixedText()."]]\n";
+               $date = ($person->hasDates()) ? " 
(".$person->getBirthDate().")" : "";
+               $out .= "* 
[[".$person->getTitle()->getPrefixedText()."]]$date\n";
        }
        return $out;
 }
diff --git a/Person.php b/Person.php
index 37b0bf1..42b5647 100644
--- a/Person.php
+++ b/Person.php
@@ -27,6 +27,34 @@
        }
 
        /**
+        * Whether or not this person has a birth or death date.
+        * @return boolean
+        */
+       public function hasDates() {
+               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.
+        * @return string
+        */
+       public function getBirthDate() {
+               $pattern = '/{{\#'.$this->magicRegex.':\s*person.*birth 
date=([^|}]*)/';
+               preg_match_all($pattern, $this->getText(), $matches);
+               if (!isset($matches[1][0])) {
+                       return FALSE;
+               }
+               $time = strtotime($matches[1][0]);
+               if ($time!==FALSE) {
+                       return date('j F Y', $time);
+               } else {
+                       return $matches[1][0];
+               }
+       }
+
+       /**
         * Get all parents.
         *
         * @return array|GenealogyPerson An array of parents, possibly empty.
@@ -36,10 +64,8 @@
                        return $this->parents;
                }
                $this->parents = array();
-               $selfPage = new WikiPage($this->title);
-               $text = ContentHandler::getContentText($selfPage->getContent());
                $pattern = 
'/{{\#'.$this->magicRegex.':\s*parent\s*\|\s*([^|}]*)/';
-               preg_match_all($pattern, $text, $matches);
+               preg_match_all($pattern, $this->getText(), $matches);
                if (isset($matches[1])) {
                        foreach ($matches[1] as $match) {
                                $parentTitle = Title::newFromText($match);
@@ -149,4 +175,15 @@
                }
                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/README.md b/README.md
index a8a35b4..186b6b1 100644
--- a/README.md
+++ b/README.md
@@ -2,20 +2,30 @@
 
 ## Usage
 
-Define this person's dates.
-{{#genealogy:person |date of birth=Y-m-d }}
+There is only one parser function, `{{#genealogy:}}`.
+Its first two parameters are unnamed (i.e. don't have equals signs), but all
+others must be (dates, etc.).
 
-Define a parent:
-{{#genealogy:parent | Page Name Here }}
+The following functions are supported, three for defining data and three for
+reporting data:
 
-List all siblings:
-{{#genealogy:siblings}}
+1. Define this person's dates.
+  `{{#genealogy:person |birth date=Y-m-d |death date=Y-m-d }}`
+2. Define a parent:
+   `{{#genealogy:parent | Page Name Here }}`
+3. Define a partner:
+   `{{#genealogy:partner | Page Name Here |start date=Y-m-d |end date=Y-m-d }}`
+4. List all siblings:
+   `{{#genealogy:siblings}}`
+5. List all partners:
+   `{{#genealogy:partners}}`
+6. List all children:
+   `{{#genealogy:children}}`
 
-Define a partner:
-{{#genealogy:partner | Page Name Here |start date=Y-m-d |end date=Y-m-d }}
+## Development
 
-List all partners:
-{{#genealogy:partners}}
+The *Genealogy* extension is developed by Sam Wilson and released under version
+3 of the GPL (see `LICENSE.txt` for details).
 
-List all children:
-{{#genealogy:children}}
+Please report all bugs via the GitHub issue tracker at
+https://github.com/samwilson/Genealogy/issues

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2062c1a2e215ad2c0c9d0586c626795d93da3a0c
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