/** * Delete a list of aliases from their ids, * passed as params * * @param array of integer $ids the list of aliases id to delete * @return boolean true if success */ public function deleteByIds($ids = array()) { if (empty($ids)) { return false; } try { ShlDbHelper::deleteIn($this->_getTableName(), 'id', $ids, ShlDbHelper::INTEGER); } catch (Exception $e) { $this->setError('Internal database error # ' . $e->getMessage()); return false; } return true; }
/** * Creates a record in the database, based * on data read from import file * * @param array $header an array of fields, as built from the header line * @param string $line raw record obtained from import file */ protected function _createRecord($header, $line) { // extract the record $line = $this->_lineToArray(trim($line)); // get table object to store record $model = ShlMvcModel_Base::getInstance('metas', 'Sh404sefModel'); // bind table to current record $record = array(); $record['newurl'] = $line[1]; $record['metatitle'] = $line[4]; $record['metadesc'] = $line[2]; $record['metakey'] = $line[3]; $record['metalang'] = $line[5]; $record['metarobots'] = $line[6]; // clean up records foreach ($record as $key => $value) { if ($value == ' ') { $record[$key] = ''; } } // find if there is already an url record for this non-sef url. If so // we want the imported record to overwrite the existing one. // while makinf sure we're doing that with the main url, not one of the duplicates $existingRecords = $model->getByAttr(array('newurl' => $record['newurl'])); if (!empty($existingRecords)) { $existingRecord = $existingRecords[0]; // getByAttr always returns an array // use the existing id, this will be enought to override existing record when saving $record['id'] = $existingRecord->id; // ensure consistency : delete the remaining records, though there is no reason // there can be more than one record with same SEF AND same SEF array_shift($existingRecords); if (!empty($existingRecords)) { ShlDbHelper::deleteIn('#__sh404sef_metas', 'id', $existingRecords, ShlDbHelper::INTEGER); } } else { $record['id'] = 0; } // save record : returns the record id, so failure is when 0 is returned $status = $model->save($record); if (!$status) { // rethrow a more appropriate error message throw new Sh404sefExceptionDefault(JText::sprintf('COM_SH404SEF_IMPORT_ERROR_INSERTING_INTO_DB', $line[0])); } }
/** * Creates a record in the database, based * on data read from import file * * @param array $header an array of fields, as built from the header line * @param string $line raw record obtained from import file */ protected function _createRecord($header, $line) { // extract the record $line = $this->_lineToArray($line); // get table object to store record $model = ShlMvcModel_Base::getInstance('aliases', 'Sh404sefModel'); // bind table to current record $record = array(); $record['newurl'] = $line[3]; if ($record['newurl'] == '__ Homepage __') { $record['newurl'] = sh404SEF_HOMEPAGE_CODE; } $record['alias'] = $line[1]; $record['type'] = $line[4]; // find if there is already same alias record for this non-sef url. If so // we want the imported record to overwrite the existing one. $existingRecords = $model->getByAttr(array('newurl' => $record['newurl'], 'alias' => $record['alias'])); if (!empty($existingRecords)) { $existingRecord = $existingRecords[0]; // getByAttr always returns an array // use the existing id, this will be enought to override existing record when saving $record['id'] = $existingRecord->id; // ensure consistency : delete the remaining records, though there is no reason // there can be more than one record with same alias AND same SEF array_shift($existingRecords); if (!empty($existingRecords)) { ShlDbHelper::deleteIn('#__sh404sef_aliases', 'id', $existingRecords, ShlDbHelper::INTEGER); } } // save record : returns the record id, so failure is when 0 is returned $saveId = $model->save($record); if (empty($saveId)) { // rethrow a more appropriate error message throw new Sh404sefExceptionDefault(JText::sprintf('COM_SH404SEF_IMPORT_ERROR_INSERTING_INTO_DB', $line[0])); } }
/** * Creates a record in the database, based * on data read from import file * * @param array $header an array of fields, as built from the header line * @param string $line raw record obtained from import file */ protected function _createRecord($header, $line) { // extract the record $line = $this->_lineToArray($line); // get table object to store record $model = ShlMvcModel_Base::getInstance('editurl', 'Sh404sefModel'); // bind table to current record $record = array(); $record['oldurl'] = $line[1]; $record['newurl'] = $line[2]; if ($record['newurl'] == '__ Homepage __') { $record['newurl'] = sh404SEF_HOMEPAGE_CODE; } $record['cpt'] = $line[3]; $record['rank'] = $line[4]; $record['dateadd'] = $line[5]; $record['metatitle'] = $line[6]; $record['metadesc'] = $line[7]; $record['metakey'] = $line[8]; $record['metalang'] = $line[9]; $record['metarobots'] = $line[10]; // find if there is already an url record for this non-sef url. If so // we want the imported record to overwrite the existing one. // while makinf sure we're doing that with the main url, not one of the duplicates $existingRecords = $model->getByAttr(array('newurl' => $record['newurl'], 'oldurl' => $record['oldurl'])); if (!empty($existingRecords)) { $existingRecord = $existingRecords[0]; // getByAttr always returns an array // use the existing id, this will be enought to override existing record when saving $record['id'] = $existingRecord->id; // ensure consistency : delete the remaining records, though there is no reason // there can be more than one record with same SEF AND same SEF array_shift($existingRecords); if (!empty($existingRecords)) { ShlDbHelper::deleteIn('#__sh404sef_urls', 'id', $existingRecords, ShlDbHelper::INTEGER); } } else { $record['id'] = 0; } // find if we already have a meta data record for this non-sef url // as we want to update it if so, instead of creating a new record $metasModel = ShlMvcModel_Base::getInstance('metas', 'Sh404sefModel'); $existingMetas = $metasModel->getByAttr(array('newurl' => $record['newurl'])); if (!empty($existingMetas)) { $existingMeta = $existingMetas[0]; // getByAttr always returns an array // use the existing id, this will be enought to override existing record when saving $record['meta_id'] = $existingMeta->id; } else { $record['meta_id'] = 0; } // for aliases, we don't import them here, but we need to create a dummy // record so as to preserve possible pre-existing aliases for the same non-sef url $aliasesModel = ShlMvcModel_Base::getInstance('editalias', 'Sh404sefModel'); $existingAliases = $aliasesModel->getByAttr(array('newurl' => $record['newurl'])); $record['shAliasList'] = ''; if (!empty($existingAliases)) { foreach ($existingAliases as $existingAlias) { // build up a text list, just as if we were to edit aliases // as this is what the model expect to receive $record['shAliasList'] .= $existingAlias->alias . "\n"; } } // save record : returns the record id, so failure is when 0 is returned $savedId = $model->save($record, sh404SEF_URLTYPE_AUTO); if (empty($savedId)) { // rethrow a more appropriate error message throw new Sh404sefExceptionDefault(JText::sprintf('COM_SH404SEF_IMPORT_ERROR_INSERTING_INTO_DB', $line[0])); } }
/** * Delete a list of urls from their ids, * passed as params * Also delete all duplicates * * @param array of integer $ids the list of url id to delete * @return boolean true if success */ public function deleteByIdsWithDuplicates($ids = array()) { if (empty($ids)) { return true; } // build a list of ids to read $whereIds = ShlDbHelper::arrayToIntValList($ids); // read urls and their duplicates $query = 'select r2.id' . ' from `#__sh404sef_urls` as r1' . ' join `#__sh404sef_urls` as r2' . ' on r1.`oldurl` = r2.`oldurl`' . ' where r1.' . $this->_db->quoteName('id') . ' in (' . $whereIds . ')'; try { // perform query $this->_db->setQuery($query); $urlsIds = $this->_db->shlLoadResultArray(); // now delete urls from db and cache $rows = $this->_getNonSefUrls($urlsIds); if (!empty($rows)) { Sh404sefHelperCache::removeURLFromCache($rows); } // finally, we can simply delete from db by ids ShlDbHelper::deleteIn('#__sh404sef_urls', 'id', $urlsIds); } catch (Exception $e) { ShlSystem_Log::error('sh404sef', '%s::%s::%d: %s', __CLASS__, __METHOD__, __LINE__, $e->getMessage()); $this->setError('Internal database error # ' . $e->getMessage()); } return true; }