Login / Status
developer.Resource
Home . Documentation . Document Library . Core Documentation
Sponsors
hosted by punkt.deTYPO3 and Open Source MagazineAOE Media

3.3. TYPO3 Core Engine (TCE)

Introduction

Database

The TYPO3 Core Engine is the class that handles all data writing to database tables configured in $TCA. In addition the class handles commands such as copy, move, delete. It will handle undo/history and versioning of records as well and everything will be logged to the sys_log. And it will make sure that write permissions are evaluated correctly for the user trying to write to the database. Generally, any processing specific option in the $TCA array is handled by TCE.

Using TCE for manipulation of the database content in the TCA configured tables guarantees that the data integrity of TYPO3 is respected. This cannot be safely guaranteed if you write to $TCA configured database tables directly. It will also manage the relations to files and other records.

TCE requires a backend login to work. This is due to the fact that permissions are observed (of course) and thus TCE needs a backend user to evaluate against. This means you cannot use TCEmain from the frontend scope. Thus writing to tables (such as a guestbook) will have to be done from the frontend without TCEmain.

The features of the $TCA (Table Configuration Array) array are discussed in the end of this document.

Files

TCE also has a part for handling files. The file operations are normally performed in the File > List module where you can manage a directory on the server by copying, moving, deleting and editing files and directories. The file operations are managed by two core classes, t3lib_basicFileFunc and t3lib_extFileFunc.

Database: t3lib_TCEmain basics

When you are using TCE from your backend applications you need to prepare two arrays of information which contain the instructions to TCEmain of what actions to perform. They fall into two categories: Data and Commands.

"Data" is when you want to write information to a database table or create a new record.

"Commands" is when you want to move, copy or delete a record in the system.

The data and commands are created as multidimensional arrays and to understand the API of TCEmain you simply need to understand the hierarchy of these two arrays.

Commands Array ($cmd):

Syntax:

$cmd[ tablename ][ uid ][ command ] = value

Description of keywords in syntax:

Key

Data type

Description

tablename

string

Name of the database table. Must be configured in $TCA array, otherwise it cannot be processed.

uid

integer

The UID of the record that is manipulated. This is always an integer.

command

string (command keyword)

The command type you want to execute.

Notice: Only one command can be executed at a time for each record! The first command in the array will be taken.

See table below for command keywords and values

value

mixed

The value for the command

See table below for command keywords and values

Command keywords and values:

Command

Data type

Value

copy

integer

The significance of the value depends on whether it is positive or negative:

  1. Positive value: The value points to a page UID. A copy of the record (and possibly child elements/tree below) will be inserted inside that page as the first element.

  2. Negative value: The (absolute) value points to another record from the same table as the record being copied. The new record will be inserted on the same page as that record and if $TCA[...]['ctrl']['sortby'] is set, then it will be positioned after.

  3. Zero value: Record is inserted on tree root level

move

integer

Works like "copy" but moves the record instead of making a copy.

delete

"1"

Value should always be "1"

This action will delete the record (or mark the record "deleted" if configured in $TCA)

undelete

“1”

Value should always be "1".

This action will set the deleted-flag back to 0.

localize

integer

Pointer to a “sys_language” uid to localize the record into. Basically a localization of a record is making a copy of the record (possibly excluding certain fields defined with “l10n_mode”) but changing relevant fields to point to the right sys language / original language record.

Requirements for a successful localization is this:

  1. [ctrl] options “languageField” and “transOrigPointerField” must be defined for the table

  1. A “sys_language” record with the given “sys_language_uid” must exist.

  2. The record to be localized by currently be set to “Default” language and not have any value set for the “transOrigPointerField” either.

  3. There cannot exist another localization to the given language for the record (looking in the original record PID).

Apart from this ordinary permissions apply as if the user wants to make a copy of the record on the same page.

version

array

Versioning action.

