PageRenderTime 66ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_sef/models/import.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 813 lines | 667 code | 85 blank | 61 comment | 79 complexity | 60d6f65da3691dc16aef4432c31e66fd MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * SEF component for Joomla!
  4. *
  5. * @package JoomSEF
  6. * @version 4.2.8
  7. * @author ARTIO s.r.o., http://www.artio.net
  8. * @copyright Copyright (C) 2012 ARTIO s.r.o.
  9. * @license GNU/GPLv3 http://www.artio.net/license/gnu-general-public-license
  10. */
  11. // Check to ensure this file is included in Joomla!
  12. defined('_JEXEC') or die();
  13. jimport('joomla.application.component.model');
  14. class SEFModelImport extends JModel
  15. {
  16. var $type = 'None';
  17. var $total = 0;
  18. var $imported = 0;
  19. var $notImported = 0;
  20. var $aceSefTablePresent = false;
  21. var $shSefTablePresent = false;
  22. var $dbChecked = false;
  23. /**
  24. * Constructor that retrieves variables from the request
  25. */
  26. function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. function import()
  31. {
  32. // Get the uploaded file information
  33. $userfile = JRequest::getVar('importfile', null, 'files', 'array' );
  34. // Make sure that file uploads are enabled in php
  35. if (!(bool) ini_get('file_uploads')) {
  36. JError::raiseWarning(100, JText::_('COM_SEF_UPLOADS_NOT_ALLOWED'));
  37. return false;
  38. }
  39. // If there is no uploaded file, we have a problem...
  40. if (!is_array($userfile)) {
  41. JError::raiseWarning(100, JText::_('COM_SEF_NO_FILE_SELECTED'));
  42. return false;
  43. }
  44. // Check if there was a problem uploading the file.
  45. if ($userfile['error'] || $userfile['size'] < 1) {
  46. JError::raiseWarning(100, JText::_('COM_SEF_ERROR_FILE_UPLOAD'));
  47. return false;
  48. }
  49. // Build the appropriate paths
  50. $config =& JFactory::getConfig();
  51. $tmp_dest = $config->getValue('config.tmp_path').DS.$userfile['name'];
  52. $tmp_src = $userfile['tmp_name'];
  53. // Move uploaded file
  54. jimport('joomla.filesystem.file');
  55. $uploaded = JFile::upload($tmp_src, $tmp_dest);
  56. if( !$uploaded ) {
  57. JError::raiseWarning( 100, JText::_('COM_SEF_ERROR_UPLOAD_FILE') );
  58. return false;
  59. }
  60. // load SQL
  61. $lines = file($tmp_dest);
  62. // We can delete the file now
  63. JFile::delete($tmp_dest);
  64. if( !is_array($lines) )
  65. {
  66. JError::raiseWarning( 100, JText::_('COM_SEF_ERROR_LOAD_FILE') );
  67. return false;
  68. }
  69. // Determine the export type (JoomSEF, sh404SEF, AceSEF)
  70. $type = $this->_determineType($lines);
  71. $result = true;
  72. $this->total = $this->imported = $this->notImported = 0;
  73. switch($type)
  74. {
  75. case 'JoomSEF':
  76. $this->type = JText::_('COM_SEF_JOOMSEF_URLS');
  77. $result = $this->_importJoomSEF($lines);
  78. break;
  79. case 'sh404SEF':
  80. $this->type = JText::_('COM_SEF_SH404SEF_URLS');
  81. $result = $this->_importSh404SEF($lines);
  82. break;
  83. case 'sh404SEFmeta':
  84. $this->type = JText::_('COM_SEF_SH404SEF_META_TAGS');
  85. $result = $this->_importSh404SEFmeta($lines);
  86. break;
  87. case 'sh404SEF_2_2_7_981':
  88. $this->type = JText::_('COM_SEF_SH404SEF_URLS_227981');
  89. $result = $this->_importSh404SEF_2_2_7_981($lines);
  90. break;
  91. case 'AceSEF':
  92. $this->type = JText::_('COM_SEF_ACESEF_URLS');
  93. $result = $this->_importAceSEF($lines);
  94. break;
  95. default:
  96. $this->type = JText::_('COM_SEF_UNKNOWN');
  97. JError::raiseWarning( 100, JText::_('COM_SEF_UNRECOGNIZED_FILE_FORMAT') );
  98. $result = false;
  99. }
  100. $this->total = $this->imported + $this->notImported;
  101. return $result;
  102. }
  103. function importDBAce()
  104. {
  105. $fieldsMap = array( 'cpt' => 'cpt',
  106. 'sefurl' => 'url_sef',
  107. 'origurl' => 'url_real',
  108. 'Itemid' => 'Itemid',
  109. 'metadesc' => 'metadesc',
  110. 'metakey' => 'metakey',
  111. 'metatitle' => 'metatitle',
  112. 'metalang' => 'metalang',
  113. 'metarobots' => 'metarobots',
  114. 'metagoogle' => 'metagoogle',
  115. 'canonicallink' => 'metacanonical',
  116. 'dateadd' => 'date',
  117. 'priority' => 'ordering');
  118. $db =& JFactory::getDBO();
  119. // Get all the data we need
  120. $query = "SELECT * FROM `#__acesef_urls`";
  121. $db->setQuery($query);
  122. $rows = $db->loadAssocList();
  123. $result = true;
  124. for( $i = 0, $n = count($rows); $i < $n; $i++ ) {
  125. $row =& $rows[$i];
  126. // Modify the assoc array to match our needs
  127. $row['cpt'] = 0;
  128. $row['Itemid'] = SEFTools::extractVariable($row['url_real'], 'Itemid');
  129. // Insert line to database
  130. if( !SEFModelImport::_insertLine($row, $fieldsMap) ) {
  131. $result = false;
  132. }
  133. }
  134. $this->type = JText::_('COM_SEF_ACESEF_DATABASE');
  135. return $result;
  136. }
  137. function importDBSh()
  138. {
  139. $fieldsMap = array( 'cpt' => 'cpt',
  140. 'sefurl' => 'oldurl',
  141. 'origurl' => 'newurl',
  142. 'Itemid' => 'Itemid',
  143. 'metadesc' => 'metadesc',
  144. 'metakey' => 'metakey',
  145. 'metatitle' => 'metatitle',
  146. 'metalang' => 'metalang',
  147. 'metarobots' => 'metarobots',
  148. 'metagoogle' => 'metagoogle',
  149. 'canonicallink' => 'canonicallink',
  150. 'dateadd' => 'dateadd',
  151. 'priority' => 'rank');
  152. $db =& JFactory::getDBO();
  153. // Get all the data we need
  154. $query = "SELECT `r`.*, `m`.`metadesc`, `m`.`metakey`, `m`.`metatitle`, `m`.`metalang`, `m`.`metarobots` FROM `#__sh404sef_urls` AS `r` LEFT JOIN `#__sh404sef_metas` AS `m` ON (`m`.`newurl` = `r`.`newurl`)";
  155. $db->setQuery($query);
  156. $rows = $db->loadAssocList();
  157. $result = true;
  158. for( $i = 0, $n = count($rows); $i < $n; $i++ ) {
  159. $row =& $rows[$i];
  160. // Modify the assoc array to match our needs
  161. $row['Itemid'] = SEFTools::extractVariable($row['newurl'], 'Itemid');
  162. if (isset($row['rank'])) {
  163. if ($row['rank'] != '0') {
  164. $row['rank'] = '100';
  165. }
  166. }
  167. else {
  168. if( !empty($row['Itemid']) ) {
  169. $row['rank'] = 90;
  170. } else {
  171. $row['rank'] = 95;
  172. }
  173. }
  174. // Insert line to database
  175. if( !SEFModelImport::_insertLine($row, $fieldsMap) ) {
  176. $result = false;
  177. }
  178. }
  179. $this->type = JText::_('COM_SEF_SH404SEF_DATABASE');
  180. return $result;
  181. }
  182. function getAceSefTablePresent()
  183. {
  184. if( !$this->dbChecked )
  185. {
  186. $this->_checkDB();
  187. }
  188. return $this->aceSefTablePresent;
  189. }
  190. function getShTablePresent()
  191. {
  192. if( !$this->dbChecked )
  193. {
  194. $this->_checkDB();
  195. }
  196. return $this->shSefTablePresent;
  197. }
  198. function _determineType(&$lines)
  199. {
  200. $type = 'Unknown';
  201. $n = count($lines);
  202. if( $n == 0 )
  203. {
  204. return $type;
  205. }
  206. // Loop through lines trying to determine file format
  207. for( $i = 0; $i < $n; $i++ )
  208. {
  209. $line = trim($lines[$i]);
  210. if( preg_match('/^INSERT\s+INTO\s+`?\w+(redirection|sefurls)`?/i', $line) > 0 )
  211. {
  212. $type = 'JoomSEF';
  213. break;
  214. }
  215. if( preg_match('/^INSERT\s+INTO\s+`?\w+acesef_urls`?/i', $line) > 0 )
  216. {
  217. $type = 'AceSEF';
  218. break;
  219. }
  220. if( strpos(strtolower($line), '"id","count","rank","sef url","non-sef url","date added"') !== false )
  221. {
  222. $type = 'sh404SEF';
  223. break;
  224. }
  225. if( strpos(strtolower($line), '"id","newurl","metadesc","metakey","metatitle","metalang","metarobots"') !== false )
  226. {
  227. $type = 'sh404SEFmeta';
  228. break;
  229. }
  230. if( strpos(strtolower($line), '"nbr","sef url","non sef url","hits","rank","date added","page title","page description","page keywords","page language","robots tag"') !== false )
  231. {
  232. $type = 'sh404SEF_2_2_7_981';
  233. break;
  234. }
  235. }
  236. return $type;
  237. }
  238. function _importJoomSEF(&$lines)
  239. {
  240. $config =& JFactory::getConfig();
  241. $dbprefix = $config->getValue('config.dbprefix');
  242. // Get allowed columns
  243. $fields = $this->_db->getTableColumns('#__sefurls');
  244. $columns = array_keys($fields);
  245. $result = true;
  246. for ($i = 0, $n = count($lines); $i < $n; $i++) {
  247. // Trim line
  248. $line = trim($lines[$i]);
  249. // Ignore empty lines
  250. if (strlen($line) == 0) continue;
  251. // If the query continues at the next line.
  252. while (substr($line, -1) != ';' && $i + 1 < count($lines)) {
  253. $i++;
  254. $newLine = trim($lines[$i]);
  255. if (strlen($newLine) == 0) continue;
  256. $line .= ' '.$lines[$i];
  257. }
  258. if (preg_match('/^INSERT(\s)+INTO(\s)+`?(\w)+(redirection|sefurls)`?/i', $line) > 0) {
  259. // fix for files exported from versions older than 1.3.0
  260. if (strstr($line, "redirection` VALUES") != false) {
  261. $line = str_replace("redirection` VALUES", "redirection` (id, cpt, sefurl, origurl, dateadd) VALUES", $line);
  262. }
  263. // fix for files exported from versions prior to 2.0.0
  264. if (!strstr($line, 'origurl') && stristr($line, "newurl, Itemid") == false) {
  265. $url = preg_replace('/.*VALUES\s*\(\'[^\']*\',\s*\'[^\']*\',\s*\'[^\']*\',\s*\'([^\']*)\'.*/', '$1', $line);
  266. $itemid = preg_replace('/.*[&?]Itemid=([^&]*).*/', '$1', $url);
  267. //$newurl = eregi_replace("Itemid=[^&]*", '', $url);
  268. $newurl = preg_replace('/Itemid=[^&]*/i', '', $url);
  269. $newurl = trim($newurl, '&?');
  270. $trans = array( '&&' => '&', '?&' => '?' );
  271. $newurl = strtr($newurl, $trans);
  272. $line = str_replace('newurl,', 'newurl, Itemid,', $line);
  273. $line = str_replace("'$url'", "'$newurl','$itemid'", $line);
  274. }
  275. // upgrade to 3.3.0
  276. $line = str_replace(array('redirection', 'newurl', 'oldurl'), array('sefurls', 'origurl', 'sefurl'), $line);
  277. // Fix the table name for different prefix
  278. $line = str_replace('jos_sefurls', "{$dbprefix}sefurls", $line);
  279. // REMOVE UNKNOWN COLUMNS
  280. // Parse columns and values
  281. $matches = array();
  282. if (preg_match('#^INSERT\s+INTO[^(]+\(([^)]+)\)\s+VALUES[^(]+\((.+)\)[^)]*$#i', $line, $matches) > 0) {
  283. $cols = $this->_parseCsvLine($matches[1]);
  284. $vals = $this->_parseCsvLine($matches[2], ',', "'");
  285. // Create associative array of columns and values
  286. if (count($cols) == count($vals)) {
  287. $cols = array_map('trim', $cols);
  288. $values = array_combine($cols, $vals);
  289. // Remove unknown columns
  290. foreach ($cols as $col) {
  291. if (!in_array($col, $columns)) {
  292. unset($values[$col]);
  293. }
  294. }
  295. // Update SQL
  296. $cols = implode(', ', array_keys($values));
  297. $vals = array();
  298. foreach ($values as $val) {
  299. $vals[] = "'".str_replace("'", "\\'", $val)."'";
  300. }
  301. $vals = implode(', ', $vals);
  302. $line = str_replace(array($matches[1], $matches[2]), array($cols, $vals), $line);
  303. }
  304. }
  305. $this->_db->setQuery($line);
  306. if (!$this->_db->query()) {
  307. $this->notImported++;
  308. JError::raiseWarning( 100, JText::_('COM_SEF_ERROR_IMPORTING_LINE') . ': ' . $line . '<br />' . $this->_db->getErrorMsg());
  309. $result = false;
  310. }
  311. else {
  312. $this->imported++;
  313. }
  314. }
  315. else {
  316. JError::raiseWarning(100, JText::_('COM_SEF_IGNORING_LINE') . ': ' . $line);
  317. }
  318. }
  319. return $result;
  320. }
  321. function _importSh404SEF(&$lines)
  322. {
  323. $fieldsMap = array( 'cpt' => 'Count',
  324. 'sefurl' => 'SEF URL',
  325. 'origurl' => 'non-SEF URL',
  326. 'Itemid' => 'Itemid',
  327. 'metadesc' => 'metadesc',
  328. 'metakey' => 'metakey',
  329. 'metatitle' => 'metatitle',
  330. 'metalang' => 'metalang',
  331. 'metarobots' => 'metarobots',
  332. 'metagoogle' => 'metagoogle',
  333. 'canonicallink' => 'canonicallink',
  334. 'dateadd' => 'Date added',
  335. 'priority' => 'priority');
  336. $result = true;
  337. $fields = array();
  338. for ($i = 0, $n = count($lines); $i < $n; $i++) {
  339. // Trim line
  340. $line = trim($lines[$i]);
  341. // Ignore empty lines
  342. if (strlen($line) == 0) continue;
  343. // Split the line to values
  344. $values = $this->_parseCsvLine($line);
  345. $this->_cleanFields($values);
  346. // If this is the first parsed line, use it for field names
  347. if( count($fields) == 0 ) {
  348. $fields = $values;
  349. continue;
  350. }
  351. // Create the associative array of fields and values
  352. $assoc = array_combine($fields, $values);
  353. // Modify the assoc array to match our needs
  354. $assoc['Itemid'] = SEFTools::extractVariable($assoc['non-SEF URL'], 'Itemid');
  355. if( !empty($assoc['Itemid']) ) {
  356. $assoc['priority'] = 90;
  357. } else {
  358. $assoc['priority'] = 95;
  359. }
  360. // Insert line to database
  361. if( !SEFModelImport::_insertLine($assoc, $fieldsMap) ) {
  362. $result = false;
  363. }
  364. }
  365. return $result;
  366. }
  367. function _importSh404SEF_2_2_7_981(&$lines)
  368. {
  369. $fieldsMap = array( 'cpt' => 'Hits',
  370. 'sefurl' => 'Sef url',
  371. 'origurl' => 'Non sef url',
  372. 'Itemid' => 'Itemid',
  373. 'metadesc' => 'Page description',
  374. 'metakey' => 'Page keywords',
  375. 'metatitle' => 'Page title',
  376. 'metalang' => 'Page language',
  377. 'metarobots' => 'Robots tag',
  378. 'metagoogle' => 'metagoogle',
  379. 'canonicallink' => 'canonicallink',
  380. 'dateadd' => 'Date added',
  381. 'priority' => 'Rank');
  382. $result = true;
  383. $fields = array();
  384. for ($i = 0, $n = count($lines); $i < $n; $i++) {
  385. // Trim line
  386. $line = trim($lines[$i]);
  387. // Ignore empty lines
  388. if (strlen($line) == 0) continue;
  389. // Split the line to values
  390. $values = $this->_parseCsvLine($line);
  391. $this->_cleanFields($values);
  392. // If this is the first parsed line, use it for field names
  393. if( count($fields) == 0 ) {
  394. $fields = $values;
  395. continue;
  396. }
  397. // Create the associative array of fields and values
  398. $assoc = array_combine($fields, $values);
  399. // Modify the assoc array to match our needs
  400. $assoc['Itemid'] = SEFTools::extractVariable($assoc['Non sef url'], 'Itemid');
  401. if (isset($assoc['Rank'])) {
  402. if ($assoc['Rank'] != '0') {
  403. $assoc['Rank'] = '100';
  404. }
  405. }
  406. // Insert line to database
  407. if( !SEFModelImport::_insertLine($assoc, $fieldsMap) ) {
  408. $result = false;
  409. }
  410. }
  411. return $result;
  412. }
  413. function _parseCsvLine($line, $comma = ',', $quote = '"')
  414. {
  415. if (strpos($line, $quote) === false) {
  416. return explode($comma, $line);
  417. }
  418. $len = strlen($line);
  419. $values = array();
  420. while ($len > 0) {
  421. $pos_comma = strpos($line, $comma);
  422. $pos_quote = strpos($line, $quote);
  423. if (is_int($pos_comma)) {
  424. // More values
  425. if (!is_int($pos_quote) || ($pos_comma < $pos_quote)) {
  426. // Value without enclosure
  427. $value = substr($line, 0, $pos_comma);
  428. $line = substr($line, $pos_comma + 1);
  429. }
  430. else {
  431. // Enclosed value
  432. $found = false;
  433. $offset = $pos_quote + 1;
  434. while (!$found) {
  435. $pos_quote2 = strpos($line, $quote, $offset);
  436. if ($pos_quote2 === false) {
  437. return false;
  438. }
  439. if (($pos_quote2 == $offset) || ($line[$pos_quote2 - 1] != '\\')) {
  440. // Ending enclosure
  441. $value = substr($line, $pos_quote + 1, $pos_quote2 - $pos_quote - 1);
  442. $value = str_replace('\\'.$quote, $quote, $value);
  443. // Truncate line
  444. $pos_comma = strpos($line, $comma, $pos_quote2 + 1);
  445. if (is_int($pos_comma)) {
  446. $line = substr($line, $pos_comma + 1);
  447. }
  448. else {
  449. $line = substr($line, $pos_quote2 + 1);
  450. }
  451. $found = true;
  452. }
  453. else {
  454. // Escaped enclosure
  455. $offset = $pos_quote2 + 1;
  456. }
  457. }
  458. }
  459. }
  460. else if (!is_int($pos_comma)) {
  461. // Last value
  462. if (is_int($pos_quote)) {
  463. // Enclosed
  464. $value = trim($line);
  465. $value = trim($value, $quote);
  466. $value = str_replace('\\'.$quote, $quote, $value);
  467. }
  468. else {
  469. // Not enclosed
  470. $value = $line;
  471. }
  472. $line = '';
  473. }
  474. $values[] = $value;
  475. $len = strlen($line);
  476. }
  477. return $values;
  478. }
  479. function _importSh404SEFmeta(&$lines)
  480. {
  481. $fieldsMap = array( 'origurl' => 'newurl',
  482. 'Itemid' => 'Itemid',
  483. 'metadesc' => 'metadesc',
  484. 'metakey' => 'metakey',
  485. 'metatitle' => 'metatitle',
  486. 'metalang' => 'metalang',
  487. 'metarobots' => 'metarobots');
  488. $updateKeys = array('origurl', 'Itemid');
  489. $result = true;
  490. $fields = array();
  491. for ($i = 0, $n = count($lines); $i < $n; $i++) {
  492. // Trim line
  493. $line = trim($lines[$i]);
  494. // Ignore empty lines
  495. if (strlen($line) == 0) continue;
  496. // Split the line to values
  497. $values = $this->_parseCsvLine($line);
  498. $this->_cleanFields($values);
  499. $this->_shUnEmptyFields($values);
  500. // If this is the first parsed line, use it for field names
  501. if( count($fields) == 0 ) {
  502. $fields = $values;
  503. continue;
  504. }
  505. // Create the associative array of fields and values
  506. $assoc = array_combine($fields, $values);
  507. // Modify the assoc array to match our needs
  508. $assoc['Itemid'] = SEFTools::extractVariable($assoc['newurl'], 'Itemid');
  509. // Update line in database
  510. if( !SEFModelImport::_updateLine($assoc, $fieldsMap, $updateKeys) ) {
  511. $result = false;
  512. }
  513. }
  514. return $result;
  515. }
  516. function _importAceSEF(&$lines)
  517. {
  518. $fieldsMap = array( 'cpt' => 'cpt',
  519. 'sefurl' => 'url_sef',
  520. 'origurl' => 'url_real',
  521. 'Itemid' => 'Itemid',
  522. 'metadesc' => 'metadesc',
  523. 'metakey' => 'metakey',
  524. 'metatitle' => 'metatitle',
  525. 'metalang' => 'metalang',
  526. 'metarobots' => 'metarobots',
  527. 'metagoogle' => 'metagoogle',
  528. 'canonicallink' => 'metacanonical',
  529. 'dateadd' => 'date',
  530. 'priority' => 'ordering');
  531. $result = true;
  532. for ($i = 0, $n = count($lines); $i < $n; $i++) {
  533. // Trim line
  534. $line = trim($lines[$i]);
  535. // Ignore empty lines
  536. if (strlen($line) == 0) continue;
  537. // If the query continues at the next line.
  538. while (substr($line, -1) != ';' && $i + 1 < count($lines)) {
  539. $i++;
  540. $newLine = trim($lines[$i]);
  541. if (strlen($newLine) == 0) continue;
  542. $line .= ' '.$lines[$i];
  543. }
  544. if (preg_match('/^INSERT\s+INTO\s+`?\w+acesef_urls`?/i', $line) > 0) {
  545. // Parse the line
  546. $pos = strpos($line, '(');
  547. if( $pos !== false ) {
  548. $line = substr($line, $pos+1);
  549. }
  550. $line = str_replace(');', '', $line);
  551. // Split the line to fields and values
  552. list($fields, $values) = explode(') VALUES (', $line);
  553. $fields = explode(',', $fields);
  554. $values = explode("', '", $values);
  555. $this->_cleanFields($fields);
  556. $this->_cleanFields($values);
  557. // Create the associative array of fields and values
  558. $assoc = array_combine($fields, $values);
  559. // Modify the assoc array to match our needs
  560. $assoc['cpt'] = 0;
  561. $assoc['Itemid'] = SEFTools::extractVariable($assoc['url_real'], 'Itemid');
  562. // Insert line to database
  563. if( !SEFModelImport::_insertLine($assoc, $fieldsMap) ) {
  564. $result = false;
  565. }
  566. }
  567. else {
  568. JError::raiseWarning(100, JText::_('COM_SEF_IGNORING_LINE') . ': ' . $line);
  569. }
  570. }
  571. return $result;
  572. }
  573. function _cleanFields(&$fields)
  574. {
  575. for( $i = 0, $n = count($fields); $i < $n; $i++ ) {
  576. $fields[$i] = trim($fields[$i], " `'\"");
  577. }
  578. }
  579. function _shUnEmptyFields(&$fields)
  580. {
  581. for( $i = 0, $n = count($fields); $i < $n; $i++ ) {
  582. if( $fields[$i] == '&nbsp' ) {
  583. $fields[$i] = '';
  584. }
  585. }
  586. }
  587. function _insertLine(&$assoc, &$fieldsMap)
  588. {
  589. // Build the SQL query
  590. $query = "INSERT INTO `#__sefurls` (";
  591. $keys = array_keys($fieldsMap);
  592. $query .= '`' . implode('`, `', $keys) . '`) VALUES (';
  593. for( $j = 0, $n2 = count($keys); $j < $n2; $j++ )
  594. {
  595. $key = $keys[$j];
  596. $map = $fieldsMap[$key];
  597. if( isset($assoc[$map]) ) {
  598. $query .= "'" . $assoc[$map] . "'";
  599. }
  600. else {
  601. $query .= "''";
  602. }
  603. if( $j < ($n2 - 1) ) {
  604. $query .= ', ';
  605. }
  606. }
  607. $query .= ')';
  608. // Try to run the query
  609. $this->_db->setQuery($query);
  610. if (!$this->_db->query()) {
  611. $this->notImported++;
  612. JError::raiseWarning( 100, JText::_('COM_SEF_ERROR_IMPORTING_LINE') . ': ' . $query . '<br />' . $this->_db->getErrorMsg());
  613. return false;
  614. }
  615. else {
  616. $this->imported++;
  617. }
  618. return true;
  619. }
  620. function _updateLine(&$assoc, &$fieldsMap, &$updateKeys)
  621. {
  622. // Build the SQL query
  623. $query = "UPDATE `#__sefurls` SET ";
  624. $keys = array_keys($fieldsMap);
  625. $set = array();
  626. $where = array();
  627. for( $j = 0, $n2 = count($keys); $j < $n2; $j++ )
  628. {
  629. $key = $keys[$j];
  630. $map = $fieldsMap[$key];
  631. if( isset($assoc[$map]) ) {
  632. $value = "`$key` = '{$assoc[$map]}'";
  633. if( in_array($key, $updateKeys) ) {
  634. $where[] = '(' . $value . ')';
  635. }
  636. else {
  637. $set[] = $value;
  638. }
  639. }
  640. }
  641. if( (count($set) == 0 )|| (count($where) == 0) ) {
  642. return false;
  643. }
  644. // Add the set, where and limit parts
  645. $query .= implode(', ', $set);
  646. $query .= ' WHERE ' . implode(' AND ', $where);
  647. $query .= ' LIMIT 1';
  648. // Try to run the query
  649. $this->_db->setQuery($query);
  650. if (!$this->_db->query()) {
  651. $this->notImported++;
  652. JError::raiseWarning( 100, JText::_('COM_SEF_ERROR_IMPORTING_LINE') . ': ' . $query . '<br />' . $this->_db->getErrorMsg());
  653. return false;
  654. }
  655. else {
  656. $this->imported++;
  657. }
  658. return true;
  659. }
  660. function _checkDB()
  661. {
  662. $db =& JFactory::getDBO();
  663. $config = JFactory::getConfig();
  664. $prefix = $config->getValue('dbprefix');
  665. $tables = $db->getTableList();
  666. // Check AceSEF installation
  667. if (in_array($prefix.'acesef_urls', $tables)) {
  668. $this->aceSefTablePresent = true;
  669. }
  670. // Check sh404SEF installation
  671. if (in_array($prefix.'sh404sef_urls', $tables)) {
  672. $query = "SELECT * FROM `#__sh404sef_urls` LIMIT 1";
  673. $db->setQuery($query);
  674. $row = $db->loadObject();
  675. if(isset($row->oldurl) && isset($row->newurl)) {
  676. $this->shSefTablePresent = true;
  677. }
  678. }
  679. $this->dbChecked = true;
  680. }
  681. }
  682. ?>