-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bloc.php
208 lines (177 loc) · 5.5 KB
/
Bloc.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
require_once './Model.php';
/**
* Model class for feed objects
*/
class Ecm_Bloc extends Model {
const BLOG_MODEL_KIND = 'blog_post_ecm',
BLOG_TITLE = 'title',
BLOG_SUBTITLE = 'subtitle',
BLOG_URL = 'url',
BLOG_MAINIMG = 'main_img',
BLOG_PUBLICATIONDATE = 'publication_date',
BLOG_AUTHOR = 'author',
BLOG_BODYMD = 'md',
BLOG_TAGS = 'tags',
BLOG_B_PUBLISH = 'b_publish';
private
$title,
$subtitle,
$url,
$main_img,
$publication_date,
$author,
$body_md,
$tags,
$b_publish;
public function __construct($title, $subtitle, $url, $main_img, $publication_date, $author, $body_md, $tags, $b_publish) {
parent::__construct();
//$this->key_name = sha1($url);
$this->key_name = $url;
$this->title = $title;
$this->subtitle = $subtitle;
$this->url = $url;
$this->main_img = $main_img;
$this->publication_date = $publication_date;
$this->author = $author;
$this->body_md = $body_md;
$this->tags = $tags;
$this->b_publish = $b_publish;
}
public function getTitle() {
return $this->title;
}
public function getSubtitle() {
return $this->subtitle;
}
public function getUrl() {
return $this->url;
}
public function getMainImg() {
return $this->main_img;
}
public function getPublicationDate() {
$d = date_create($this->publication_date);
return date_format($d, 'd M Y');
}
public function getAuthor() {
return $this->author;
}
public function getBody() {
return $this->body_md;
}
public function getTags() {
return $this->tags;
}
public function getPublish() {
return $this->b_publish;
}
public function get_posts() {
$query = self::createQuery(static::getKindName());
self::addOrder($query, self::BLOG_PUBLICATIONDATE, $direction = 'descending');
$results = self::executeQuery($query);
return static::extractQueryResults($results);
}
protected static function getKindName() {
return self::BLOG_MODEL_KIND;
}
/**
* Generate the entity property map from the feed object fields.
*/
protected function getKindProperties() {
$property_map = [];
$property_map[self::BLOG_TITLE] =
parent::createStringProperty($this->title, true);
$property_map[self::BLOG_URL] =
parent::createStringProperty($this->url, true);
$property_map[self::BLOG_BODYMD] =
parent::createStringProperty($this->body_md);
$property_map[self::BLOG_SUBTITLE] =
parent::createStringProperty($this->subtitle, true);
$property_map[self::BLOG_PUBLICATIONDATE] =
parent::createDateProperty($this->publication_date, true);
$property_map[self::BLOG_AUTHOR] =
parent::createStringProperty($this->author);
$property_map[self::BLOG_TAGS] =
//parent::createStringListProperty(explode(',', $this->tags), true);
parent::createStringProperty($this->tags);
$property_map[self::BLOG_MAINIMG] =
parent::createStringProperty($this->main_img, true);
$property_map[self::BLOG_B_PUBLISH] =
parent::createBooleanProperty($this->b_publish, true);
return $property_map;
}
/**
* Fetch a feed object given its feed URL. If get a cache miss, fetch from the Datastore.
* @param $url URL of the feed.
*/
public static function get($url) {
$mc = new Memcache();
$key = self::getCacheKey($url);
$response = $mc->get($key);
if ($response) {
return [$response];
}
$query = parent::createQuery(self::BLOG_MODEL_KIND);
$feed_url_filter = parent::createStringFilter(self::BLOG_TITLE,
$url);
$filter = parent::createCompositeFilter([$feed_url_filter]);
$query->setFilter($filter);
$results = parent::executeQuery($query);
$extracted = self::extractQueryResults($results);
return $extracted;
}
/**
* This method will be called after a Datastore put.
*/
protected function onItemWrite() {
$mc = new Memcache();
try {
$key = self::getCacheKey($this->url);
$mc->add($key, $this, 0, 120);
}
catch (Google_Cache_Exception $ex) {
syslog(LOG_WARNING, "in onItemWrite: memcache exception");
}
}
/**
* This method will be called prior to a datastore delete
*/
protected function beforeItemDelete() {
$mc = new Memcache();
$key = self::getCacheKey($this->url);
$mc->delete($key);
}
/**
* Extract the results of a Datastore query into FeedModel objects
* @param $results Datastore query results
*/
protected static function extractQueryResults($results) {
$query_results = [];
foreach($results as $result) {
$id = @$result['entity']['key']['path'][0]['id'];
$key_name = @$result['entity']['key']['path'][0]['name'];
$props = $result['entity']['properties'];
$title = $props[self::BLOG_TITLE]->getStringValue();
$subtitle = $props[self::BLOG_SUBTITLE]->getStringValue();
$url = $props[self::BLOG_URL]->getStringValue();
$main_img = $props[self::BLOG_MAINIMG]->getStringValue();
$publication_date = $props[self::BLOG_PUBLICATIONDATE]->getDateTimeValue();
$author = $props[self::BLOG_AUTHOR]->getStringValue();
$body_md = $props[self::BLOG_BODYMD]->getStringValue();
//$tags = $props[self::BLOG_TAGS]->getListValue();
$tags = $props[self::BLOG_TAGS]->getStringValue();
$b_publish = $props[self::BLOG_B_PUBLISH]->getBooleanValue();
$blog_model = new Ecm_Bloc($title, $subtitle, $url, $main_img, $publication_date, $author, $body_md, $tags, $b_publish);
$blog_model->setKeyId($id);
$blog_model->setKeyName($key_name);
// Cache this read feed.
$blog_model->onItemWrite();
$query_results[] = $blog_model;
}
return $query_results;
}
private static function getCacheKey($feed_url) {
return sprintf("%s_%s", self::BLOG_MODEL_KIND, sha1($feed_url));
}
}