Keys:

  1. [action] : Keyword determining the versioning action. Options are:

    1. “new” : Indicates that a new version of the record should be created.Additional keys, specific for “new” action:

      1. [treeLevels] : (Only pages)  Integer, -1 to 4, indicating the number of levels of the page tree to version together with a page. This is also referred to as the versioning type:-1 (“element”) means only the page record gets versioned (default) 0 (“page”) means the page + content tables (defined by ctrl-flag “versioning_followPages”)>0 (“branch”) means the the whole branch is versioned (full copy of all tables), down to the level indicated by the value (1= 1 level down, 2= 2 levels down, etc.)The treeLevel is recorded in the field “t3ver_swapmode” and will be observed when the record is swapped during publishing.

      2. [label] : Indicates the version label to apply. If not given, a standard label including version number and date is added.

    2. “swap” : Indicates that the current online version should be swapped with another.Additional keys, specific for “swap” action:

      1. [swapWith] : Indicates the uid of the record to swap current version with!

      2. [swapIntoWS]: Boolean, indicates that when a version is published it should be swapped into the workspace of the offline record.

    3. “clearWSID” : Indicates that the workspace of the record should be set to zero (0). This removes versions out of workspaces without publishing them.

    4. “setStage” : Sets the stage of an element. Special feature: The id-key in the array can be a comma list of ids in order to perform the stageChange over a number of records. Also, the internal variable ->generalComment (also available through tce_db.php as "&generalComment") can be used to set a default comment for all stage changes of an instance of tcemain. Additional keys for this action is:

      1. [stageId] : Values are: -1 (rejected), 0 (editing, default), 1 (review), 10 (publish)

      2. [comment] : Comment string that goes into the log.

Examples of Commands:

$cmd['tt_content'][54]['delete'] = 1;    // Deletes tt_content record with uid=54

$cmd['pages'][1203]['copy'] = -303;   //Copies page id=1203 to the position after page 303

$cmd['pages'][1203]['move'] = 303;  // Moves page id=1203 to the first position in p. 303

Data Array ($data):

Syntax:

$data[ tablename ][ uid ][ fieldname ] = value

Description of keywords in syntax:

Key

Data type

Description

tablename

string

Name of the database table. Must be configured in $TCA array, otherwise it cannot be processed.

uid

mixed

The UID of the record that is modified. If the record already exists, this is an integer. If you're creating new records, use a random string prefixed with "NEW", eg. "NEW7342abc5e6d".

fieldname

string

Name of the database field you want to set a value for. Must be configure in $TCA[ tablename ]['columns']

value

string

Value for "fieldname".

(Always make sure $this->stripslashes_values is false before using TCEmain.)

Notice: For FlexForms the data array of the FlexForm field is deeper than three levels. The number of possible levels for FleFforms is infinite and defined by the data structure of the FlexForm. But FlexForm fields always end with a "regular value" of course.

Examples of Data submission:

This creates a new page titled "The page title" as the first page inside page id 45:

$data['pages']['NEW9823be87'] = array(

    "title" => "The page title",

    "subtitle" => "Other title stuff",

    "pid" => "45"

);

This creates a new page titled "The page title" right after page id 45 in the tree:

$data['pages']['NEW9823be87'] = array(

    "title" => "The page title",

    "subtitle" => "Other title stuff",

    "pid" => "-45"

);

This creates two new pages right after each other, located right after the page id 45:

$data['pages']['NEW9823be87'] = array(

    "title" => "Page 1",

    "pid" => "-45"

);

$data['pages']['NEWbe68s587'] = array(

    "title" => "Page 2",

    "pid" => "-NEW9823be87"

);

Notice how the second "pid" value points to the "NEW..." id placeholder of the first record. This works because the new id of the first record can be accessed by the second record. However it works only when the order in the array is as above since the processing happens in that order!

This updates the page with uid=9834 to a new title, "New title for this page", and no_cache checked:

$data['pages'][9834] = array(

    "title" => "New title for this page",

    "no_cache" => "1"

);

Clear cache

TCE also has an API for clearing the cache tables of TYPO3:

Syntax:

$tce->clear_cacheCmd($cacheCmd);

$cacheCmd values

Description

[integer]

Clear the cache for the page id given.

"all"

Clears all cache tables (cache_pages, cache_pagesection, cache_hash).

Only available for admin-users unless expressly allowed by User TSconfig "options.clearCache.all"

"pages"

Clears all pages from cache_pages.

Only available for admin-users unless expressly allowed by User TSconfig "options.clearCache.pages"

"temp_CACHED"

Clears the temp_CACHED fiels in typo3conf/

