2009-01-13 02:03:41 +01:00
< ? php
2009-03-22 23:59:14 +01:00
/**
* @ package sapphire
* @ subpackage tasks
*/
2009-01-13 02:03:41 +01:00
class MigrateTranslatableTask extends BuildTask {
protected $title = " Migrate Translatable Task " ;
protected $description = " Migrates site translations from SilverStripe 2.1/2.2 to new database structure. " ;
function init () {
if ( ! Director :: is_cli () && ! Director :: isDev () && ! Permission :: check ( " ADMIN " )) Security :: permissionFailure ();
parent :: init ();
}
function run ( $request ) {
$ids = array ();
2009-01-13 06:04:21 +01:00
//$_REQUEST['showqueries'] = 1;
2009-01-13 02:03:41 +01:00
foreach ( array ( 'Stage' , 'Live' ) as $stage ) {
echo " <h2>Migrating stage $stage </h2> " ;
echo " <ul> " ;
$suffix = ( $stage == 'Live' ) ? '_Live' : '' ;
// First get all entries in SiteTree_lang
// This should be all translated pages
$trans = DB :: query ( 'SELECT * FROM SiteTree_lang' . $suffix );
// Iterate over each translated pages
foreach ( $trans as $oldtrans ) {
echo " <li>Migrating $oldtrans[Lang] translation of " . Convert :: raw2xml ( $oldtrans [ 'Title' ]) . '</li>' ;
// Get the untranslated page
$original = Versioned :: get_one_by_stage ( $oldtrans [ 'ClassName' ], $stage , '`SiteTree`.ID = ' . $oldtrans [ 'OriginalLangID' ]);
2009-01-13 06:04:21 +01:00
2009-01-13 02:03:41 +01:00
// Clone the original, and set it up as a translation
$newtrans = $original -> duplicate ( false );
$newtrans -> OriginalID = $original -> ID ;
ENHANCEMENT Adjusted SearchForm, Debug, ErrorPage, SiteTree to using locales instead of lang codes
API CHANGE Changed Translatable datamodel to use locales ("en_US") instead of lang values ("en).
API CHANGE Changed Translatable::$default_lang to $default_locale, Translatable::$reading_lang to $reading_locale
API CHANGE Using "locale" instead of "lang" in Translatable::choose_site_lang() to auto-detect language from cookies or GET parameters
API CHANGE Deprecated Translatable::is_default_lang(), set_default_lang(), get_default_lang(), current_lang(), set_reading_lang(), get_reading_lang(), get_by_lang(), get_one_by_lang()
API CHANGE Removed Translatable::get_original() - with the new "translation groups" concept there no longer is an original for a translation
BUGFIX Updated MigrateTranslatableTask to new Locale based datamodel
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@73468 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-03-20 09:47:06 +01:00
// we have to "guess" a locale based on the language
$newtrans -> Locale = i18n :: get_locale_from_lang ( $oldtrans [ 'Lang' ]);
2009-01-13 02:03:41 +01:00
if ( $stage == 'Live' && array_key_exists ( $original -> ID , $ids )) {
$newtrans -> ID = $ids [ $original -> ID ];
}
// Look at each class in the ancestry, and see if there is a _lang table for it
foreach ( ClassInfo :: ancestry ( $oldtrans [ 'ClassName' ]) as $classname ) {
$oldtransitem = false ;
// If the class is SiteTree, we already have the DB record, else check for the table and get the record
if ( $classname == 'SiteTree' ) {
$oldtransitem = $oldtrans ;
} elseif ( in_array ( strtolower ( $classname ) . '_lang' , DB :: tableList ())) {
$oldtransitem = DB :: query ( 'SELECT * FROM ' . $classname . '_lang' . $suffix . ' WHERE OriginalLangID = ' . $original -> ID . ' AND Lang = \'' . $oldtrans [ 'Lang' ] . '\'' ) -> first ();
}
// Copy each translated field into the new translation
if ( $oldtransitem ) foreach ( $oldtransitem as $key => $value ) {
ENHANCEMENT Adjusted SearchForm, Debug, ErrorPage, SiteTree to using locales instead of lang codes
API CHANGE Changed Translatable datamodel to use locales ("en_US") instead of lang values ("en).
API CHANGE Changed Translatable::$default_lang to $default_locale, Translatable::$reading_lang to $reading_locale
API CHANGE Using "locale" instead of "lang" in Translatable::choose_site_lang() to auto-detect language from cookies or GET parameters
API CHANGE Deprecated Translatable::is_default_lang(), set_default_lang(), get_default_lang(), current_lang(), set_reading_lang(), get_reading_lang(), get_by_lang(), get_one_by_lang()
API CHANGE Removed Translatable::get_original() - with the new "translation groups" concept there no longer is an original for a translation
BUGFIX Updated MigrateTranslatableTask to new Locale based datamodel
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@73468 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-03-20 09:47:06 +01:00
if ( ! in_array ( $key , array ( 'ID' , 'OriginalLangID' , 'ClassName' ))) {
2009-01-13 02:03:41 +01:00
$newtrans -> $key = $value ;
}
}
}
2009-01-13 06:04:21 +01:00
2009-01-13 02:03:41 +01:00
// Write the new translation to the database
ENHANCEMENT Adjusted SearchForm, Debug, ErrorPage, SiteTree to using locales instead of lang codes
API CHANGE Changed Translatable datamodel to use locales ("en_US") instead of lang values ("en).
API CHANGE Changed Translatable::$default_lang to $default_locale, Translatable::$reading_lang to $reading_locale
API CHANGE Using "locale" instead of "lang" in Translatable::choose_site_lang() to auto-detect language from cookies or GET parameters
API CHANGE Deprecated Translatable::is_default_lang(), set_default_lang(), get_default_lang(), current_lang(), set_reading_lang(), get_reading_lang(), get_by_lang(), get_one_by_lang()
API CHANGE Removed Translatable::get_original() - with the new "translation groups" concept there no longer is an original for a translation
BUGFIX Updated MigrateTranslatableTask to new Locale based datamodel
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@73468 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-03-20 09:47:06 +01:00
$sitelang = Translatable :: current_locale ();
Translatable :: set_reading_locale ( $newtrans -> Locale );
2009-01-13 06:04:21 +01:00
$newtrans -> writeToStage ( $stage , true );
ENHANCEMENT Adjusted SearchForm, Debug, ErrorPage, SiteTree to using locales instead of lang codes
API CHANGE Changed Translatable datamodel to use locales ("en_US") instead of lang values ("en).
API CHANGE Changed Translatable::$default_lang to $default_locale, Translatable::$reading_lang to $reading_locale
API CHANGE Using "locale" instead of "lang" in Translatable::choose_site_lang() to auto-detect language from cookies or GET parameters
API CHANGE Deprecated Translatable::is_default_lang(), set_default_lang(), get_default_lang(), current_lang(), set_reading_lang(), get_reading_lang(), get_by_lang(), get_one_by_lang()
API CHANGE Removed Translatable::get_original() - with the new "translation groups" concept there no longer is an original for a translation
BUGFIX Updated MigrateTranslatableTask to new Locale based datamodel
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@73468 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-03-20 09:47:06 +01:00
Translatable :: set_reading_locale ( $sitelang );
2009-01-13 02:03:41 +01:00
if ( $stage == 'Stage' ) {
$ids [ $original -> ID ] = $newtrans -> ID ;
}
}
echo '</ul>' ;
}
echo '<strong>Done!</strong>' ;
}
}
ENHANCEMENT Adjusted SearchForm, Debug, ErrorPage, SiteTree to using locales instead of lang codes
API CHANGE Changed Translatable datamodel to use locales ("en_US") instead of lang values ("en).
API CHANGE Changed Translatable::$default_lang to $default_locale, Translatable::$reading_lang to $reading_locale
API CHANGE Using "locale" instead of "lang" in Translatable::choose_site_lang() to auto-detect language from cookies or GET parameters
API CHANGE Deprecated Translatable::is_default_lang(), set_default_lang(), get_default_lang(), current_lang(), set_reading_lang(), get_reading_lang(), get_by_lang(), get_one_by_lang()
API CHANGE Removed Translatable::get_original() - with the new "translation groups" concept there no longer is an original for a translation
BUGFIX Updated MigrateTranslatableTask to new Locale based datamodel
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@73468 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-03-20 09:47:06 +01:00
?>