[ Index ] |
|
Code source de GeekLog 1.4.1 |
1 <?php 2 /****************************************************************************/ 3 /* FeedParserBase.class.php */ 4 /* */ 5 /****************************************************************************/ 6 /* Copyright (c) 2004 Michael Jervis (mike@fuckingbrit.com) */ 7 /* */ 8 /* This software is licensed under the terms of the ZLIB License: */ 9 /* */ 10 /* This software is provided 'as-is', without any express or implied */ 11 /* warranty. In no event will the authors be held liable for any damages */ 12 /* arising from the use of this software. */ 13 /* */ 14 /* Permission is granted to anyone to use this software for any purpose, */ 15 /* including commercial applications, and to alter it and redistribute it */ 16 /* freely, subject to the following restrictions: */ 17 /* */ 18 /* 1. The origin of this software must not be misrepresented; you must not */ 19 /* claim that you wrote the original software. If you use this software */ 20 /* in a product, an acknowledgment in the product documentation would be*/ 21 /* appreciated but is not required. */ 22 /* */ 23 /* 2. Altered source versions must be plainly marked as such, and must not */ 24 /* be misrepresented as being the original software. */ 25 /* */ 26 /* 3. This notice may not be removed or altered from any source */ 27 /* distribution. */ 28 /****************************************************************************/ 29 30 /** 31 * FeedParserBase provides an abstract ancestor class for feed parsers. 32 * 33 * @author Michael Jervis (mike@fuckingbrit.com) 34 * @copyright Michael Jervis 2004 35 * @abstract 36 */ 37 class FeedParserBase 38 { 39 /** 40 * An array of items. 41 * 42 * This holds all the news from the source. This should be an array of 43 * associative arrays. Each item will have: 44 * title - The title 45 * URI - Link to the full story 46 * date - The date of the article 47 * Optional (pre-defined) items are: 48 * summary - Short version of article 49 * text - full version 50 * author - Who wrote the article 51 */ 52 var $articles; 53 54 /** 55 * Encoding tag for the XML declaration 56 */ 57 var $encoding; 58 59 /** 60 * Language for the feed 61 */ 62 var $lang; 63 64 /** 65 * Title for the feed 66 */ 67 var $title; 68 69 /** 70 * The description of the feed 71 */ 72 var $description; 73 74 /** 75 * The URL of the feed 76 */ 77 var $url; 78 79 /** 80 * URL of the site 81 */ 82 var $sitelink; 83 84 /** 85 * Site contact 86 */ 87 var $sitecontact; 88 89 /** 90 * copyright tag: 91 */ 92 var $copyright; 93 94 /** 95 * system powering the feed 96 */ 97 var $system; 98 99 /** 100 * Image to link to the feed. 101 */ 102 var $feedlogo; 103 104 /** 105 * Additional namespaces to add. 106 */ 107 var $namespaces; 108 109 /** 110 * Additional tags to add. 111 */ 112 var $extensions; 113 114 /** 115 * Stuff for parsing XML 116 */ 117 var $_currentTag; 118 119 function FeedParserBase() 120 { 121 $this->encoding = 'iso-8859-1'; 122 $title = 'Killer Feed System Feed'; 123 $this->lang = 'en-gb'; 124 $currentTag = ''; 125 $articles = array(); 126 } 127 128 /** 129 * Create a file for the stream 130 * 131 * Writes the $items content to the file supplied in the format we have 132 * specified. Uses the (abstract) function formatArticle to return XML 133 * for an article. 134 * 135 * @param string $fileName The fully qualified path to the file to create. 136 * @param integer $limit (optional) max number of items to write. 137 */ 138 function createFeed( $fileName, $limit='' ) 139 { 140 /* If we have no items, return false */ 141 if( count( $this->articles ) == 0 ) 142 { 143 return false; 144 } else { 145 /* Start the XML Feed formating */ 146 $xml = $this->_feedHeader(); 147 148 /* Start with a limit of the size of the array, then, if we have a 149 * specific max length use that unless it's bigger than our count */ 150 $count = count( $this->articles ); 151 if( $limit ) 152 { 153 if( $limit < $count ) 154 { 155 $count = $limit; 156 } 157 } 158 159 /* Put the first $count items into the xml, using formatArticle */ 160 for( $i = 0; $i < $count; $i++ ) 161 { 162 $xml .= $this->_formatArticle( $this->articles[$i] ); 163 } 164 165 /* Close off the feed */ 166 $xml .= $this->_feedFooter(); 167 /* And write it to file */ 168 return $this->_writeFile( $fileName, $xml ); 169 } 170 } 171 172 function _writeFile( $fileName, $data ) 173 { 174 if( $fp = @fopen( $fileName, 'w' ) ) 175 { 176 fputs( $fp, $data ); 177 fclose( $fp ); 178 return true; 179 } else { 180 return false; 181 } 182 } 183 184 /** 185 * Format an article into feed specific XML. 186 * 187 * Takes an associative article array and turns it into an XML definition 188 * of an article. 189 * @param array $article ASsociative array describing an article. 190 */ 191 function _formatArticle( $article ) 192 { 193 $xml = "<article>\n"; 194 while( list($key, $value) = each( $article ) ) 195 { 196 if($key != 'extensions') 197 { 198 $value = $this->safeXML( $value ); 199 $xml .= "<$key>$value</$key>\n>"; 200 } else { 201 if(is_array($value)) 202 { 203 foreach( $value as $ext ) 204 { 205 $xml .= $ext."\n"; 206 } 207 } else { 208 $xml .= $value."\n"; 209 } 210 } 211 212 } 213 $xml .= "</article>\n"; 214 return $xml; 215 } 216 217 /** 218 * Make sure a string is safe to be chardata in an xml element 219 * 220 * @param string $string the string to escape. 221 */ 222 function _safeXML( $string ) 223 { 224 return htmlspecialchars($string); 225 } 226 227 /** 228 * Return the formatted start of a feed. 229 * 230 * This will start the xml and create header information about the feed 231 * itself. 232 */ 233 function _feedHeader() 234 { 235 $xml = "<?xml version=\"1.0\" encoding=\"{$this->encoding}\"?>\n\n"; 236 237 $xml .= '<feed'.$this->_injectNamespaces().">\n"; 238 239 $xml .= "<title>{$this->title}</title>\n"; 240 foreach( $this->extensions as $extendingTag ) 241 { 242 $xml .= $extendingTag."\n"; 243 } 244 return $xml; 245 } 246 247 /** 248 * Inject XMLNS items into the feed master element, 249 * if needed. 250 */ 251 function _injectNamespaces() 252 { 253 $xml = ' '; 254 if( is_array($this->namespaces) ) 255 { 256 $xml .= implode(' ', $this->namespaces); 257 } 258 259 return $xml; 260 } 261 262 /** 263 * Return the formatted end of a feed. 264 * 265 * just closes things off nicely. 266 */ 267 function _feedFooter() 268 { 269 $xml = '</feed>'; 270 return $xml; 271 } 272 273 /** 274 * Handle the begining of an XML element 275 * 276 * This is called from the parserfactory once the type of data has been 277 * determined. Standard XML_PARSER element handler. 278 * 279 * @author Michael Jervis (mike@fuckingbrit.com) 280 * @copyright Michael Jervis 2004 281 */ 282 function startElement($parser, $name, $attributes) 283 { 284 } 285 286 /** 287 * Handle the close of an XML element 288 * 289 * Called by the parserfactory during parsing. 290 */ 291 function endElement($parser, $name) 292 { 293 } 294 295 /** 296 * Handles character data. 297 * 298 * Called by the parserfactory during parsing. 299 */ 300 function charData($parser, $data) 301 { 302 } 303 } 304 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Wed Nov 21 12:27:40 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |