[geeklog-cvs] geeklog: Implemented extended API for PLG_getItemInfo

geeklog-cvs at lists.geeklog.net geeklog-cvs at lists.geeklog.net
Mon Dec 22 05:51:31 EST 2008


details:   http://project.geeklog.net/cgi-bin/hgweb.cgi/rev/0d0c4bff08d5
changeset: 6569:0d0c4bff08d5
user:      Dirk Haun <dirk at haun-online.de>
date:      Sat Dec 20 15:41:14 2008 +0100
description:
Implemented extended API for PLG_getItemInfo

diffstat:

4 files changed, 163 insertions(+), 100 deletions(-)
public_html/admin/trackback.php |   38 ++------
system/lib-plugins.php          |   40 ++++++---
system/lib-story.php            |  168 +++++++++++++++++++++++++++------------
system/lib-trackback.php        |   17 ---

diffs (truncated from 417 to 300 lines):

diff -r 4422583786be -r 0d0c4bff08d5 public_html/admin/trackback.php
--- a/public_html/admin/trackback.php	Sun Dec 14 09:43:17 2008 +0100
+++ b/public_html/admin/trackback.php	Sat Dec 20 15:41:14 2008 +0100
@@ -168,7 +168,7 @@
     $cid = addslashes ($id);
     $result = DB_query ("SELECT sid,type FROM {$_TABLES['trackback']} WHERE cid = '$cid'");
     list ($sid, $type) = DB_fetchArray ($result);
