metainfo.php
9.32 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
// namespace administrator\components\com_jmap\models;
/**
* @package JMAP::METAINFO::administrator::components::com_jmap
* @subpackage models
* @author Joomla! Extensions Store
* @copyright (C) 2015 - Joomla! Extensions Store
* @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
*/
defined ( '_JEXEC' ) or die ( 'Restricted access' );
jimport ( 'joomla.filesystem.file' );
/**
* Metainfo concrete model
* Operates not on DB but directly on a cached copy of the XML sitemap file
*
* @package JMAP::METAINFO::administrator::components::com_jmap
* @subpackage models
* @since 3.2
*/
class JMapModelMetainfo extends JMapModel {
/**
* Number of XML records
*
* @access private
* @var Int
*/
private $recordsNumber;
/**
* Main get data method
*
* @access public
* @return Object[]
*/
public function getData() {
// Load data from XML file, parse it to load records
$cachedSitemapFilePath = JPATH_COMPONENT_ADMINISTRATOR . '/cache/metainfo/';
// Has sitemap some vars such as lang or Itemid?
$sitemapLang = $this->getState('sitemaplang', '');
$sitemapLinksLang = $sitemapLang ? $sitemapLang . '/' : '';
$sitemapLang = $sitemapLang ? '_' . $sitemapLang : '';
$sitemapDataset = $this->getState('sitemapdataset', '');
$sitemapDataset = $sitemapDataset ? '_dataset' . (int)$sitemapDataset : '';
$sitemapItemid = $this->getState('sitemapitemid', '');
$sitemapItemid = $sitemapItemid ? '_menuid' . (int)$sitemapItemid : '';
// Final name
$cachedSitemapFilename = 'sitemap_xml' . $sitemapLang . $sitemapDataset . $sitemapItemid . '.xml';
// Detect PHP 7
$php7 = false;
if (version_compare(PHP_VERSION, '7.0', '>=')) {
$php7 = true;
}
// Start processing
try {
// Now check if the file correctly exists
if(JFile::exists($cachedSitemapFilePath . $cachedSitemapFilename)) {
$loadedSitemapXML = simplexml_load_file($cachedSitemapFilePath . $cachedSitemapFilename);
if(!$loadedSitemapXML) {
throw new JMapException ( 'Invalid XML', 'error' );
}
} else {
throw new JMapException ( JText::sprintf ( 'COM_JMAP_METAINFO_NOCACHED_FILE_EXISTS', $this->_db->getErrorMsg () ), 'error' );
}
// Execute HTTP request and associate HTTP response code with each record links
if($loadedSitemapXML->url->count()) {
// Manage splice pagination here for the XML records
$convertedIteratorToArray = iterator_to_array($loadedSitemapXML->url, false);
// Store number of records for pagination
$this->recordsNumber = count($convertedIteratorToArray);
// Execute pagination splicing if any limit is set
$limit = $this->getState ( 'limit' );
if($limit) {
$loadedSitemapXML = array_splice($convertedIteratorToArray, $this->getState ( 'limitstart' ), $this->getState ( 'limit' ));
} else {
$loadedSitemapXML = $convertedIteratorToArray;
}
// Get a search filter
$searchFilter = $this->state->get('searchpageword', null);
$stateFilter = $this->state->get('state', null);
$excludeStateFilter = $this->state->get('excludestate', null);
// Cycle on every links, filter by search word if any and augment with link metainfo
foreach ($loadedSitemapXML as $index=>&$url) {
// Evaluate filtering by search word
if($searchFilter) {
// Evaluate position or exact match
if($this->getState('exactsearchpage', null)) {
$isMatching = $url->loc == $searchFilter;
} else {
$isMatching = (stripos($url->loc, $searchFilter) !== false);
}
if(!$isMatching) {
array_splice($loadedSitemapXML, $index, 1);
// Re-assign array
if($php7) {
$tmp = array_values($loadedSitemapXML);
$loadedSitemapXML = $tmp;
}
continue;
}
}
$url = (object)(array)$url;
// Load meta info for this link from the table and augment the array and object
$query = "SELECT *" .
"\n FROM " . $this->_db->quoteName('#__jmap_metainfo') .
"\n WHERE " . $this->_db->quoteName('linkurl') . " = " . $this->_db->quote($url->loc);
$metaInfos = $this->_db->setQuery($query)->loadObject();
// This link has valid metainfo
if(isset($metaInfos->id)) {
$url->metainfos = $metaInfos;
}
// Evaluate the first link scheme to detect a possible unmatching and required https migration
if($index == 0) {
$query = "SELECT " . $this->_db->quoteName('linkurl') .
"\n FROM " . $this->_db->quoteName('#__jmap_metainfo');
$sampleUrl = $this->_db->setQuery($query)->loadResult();
// Get current URI scheme
$currentUriScheme = JUri::getInstance()->getScheme();
if($sampleUrl && stripos($sampleUrl, $currentUriScheme) === false) {
$this->setState('needhttpsmigration', true);
}
}
// Evaluate filtering by state
if($stateFilter) {
if(isset($url->metainfos->published)) {
$isMatching = ((int)$url->metainfos->published === ($stateFilter == 'P' ? 1 : 0));
if(!$isMatching) {
array_splice($loadedSitemapXML, $index, 1);
// Re-assign array
if($php7) {
$tmp = array_values($loadedSitemapXML);
$loadedSitemapXML = $tmp;
}
continue;
}
} else {
array_splice($loadedSitemapXML, $index, 1);
// Re-assign array
if($php7) {
$tmp = array_values($loadedSitemapXML);
$loadedSitemapXML = $tmp;
}
continue;
}
}
// Evaluate filtering by exclude state
if(is_numeric($excludeStateFilter)) {
if(isset($url->metainfos->excluded)) {
$isMatching = ((int)$url->metainfos->excluded === (int)$excludeStateFilter);
if(!$isMatching) {
array_splice($loadedSitemapXML, $index, 1);
// Re-assign array
if($php7) {
$tmp = array_values($loadedSitemapXML);
$loadedSitemapXML = $tmp;
}
continue;
}
} else {
if((int)$excludeStateFilter == 1) {
array_splice($loadedSitemapXML, $index, 1);
// Re-assign array
if($php7) {
$tmp = array_values($loadedSitemapXML);
$loadedSitemapXML = $tmp;
}
continue;
}
}
}
}
// Perform array sorting if any
$order = $this->getState('order', null);
$jmapMetainfoOrderDir = $this->getState('order_dir', 'asc');
if($order == 'link') {
function cmpAsc($a, $b){
return strcmp($a->loc, $b->loc);
}
function cmpDesc($a, $b){
return strcmp($b->loc, $a->loc);
}
$callbackName = ($jmapMetainfoOrderDir == 'asc') ? 'cmpAsc' : 'cmpDesc';
usort($loadedSitemapXML, $callbackName);
}
} else {
throw new JMapException ( JText::sprintf ( 'COM_JMAP_METAINFO_EMPTY_SITEMAP', $this->_db->getErrorMsg () ), 'notice' );
}
} catch ( JMapException $e ) {
$this->app->enqueueMessage ( $e->getMessage (), $e->getErrorLevel () );
$loadedSitemapXML = array ();
} catch ( Exception $e ) {
$jmapException = new JMapException ( $e->getMessage (), 'error' );
$this->app->enqueueMessage ( $jmapException->getMessage (), $jmapException->getErrorLevel () );
$loadedSitemapXML = array ();
}
return $loadedSitemapXML;
}
/**
* Delete DB meta info completely
*
* @access public
* @return boolean
*/
public function deleteEntity($ids) {
try {
$query = "DELETE FROM #__jmap_metainfo";
$this->_db->setQuery($query);
if(!$this->_db->execute()) {
throw new JMapException($this->_db->getErrorMsg(), 'error');
}
} catch (JMapException $e) {
$this->setError($e);
return false;
} catch (Exception $e) {
$jMapException = new JMapException($e->getMessage(), 'error');
$this->setError($jMapException);
return false;
}
return true;
}
/**
* Update metainfo records to https domain
*
* @access public
* @return boolean
*/
public function httpsMigrate() {
try {
$query = "UPDATE " . $this->_db->quoteName('#__jmap_metainfo') .
"\n SET " . $this->_db->quoteName('linkurl') . " = REPLACE(" . $this->_db->quoteName('linkurl') . ", 'http://', 'https://')";
$this->_db->setQuery($query);
if(!$this->_db->execute()) {
throw new JMapException($this->_db->getErrorMsg(), 'error');
}
} catch (JMapException $e) {
$this->setError($e);
return false;
} catch (Exception $e) {
$jMapException = new JMapException($e->getMessage(), 'error');
$this->setError($jMapException);
return false;
}
return true;
}
/**
* Counter result set
*
* @access public
* @return int
*/
public function getTotal() {
// Return simply the XML records number
return $this->recordsNumber;
}
/**
* Return select lists used as filter for listEntities
*
* @access public
* @return array
*/
public function getFilters() {
$filters = array ();
$filters ['state'] = JHtml::_ ( 'grid.state', $this->getState ( 'state' ) );
$excludeStates = array();
$excludeStates[] = JHtml::_('select.option', null, JText::_('COM_JMAP_ALL_METAINFO_LINKS'));
$excludeStates[] = JHtml::_('select.option', 1, JText::_('COM_JMAP_EXCLUDED_LINKS'));
$excludeStates[] = JHtml::_('select.option', 0, JText::_('COM_JMAP_NOT_EXCLUDED_LINKS'));
$filters ['excludestate'] = JHtml::_ ( 'select.genericlist', $excludeStates, 'filter_excludestate', 'onchange="Joomla.submitform();"', 'value', 'text', $this->getState ( 'excludestate' ));
return $filters;
}
}