From bda08c6988d40253ce2df8c65ede68982deacfed Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Mon, 20 Dec 2010 03:18:51 +0000 Subject: [PATCH] ENHANCEMENT: abstract generateURLSegments functionality out to Convert::raw2url() to allow non site tree objects to safely make use of the logic. BUGFIX: #5586 Group::setCode() now calls Convert::raw2url() rather than requiring a SiteTree instance git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@115205 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/Convert.php | 38 ++++++++++++++++++++++++++++++++++---- core/model/SiteTree.php | 9 ++------- security/Group.php | 4 +++- tests/ConvertTest.php | 10 ++++++++++ 4 files changed, 49 insertions(+), 12 deletions(-) diff --git a/core/Convert.php b/core/Convert.php index 228369b25..b0a3aad0a 100755 --- a/core/Convert.php +++ b/core/Convert.php @@ -195,7 +195,12 @@ class Convert { } /** - * @uses recursiveXMLToArray() + * Converts an XML string to a PHP array + * + * @uses {@link recursiveXMLToArray()} + * @param string + * + * @return array */ static function xml2array($val) { $xml = new SimpleXMLElement($val); @@ -203,8 +208,12 @@ class Convert { } /** - * Function recursively run from {@link Convert::xml2array()} - * @uses SimpleXMLElement + * Convert a XML string to a PHP array recursively. Do not + * call this function directly, Please use {@link Convert::xml2array()} + * + * @param SimpleXMLElement + * + * @return mixed */ protected static function recursiveXMLToArray($xml) { if(is_object($xml) && get_class($xml) == 'SimpleXMLElement') { @@ -223,11 +232,13 @@ class Convert { if(isset($a)) $r['@'] = $a; // Attributes return $r; } + return (string) $xml; } /** * Create a link if the string is a valid URL + * * @param string The string to linkify * @return A link to the URL if string is a URL */ @@ -335,5 +346,24 @@ class Convert { $data ); } - + + /** + * Convert a string (normally a title) to a string suitable for using in + * urls and other html attributes + * + * @param string + * + * @return string + */ + public static function raw2url($title) { + $t = (function_exists('mb_strtolower')) ? mb_strtolower($title) : strtolower($title); + $t = Object::create('Transliterator')->toASCII($t); + $t = str_replace('&','-and-',$t); + $t = str_replace('&','-and-',$t); + $t = ereg_replace('[^A-Za-z0-9]+','-',$t); + $t = ereg_replace('-+','-',$t); + $t = trim($t, '-'); + + return $t; + } } \ No newline at end of file diff --git a/core/model/SiteTree.php b/core/model/SiteTree.php index 2e835fee6..abe48f2fb 100755 --- a/core/model/SiteTree.php +++ b/core/model/SiteTree.php @@ -1521,16 +1521,11 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid * @return string Generated url segment */ function generateURLSegment($title){ - $t = (function_exists('mb_strtolower')) ? mb_strtolower($title) : strtolower($title); - $t = Object::create('Transliterator')->toASCII($t); - $t = str_replace('&','-and-',$t); - $t = str_replace('&','-and-',$t); - $t = ereg_replace('[^A-Za-z0-9]+','-',$t); - $t = ereg_replace('-+','-',$t); + $t = Convert::raw2url($title); + if(!$t || $t == '-' || $t == '-1') { $t = "page-$this->ID"; } - $t = trim($t, '-'); // Hook for decorators $this->extend('updateURLSegment', $t, $title); diff --git a/security/Group.php b/security/Group.php index 913bac13c..0bd4a1d6e 100644 --- a/security/Group.php +++ b/security/Group.php @@ -313,9 +313,11 @@ class Group extends DataObject { /** * Overloaded to ensure the code is always descent. + * + * @param string */ public function setCode($val){ - $this->setField("Code",SiteTree::generateURLSegment($val)); + $this->setField("Code", Convert::raw2url($val)); } function onBeforeWrite() { diff --git a/tests/ConvertTest.php b/tests/ConvertTest.php index d34326e7f..6a271fe56 100644 --- a/tests/ConvertTest.php +++ b/tests/ConvertTest.php @@ -96,5 +96,15 @@ class ConvertTest extends SapphireTest { $this->assertEquals('Jones', $obj->Tom); $this->assertEquals('Structure', $obj->My->Complicated); } + + /** + * @todo test toASCII() + */ + function testRaw2URL() { + $this->assertEquals('foo', Convert::raw2url('foo')); + $this->assertEquals('foo-and-bar', Convert::raw2url('foo & bar')); + $this->assertEquals('foo-and-bar', Convert::raw2url('foo & bar!')); + $this->assertEquals('foo-s-bar-2', Convert::raw2url('foo\'s [bar] (2)')); + } } \ No newline at end of file