Revision history for GenerateUniqueId


Revision [19163]

Last edited on 2008-01-28 00:14:04 by JavaWoman [Modified links pointing to docs server]
Additions:
The method will become part of the [[Docs:WikkaCore Wikka core]], and have public access so user-contributed extensions can (//should//) make use of it.
Deletions:
The method will become part of the [[WikkaCore Wikka core]], and have public access so user-contributed extensions can (//should//) make use of it.


Revision [16917]

Edited on 2007-05-31 23:27:17 by JavaWoman [Reverted]
Additions:
$id = preg_replace('/\s+/','_',trim($id)); # replace any whitespace sequence in $id with a single underscore
$idOut .= '_'.++$aSeq[$group]; # add suffiX to make ID unique
Deletions:
$id = preg_replace('/\s /','_',trim($id)); # replace any whitespace sequence in $id with a single underscore
$idOut .= '_'. $aSeq[$group]; # add suffiX to make ID unique


Revision [16716]

Edited on 2007-05-31 10:39:38 by RufA41 [Reverted]
Additions:
$id = preg_replace('/\s /','_',trim($id)); # replace any whitespace sequence in $id with a single underscore
$idOut .= '_'. $aSeq[$group]; # add suffiX to make ID unique
Deletions:
$id = preg_replace('/\s+/','_',trim($id)); # replace any whitespace sequence in $id with a single underscore
$idOut .= '_'.++$aSeq[$group]; # add suffiX to make ID unique


Revision [8926]

Edited on 2005-06-07 17:53:50 by JavaWoman [refer to ArrayToList for ID_LENGTH constant]
Additions:
See ArrayToList for the code (for this and some other constants) and where to add it.
Deletions:
To define it, find this in ##wikka.php##:
define("WAKKA_VERSION", "1.1.6.0");
%% ---
and insert the following code right after that line:
// other constants
/**
* Length to use for generated part of id attribute.
*/
define('ID_LENGTH',10); # @@@ maybe make length configurable


Revision [8581]

Edited on 2005-05-28 11:43:10 by JavaWoman [move to subcategory]
Additions:
CategoryDevelopmentCore
Deletions:
CategoryDevelopment


Revision [8382]

Edited on 2005-05-22 14:14:26 by JavaWoman [minor]
Additions:
In order to ensure really unique ids on a page, it's important to actually **use this method wherever an id is needed** (or, in the case of embedded HTML, already used). Even existing ids will then be automatically validated and "recorded" to prevent duplicates from happening.
Deletions:
In order to ensure really unique ids on a page, it's important to actually **use this method wherever an id is needed** (or, in the case of embedded HTML) already used. Even existing ids will then be automatically validated and "recorded" to prevent duplicates from happening.


Revision [8381]

Edited on 2005-05-22 13:38:28 by JavaWoman [minor]
Additions:
~-Create an id attribute for a form, given an "id proposal" derived from parameters for the ##""FormOpen()""## method: %%(php)$attrId = ' id="'.$this->makeId('form',$id).'"';%% --- The new [[AdvancedFormOpen FormOpen()]] always uses this so every form in Wikka built using this method to open a form element will have an id in the 'form' group.
Deletions:
~-Create an id attribute for a form, given an "id proposal" derived from parameters for the ##""FormOpen()""## method: %%(php)$attrId = ' id="'.$this->makeId('form',$id).'"';%% --- The new [[AdvancedFormOpen FormOpen()]] always uses this so every form in Wikka using this method to open a form element will have an id in the 'form' group.


Revision [8380]

Edited on 2005-05-22 11:53:28 by JavaWoman [improved validation]
Additions:
// validation (full for 'embed', characters only for other groups since we'll add a prefix)
if ('embed' == $group)
$validId = preg_match('/^[A-Za-z][A-Za-z0-9_:.-]*$/',$id); # ref: http://www.w3.org/TR/html4/types.html#type-id
else
$validId = preg_match('/^[A-Za-z0-9_:.-]*$/',$id);
Deletions:
// validation
$validId = preg_match('/^[A-Za-z][A-Za-z0-9_:.-]*$/',$id); # ref: http://www.w3.org/TR/html4/types.html#type-id


Revision [8378]

Edited on 2005-05-22 11:12:54 by JavaWoman [added 'embed' example, minor changes]
Additions:
In order to ensure really unique ids on a page, it's important to actually **use this method wherever an id is needed** (or, in the case of embedded HTML) already used. Even existing ids will then be automatically validated and "recorded" to prevent duplicates from happening.
The method can take two parameters, the first of which, ##$group##, is required. The optional parameter $id can be used to record and validate an existing or proposed id. A few (somewhat simplified) examples:
~-Generate a new id for a list: %%(php)$id = $this->makeId('list');%% --- If used for several lists, this code will result in similar ids for each, made different from each other with a sequence number.
~-Given an embedded HTML element (##$element##) that already has an id (##$id##), ensure the id is valid and unique: %%(php) $newid = $wakka->makeId('embed',$id);
if ($newid != $id) # replace if we got a different id back
$element = str_replace('id="'.$id.'"','id="'.$newid.'"',$element);
%% --- Note that we use the reserved 'embed' group here: provided the given id is valid and unique, it will remain unchanged; but at the same time, ##""makeId()""## will record the id so others can be compared to ensure uniqueness.
Deletions:
In order to ensure really unique ids on a page, it's important to actually use this method wherever an id is needed (or, in the case of embedded HTML) already used.
The method can take two parameters, the first of which, ##$group##, is required. The optional parameter $id can be used to record and validate an existing or proposed id. A few examples:
~-Generate a new id for a list: %%(php)$id = $this->makeId('list');%% --- if used for several lists, this code will result in similar ids, made different from each other with a sequence number.


Revision [8377]

Edited on 2005-05-22 10:57:02 by JavaWoman [usage and examples added]
Additions:
In order to ensure really unique ids on a page, it's important to actually use this method wherever an id is needed (or, in the case of embedded HTML) already used.
The method can take two parameters, the first of which, ##$group##, is required. The optional parameter $id can be used to record and validate an existing or proposed id. A few examples:
~-Create an id attribute for a form, given an "id proposal" derived from parameters for the ##""FormOpen()""## method: %%(php)$attrId = ' id="'.$this->makeId('form',$id).'"';%% --- The new [[AdvancedFormOpen FormOpen()]] always uses this so every form in Wikka using this method to open a form element will have an id in the 'form' group.
~-Create an id for a heading element based on the heading's content: %%(php)$heading = 'This is a nice heading';
$id = $this->makeId('hn',$heading);
$h4 = '<h4 id="'.$id.'">'.$heading.'</h4>';%% --- Provided the id didn't already exist, this will result in: --- <h4 id="hn_This_is_a_nice_heading">This is a nice heading</h4>
~-Generate a new id for a list: %%(php)$id = $this->makeId('list');%% --- if used for several lists, this code will result in similar ids, made different from each other with a sequence number.
Deletions:
//follows//


Revision [8373]

Edited on 2005-05-22 10:12:46 by JavaWoman [improved documentation and validation in code]
Additions:
* Although - given Wikka accepts can use embedded HTML - it cannot be
* guaranteed that an id generated by this method is unique it tries its
* best to make it unique:
* - if an id is specified it is compared with other ids in the same group;
* if an identical id exists within the same group, a sequence suffix is
* added, otherwise the specified id is accepted and recorded as a member
* of the group
* - if no id is specified (or an invalid one) an id will be generated, and
* given a sequence suffix if needed
* For headings, it is possible to derive an id from the heading content;
* to support this, any embedded whitespace is replaced with underscores
* to generate a recognizable id that will remain (mostly) constant even if
* new headings are inserted in a page. (This is not done for embedded
* HTML.)
* The method supports embedded HTML as well: as long as the formatter
* passes each id found in embedded HTML through this method it can take
* care that the id is valid and unique.
* This works as follows:
* - indicate an 'embedded' id with group 'embed'
* - NO prefix will be added for this reserved group
* - already-existing ids in the group are given a sequence suffix
* The result is that as long as the already-defined id is valid and
* unique, it will be remain unchanged (but recorded to ensure uniqueness
* overall).
* @param string $group required: id group (e.g. form, head); will be
* used as prefix (except for the reserved group
* 'embed' to be used for embedded HTML only)
* @param string $id optional: id to use; if not specified or
* invalid, an id will be generated; if not
* unique, a sequence number will be appended
static $aSeq = array(); # group sequences
static $aIds = array(); # used ids
// preparation for group
if (!preg_match('/^[A-Z-a-z]/',$group)) # make sure group starts with a letter
if (!isset($aSeq[$group]))
if (!isset($aIds[$group]))
if ('embed' != $group)
$id = preg_replace('/\s+/','_',trim($id)); # replace any whitespace sequence in $id with a single underscore
$validId = preg_match('/^[A-Za-z][A-Za-z0-9_:.-]*$/',$id); # ref: http://www.w3.org/TR/html4/types.html#type-id
if ('' == $id || !$validId || in_array($id,$aIds)) # ignore specified id if it is invalid or exists already
$id = substr(md5($group.$id),0,ID_LENGTH); # use group and id as basis for generated id
$idOut = ('embed' == $group) ? $id : $group.'_'.$id; # add group prefix (unless embedded HTML)
$idOut .= '_'.++$aSeq[$group]; # add suffiX to make ID unique
$aIds[$group][] = $id; # keep track of both specified and generated ids (without suffix)
Deletions:
* Although - given Wikka accepts can use embedded HTML - it cannot be guaranteed that an id
* generated by this method is unique it tries its best to make it unique:
* - if an id is specified it is compared with other ids in the same group; if an identical
* exists with in the same group a sequence suffix is added, otherwise the specified id
* is accepted and recorded as a member of the group
* - if no id is specified (or an invalid one) an id will be generated, and given a sequence
* suffix if needed
* Embedded HTML will have its already-defined ids (generally) honored:
* - indicate these with group 'embed': no prefix will be added
* - already-existing ids in the group are gven a sequence suffix
* - NO prefix is used
* The result is that as long as the already-defined id is valid and unique, it will be used
* unchanged (but recorded to ensure uniqueness overall).
* Note: the Regex for a valid id actually is ^[A-Za-z][A-Za-z0-9_:.-]*$ but because we add
* a group prefix anyway (which must start with a letter) we don't need to check whether the
* passed id starts with a letter.
* See http://www.w3.org/TR/html4/types.html#type-id for the specification of a valid id.
* @param string $group required: id group (e.g. form, head); will be used as prefix
* @param string $id optional: id to use; if not specified or invalid, an id will
* be generated
static $aSeq = array(); # group sequences
static $aIds = array(); # used ids
// preparation
if (!preg_match('/^[A-Z-a-z]/',$group)) # make sure group starts with a letter
if (!isset($aSeq[$group])) # initialize group sequence
if (!isset($aIds[$group])) # initialize group sequence
$id = preg_replace('/\s+/','_',trim($id)); # replace any whitespace sequence in $id with a single underscore
$validId = preg_match('/^[A-Za-z0-9_:.-]*$/',$id);
if ('' == $id || !$validId || in_array($id,$aIds)) # ignore specified id if it is invalid or exists already
$id = substr(md5($group.$id),0,ID_LENGTH); # use group and id as basis for generated id
$idOut = ('embed' == $group) ? $id : $group.'_'.$id; # add group prefix (unless embedded HTML)
$idOut .= '_'.++$aSeq[$group]; # add suffiX to make ID unique
$aIds[$group][] = $id; # keep track of both specified and generated ids (without suffix)


Revision [8372]

Edited on 2005-05-22 09:25:04 by JavaWoman [minor]
Additions:
As can be seen (and is documented in the docblock) the new ##""makeId()""## method uses a constant that doesn't exist yet in ##wikka.php##.
Deletions:
As can be seen (and is documented in the docblock) the new ##""FormOpen()""## method uses a constant that doesn't exist yet in ##wikka.php##.


Revision [8371]

Edited on 2005-05-22 09:22:58 by JavaWoman [minor]
Additions:
$id = substr(md5($group.$id),0,ID_LENGTH); # use group and id as basis for generated id
define('ID_LENGTH',10); # @@@ maybe make length configurable
Deletions:
$id = substr(md5($group.$id),0,ID_LENGTH); # use group and id as basis for generated id @@@ maybe make length configurable
define('ID_LENGTH',10); # @@@ maybe make configurable


Revision [8369]

Edited on 2005-05-22 09:06:26 by JavaWoman [minor fix]
Additions:
The following code should be inserted in the ""//MISC"" section of ##wikka.php##, right after the ##""ReturnSafeHTML()""## method:
Deletions:
The following code should be inserted in the ""//MISC"" section of ##wikka.php##, right after the ##""ReturnSafeHTML()""## method (or, if you are also implementing the AdvancedFormOpen method, after the new ##""_initSystem()""## method):


Revision [8368]

The oldest known version of this page was created on 2005-05-22 08:53:58 by JavaWoman [minor fix]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki