]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Add XMLStringer for building XML strings
authorEvan Prodromou <evan@controlyourself.ca>
Thu, 12 Feb 2009 13:38:43 +0000 (08:38 -0500)
committerEvan Prodromou <evan@controlyourself.ca>
Thu, 12 Feb 2009 13:38:43 +0000 (08:38 -0500)
We had a bunch of

lib/util.php
lib/xmlstringer.php [new file with mode: 0644]

index 03e3618db8e1b751a1773231c1adc8b6ef4d6d5e..a6f6a935186ef4ca754837a43f9be86850cc5cc4 100644 (file)
@@ -475,13 +475,14 @@ function common_linkify($url) {
     $display = $url;
     $url = (!preg_match('#^([a-z]+://|(mailto|aim|tel):)#i', $url)) ? 'http://'.$url:$url;
 
+    $attrs = array('href' => $url, 'rel' => 'external');
+
     if ($longurl = common_longurl($url)) {
         $longurl = htmlentities($longurl, ENT_QUOTES, 'UTF-8');
-        $title = "title=\"$longurl\"";
+        $attrs['title'] = $longurl;
     }
-    else $title = '';
 
-    return "<a href=\"$url\" $title rel=\"external\">$display</a>";
+    return XMLStringer::estring('a', $attrs, $display);
 }
 
 function common_longurl($short_url)
@@ -582,7 +583,13 @@ function common_tag_link($tag)
 {
     $canonical = common_canonical_tag($tag);
     $url = common_local_url('tag', array('tag' => $canonical));
-    return '<span class="tag"><a href="' . htmlspecialchars($url) . '" rel="tag">' . htmlspecialchars($tag) . '</a></span>';
+    $xs = new XMLStringer();
+    $xs->elementStart('span', 'tag');
+    $xs->element('a', array('href' => $url,
+                            'rel' => 'tag'),
+                 $tag);
+    $xs->elementEnd();
+    return $xs->getString();
 }
 
 function common_canonical_tag($tag)
@@ -600,7 +607,14 @@ function common_at_link($sender_id, $nickname)
     $sender = Profile::staticGet($sender_id);
     $recipient = common_relative_profile($sender, common_canonical_nickname($nickname));
     if ($recipient) {
-        return '<span class="vcard"><a href="'.htmlspecialchars($recipient->profileurl).'" class="url"><span class="fn nickname">'.$nickname.'</span></a></span>';
+        $xs = new XMLStringer(false);
+        $xs->elementStart('span', 'vcard');
+        $xs->elementStart('a', array('href' => $recipient->profileurl,
+                                     'class' => 'url'));
+        $xs->element('span', 'fn nickname', $nickname);
+        $xs->elementEnd('a');
+        $xs->elementEnd('span');
+        return $xs->getString();
     } else {
         return $nickname;
     }
@@ -611,7 +625,14 @@ function common_group_link($sender_id, $nickname)
     $sender = Profile::staticGet($sender_id);
     $group = User_group::staticGet('nickname', common_canonical_nickname($nickname));
     if ($group && $sender->isMember($group)) {
-        return '<span class="vcard"><a href="'.htmlspecialchars($group->permalink()).'" class="url"><span class="fn nickname">'.$nickname.'</span></a></span>';
+        $xs = new XMLStringer();
+        $xs->elementStart('span', 'vcard');
+        $xs->elementStart('a', array('href' => $group->permalink(),
+                                     'class' => 'url'));
+        $xs->element('span', 'fn nickname', $nickname);
+        $xs->elementEnd('a');
+        $xs->elementEnd('span');
+        return $xs->getString();
     } else {
         return $nickname;
     }
@@ -628,7 +649,13 @@ function common_at_hash_link($sender_id, $tag)
         $url = common_local_url('subscriptions',
                                 array('nickname' => $user->nickname,
                                       'tag' => $tag));
-        return '<span class="tag"><a href="'.htmlspecialchars($url).'" rel="tag">'.$tag.'</a></span>';
+        $xs = new XMLStringer();
+        $xs->elementStart('span', 'tag');
+        $xs->element('a', array('href' => $url,
+                                'rel' => $tag),
+                     $tag);
+        $xs->elementEnd('span');
+        return $xs->getString();
     } else {
         return $tag;
     }
diff --git a/lib/xmlstringer.php b/lib/xmlstringer.php
new file mode 100644 (file)
index 0000000..951b13b
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+/**
+ * Laconica, the distributed open-source microblogging tool
+ *
+ * Generator for in-memory XML
+ *
+ * PHP version 5
+ *
+ * LICENCE: This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @category  Output
+ * @package   Laconica
+ * @author    Evan Prodromou <evan@controlyourself.ca>
+ * @copyright 2009 Control Yourself, Inc.
+ * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
+ * @link      http://laconi.ca/
+ */
+
+if (!defined('LACONICA')) {
+    exit(1);
+}
+
+/**
+ * Create in-memory XML
+ *
+ * @category Output
+ * @package  Laconica
+ * @author   Evan Prodromou <evan@controlyourself.ca>
+ * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
+ * @link     http://laconi.ca/
+ * @see      Action
+ * @see      HTMLOutputter
+ */
+
+class XMLStringer extends XMLOutputter
+{
+    function __construct($indent=false)
+    {
+        $this->xw = new XMLWriter();
+        $this->xw->openMemory();
+        $this->xw->setIndent($indent);
+    }
+
+    function getString()
+    {
+        return $this->xw->outputMemory();
+    }
+
+    // utility for quickly creating XML-strings
+
+    static function estring($tag, $attrs=null, $content=null)
+    {
+        $xs = new XMLStringer();
+        $xs->element($tag, $attrs, $content);
+        return $xs->getString();
+    }
+}
\ No newline at end of file