Typo3: How to create typolinks in a scheduler/cron script
I need to create links to the frontend website inside a scheduler task. I've searched and looked around and what I have found is an example script that initiates the TSFE. After that I can initiate tslib_cObj to create some links; I thought.
But what I get is a recursion error as soon as I try to create a typolink using $cObj->typoLink_URL(1); fe. or any other method that allows creating a typolink.
The following is the script I use to initiate the TSFE inside $GLOBALS, just like when working inside an extension in the frontend:
<?php
function initTSFE($pageUid = 1, $overrule = FALSE) {
// declare
$temp_TSFEclassName = t3lib_div::makeInstanceClassName('tslib_fe');
// begin
if (!is_object($GLOBALS['TT']) || $overrule === TRUE) {
$GLOBALS['TT'] = new t3lib_timeTrack;
$GLOBALS['TT']->start();
}
if ((!is_object($GLOBALS['TSFE']) || $overrule === TRUE) && is_int($pageUid)) {
// builds TSFE object
$GLOBALS['TSFE'] = new $temp_TSFEclassName($GLOBALS['TYPO3_CONF_VARS'],
$pageUid, $type=0, $no_cache=0, $cHash='', $jumpurl='', $MP='', $RDCT='');
// builds rootline
$GLOBALS['TSFE']->sys_page = t3lib_div::makeInstance('t3lib_pageSelect');
$rootLine = $GLOBALS['TSFE']->sys_page->getRootLine($pageUid);
// init template
$GLOBALS['TSFE']->tmpl = t3lib_div::makeInstance('t3lib_tsparser_ext');
$GLOBALS['TSFE']->tmpl->tt_track = 0;// Do not log time-performance information
$GLOBALS['TSFE']->tmpl->init();
// this generates the constants/config + hierarchy info for the template.
$GLOBALS['TSFE']->tmpl->runThroughTemplates($rootLine, $start_template_uid=0);
$GLOBALS['TSFE']->tmpl->generateConfig();
$GLOBALS['TSFE']->tmpl->loaded=1;
// get config array and other init from pagegen
$GLOBALS['TSFE']->getConfigArray();
$GLOBALS['TSFE']->linkVars = ''.$GLOBALS['TSFE']->config['config']['linkVars'];
if ($GLOBALS['TSFE']->config['config']['simulateStaticDocuments_pEnc_onlyP']) {
foreach (t3lib_div::trimExplode(',',$GLOBALS['TSFE']->config['config']['simulateStaticDocuments_pEnc_onlyP'],1) as $temp_p) {
$GLOBALS['TSFE']->pEncAllowedParamNames[$temp_p]=1;
}
}
// builds a cObj
$GLOBALS['TSFE']->newCObj();
}
}
What I am trying todo in the scheduler task is the following:
<?php
public function execute() {
$this->initTSFE();
$cObj = t3lib_div::makeInstance('tslib_cObj');
var_dump($cObj->getTypoLink_URL(1));exit;
}
This is showi开发者_开发技巧ng me the following result upon executing this task via the scheduler: http://i.imgur.com/DHzys.png
Any help is very much appreciated =)
NOTE: The 1 value inside getTypoLink_URL does exist as a page in Typo3.
There seems to be something wrong with this initTSFE() method I was trying to use. I am not quit sure what, but I've found a different version of it that does seem to work:
public function initTSFE($pageUid=1) {
require_once(PATH_tslib.'class.tslib_fe.php');
require_once(PATH_t3lib.'class.t3lib_userauth.php');
require_once(PATH_tslib.'class.tslib_feuserauth.php');
require_once(PATH_t3lib.'class.t3lib_cs.php');
require_once(PATH_tslib.'class.tslib_content.php');
require_once(PATH_t3lib.'class.t3lib_tstemplate.php');
require_once(PATH_t3lib.'class.t3lib_page.php');
$TSFEclassName = t3lib_div::makeInstanceClassName('tslib_fe');
if (!is_object($GLOBALS['TT'])) {
$GLOBALS['TT'] = new t3lib_timeTrack;
$GLOBALS['TT']->start();
}
// Create the TSFE class.
$GLOBALS['TSFE'] = new $TSFEclassName($GLOBALS['TYPO3_CONF_VARS'],$pageUid,'0',1,'','','','');
$GLOBALS['TSFE']->connectToMySQL();
$GLOBALS['TSFE']->initFEuser();
$GLOBALS['TSFE']->fetch_the_id();
$GLOBALS['TSFE']->getPageAndRootline();
$GLOBALS['TSFE']->initTemplate();
$GLOBALS['TSFE']->tmpl->getFileName_backPath = PATH_site;
$GLOBALS['TSFE']->forceTemplateParsing = 1;
$GLOBALS['TSFE']->getConfigArray();
}
Thank you voancea!
精彩评论