-
Notifications
You must be signed in to change notification settings - Fork 53
/
News.php
89 lines (81 loc) · 2.32 KB
/
News.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
require_once('Objects/BasicObject.php');
/**
* The news class represents a news item on the website.
*/
class News extends BasicObject {
private $_title;
private $_date;
private $_author;
private $_image;
private $_content;
private $_filename;
/**
* News object constructor that extracts the data from the pseudo-XML scheme
* used. The format looks like this:
*
* <NAME>Schwag</NAME>
* <DATE>1st january, 1770</DATE>
* <AUTHOR>Guybrush Threepwood</AUTHOR>
* <IMG>path/to/treasure.jpg</IMG>
* <BODY>All I got was this lousy t-shirt!</BODY>
*
*
* FIXME: It currently fails at grabbing the image (see 20020214.xml)
*/
public function __construct($data, $filename, $processContent = false) {
$vars = array();
preg_match("/<NAME>(.*)<\/NAME>.* # Grab the title
<DATE>(.*)<\/DATE>.* # Grab the date
<AUTHOR>(.*)<\/AUTHOR>.* # Grab the author
(?:<IMG>(.*)<\/IMG>.*)? # image if set (only once)
<BODY>(.*)<\/BODY> # Grab the body
/sUx", $data, $vars);
if (count($vars) == 6) {
$this->_title = $processContent ? $this->processText($vars[1]) : $vars[1];
/* Store the date as an unix timestamp*/
$this->_date = date_timestamp_get(date_create_from_format("M d, Y", $vars[2]));
$this->_author = $vars[3];
$this->_image = $vars[4];
$this->_content = $processContent ? $this->processText($vars[5]) : $vars[5];
}
$this->_filename = basename($filename);
}
/**
* Search and replace specific text from the title and content of a news item.
* Used to filter out entities from the RSS/atom feeds that are not in the XML
* standard.
*
* Check:
* http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
* for a list of valid entities for both XML and HTML
*/
function processText($text) {
return html_entity_decode($text, ENT_COMPAT, 'UTF-8');
}
/* Get the title. */
public function getTitle() {
return $this->_title;
}
/* Get the date. */
public function getDate() {
return $this->_date;
}
/* Get the author. */
public function getAuthor() {
return $this->_author;
}
/* Get the optional image. */
public function getImage() {
return $this->_image;
}
/* Get the content. */
public function getContent() {
return $this->_content;
}
/* Get the filename. */
public function getFilename() {
return $this->_filename;
}
}
?>