Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions phpdotnet/phd/Package/PHP/XHTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,26 +341,48 @@ public static function generateAcronymInfo($filename) {
return array();
}

$r = new \XMLReader;
if (!$r->open($filename)) {
$subset = file_get_contents($filename);
if ($subset === false) {
throw new \Error(vsprintf('Could not open file for accessing acronym information (%s)', [$filename]));
}

$acronyms = array();
$k = "";
while ($r->read()) {
if ($r->nodeType != \XMLReader::ELEMENT) {
continue;
}
if ($r->name == "term") {
$r->read();
$k = $r->value;
$acronyms[$k] = "";
} else if ($r->name == "simpara") {
$r->read();
$acronyms[$k] = $r->value;
// libxml only exposes entity content once referenced, so collect
// the acronym.* entity names first, then reference each of them.
$useInternalErrors = libxml_use_internal_errors(true);
$dom = new \DOMDocument;
$loaded = $dom->loadXML("<!DOCTYPE acronyms [" . $subset . "]><acronyms/>");
if (!$loaded) {
libxml_clear_errors();
libxml_use_internal_errors($useInternalErrors);
trigger_error(vsprintf("Can't parse acronym file (%s), skipping", [$filename]), E_USER_WARNING);
return [];
}

$names = [];
foreach ($dom->doctype->entities as $entity) {
if (strncmp($entity->nodeName, "acronym.", 8) === 0) {
Comment thread
jordikroon marked this conversation as resolved.
Outdated
$names[] = $entity->nodeName;
}
}

$body = "";
foreach ($names as $name) {
$body .= '<acronym name="' . $name . '">&' . $name . ';</acronym>';
}
$dom = new \DOMDocument;
$loaded = $dom->loadXML("<!DOCTYPE acronyms [" . $subset . "]><acronyms>" . $body . "</acronyms>", LIBXML_NOENT);
libxml_clear_errors();
libxml_use_internal_errors($useInternalErrors);
if (!$loaded) {
trigger_error(vsprintf("Can't expand acronym entities (%s), skipping", [$filename]), E_USER_WARNING);
return [];
}

$acronyms = [];
foreach ($dom->documentElement->childNodes as $node) {
$acronyms[substr($node->getAttribute("name"), 8)] = trim(preg_replace('/\s+/', ' ', $node->textContent));
Comment thread
jordikroon marked this conversation as resolved.
Outdated
}
ksort($acronyms);
$info = $acronyms;
return $acronyms;
}
Expand Down
2 changes: 1 addition & 1 deletion render.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
. "phd" . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR
. "langs" . DIRECTORY_SEPARATOR,
"phpwebVersionFilename" => $config->xmlRoot . DIRECTORY_SEPARATOR . 'version.xml',
"phpwebAcronymFilename" => $config->xmlRoot . DIRECTORY_SEPARATOR . 'entities' . DIRECTORY_SEPARATOR . 'acronyms.xml',
"phpwebAcronymFilename" => $config->xmlRoot . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'entities.ent',
"phpwebSourcesFilename" => $config->xmlRoot . DIRECTORY_SEPARATOR . 'sources.xml',
"phpwebHistoryFilename" => $config->xmlRoot . DIRECTORY_SEPARATOR . 'fileModHistory.php',
));
Expand Down
23 changes: 23 additions & 0 deletions tests/package/php/acronym_info_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
Acronym info 001 - Acronyms are read from acronym.* XML entities
--FILE--
<?php
namespace phpdotnet\phd;

require_once __DIR__ . "/../../setup.php";

$acronyms = Package_PHP_XHTML::generateAcronymInfo(__DIR__ . "/data/acronyms.ent");

var_dump($acronyms);
?>
--EXPECT--
array(4) {
["API"]=>
string(33) "Application Programming Interface"
["CSPRNG"]=>
string(54) "Cryptographically Secure PseudoRandom Number Generator"
["PHP"]=>
string(27) "PHP: Hypertext Preprocessor"
["cURL"]=>
string(18) "Client URL Library"
}
16 changes: 16 additions & 0 deletions tests/package/php/data/acronyms.ent
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

<!-- DO NOT COPY / DO NOT TRANSLATE -->
<!-- Autogenerated by text-entities.php -->

<!ENTITY sample.other 'Some other, non acronym, entity'>

<!ENTITY acronym.API 'Application Programming Interface'>

<!ENTITY acronym.CSPRNG '
Cryptographically Secure
PseudoRandom Number Generator
'>

<!ENTITY acronym.PHP 'PHP: Hypertext Preprocessor'>

<!ENTITY acronym.cURL 'Client URL Library'>
Loading