]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/XML.php
Merge pull request #11490 from annando/issue-11487
[friendica.git] / src / Util / XML.php
index 4eed3a85f84eb511b0859521d75be1a34a99a760..963161fbffb2add74f70d8874380a795800e6a1e 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -250,7 +250,7 @@ class XML
         * Examples: $array =  Xml::toArray(file_get_contents('feed.xml'));
         *        $array =  Xml::toArray(file_get_contents('feed.xml', true, 1, 'attribute'));
         *
-        * @param object  $contents         The XML text
+        * @param string  $contents         The XML text
         * @param boolean $namespaces       True or false include namespace information
         *                                  in the returned array as array elements.
         * @param integer $get_attributes   1 or 0. If this is 1 the function will get the attributes as well as the tag values -
@@ -268,7 +268,7 @@ class XML
                }
 
                if (!function_exists('xml_parser_create')) {
-                       Logger::log('Xml::toArray: parser function missing');
+                       Logger::notice('Xml::toArray: parser function missing');
                        return [];
                }
 
@@ -283,7 +283,7 @@ class XML
                }
 
                if (! $parser) {
-                       Logger::log('Xml::toArray: xml_parser_create: no resource');
+                       Logger::notice('Xml::toArray: xml_parser_create: no resource');
                        return [];
                }
 
@@ -295,9 +295,9 @@ class XML
                @xml_parser_free($parser);
 
                if (! $xml_values) {
-                       Logger::log('Xml::toArray: libxml: parse error: ' . $contents, Logger::DATA);
+                       Logger::debug('Xml::toArray: libxml: parse error: ' . $contents);
                        foreach (libxml_get_errors() as $err) {
-                               Logger::log('libxml: parse: ' . $err->code . " at " . $err->line . ":" . $err->column . " : " . $err->message, Logger::DATA);
+                               Logger::debug('libxml: parse: ' . $err->code . " at " . $err->line . ":" . $err->column . " : " . $err->message);
                        }
                        libxml_clear_errors();
                        return;
@@ -475,7 +475,7 @@ class XML
 
        public static function getFirstAttributes(DOMXPath $xpath, $element, $context = null)
        {
-               $result = $xpath->query($element, $context);
+               $result = @$xpath->query($element, $context);
                if (!is_object($result)) {
                        return false;
                }
@@ -488,6 +488,21 @@ class XML
                return $first_item->attributes;
        }
 
+       public static function getFirstValue($xpath, $search, $context)
+       {
+               $result = @$xpath->query($search, $context);
+               if (!is_object($result)) {
+                       return '';
+               }
+
+               $first_item = $result->item(0);
+               if (!is_object($first_item)) {
+                       return '';
+               }
+
+               return $first_item->nodeValue;
+       }
+
        /**
         * escape text ($str) for XML transport
         *