Skip to content

Commit d3add36

Browse files
committed
N°8123 - Improve on mention data parsing
1 parent 909469c commit d3add36

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

application/utils.inc.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3094,7 +3094,6 @@ public static function ToAcronym(string $sInput): string
30943094
* Note: Only works for backoffice URLs for now
30953095
*
30963096
* @param string $sText Text containing the mentioned objects to be found
3097-
* @param string $sFormat {@uses static::ENUM_TEXT_FORMAT_HTML, ...}
30983097
*
30993098
* @return array Array of object classes / IDs for the ones found in $sText
31003099
*
@@ -3109,27 +3108,29 @@ public static function ToAcronym(string $sInput): string
31093108
public static function GetMentionedObjectsFromText(string $sText): array
31103109
{
31113110
$aMentionedObjects = [];
3112-
$aMentionMatches = [];
3113-
$sText = html_entity_decode($sText);
3111+
$oDom = new \DOMDocument();
3112+
libxml_use_internal_errors(true); // to keep processing even in case of "invalid" HTML, cf. testGetMentionedObjectsFromText
3113+
$oDom->loadHTML($sText);
31143114

3115-
preg_match_all('/<a\s*([^>]*)data-object-class="([^"]*)"\s.*data-object-key="([^"]*)"/Ui', $sText, $aMentionMatches);
3116-
foreach ($aMentionMatches[0] as $iMatchIdx => $sCompleteMatch) {
3117-
$sMatchedClass = $aMentionMatches[2][$iMatchIdx];
3118-
$sMatchedId = $aMentionMatches[3][$iMatchIdx];
3115+
$oXpath = new \DOMXPath($oDom);
3116+
$oNodes = $oXpath->query('//a[@data-object-class and @data-object-key]');
3117+
3118+
foreach ($oNodes as $oNode) {
3119+
$sObjClass = $oNode->getAttribute('data-object-class');
3120+
$sObjId = $oNode->getAttribute('data-object-key');
31193121

31203122
// Prepare array for matched class if not already present
3121-
if (!array_key_exists($sMatchedClass, $aMentionedObjects)) {
3122-
$aMentionedObjects[$sMatchedClass] = array();
3123+
if (!array_key_exists($sObjClass, $aMentionedObjects)) {
3124+
$aMentionedObjects[$sObjClass] = [];
31233125
}
31243126
// Add matched ID if not already there
3125-
if (!in_array($sMatchedId, $aMentionedObjects[$sMatchedClass])) {
3126-
$aMentionedObjects[$sMatchedClass][] = $sMatchedId;
3127+
if (!in_array($sObjId, $aMentionedObjects[$sObjClass])) {
3128+
$aMentionedObjects[$sObjClass][] = $sObjId;
31273129
}
31283130
}
31293131

31303132
return $aMentionedObjects;
31313133
}
3132-
31333134
/**
31343135
* Note: This method is not ideal, but other solutions seemed even less ideal:
31353136
* * Add a "$sMaxLength" param. to utils::ToAcronym(): Does not work for every use cases (see corresponding ticket) as in some parts utils::ToAcronym isn't necessarly meant to be used in a medallion.

0 commit comments

Comments
 (0)