Hook for cache post processing

You can configure cache post processing with a user defined PHP function. Configuration of the hook can be done from (ext_)localconf.php. An example look like:

$TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['clearCachePostProc'][]='myext_cacheProc->proc';

require_once(t3lib_extMgm::extPath('myext').'class.myext_cacheProc.php');

Flags in TCEmain

There are a few internal variables you can set prior to executing commands or data submission. These are the most significant:

Internal variable

Data type

Description

->deleteTree

Boolean

Sets whether a page tree branch can be recursively deleted.

If this is set, then a page is deleted by deleting the whole branch under it (user must have deletepermissions to it all). If not set, then the page is deleted only if it has no branch.

Default is false.

->copyTree

Integer

Sets the number of branches on a page tree to copy.

If 0 then branch is not copied. If 1 then pages on the 1st level is copied. If 2 then pages on the second level is copied ... and so on.

Default is zero.

->reverseOrder

Boolean

If set, the data array is reversed in the order, which is a nice thing if you're creating a whole bunch of new records.

Default is zero.

->copyWhichTables

list of strings (tables)

This list of tables decides which tables will be copied. If empty then none will. If "*" then all will (that the user has permission to of course).

Default is "*"

->stripslashes_values

boolean

If set, then all values will be passed through stripslashes(). This has been the default since the birth of TYPO3 in times when input from POST forms were always escaped an needed to be unescaped. Today this is deprecated and values should be passed around without escaped characters.

It is highly recommended to set this value to zero every time the class is used!

If you set this value to false you can pass values as-is to the class and it is most like that this is what you want. Otherwise you would have to pass all values through addslashes() first.

Default is (currently) "1" (true) but might be changed in the future!

Using t3lib_TCEmain in scripts

It's really easy to use the class "t3lib_TCEmain" in your own scripts. All you need to do is include the class, build a $data/$cmd array you want to pass to the class and call a few methods.

First of all they have to be run in the backend scope, mind you that! There must be a global $BE_USER object.

In your script you simply insert this line to include the class:

require_once (PATH_t3lib."class.t3lib_tcemain.php");

When that is done you can create an instance of t3lib_TCEmain. Here follows a few code listings with comments which will provide you with enough knowledge to get started. It is assumed that you have populated the $data and $cmd arrays correctly prior to these chunks of code. The syntax for these two arrays is explained on the previous pages.

Example: Submitting data

This is the most basic example of how to submit data into the database. It is four lines. Line 1 instantiates the class, line 2 defines that values will be provided without escaped characters (recommended!), line 3 registers the $data array inside the class and initializes the class internally! Finally line 4 will execute the data submission.

   1: $tce = t3lib_div::makeInstance('t3lib_TCEmain');

   2: $tce->stripslashes_values = 0;

   3: $tce->start($data,array());

   4: $tce->process_datamap();

Example: Executing commands

The most basic way of executing commands. Line 1 creates the object, line 2 defines that values will be provided without escaped characters (recommended), line 3 registers the $cmd array inside the class and initializes the class internally! Finally line 4 will execute the commands.

   1: $tce = t3lib_div::makeInstance('t3lib_TCEmain');

   2: $tce->stripslashes_values=0;

   3: $tce->start(array(),$cmd);

   4: $tce->process_cmdmap();

Example: Clearing cache

In this example the clear-cache API is used. No data is submitted, no commands executed. Still you will have to initialize the class by calling the start() method (which will initialize internal variables).

Notice: Clearing "all" cache will be possible only for users that are "admin" or for users with specific permissions to do so.

   1: $tce = t3lib_div::makeInstance('t3lib_TCEmain');

   2: $tce->start(Array(),Array());

   3: $tce->clear_cacheCmd('all');

Example: Complex data submission

Imagine the $data array something like this:

$data = array(

    'pages' => array(

        'NEW_1' => array(

            'pid' => 456,

            'title' => 'Title for page 1',

        ),

        'NEW_2' => array(

            'pid' => 456,

            'title' => 'Title for page 2',

        ),

    )

);

This aims to create two new pages in the page with uid "456". In the follow code this is submitted to the database. Notice how line 3 reverses the order of the array. This is done because otherwise "page 1" is created first, then "page 2" in the same PID meaning that "page 2" will end up above "page 1" in the order. Reversing the array will create "page 2" first and then "page 1" so the "expected order" is preserved.

