Samwilson has uploaded a new change for review.

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

Change subject: Switch to using page properties for storage.
......................................................................

Switch to using page properties for storage.

Change-Id: Ie781804312cd92332220b66539ba5d08e818f0d8
---
M Genealogy.php
M Person.php
M README.md
3 files changed, 156 insertions(+), 77 deletions(-)


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

diff --git a/Genealogy.php b/Genealogy.php
index b85f2e6..5dfaae5 100644
--- a/Genealogy.php
+++ b/Genealogy.php
@@ -72,16 +72,19 @@
        switch ($type) {
                case 'person':
                        $out .= 'b. '.$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]."]]";
+                       //$out .= "[[".$params[0]."]]";
+                       GenealogySaveProp($parser, 'partner', $params[0]);
                        break;
                case 'partners':
                        $person = new GenealogyPerson($parser->getTitle());
@@ -100,6 +103,19 @@
        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.
diff --git a/Person.php b/Person.php
index 42b5647..c8bfb81 100644
--- a/Person.php
+++ b/Person.php
@@ -2,6 +2,7 @@
 
 class GenealogyPerson {
 
+       /** @var Title */
        private $title;
 
        private $parents;
@@ -31,7 +32,7 @@
         * @return boolean
         */
        public function hasDates() {
-               return $this->getBirthDate()!==FALSE;
+               return $this->getBirthDate() !== FALSE;
        }
 
        /**
@@ -41,16 +42,12 @@
         * @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) {
+               $birthDate = $this->getPropSingle('birth date');
+               $time = strtotime($birthDate);
+               if ($time !== FALSE) {
                        return date('j F Y', $time);
                } else {
-                       return $matches[1][0];
+                       return $birthDate;
                }
        }
 
@@ -63,15 +60,7 @@
                if (is_array($this->parents)) {
                        return $this->parents;
                }
-               $this->parents = array();
-               $pattern = 
'/{{\#'.$this->magicRegex.':\s*parent\s*\|\s*([^|}]*)/';
-               preg_match_all($pattern, $this->getText(), $matches);
-               if (isset($matches[1])) {
-                       foreach ($matches[1] as $match) {
-                               $parentTitle = Title::newFromText($match);
-                               
$this->parents[$parentTitle->getPrefixedDBkey()] = new 
GenealogyPerson($parentTitle);
-                       }
-               }
+               $this->parents = $this->getPropMulti('parent');
                return $this->parents;
        }
 
@@ -102,7 +91,11 @@
                if (is_array($this->partners)) {
                        return $this->partners;
                }
-               $this->partners = $this->whatLinksHere('partner');
+               $this->partners = array_merge(
+                       $this->getPropInbound('partner'),
+                       $this->getPropMulti('partner')
+               );
+               //unset($this->partners[$this->title->getPrefixedDBkey()]);
                return $this->partners;
        }
 
@@ -115,75 +108,145 @@
                if (is_array($this->children)) {
                        return $this->children;
                }
-               $this->children = 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) {
-                       $childTitle = Title::makeTitle($row->page_namespace, 
$row->page_title);
-                       $poss_child = new WikiPage($childTitle);
-                       $content = $poss_child->getContent();
-                       $text = ContentHandler::getContentText($content);
-                       $pattern = 
'/{{\#'.$this->magicRegex.':\s*parent\s*\|\s*'.$prefexedTitle.'/';
-                       if(preg_match($pattern, $text)===1) {
-                               $this->children[] = new 
GenealogyPerson($childTitle);
-                       }
-               }
+               $this->children = $this->getPropInbound('parent');
+//             $this->children = array();
+//             $dbr = wfGetDB(DB_SLAVE);
+//             $children = $dbr->select(
+//                     array('pp'=>'page_props', 'p'=>'page'), // tables
+//                     array('pp_value', 'page_title'), // columns
+//                     array( // where conditions
+//                             'pp_value' => $this->title->getPrefixedText(),
+//                             "pp_propname LIKE 'genealogy parent %'",
+//                             'pp_page = page_id',
+//                     ),
+//                     __METHOD__,
+//                     array(),
+//                     array('page'=>array())
+//             );
+//             foreach ($children as $child) {
+//                     $childTitle = Title::newFromText($child->page_title);
+//                     $this->children[$childTitle->getPrefixedDBkey()] = new 
GenealogyPerson($childTitle);
+//             }
+
+//             $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) {
+//                     $childTitle = Title::makeTitle($row->page_namespace, 
$row->page_title);
+//                     $poss_child = new WikiPage($childTitle);
+//                     $content = $poss_child->getContent();
+//                     $text = ContentHandler::getContentText($content);
+//                     $pattern = 
'/{{\#'.$this->magicRegex.':\s*parent\s*\|\s*'.$prefexedTitle.'/';
+//                     if(preg_match($pattern, $text)===1) {
+//                             $this->children[] = new 
GenealogyPerson($childTitle);
+//                     }
+//             }
                return $this->children;
        }
 
-       /**
-        * 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) {
+       private function getPropInbound($type) {
                $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'
+               $results = $dbr->select(
+                       array('pp'=>'page_props', 'p'=>'page'), // tables
+                       array('pp_value', 'page_title'), // columns
+                       array( // where conditions
+                               'pp_value' => $this->title->getPrefixedText(),
+                               "pp_propname LIKE 'genealogy $type %'",
+                               'pp_page = page_id',
                        ),
                        __METHOD__,
                        array(),
-                       array('page' => 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);
-                       }
+               foreach ($results as $res) {
+                       $title = Title::newFromText($res->page_title);
+                       $out[$title->getPrefixedDBkey()] = new 
GenealogyPerson($title);
                }
                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;
+       public function getPropSingle($prop) {
+               $dbr = wfGetDB(DB_SLAVE);
+               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
+                       __METHOD__
+               );
        }
 
+       private function getPropMulti($type) {
+               $out = array();
+               $dbr = wfGetDB(DB_SLAVE);
+               $results = $dbr->select(
+                       'page_props', // table to use
+                       'pp_value', // Field to select
+                       array( // where conditions
+                               'pp_page' => $this->title->getArticleID(),
+                               "pp_propname LIKE 'genealogy $type %'"
+                       ),
+                       __METHOD__
+               );
+               foreach ($results as $result) {
+                       $title = Title::newFromText($result->pp_value);
+                       $out[$title->getPrefixedDBkey()] = new 
GenealogyPerson($title);
+               }
+               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/README.md b/README.md
index 186b6b1..6412e98 100644
--- a/README.md
+++ b/README.md
@@ -13,7 +13,7 @@
   `{{#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:
+3. Define a partner (no output produced; use `partners` to list):
    `{{#genealogy:partner | Page Name Here |start date=Y-m-d |end date=Y-m-d }}`
 4. List all siblings:
    `{{#genealogy:siblings}}`

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie781804312cd92332220b66539ba5d08e818f0d8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Genealogy
Gerrit-Branch: master
Gerrit-Owner: Samwilson <[email protected]>

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

Reply via email to