-    $url = getItemInfo ($type, $sid, 'url');
+    $url = PLG_getItemInfo($type, $sid, 'url');
 
     if (TRB_allowDelete ($sid, $type)) {
         TRB_deleteTrackbackComment ($id);
@@ -222,7 +222,7 @@
 
     $retval = '';
 
-    list ($url, $text) = getItemInfo ($type, $id, 'url,description');
+    list($url, $text) = PLG_getItemInfo($type, $id, 'url,description');
 
     // extract all links from the text
     preg_match_all ("/<a[^>]*href=[\"']([^\"']*)[\"'][^>]*>(.*?)<\/a>/i", $text,
@@ -327,7 +327,7 @@
 
     $retval = '';
 
-    list ($itemurl,$feedurl) = getItemInfo ($type, $id, 'url,feed');
+    list($itemurl,$feedurl) = PLG_getItemInfo($type, $id, 'url,feed');
 
     $template = new Template ($_CONF['path_layout'] . 'admin/trackback');
     $template->set_file (array ('list' => 'pinglist.thtml',
@@ -451,24 +451,6 @@
     }
 
     return $retval;
-}
-
-/**
-* Wrapper for STORY_getItemInfo / PLG_getItemInfo to keep things readable
-*
-* @param    string  $type   type of entry ('article' = story, else plugin)
-* @param    string  $id     ID of that entry
-* @param    string  $what   info requested
-* @return   mixed           requested info, as a string or array of strings
-*
-*/
-function getItemInfo ($type, $id, $what)
-{
-    if ($type == 'article') {
-        return STORY_getItemInfo ($id, $what);
-    } else {
-        return PLG_getItemInfo ($type, $id, $what);
-    }
 }
 
 /**
@@ -932,8 +914,8 @@
     }
     $id = COM_applyFilter ($_REQUEST['id']);
     if (!empty ($id)) {
-        list ($url, $title, $excerpt) = getItemInfo ($type, $id,
-                                                     'url,title,excerpt');
+        list($url, $title, $excerpt) = PLG_getItemInfo($type, $id,
+                                                       'url,title,excerpt');
         $excerpt = trim (strip_tags ($excerpt));
         $blog = TRB_filterBlogname ($_CONF['site_name']);
 
@@ -996,7 +978,7 @@
         }
     }
 
-    $title = getItemInfo ($type, $id, 'title');
+    $title = PLG_getItemInfo($type, $id, 'title');
 
     $display .= COM_siteHeader ('menu', $LANG_TRB['send_pings']);
     $display .= COM_startBlock (sprintf ($LANG_TRB['send_pings_for'], $title));
@@ -1086,7 +1068,7 @@
         $type = 'article';
     }
 
-    $fulltext = getItemInfo ($type, $id, 'description');
+    $fulltext = PLG_getItemInfo($type, $id, 'description');
 
     $display .= COM_siteHeader ('menu', $LANG_TRB['trackback'])
               . COM_startBlock ($LANG_TRB['select_url'], $_CONF['site_url']
@@ -1111,7 +1093,7 @@
 
     $trackbackUrl = TRB_detectTrackbackUrl ($url);
 
-    list ($url, $title, $excerpt) = getItemInfo ($type, $id,
+    list($url, $title, $excerpt) = PLG_getItemInfo($type, $id,
                                                  'url,title,excerpt');
     $excerpt = trim (strip_tags ($excerpt));
     $blog = TRB_filterBlogname ($_CONF['site_name']);
@@ -1152,8 +1134,8 @@
         $id = COM_applyFilter ($_REQUEST['id']);
         $type = COM_applyFilter ($_REQUEST['type']);
         if (!empty ($id) && !empty ($type)) {
-            list ($newurl, $newtitle, $newexcerpt) =
-                                getItemInfo ($type, $id, 'url,title,excerpt');
+            list($newurl, $newtitle, $newexcerpt) =
+                            PLG_getItemInfo($type, $id, 'url,title,excerpt');
             $newexcerpt = trim (strip_tags ($newexcerpt));
 
             if (empty ($url) && !empty ($newurl)) {
diff -r 4422583786be -r 0d0c4bff08d5 system/lib-plugins.php
--- a/system/lib-plugins.php	Sun Dec 14 09:43:17 2008 +0100
+++ b/system/lib-plugins.php	Sat Dec 20 15:41:14 2008 +0100
@@ -1962,16 +1962,21 @@
 /**
 * Ask plugin for information about one of its items
 *
-* @param    string  $type       plugin type
-* @param    string  $id         ID of an item under the plugin's control
+* @param    string  $type       plugin type (incl. 'article' for stories)
+* @param    string  $id         ID of an item under the plugin's control or '*'
 * @param    string  $what       comma-separated list of item properties
+* @param    int     $uid        if > 0: only return items accessible by user
+* @param    array   $options    (reserved for future extensions)
 * @return   mixed               string or array of strings with the information
 *
 * Item properties that can be requested:
-* 'url'         - URL of the item
-* 'title'       - title of the item
-* 'excerpt'     - short description of the item
-* 'description' - full description of the item
+* 'date-created'  - creation date, if available
+* 'date-modified' - date of last modification, if available
+* 'description'   - full description of the item
+* 'excerpt'       - short description of the item
+* 'id'            - ID of the item, e.g. sid for articles
+* 'title'         - title of the item
+* 'url'           - URL of the item
 *
 * 'excerpt' and 'description' may return the same value. Properties should be
 * returned in the order they are listed in $what. Properties that are not
@@ -1979,14 +1984,27 @@
 * Return false for errors (e.g. access denied, item does not exist, etc.).
 *
 */
-function PLG_getItemInfo ($type, $id, $what)
+function PLG_getItemInfo($type, $id, $what, $uid = 0, $options = array())
 {
-    $args[1] = $id;
-    $args[2] = $what;
+    if ($type == 'article') {
 
-    $function = 'plugin_getiteminfo_' . $type;
+        global $_CONF;
 
-    return PLG_callFunctionForOnePlugin ($function, $args);
+        require_once $_CONF['path_system'] . 'lib-story.php';
+
+        return STORY_getItemInfo($id, $what, $uid, $options);
+
+    } else {
+
+        $args[1] = $id;
+        $args[2] = $what;
+        $args[3] = $uid;
+        $args[4] = $options;
+
+        $function = 'plugin_getiteminfo_' . $type;
+
+        return PLG_callFunctionForOnePlugin($function, $args);
+    }
 }
 
 /**
diff -r 4422583786be -r 0d0c4bff08d5 system/lib-story.php
--- a/system/lib-story.php	Sun Dec 14 09:43:17 2008 +0100
+++ b/system/lib-story.php	Sat Dec 20 15:41:14 2008 +0100
@@ -692,59 +692,103 @@
 * This is the story equivalent of PLG_getItemInfo. See lib-plugins.php for
 * details.
 *
-* @param    string  $sid    story ID
-* @param    string  $what   comma-separated list of story properties
-* @return   mixed           string or array of strings with the information
+* @param    string  $sid        story ID or '*'
+* @param    string  $what       comma-separated list of story properties
+* @param    int     $uid        if > 0: only return items accessible by user
+* @param    array   $options    (reserved for future extensions)
+* @return   mixed               string or array of strings with the information
 *
 */
-function STORY_getItemInfo ($sid, $what)
+function STORY_getItemInfo($sid, $what, $uid = 0, $options = array())
 {
     global $_CONF, $_TABLES;
 
-    $properties = explode (',', $what);
-    $fields = array ();
+    // parse $what to see what we need to pull from the database
+    $properties = explode(',', $what);
+    $fields = array();
     foreach ($properties as $p) {
         switch ($p) {
-            case 'description':
-                $fields[] = 'introtext';
-                $fields[] = 'bodytext';
-                break;
-            case 'excerpt':
-                $fields[] = 'introtext';
-                break;
-            case 'feed':
-                $fields[] = 'tid';
-                break;
-            case 'title':
-                $fields[] = 'title';
-                break;
-            default: // including 'url'
-                // nothing to do
-                break;
+        case 'date-created':
+            $fields[] = 'UNIX_TIMESTAMP(date) AS unixdate';
+            break;
+        case 'description':
+            $fields[] = 'introtext';
+            $fields[] = 'bodytext';
+            break;
+        case 'excerpt':
+            $fields[] = 'introtext';
+            break;
+        case 'feed':
+            $fields[] = 'tid';
+            break;
+        case 'id':
+            $fields[] = 'sid';
+            break;
+        case 'title':
+            $fields[] = 'title';
+            break;
+        case 'url':
+            if ($sid == '*') {
+                // in this case, we need the sid to build the URL
+                $fields[] = 'sid';
+            }
+            break;
+        default:
+            // nothing to do
+            break;
         }
     }
 
-    if (count ($fields) > 0) {
-        $result = DB_query ("SELECT " . implode (',', $fields)
-                    . " FROM {$_TABLES['stories']} WHERE sid = '$sid'"
-                    . COM_getPermSql ('AND') . COM_getTopicSql ('AND'));
-        $A = DB_fetchArray ($result);
-    } else {
-        $A = array ();
+    $fields = array_unique($fields);
+
+    if (count($fields) == 0) {
+        $retval = array();
+
+        return $retval;
     }
 
-    $retval = array ();
-    foreach ($properties as $p) {
-        switch ($p) {
+    // prepare SQL request
+    if ($sid == '*') {
+        $where = '';
+        $permOp = 'WHERE';
+    } else {
+        $where = " WHERE sid = '$sid'";
+        $permOp = 'AND';
+    }
+    if ($uid > 0) {
+        $permSql = COM_getPermSql($permOp, $uid)
+                 . COM_getTopicSql('AND', $uid);
+    } else {
+        $permSql = COM_getPermSql($permOp) . COM_getTopicSql('AND');
+    }
+    $sql = "SELECT " . implode(',', $fields)
+            . " FROM {$_TABLES['stories']}" . $where . $permSql;
+    if ($sid != '*') {
+        $sql .= ' LIMIT 1';
+    }
+
+    $result = DB_query($sql);
+    $numRows = DB_numRows($result);
+
+    $retval = array();
+    for ($i = 0; $i < $numRows; $i++) {
+        $A = DB_fetchArray($result);
+
+        $props = array();
+        foreach ($properties as $p) {
+            switch ($p) {
+            case 'date-created':
+                $props['date-created'] = $A['unixdate'];
+                break;
             case 'description':
-                $retval[] = trim (PLG_replaceTags (stripslashes ($A['introtext']) . ' ' . stripslashes ($A['bodytext'])));
+                $props['description'] = trim(PLG_replaceTags(stripslashes($A['introtext']) . ' ' . stripslashes($A['bodytext'])));
                 break;
             case 'excerpt':



More information about the geeklog-cvs mailing list