Apart from this line 6 will send a "signal" that the page tree should be updated at the earliest occasion possible. Finally, the cache for all pages is cleared in line 7.

   1: $tce = t3lib_div::makeInstance('t3lib_TCEmain');

   2: $tce->stripslashes_values = 0;

   3: $tce->reverseOrder = 1;

   4: $tce->start($data,array());

   5: $tce->process_datamap();

   6: t3lib_BEfunc::getSetUpdateSignal('updatePageTree');

   7: $tce->clear_cacheCmd('pages');

Example: Both data and commands executed with alternative user object

In this case it is shown how you can use the same object instance to submit both data and execute commands if you like. The order will depend on the order of line 4 and 5.

In line 3 the start() method is called, but this time with the third possible argument which is an alternative BE_USER object. This allows you to force another backend user account to create stuff in the database. This may be useful in certain special cases. Normally you should not set this argument since you want TCE to use the global $BE_USER.

   1: $tce = t3lib_div::makeInstance('t3lib_TCEmain');

   2: $tce->stripslashes_values = 0;

   3: $tce->start($data,$cmd,$alternative_BE_USER);

   4: $tce->process_datamap();

   5: $tce->process_cmdmap();

The "tce_db.php" API

This script is a gateway for POST forms to class.t3lib_TCEmain. It has historically been the script to which data was posted when you wanted to update something in the database.

Today it is used for editing by only a few scripts, actually only the "Quick Edit" module in "Web>Page" (frontend). The standard forms you find in TYPO3 are normally rendered and handled by "alt_doc.php" which includes t3lib_TCEmain on its own.

For commands it is still used from various locations.

You can send data to this file either as GET or POST vars where POST takes precedence. The variable names you can use are:

GP var name:

Data type

Description

data

array

Data array on the form [tablename][uid][fieldname] = value

Typically it comes from a POST form which submits a form field like <input name="data[tt_content][123][header]" value="This is the headline" />

cmd

array

Command array on the form [tablename][uid][command] = value. This array may get additional data set internally based on clipboard commands send in CB var!

Typically this comes from GET vars passed to the script like "&cmd[tt_content][123][delete]=1" which will delete Content Element with UID 123

cacheCmd

string

Cache command sent to ->clear_cacheCmd

redirect

string

Redirect URL. Script will redirect to this location after performing operations (unless errors has occured)

flags

array

Accepts options to be set in TCE object. Currently it supports "reverseOrder" (boolean).

mirror

array

Example: [mirror][table][11] = '22,33' will look for content in [data][table][11] and copy it to [data][table][22] and [data][table][33]

prErr

boolean

If set, errors will be printed on screen instead of redirection. Should always be used, otherwise you will see no errors if they happen.

CB

array

Clipboard command array. May trigger changes in "cmd"

vC

string

Verification code

uPT

string

Update Page Tree Trigger. If set and the manipulated records are pages then the update page tree signal will be set.

Files: t3lib_extFileFunctions basics

File operations in TCE is handled by the class "t3lib_extFileFunctions" which extends "t3lib_basicFileFunctions". The instructions for file manipulation is passed to this class as a multidimensional array.

Files Array ($file):

Syntax:

$file[ command ][ index ][ key ] = value

Description of keywords in syntax:

Key

Data type

Description

command

string (command keyword)

The command type you want to execute.

See table below for command keywords, keys and values

index

integer

Integer index in the array which separates multiple commands of the same type.

key

string

Depending on the command type. The keys will carry the information needed to perform the action. Typically a "target" key is used to point to the target directory or file while a "data" key carries the data.

See table below for command keywords, keys and values

value

string

The value for the command

See table below for command keywords, keys and values

Command keywords and values:

Command

Keys

Value

delete

"data"

"data" = Absolute path to the file/folder to delete

copy

"data"

"target"

"altName"

"data" = Absolute path to the file/folder to copy

"target" = Absolute path to the folder to copy to (destination)

"altName" = (boolean): If set, a new filename is made by appending numbers/unique-string in case the target already exists.

move

"data"

"target"

"altName"

