Samwilson has uploaded a new change for review. https://gerrit.wikimedia.org/r/321208
Change subject: Different date handling, and a start on some tests. ...................................................................... Different date handling, and a start on some tests. Change-Id: I9bc4c01775b77448539872af7df85d070cd327db --- M Core.php M Genealogy.i18n.php M Genealogy.php M Person.php A tests/phpunit/PersonTest.php 5 files changed, 90 insertions(+), 88 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Genealogy refs/changes/08/321208/1 diff --git a/Core.php b/Core.php index 9ecc40a..cdb039b 100644 --- a/Core.php +++ b/Core.php @@ -2,8 +2,23 @@ class GenealogyCore { - static function SetupParserFunction(Parser &$parser) { + /** + * Hooked to ParserFirstCallInit. + * @param Parser $parser + * @return boolean + */ + static function onParserFirstCallInit(Parser &$parser) { $parser->setFunctionHook('genealogy', 'GenealogyCore::RenderParserFunction'); + return true; + } + + /** + * Hooked to UnitTestsList. + * @param array|String $files + * @return boolean + */ + static function onUnitTestsList(&$files) { + $files = array_merge($files, glob(__DIR__ . '/tests/phpunit/*Test.php')); return true; } @@ -37,11 +52,11 @@ switch ($type) { case 'person': if (isset($params['birth date'])) { - $out .= 'b. ' . $params['birth date']; + $out .= $params['birth date']; self::SaveProp($parser, 'birth date', $params['birth date'], false); } if (isset($params['death date'])) { - $out .= 'd. ' . $params['death date']; + $out .= $params['death date']; self::SaveProp($parser, 'death date', $params['death date'], false); } break; diff --git a/Genealogy.i18n.php b/Genealogy.i18n.php index 44c2ba8..0e9f241 100644 --- a/Genealogy.i18n.php +++ b/Genealogy.i18n.php @@ -15,8 +15,10 @@ $messages['en'] = array( 'genealogy' => "Genealogy", 'genealogy-desc' => "Adds a parser function for easier linking between genealogical records", - 'ancestor' => 'Ancestor', - 'descendant' => 'Descendant', - 'ancestors' => 'Ancestors', - 'descendants' => 'Descendants', + 'genealogy-born' => 'b.', + 'genealogy-died' => 'd.', + 'genealogy-ancestor' => 'Ancestor', + 'genealogy-descendant' => 'Descendant', + 'genealogy-ancestors' => 'Ancestors', + 'genealogy-descendants' => 'Descendants', ); diff --git a/Genealogy.php b/Genealogy.php index 9c1f17d..ea3ce10 100644 --- a/Genealogy.php +++ b/Genealogy.php @@ -43,6 +43,7 @@ $wgAutoloadClasses['GenealogyTraverser'] = __DIR__ . '/Traverser.php'; /** - * Parser function + * Hooks */ -$wgHooks['ParserFirstCallInit'][] = 'GenealogyCore::SetupParserFunction'; +$wgHooks['ParserFirstCallInit'][] = 'GenealogyCore::onParserFirstCallInit'; +$wgHooks['UnitTestsList'][] = 'GenealogyCore::onUnitTestsList'; diff --git a/Person.php b/Person.php index ac7a695..3f21c80 100644 --- a/Person.php +++ b/Person.php @@ -37,15 +37,15 @@ } public function getWikiLink() { - $birthYear = $this->getBirthDate('Y'); - $deathYear = $this->getDeathDate('Y'); + $birthYear = $this->getDateYear($this->getBirthDate()); + $deathYear = $this->getDateYear($this->getDeathDate()); $dateString = ''; if (!empty($birthYear) && !empty($deathYear)) { $dateString = "($birthYear–$deathYear)"; } elseif (!empty($birthYear) && empty($deathYear)) { - $dateString = "(b. $birthYear)"; + $dateString = "(".wfMessage('genealogy-born')." $birthYear)"; } elseif (empty($birthYear) && !empty($deathYear)) { - $dateString = "(d. $deathYear)"; + $dateString = "(".wfMessage('genealogy-died')." $deathYear)"; } $date = ($this->hasDates()) ? " $dateString" : ""; return "[[" . $this->getTitle()->getPrefixedText() . "]]$date"; @@ -56,41 +56,44 @@ * @return boolean */ public function hasDates() { - return $this->getBirthDate() !== false; + return $this->getBirthDate() !== false || $this->getDeathDate() !== false; } /** - * Get the birth date of this person. - * @uses GenealogyPerson::getDate() + * Get the birth date of this person. * @return string */ - public function getBirthDate($format = 'j F Y') { - return $this->getDate('birth', $format); + public function getBirthDate() { + return $this->getPropSingle("birth date"); } /** * Get the death date of this person. - * @uses GenealogyPerson::getDate() * @return string */ - public function getDeathDate($format = 'j F Y') { - return $this->getDate('death', $format); + public function getDeathDate() { + return $this->getPropSingle("death date"); } /** - * 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 + * Get a year out of a date if possible. + * @param string $date + * @return string The year as a string, or the full date. */ - public function getDate($type, $format) { - $date = $this->getPropSingle("$type date"); - $time = strtotime($date); - if ($time !== false) { - return date($format, $time); + public function getDateYear($date) { +// if (empty($rawDate)) { +// return false; +// } +// try { +// $date = new DateTime($rawDate); +// return $date->format('Y'); +// } catch (Exception $e) { +// echo $e->getMessage(); +// return $date; +// } + preg_match('/(\d{4})/', $date, $matches); + if (isset($matches[1])) { + return $matches[1]; } else { return $date; } @@ -154,49 +157,6 @@ return $this->children; } $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; } @@ -205,11 +165,11 @@ $dbr = wfGetDB(DB_SLAVE); $results = $dbr->select( array('pp'=>'page_props', 'p'=>'page'), // tables - array('pp_value', 'page_title'), // columns + array('pp_value', 'page_title'), // columns array( // where conditions - 'pp_value' => $this->title->getPrefixedText(), - "pp_propname LIKE 'genealogy $type %'", - 'pp_page = page_id', + 'pp_value' => $this->title->getPrefixedText(), + "pp_propname LIKE 'genealogy $type %'", + 'pp_page = page_id', ), __METHOD__, array(), @@ -225,11 +185,11 @@ public function getPropSingle($prop) { $dbr = wfGetDB(DB_SLAVE); return $dbr->selectField( - 'page_props', // table to use - 'pp_value', // Field to select + 'page_props', // table to use + 'pp_value', // Field to select array( // where conditions - 'pp_page' => $this->title->getArticleID(), - 'pp_propname' => "genealogy $prop" + 'pp_page' => $this->title->getArticleID(), + 'pp_propname' => "genealogy $prop" ), __METHOD__ ); @@ -239,11 +199,11 @@ $out = array(); $dbr = wfGetDB(DB_SLAVE); $results = $dbr->select( - 'page_props', // table to use - 'pp_value', // Field to select + 'page_props', // table to use + 'pp_value', // Field to select array( // where conditions - 'pp_page' => $this->title->getArticleID(), - "pp_propname LIKE 'genealogy $type %'" + 'pp_page' => $this->title->getArticleID(), + "pp_propname LIKE 'genealogy $type %'" ), __METHOD__ ); diff --git a/tests/phpunit/PersonTest.php b/tests/phpunit/PersonTest.php new file mode 100644 index 0000000..41d055d --- /dev/null +++ b/tests/phpunit/PersonTest.php @@ -0,0 +1,24 @@ +<?php + +/** + * @group extensions + * @group Genealogy + */ +class PersonTest extends MediaWikiTestCase { + + public function testName() { + $person = new GenealogyPerson('Will'); + $this->assertEquals('Will', $person->getTitle()); + } + + public function testDates() { + $person = new GenealogyPerson('Will'); + $this->assertEquals(false, $person->getDateYear('')); + $this->assertEquals('1804', $person->getDateYear('1804')); + $this->assertEquals('2014', $person->getDateYear('2014-10-01')); + $this->assertEquals('2014', $person->getDateYear('1 September 2014')); + $this->assertEquals('1803', $person->getDateYear('June 1803')); + $this->assertEquals('1890', $person->getDateYear('c. 1890')); + } + +} -- To view, visit https://gerrit.wikimedia.org/r/321208 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I9bc4c01775b77448539872af7df85d070cd327db 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