RdfScript

Aus DebianforumWiki
Wechseln zu: Navigation, Suche
LizenzOffen.png Lizenz: Dieser Artikel wurde als problematisch in Bezug auf der Lizenz markiert, dafür kann es verschiedene Gründe geben:
  • die Lizenz des Ursprünglichen Artikels ist unbekannt oder nicht kompatiblen mit der CC by SA
  • durch die Migration wurden der/die ursprüngliche(n) Autor(en) aus der History gelöscht
  • es wurden Inhalte ohne Zustimmung des ursprünglichen Autors übernommen
  • es gibt andere urheberrechtliche Bedenken gegen diesen Artikel.

Bitte hilf mit alle diese Fragen zunächst zu klären, danach kann dieser Hinweis entfernt werden.


Scripting / Scripting Web / Netzwerk / RdfScript


  1 <?php
  2 /***************************************************************************
  3 *                                  rdf.php
  4 *                            -------------------
  5 *   begin                : Saturday, Mar 2, 2002
  6 *   copyright            : (C) 2002 Matthijs van de Water
  7 *                                   Sascha Carlin
  8 *   email                : matthijs@beryllium.net
  9 *                          sc@itst.org
 10 *
 11 *   $Id: rdf.php,v 1.2 2002/04/15 13:15:01 mvdwater Exp $
 12 *
 13 *
 14 ***************************************************************************/
 15 
 16 /***************************************************************************
 17 *
 18 *   I added a cache table to this software because we had big performace
 19 *   problems caused by this script
 20 *   See http://www.debianforum.de/wiki/NewRDFScript
 21 *
 22 *   Martin Lohmeier, debianforum.de Jan 2005
 23 *
 24 ***************************************************************************/
 25 
 26 /***************************************************************************
 27 *
 28 *   This program is free software; you can redistribute it and/or modify
 29 *   it under the terms of the GNU General Public License as published by
 30 *   the Free Software Foundation; either version 2 of the License, or
 31 *   (at your option) any later version.
 32 *
 33 ***************************************************************************/
 34 
 35 /***************************************************************************
 36 *
 37 *   PHPBB 2.0 RDF CONTENT SYNDICATOR
 38 *   Shows last active topics RDF XML form
 39 *
 40 ***************************************************************************
 41 *
 42 *   Put this file in your phpBB2 directory.
 43 *   You can call this script without parameters, what will
 44 *   result in an RDF with the 10 latest posts from all your forums.
 45 *   You can specify the number of posts via the url parameter count:
 46 *
 47 *   http://www.domain.com/phpBB2/rdf.php?count=50
 48 *
 49 *   This will result in an RDF file with the latest 50 posts from
 50 *   all your forums.
 51 *
 52 *   You can also specify to look only at a specified forum using the
 53 *   fid parameter:
 54 *
 55 *   http://www.domain.com/phpBB2/rdf.php?fid=9
 56 *
 57 *   This will result in an RDF file with the latest 10 posts from
 58 *   your forum with the id 9.
 59 *
 60 *   You can also mix the paramters:
 61 *
 62 *   http://www.domain.com/phpBB2/rdf.php?fid=5&count=3
 63 *
 64 *   will show you the latest 3 posts from forum 5.
 65 *
 66 *   THIS SCRIPT WILL ONLY SHOW POSTS FROM PUBLIC FORUMS
 67 *
 68 ***************************************************************************/
 69 
 70 // XML and nocaching headers
 71 header ('Cache-Control: private, pre-check=0, post-check=0, max-age=0');
 72 header ('Expires: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
 73 header ('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
 74 header ('Content-Type: text/xml');
 75 
 76 // Includes of phpBB scripts
 77 define ('IN_PHPBB', true);
 78 $phpbb_root_path = './';
 79 include($phpbb_root_path . 'extension.inc');
 80 include($phpbb_root_path . 'common.'.$phpEx);
 81 
 82 
 83 
 84 // for the cache function
 85 //
 86 define(CACHE_TABLE, "rdf_cache");
 87 define(POSTS_PER_FORUM, "30");
 88 define(MAX_CACHE_SIZE, "1000");
 89 
 90 
 91 // which is the biggest post_id in cache
 92 //
 93 $sql = "SELECT MAX(post_id) AS post_id FROM " . CACHE_TABLE . ";";
 94 $max_post_id_query = $db->sql_query($sql);
 95 if( !$max_post_id_query ) {
 96         die("SQL Error: $sql <br>" . mysql_error() );
 97 }
 98 else {
 99         $max_post_id = $db->sql_fetchrowset($max_post_id_query);
100 }
101 
102 // get all new post_id's
103 //
104 $sql = "SELECT post_id FROM " . POSTS_TABLE ." WHERE post_id > " . $max_post_id[0]['post_id'] . ";";
105 $new_posts_query = $db->sql_query($sql);
106 if( !$new_posts_query ) {
107         die("SQL Error: $sql <br>" . mysql_error() );
108 }
109 else {
110         $new_posts = $db->sql_fetchrowset($new_posts_query);
111         
112         for ($i = 0; $i < count($new_posts); $i++) {
113                 // get new post's and all data that is needed for the rdf part
114                 //
115                 $sql = "INSERT INTO " . CACHE_TABLE . " SELECT p.post_id, p.post_time, pt.post_text, pt.bbcode_uid, t.topic_id, t.topic_time, t.topic_title, f.forum_id, f.forum_name, t.topic_first_post_id, t.topic_last_post_id FROM dfdeforum_topics AS t, dfdeforum_posts AS p, dfdeforum_posts_text AS pt, dfdeforum_forums as f WHERE p.post_id = '" . $new_posts[$i]['post_id'] . "' AND f.auth_view = '0' AND p.topic_id = t.topic_id AND pt.post_id = p.post_id AND p.forum_id = f.forum_id;";
116                 $post_cache_query = $db->sql_query($sql) or die("SQL Error: " . mysql_error() );
117                 
118         }
119 }
120 
121 // clean up cache if it is to big
122 // 
123 //
124 $sql = "SELECT COUNT(post_id) AS post_id FROM " . CACHE_TABLE . ";";
125 $cache_count_query = $db->sql_query($sql) or die("SQL Error $sql : " . mysql_error() );
126 $cache_count = $db->sql_fetchrowset($cache_count_query);
127 
128 
129 if($cache_count[0]['post_id'] > MAX_CACHE_SIZE ) {
130         $sql = "SELECT forum_id FROM " . FORUMS_TABLE . ";";
131         $all_forums_query = $db->sql_query($sql);
132         if( !$all_forums_query ) {
133                 die("SQL Error: $sql <br>" . mysql_error() );
134         }
135         else {
136 
137                 $all_forums = $db->sql_fetchrowset($all_forums_query);
138                 for ($i = 0; $i < count($all_forums); $i++) {
139                         // get COUNT
140                         //
141                         $sql = "SELECT COUNT(post_id) AS post_id FROM " . CACHE_TABLE . " WHERE forum_id = '" . $all_forums[$i]['forum_id'] . "';";
142                         $posts_per_forum_query = $db->sql_query($sql) or die("SQL Error $sql : " . mysql_error() );
143                         $posts_per_forum = $db->sql_fetchrowset($posts_per_forum_query);
144                         
145                         // selecet range of messages that should be deleted
146                         //
147                         if($posts_per_forum[0]['post_id'] <= POSTS_PER_FORUM ) {
148                                 continue;
149                         }
150                         $limit = $posts_per_forum[0]['post_id'] - POSTS_PER_FORUM;
151                         
152                         // delete messages
153                         //
154                         $sql = "SELECT post_id FROM rdf_cache WHERE forum_id = '" . $all_forums[$i]['forum_id'] . "' ORDER BY post_id ASC LIMIT " . $limit . ";";
155                         $delete_query = $db->sql_query($sql) or die("SQL Error $sql : " . mysql_error() );
156                         $delete = $db->sql_fetchrowset($delete_query);
157                         
158                         for ($j = 0; $j < count($delete);$j++) {
159                                 $sql = "DELETE FROM " . CACHE_TABLE . " WHERE post_id = '" . $delete[$j]['post_id'] . "';";
160                                 $delete_post_query = $db->sql_query($sql) or die("SQL Error $sql : " . mysql_error() );
161                         }
162                         
163                         
164                 }
165 }
166 
167 }
168 
169 
170 // If not set, set the output count to 10
171 $count = ( !isset($HTTP_GET_VARS['count']) ) ? 10 : intval($HTTP_GET_VARS['count']);
172 $count = ( $count == 0 ) ? 10 : $count;
173 
174 // characters
175 //
176 $chars = ( isset($HTTP_GET_VARS['chars']) ) ? intval($HTTP_GET_VARS['chars']) : 200;
177 if($chars<0 || $chars>500) $chars=500; //Maximum
178 $type = ( isset($HTTP_GET_VARS['type']) ) ? $HTTP_GET_VARS['type'] : 'latest';
179 $news = ( $type == 'news' );
180 
181 // Create main board url (some code borrowed from functions_post.php)
182 $script_name = preg_replace('/^\/?(.*?)\/?$/', '\1', trim($board_config['script_path']));
183 $viewtopic = ( $script_name != '' ) ? $script_name . '/viewtopic.' . $phpEx : 'viewtopic.'. $phpEx;
184 $index = ( $script_name != '' ) ? $script_name . '/index.' . $phpEx : 'index.'. $phpEx;
185 $server_name = trim($board_config['server_name']);
186 $server_protocol = ( $board_config['cookie_secure'] ) ? 'https://' : 'http://';
187 $server_port = ( $board_config['server_port'] <> 80 ) ? ':' . trim($board_config['server_port']) . '/' : '/';
188 
189 $index_url = $server_protocol . $server_name . $server_port . $index;
190 $viewtopic_url = $server_protocol . $server_name . $server_port . $viewtopic;
191 
192 $rdf = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>
193 <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns=\"http://my.netscape.com/rdf/simple/0.9/\">
194 
195 <channel>
196     <title>" . $board_config['sitename'] . " Forum</title>
197     <link>" . $index_url . "</link>
198     <description>" . $board_config['site_desc'] . "</description>
199 </channel>
200 ";
201 
202 $fid = ( isset($HTTP_GET_VARS['fid']) ) ? $HTTP_GET_VARS['fid'] : array();
203 if(!is_array($fid)) $fid = array($fid);
204 
205 $fid_new = array();
206 for($i=0; $i<sizeof($fid); $i++)
207 {
208     if(intval($fid[$i]) > 0)
209     {
210         if(!in_array($fid[$i], $fid_new))
211         {
212             $fid_new[] = $fid[$i];
213         }
214     }
215 }
216 $fid = $fid_new;
217 $sql_where = ( sizeof($fid)>0 ) ? "AND forum_id IN (" . implode($fid, ", ") . ")" : " ";
218 
219 $sql_orderby = $news ? 'topic_time DESC' : 'post_time DESC';
220 $sql_post_id_field = $news ? 'topic_first_post_id' : 'topic_last_post_id';
221 
222 // SQL statement to fetch active topics of public forums
223 //
224 $sql = "SELECT topic_id, topic_title, post_id, post_time, post_text, bbcode_uid, forum_name
225         FROM " . CACHE_TABLE . " WHERE
226         post_id = $sql_post_id_field
227         $sql_where
228         ORDER BY $sql_orderby LIMIT $count";
229 
230 $topics_query = $db->sql_query($sql);
231 
232 if ( !$topics_query )
233 {
234     die("Failed obtaining list of active topics <b>$sql</b>" . mysql_error() );
235 }
236 else
237 {
238     $topics = $db->sql_fetchrowset($topics_query);
239 }
240 
241 if ( count($topics) == 0 )
242 {
243     die("No topics found");
244 }
245 else
246 {
247     // $topics contains all interesting data
248     for ($i = 0; $i < count($topics); $i++)
249     {
250         if(isset($HTTP_GET_VARS['titlepattern']))
251         {
252             $title = $HTTP_GET_VARS['titlepattern'];
253             $title = str_replace('__DATE__', date("d.m.Y H:i", $topics[$i]['post_time']), $title);
254             $title = str_replace('__TITLE__', $topics[$i]['topic_title'], $title);
255             $title = str_replace('__FORUM__', $topics[$i]['forum_name'], $title);
256         }
257         else
258         {
259             $title = $topics[$i]['topic_title'];
260         }
261         //$title = "[" . date("d.m.Y H:i", $topics[$i]['post_time']) . "|" . $topics[$i]['forum_name'] . "] " . $topics[$i]['topic_title'];
262         $url = ($news) ? $viewtopic_url . "?" . POST_TOPIC_URL . "=" . $topics[$i]['topic_id'] : $viewtopic_url . "?" . POST_POST_URL . "=" . $topics[$i]['post_id'] . "#" . $topics[$i]['post_id'];
263 
264         $message = $topics[$i]['post_text'];
265         $bbcode_uid = $topics[$i]['bbcode_uid'];
266         $message = strip_tags($message);
267         $message = preg_replace("/\[.*?:$bbcode_uid:?.*?\]/si", '', $message);
268         $message = preg_replace('/\[url\]|\[\/url\]/si', '', $message);
269         if($chars > 0) {
270                 $message = ( strlen($message) > $chars ) ? substr($message, 0, ($chars - 4)) . ' ...' : $message;
271         }
272 
273         $rdf .= "
274 <item>
275     <title>" . $title . "</title>
276     <link>" . $url . "</link>";
277 //    <pubDate>" . date('r', $topics[$i]['post_time']) . "</pubDate>";
278 
279 if($chars != 0 && strlen($message)>0) $rdf .= "
280     <description>" . htmlspecialchars($message) . "</description>";
281 
282 $rdf.="
283 </item>
284 ";
285     }
286 }
287 
288 // Create RDF footer
289 $rdf .= "
290 </rdf:RDF>";
291 
292 // Output the RDF
293 echo $rdf;
294 
295 
296 
297 ?>

Das Script benötigt folgende Tabelle:

CREATE TABLE `rdf_cache` (
  `post_id` mediumint(8) unsigned NOT NULL default '0',
  `post_time` int(11) unsigned NOT NULL default '0',
  `post_text` text NOT NULL,
  `bbcode_uid` varchar(10) NOT NULL default '',
  `topic_id` mediumint(8) unsigned NOT NULL default '0',
  `topic_time` int(11) unsigned NOT NULL default '0',
  `topic_title` varchar(60) NOT NULL default '',
  `forum_id` smallint(5) unsigned NOT NULL default '0',
  `forum_name` varchar(150) NOT NULL default '',
  `topic_first_post_id` mediumint(8) unsigned NOT NULL default '0',
  `topic_last_post_id` mediumint(8) unsigned NOT NULL default '0',
  UNIQUE KEY `post_id` (`post_id`)
) TYPE=MyISAM COMMENT='damit das rdf script schneller wird';

Scripting / Scripting Web / Netzwerk / RdfScript