(Exactly like copy, just replace the word "copy" with "move")

rename

"data"

"target"

"data" = New name, max 30 characters alphanumeric

"target" = Absolute path to the target file/folder

newfolder

"data"

"target"

"data" = Folder name, max 30 characters alphanumeric

"target" = Absolute path to the folder where to create it

newfile

"data"

"target"

"data" = New filename

"target" = Absolute path to the folder where to create it

editfile

"data"

"target"

"data" = The new content

"target" = Absolute path to the target file

upload

"data"

"target"

upload_$id

"data" = ID-number (points to the global var that holds the filename-ref  ($GLOBALS["HTTP_POST_FILES"]["upload_".$id]["name"])

"target" = Absolute path to the target folder (destination)

upload_$id = File reference. $id must equal value of file[upload][...][data]!

See t3lib_t3lib_extFileFunctions::func_upload()

unzip

"data"

"target"

"data" = Absolute path to the zip-file. (fileextension must be "zip")

"target" = The absolute path to the target folder (destination) (if not set, default is the same as the zip-file)

It is unlikely that you will need to use this internally in your scripts like you will need t3lib_TCEmain. It is fairly uncommon to need the file manipulations in own scripts unless you make a special application. Therefore the most typical usage of this API is from tce_file.php and the core scripts that are activated by the "File > List" module.

However, if you need it this is an example (taken from tce_file.php) of how to initialize the usage.

   1:     // Initializing:

   2: $this->fileProcessor = t3lib_div::makeInstance('t3lib_extFileFunctions');

   3: $this->fileProcessor->init($FILEMOUNTS, $TYPO3_CONF_VARS['BE']['fileExtensions']);

   4: $this->fileProcessor->init_actionPerms($BE_USER->user['fileoper_perms']);

   5:

   6: $this->fileProcessor->start($this->file);

   7: $this->fileProcessor->processData();

Line 2 makes an instance of the class and line 3 initializes the object with the filemounts of the current user and the array of allow/deny file extensions in web-space and ftp-space (see below). Then the file operation permissions are loaded from the user object in line 4. Finally, the file command array is loaded in line 6 (and internally additional configuration takes place from $TYPO3_CONF_VARS!). In line 7 the command map is executed.

Web-space, Ftp-space and $TYPO3_CONF_VARS['BE']['fileExtensions']

The control of fileextensions goes in two catagories. Webspace and Ftpspace. Webspace is folders accessible from a webbrowser (below TYPO3_DOCUMENT_ROOT) and ftpspace is everything else.

The control is done like this: If an extension matches 'allow' then the check returns true. If not and an extension matches 'deny' then the check return false. If no match at all, returns true.

You list extensions comma-separated. If the value is a '*' every extension is matched. If no fileextension, true is returned if 'allow' is '*', false if 'deny' is '*' and true if none of these matches. This (default) configuration below accepts everything in ftpspace and everything in webspace except php3 or php files:

$TYPO3_CONF_VARS['BE']['fileExtensions'] = array (

    'webspace' => array('allow'=>'', 'deny'=>'php3,php'),

    'ftpspace' => array('allow'=>'*', 'deny'=>'')

);

The "tce_file.php" API

This script serves as the file administration part of the TYPO3 Core Engine. It's a gateway for TCE (TYPO3 Core Engine) file-handling through POST forms. It uses "t3lib_extfilefunc" for the manipulation of the files.

This script is used from the File > List module where you can rename, create, delete etc. files and directories on the server.

You can send data to this file either as GET or POST vars where POST takes precedence. The variable names you can use are:

GP var name:

Data type

Description

file

array

Array of file operations. See previous information about "t3lib_extFileFunctions"

This could typically be a GET var like "&file[delete][0][data]=[absolute file path]" or a POST form field like "<input type="text" name="file[newfolder][0][data]" value=""/><input type="hidden" name="file[newfolder][0][target]" value="[absolute path to folder to create in]"/>"

redirect

string

Redirect URL. Script will redirect to this location after performing operations.

CB

array

Clipboard command array. May trigger changes in "file"

vC

string

Verification code

overwriteExistingFiles

boolean

If existing files should be overridden.



TYPO3 Core API

TSRef

TYPO3 Coding Guidelines