-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathextension.driver.php
executable file
·202 lines (146 loc) · 6.69 KB
/
extension.driver.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
<?php
require_once(EXTENSIONS . '/static_site_exporter/lib/class.crawler.php');
Class extension_static_site_exporter extends Extension{
public function fetchNavigation(){
return array(
array(
'location' => 'System',
'name' => 'Static Site Exporter',
'link' => '/'
)
);
}
public function getSubscribedDelegates(){
return array(
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'appendPreferences'
),
array(
'page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => 'savePreferences'
),
);
}
public function savePreferences($context){
$pairs = array();
$conf = array_map('trim', $context['settings']['static-site-exporter']);
$force_include = preg_split('/[\r\n]+/i', $conf['force-include'], -1, PREG_SPLIT_NO_EMPTY);
$force_include = array_map('trim', $force_include);
$context['settings']['static-site-exporter'] = array(
'force-include' => implode(',', $force_include),
'include-404' => (isset($conf['include-404']) ? 'yes' : 'no'),
'index-file-name' => $conf['index-file-name'],
'export-location' => rtrim($conf['export-location'], '/ ')
);
}
public function appendPreferences($context){
$fieldset = new XMLElement('fieldset');
$fieldset->setAttribute('class', 'settings');
$fieldset->appendChild(new XMLElement('legend', 'Static Site Exporter'));
$ul = new XMLElement('ul');
$ul->setAttribute('class', 'group');
$li = new XMLElement('li');
$label = Widget::Label('Index File Name');
$label->appendChild(Widget::Input('settings[static-site-exporter][index-file-name]', General::Sanitize(Administration::Configuration()->get('index-file-name', 'static-site-exporter'))));
$li->appendChild($label);
$ul->appendChild($li);
$li = new XMLElement('li');
$label = Widget::Label('Export Location');
$label->appendChild(new XMLElement('i', 'Optional'));
$label->appendChild(Widget::Input('settings[static-site-exporter][export-location]', General::Sanitize(Administration::Configuration()->get('export-location', 'static-site-exporter'))));
$li->appendChild($label);
$li->appendChild(new XMLElement('p', 'Leave blank for default.', array('class' => 'help', 'title' => EXTENSIONS.'/static_site_exporter/exports/')));
$ul->appendChild($li);
$fieldset->appendChild($ul);
$ul = new XMLElement('ul');
$ul->setAttribute('class', 'group');
$force_include = preg_replace('/,/i', "\r\n", Administration::Configuration()->get('force-include', 'static-site-exporter'));
$li = new XMLElement('li');
$label = Widget::Label('Force Include');
$label->appendChild(new XMLElement('i', 'Optional'));
$label->appendChild(Widget::Textarea('settings[static-site-exporter][force-include]', 5, 50, General::sanitize($force_include)));
$li->appendChild($label);
$li->appendChild(new XMLElement('p', 'Relative to <code>'.DOCROOT.'</code>. Can be files or folders. Folders will be traversed and all sub-folders added. Separate each with a new line.', array('class' => 'help')));
$ul->appendChild($li);
$li = new XMLElement('li');
$label = Widget::Label();
$input = Widget::Input('settings[static-site-exporter][include-404]', 'yes', 'checkbox');
if(Symphony::Configuration()->get('include-404', 'static-site-exporter') == 'yes') $input->setAttribute('checked', 'checked');
$label->setValue($input->generate() . ' Include 404 pages in export archive');
$li->appendChild($label);
//$li->appendChild(new XMLElement('p', 'Checking this will leave <code>404 Not Found</code> pages in the archive.', array('class' => 'help')));
$ul->appendChild($li);
$fieldset->appendChild($ul);
$context['wrapper']->appendChild($fieldset);
}
public function fetchLinks(){
return Symphony::Database()->fetch("SELECT `last_indexed`, `time_to_index`, `status`, `url` FROM `".Crawler::TABLE."` ORDER BY `last_indexed` ASC");
}
public function averageTime(){
return Symphony::Database()->fetchVar('sum', 0, "SELECT SUM(`time_to_index`) AS `sum` FROM `".Crawler::TABLE."`");
}
public function totalLinks(){
return Symphony::Database()->fetchVar('count', 0, "SELECT COUNT(*) AS `count` FROM `".Crawler::TABLE."`");
}
public function lastIndexed(){
return Symphony::Database()->fetchVar('timestamp', 0, "SELECT UNIX_TIMESTAMP(`last_indexed`) AS `timestamp` FROM `".Crawler::TABLE."` ORDER BY `last_indexed` DESC LIMIT 1");
}
public function exportDestination(){
$dest = Symphony::Configuration()->get('export-location', 'static-site-exporter');
if(empty($dest)){
return MANIFEST . '/tmp';
}
else {
return DOCROOT . '/' . Symphony::Configuration()->get('export-location', 'static-site-exporter');
}
return $dest;
}
public function exportDestinationLink(){
return str_replace(DOCROOT, URL, $this->exportDestination()) . '/';
}
public function lastBuild(){
$dir = new DirectoryIterator($this->exportDestination());
$match = NULL;
foreach ($dir as $file){
if(is_array($match) && $file->getMTime() < $match['creation']) continue;
if(!$file->isDot() && !$file->isDir() && preg_match('/\.zip$/i', $file->getFilename())){
$match = array('filename' => $file->getFilename(), 'creation' => $file->getMTime());
}
}
return $match;
}
public function fetchStatusCodes(){
$codes = Symphony::Database()->fetch("SELECT `status`, COUNT(`status`) AS `count` FROM `".Crawler::TABLE."` GROUP BY `status`");
$tmp = array();
foreach($codes as $row){
$tmp[$row['status']] = $row['count'];
}
return $tmp;
}
public function install(){
Symphony::Configuration()->set('include-404', 'no', 'static-site-exporter');
Symphony::Configuration()->set('index-file-name', 'index.html', 'static-site-exporter');
Administration::instance()->saveConfig();
return Symphony::Database()->query("
CREATE TABLE `".Crawler::TABLE."` (
`id` int(11) unsigned NOT NULL auto_increment,
`url` varchar(255) NOT NULL,
`last_indexed` datetime default NULL,
`time_to_index` float default NULL,
`contents` text,
`status` int(4) unsigned default '200',
PRIMARY KEY (`id`),
UNIQUE KEY `url` (`url`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
");
}
public function uninstall(){
Symphony::Configuration()->remove('static-site-exporter');
Administration::instance()->saveConfig();
return Symphony::Database()->query('DROP TABLE `'.Crawler::TABLE.'`');
}
}
?>