Here I give you one example for how we migrate Joomla k2 component items and category to Joomla 3+ standard article and category….
Using this script I was migrated 400+ articles and many categories. and now I showing you how you save your time which is i already spend on then ! So lets do it and migrate your category and articles to Joomla 3+.
Follow below steps for execute PHP script in your custom Joomla component:
Step 1: Create one sample component on client side if you not have. (How to create a component)
Step 2: Create below controller file migrationk2j.php for executing migration process through Joomla component model
<?php // No direct access. defined('_JEXEC') or die; jimport('joomla.application.component.controller'); class qb_ComponentNameControllerMigrationk2j extends JControllerLegacy { /** * Proxy for getModel. * @since 1.6 */ public function process() { // 27 is a category ID which is you want to migrate with all of the articles $this->getModel('migrationk2j')->startMigration(27); } }
Step 3: add one model file for whole migration process and file name is migrationk2j.php and white below code
<?php defined('_JEXEC') or die; jimport('joomla.application.component.modellist'); /** * Methods supporting a list of qb_Component records. */ class qb_ComponentNameModelMigrationk2j extends JModelList { /** * Constructor. * * @param array An optional associative array of configuration settings. * * @see JController * @since 1.6 */ public function __construct($config = array()) { parent::__construct($config); } private function get_k2_cat($cID) { $db = JFactory::getDbo(); $query = $db->getQuery(true); $query ->select("*") ->from('#__k2_categories') ->where("id=".$db->escape($cID)) ->where("trash!=1"); $db->setQuery($query); $results = $db->loadObject(); return $results; } private function get_k2_article($cID) { $db = JFactory::getDbo(); $query = $db->getQuery(true); $query ->select("*") ->from('#__k2_items') ->where("catid=".$db->escape($cID)) ->where("trash!=1"); $db->setQuery($query); $results = $db->loadObjectList(); return $results; } function startMigration($cID=null) { $category=$this->get_k2_cat($cID); $table = JTableCategory::getInstance('category', 'JTable'); $table->bind(array( 'extension' => 'com_content', 'title' => $category->name, 'path' => $category->alias, 'access' => $category->access, 'param' => $category->param, 'alias' => $category->alias, 'description' => $category->description, 'published' => $category->published, 'created_user_id' => JFactory::getUser()->id, 'language'=>'*', )); $table->store(); $table->store(); $primary_keys = $table->getPrimaryKey(); $ncID = $primary_keys['id']; if($ncID==null) { $db = JFactory::getDbo(); $query = $db->getQuery(true); $query ->select("*") ->from('#__categories') ->where("path=".$db->quote($category->alias)); $db->setQuery($query); $ncID=$db->loadObject()->id; } if($ncID>0) { $art=$this->get_k2_article($cID); foreach ($art as $item) { //here is encripted k2 compoenent image path findout and attach image or media with k2 compoentn item to joomla item $images=array('image_intro'=>'','float_intro'=>'','image_intro_alt'=>'','image_intro_caption'=>'','image_fulltext'=>'','float_fulltext'=>'','image_fulltext_alt'=>'','image_fulltext_caption'=>''); if(file_exists(JPATH_BASE.'/images/blog/qb/'.md5("Image".$item->id)."_XL.jpg")) { $images['image_intro']='/images/blog/qb/'.md5("Image".$item->id)."_L.jpg"; $images['float_intro']=''; $images['image_intro_alt']=$item->title; $images['image_intro_caption']=''; $images['image_fulltext']=''; $images['float_fulltext']='/images/blog/qb/'.md5("Image".$item->id)."_XL.jpg"; $images['image_fulltext_alt']=$item->title; $images['image_fulltext_caption']=''; } $extrafield=$this->generate_fulltext($item); $table = JTableContent::getInstance('content', 'JTable'); $table->bind(array( 'title' => $item->title, 'alias' => $item->alias, 'catid' => $ncID, 'state' => $item->published, 'created' => $item->created, 'created_by' => $item->created_by, 'created_by_alias' => $item->created_by_alias, 'publish_up' => $item->publish_up, 'publish_down' => $item->publish_down, 'introtext' => $item->introtext, 'fulltext' => $item->fulltext.$extrafield, 'metakey'=>$item->metakey, 'metadesc'=>$item->metadesc, 'hits' => $item->hits, 'access'=>$item->access, 'language'=>'*', 'images'=>json_encode($images), )); $table->store(); } } } private function generate_fulltext($item) { $fulltext = $item->fulltext; $db = JFactory::getDbo(); $query = $db->getQuery(true); $query ->select("id,name") ->from('#__k2_extra_fields'); $db->setQuery($query); $results = $db->loadObjectList(); $field=[]; foreach ($results as $key => $value) { $field[$value->id]=$value->name; } //You can migrate all extra fields in k2 compoent to joomla 3+ articles as you show in below if(count($field)>0) { $table="<table class='jtable'>"; foreach (json_decode($item->extra_fields) as $key => $value) { if (!is_array($value->value) && filter_var($value->value, FILTER_VALIDATE_EMAIL)) $table.="<tr><th align='left'>".$field[$value->id]."</th><td align='left'><a href='mailto:".$value->value."'>".$value->value."</a></td></tr>"; else if(is_array($value->value) && preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$value->value[0])) $table.="<tr><th align='left'>".$field[$value->id]."</th><td align='left'><a href='".(isset($value->value[1])?$value->value[1]:'http://').$value->value[0]."' target=".((isset($value->value[2]) && $value->value[2]=='new')?'__blank':'__self').">".$value->value[0]."</a></td></tr>"; else if(!is_array($value->value) && strlen(trim($value->value))>0) $table.="<tr><th align='left'>".$field[$value->id]."</th><td align='left'>".$value->value."</td></tr>"; } $table.="</table>"; } return $table; } }
After you created component with appending above file you just need to be change component name in from above file and execute script using below url … wait …. and DONE
http://yoursite.com/component/qb_ComponentName/migrationk2j/process
I hope you understand and help full this article for save your time and knowledge.
Also let me know if there any issue or my help.
Thanks
gia xe toyota hiace says
My family members always say that I am killing my time here at
web, except I know I am getting know-how all the time by reading thes fastidious content.