πŸ“œ ⬆️ ⬇️

Bitrix. Development. How not to fill in the data for testing manually.

Many criticize Bitrix, give examples of huge requests, spit on resource intensity ... and I understand the reasons.

Bitrix is ​​really far from perfect, but from my point of view it deserves attention. It is now the most earning CMS in at least Russia.
Those. The knowledge of Bitrix is ​​a significant contribution to the developer's pocket.

Now I do not have enough karma, but in the future I plan to start a blog in which I will begin to share best practices in this area.
')
Article number 1. How not to fill in the data for testing manually.
In the development of almost any project there is a problem of stuffing data for testing. This case has always made me very tired.
Having finally decided that I am a developer, and not an operator, took up the script.

So the task: to develop a script for driving test data (fish) in the Information Blocks project on Bitrix.

Fish for the articles, it was decided to take on ru.lipsum.com .

So what happened, the script :
<?
require( $_SERVER [ "DOCUMENT_ROOT" ]. "/bitrix/header.php" );
CModule :: IncludeModule ( "iblock" );
?>
<?
### CONFIG ###
$file = "delfin_test.dat1" ;

### TOOLS ###
function getIpsum ( $cnt , $what )
{
$url = "http://www.lipsum.com/feed/html?amount=$cnt&what=$what" ;
$text = file_get_contents ( $url );
preg_match ( "'<div id=\"lipsum\"[^>]*?>(.*?)</div>'si" , $text , $matches );
return
$matches [ 1 ];
}
?>
<?
include( $file );

$obSect = new CIBlockSection ;
$obItem = new CIBlockElement ;

while ( list(
$IBLOCK_ID , $arrCfg ) = each ( $cfg ) )
{
$sectCnt = 0 ;
$itemCnt = 0 ;
$sect = array();
$time = strtotime ( "-60 days" );

if (
$arrCfg [ "sect_count" ] > 0 )
{
for (
$i = 0 ; $i < $arrCfg [ "sect_count" ]; $i ++)
{
$sectCnt ++;
$arSect = Array(
"IBLOCK_ID" => $IBLOCK_ID ,
"NAME" => str_replace ( "#NUM#" , $sectCnt , $arrCfg [ "sect_name_tpl" ])
);

$ID = $obSect -> Add ( $arSect );
if (
$ID > 0 )
$sect [] = $ID ;
else
echo
$obSect -> LAST_ERROR ;
}

}
else
{
$sect [] = 0 ;
}
reset ( $sect );
while ( list(,
$SECTION_ID ) = each ( $sect ) )
{
for (
$i = 0 ; $i < $arrCfg [ "items_count" ]; $i ++)
{
$itemCnt ++;
$arTmp = explode ( ":" , $arrCfg [ "item_preview_txt" ]);
if (
$arTmp [ 0 ] == "ipsum" )
{
$arTmp = explode ( "#" , $arTmp [ 1 ]);
$preview = getIpsum ( $arTmp [ 0 ], $arTmp [ 1 ]);
}
$arTmp = explode ( ":" , $arrCfg [ "item_detail_txt" ]);
if (
$arTmp [ 0 ] == "ipsum" )
{
$arTmp = explode ( "#" , $arTmp [ 1 ]);
$detail = getIpsum ( $arTmp [ 0 ], $arTmp [ 1 ]);
}

$arItem = Array(
"ACTIVE" => "Y" ,
"IBLOCK_ID" => $IBLOCK_ID ,
"IBLOCK_SECTION" => $SECTION_ID ,
"NAME" => str_replace ( "#NUM#" , $itemCnt , $arrCfg [ "item_name_tpl" ]),
"ACTIVE_FROM" => ConvertTimeStamp ( $time + $itemCnt * 24 * 60 * 60 ),
"PREVIEW_TEXT" => $preview ,
"DETAIL_TEXT" => $detail ,
"PREVIEW_TEXT_TYPE" => "html" ,
"DETAIL_TEXT_TYPE" => "html" ,
);
$ID = $obItem -> Add ( $arItem );
if (
$ID > 0 )
$items [] = $ID ;
else
echo
$obItem -> LAST_ERROR ;

}
}
}

?>



I will make a reservation - the first version, but workable.

Here is an example config:
<?
$cfg
= Array(
1 => Array(
"sect_count" => 3 ,
"items_count" => 5 ,
"sect_name_tpl" => " β„–#NUM#" ,
"item_name_tpl" => " β„–#NUM#" ,
"item_preview_txt" => "ipsum:20#words" ,
"item_detail_txt" => "ipsum:5#paras" ,
),
2 => Array(
"sect_count" => 0 ,
"items_count" => 15 ,
"item_name_tpl" => " β„–#NUM#" ,
"item_preview_txt" => "ipsum:20#words" ,
"item_detail_txt" => "ipsum:5#paras" ,
),
3 => Array(
"sect_count" => 0 ,
"items_count" => 10 ,
"item_name_tpl" => " β„–#NUM#" ,
"item_preview_txt" => "ipsum:2#paras" ,
"item_detail_txt" => "ipsum:7#paras" ,
),
4 => Array(
"sect_count" => 0 ,
"items_count" => 10 ,
"item_name_tpl" => " β„–#NUM#" ,
"item_preview_txt" => "ipsum:5#words" ,
"item_detail_txt" => "ipsum:2#paras" ,
),
5 => Array(
"sect_count" => 0 ,
"items_count" => 10 ,
"item_name_tpl" => " β„–#NUM#" ,
"item_preview_txt" => "ipsum:3#words" ,
"item_detail_txt" => "ipsum:3#paras" ,
),
6 => Array(
"sect_count" => 5 ,
"items_count" => 8 ,
"item_name_tpl" => " β„–#NUM#" ,
"sect_name_tpl" => " β„–#NUM#" ,
"item_preview_txt" => "ipsum:20#words" ,
"item_detail_txt" => "ipsum:2#paras" ,
),

);
?>



Let us analyze in order what and how.

Config:
Each element of the $ cfg array is a conftiguation for a specific information block. The array key is the information block ID.
options:
"Sect_count" - how many sections to make (if there are no sections - 0)
"Items_count" => how many elements to make in each section.
"Sect_name_tpl" => section name pattern
"Item_name_tpl" => item name pattern
"Item_preview_txt" => than fill in a short description of the element *
"Item_detail_txt" => than fill in the full description of the element *

* - currently only one filling format is implemented - fish from the site.
The format is ipsum: [number] # [what to generate (words | paras | bytes | lists)]
those. ipsum: 2 # paras means - 2 paragraphs, and ipsum: 20 # words - 20 words.

The script for each information block specified in the config generates the specified number of sections. Each section creates a specified number of items. Fill in the Name, Preliminary Description and Detailed Description for Elements.

Also set the start date of the activity (usually used as the publication date) is different for all elements.
The date is set as follows:
The date for the first item in the section is 60 days ago.
For each subsequent element in the section, 1 day is added.

As a result of the script, we get the fish in the database of the project for 10 minutes. This can usually take up to 4-5 hours of dull mechanical work.

Use. I also have scripts for converting a regular site into a Bitrix site and a script for forming the site structure from the list of sections (with the formation of a menu system). But more about that next time.

Source: https://habr.com/ru/post/44359/


All Articles