| Conditions | 21 |
| Paths | 669 |
| Total Lines | 138 |
| Code Lines | 78 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 62 | public function run($request) |
||
| 63 | { |
||
| 64 | $canAccess = (Director::isDev() || Director::is_cli() || Permission::check("ADMIN")); |
||
| 65 | if (!$canAccess) { |
||
| 66 | return Security::permissionFailure($this); |
||
| 67 | } |
||
| 68 | |||
| 69 | $ids = array(); |
||
| 70 | |||
| 71 | echo "#################################\n"; |
||
| 72 | echo "# Adding translation groups to existing records" . "\n"; |
||
| 73 | echo "#################################\n"; |
||
| 74 | |||
| 75 | $allSiteTreeIDs = DB::query('SELECT "ID" FROM "SiteTree"')->column(); |
||
| 76 | if ($allSiteTreeIDs) { |
||
| 77 | foreach ($allSiteTreeIDs as $id) { |
||
| 78 | $original = DataObject::get_by_id(SiteTree::class, $id); |
||
| 79 | $existingGroupID = $original->getTranslationGroup(); |
||
| 80 | if (!$existingGroupID) { |
||
| 81 | $original->addTranslationGroup($original->ID); |
||
| 82 | } |
||
| 83 | $original->destroy(); |
||
| 84 | unset($original); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | DataObject::flush_and_destroy_cache(); |
||
| 89 | |||
| 90 | echo sprintf("Created translation groups for %d records\n", count($allSiteTreeIDs)); |
||
| 91 | |||
| 92 | foreach (array('Stage', 'Live') as $stage) { |
||
| 93 | echo "\n\n#################################\n"; |
||
| 94 | echo "# Migrating stage $stage" . "\n"; |
||
| 95 | echo "#################################\n"; |
||
| 96 | |||
| 97 | $suffix = ($stage == 'Live') ? '_Live' : ''; |
||
| 98 | |||
| 99 | // First get all entries in SiteTree_lang |
||
| 100 | // This should be all translated pages |
||
| 101 | $trans = DB::query(sprintf('SELECT * FROM "_obsolete_SiteTree_lang%s"', $suffix)); |
||
| 102 | |||
| 103 | // Iterate over each translated pages |
||
| 104 | foreach ($trans as $oldtrans) { |
||
| 105 | $newLocale = i18n::get_locale_from_lang($oldtrans['Lang']); |
||
| 106 | |||
| 107 | echo sprintf( |
||
| 108 | "Migrating from %s to %s translation of '%s' (#%d)\n", |
||
| 109 | $oldtrans['Lang'], |
||
| 110 | $newLocale, |
||
| 111 | Convert::raw2xml($oldtrans['Title']), |
||
| 112 | $oldtrans['OriginalLangID'] |
||
| 113 | ); |
||
| 114 | |||
| 115 | // Get the untranslated page |
||
| 116 | |||
| 117 | $original = Versioned::get_one_by_stage( |
||
| 118 | $oldtrans['ClassName'], |
||
| 119 | $stage, |
||
| 120 | '"SiteTree"."ID" = ' . $oldtrans['OriginalLangID'] |
||
| 121 | ); |
||
| 122 | |||
| 123 | if (!$original) { |
||
| 124 | echo sprintf("Couldn't find original for #%d", $oldtrans['OriginalLangID']); |
||
| 125 | continue; |
||
| 126 | } |
||
| 127 | |||
| 128 | // write locale to $original |
||
| 129 | $original->Locale = i18n::get_locale_from_lang(Translatable::default_lang()); |
||
| 130 | $original->writeToStage($stage); |
||
| 131 | |||
| 132 | // Clone the original, and set it up as a translation |
||
| 133 | $existingTrans = $original->getTranslation($newLocale, $stage); |
||
| 134 | |||
| 135 | if ($existingTrans) { |
||
| 136 | echo sprintf( |
||
| 137 | "Found existing new-style translation for #%d. Already merged? Skipping.\n", |
||
| 138 | $oldtrans['OriginalLangID'] |
||
| 139 | ); |
||
| 140 | continue; |
||
| 141 | } |
||
| 142 | |||
| 143 | // Doesn't work with stage/live split |
||
| 144 | //$newtrans = $original->createTranslation($newLocale); |
||
| 145 | |||
| 146 | $newtrans = $original->duplicate(false); |
||
| 147 | $newtrans->OriginalID = $original->ID; |
||
| 148 | // we have to "guess" a locale based on the language |
||
| 149 | $newtrans->Locale = $newLocale; |
||
| 150 | if ($stage == 'Live' && array_key_exists($original->ID, $ids)) { |
||
| 151 | $newtrans->ID = $ids[$original->ID]; |
||
| 152 | } |
||
| 153 | |||
| 154 | // Look at each class in the ancestry, and see if there is a _lang table for it |
||
| 155 | foreach (ClassInfo::ancestry($oldtrans['ClassName']) as $classname) { |
||
| 156 | $oldtransitem = false; |
||
| 157 | |||
| 158 | // If the class is SiteTree, we already have the DB record, |
||
| 159 | // else check for the table and get the record |
||
| 160 | if ($classname == SiteTree::class) { |
||
| 161 | $oldtransitem = $oldtrans; |
||
| 162 | } elseif (in_array(strtolower($classname) . '_lang', DB::tableList())) { |
||
| 163 | $oldtransitem = DB::query(sprintf( |
||
| 164 | 'SELECT * FROM "_obsolete_%s_lang%s" WHERE "OriginalLangID" = %d AND "Lang" = \'%s\'', |
||
| 165 | $classname, |
||
| 166 | $suffix, |
||
| 167 | $original->ID, |
||
| 168 | $oldtrans['Lang'] |
||
| 169 | ))->first(); |
||
| 170 | } |
||
| 171 | |||
| 172 | // Copy each translated field into the new translation |
||
| 173 | if ($oldtransitem) { |
||
| 174 | foreach ($oldtransitem as $key => $value) { |
||
| 175 | if (!in_array($key, array('ID', 'OriginalLangID'))) { |
||
| 176 | $newtrans->$key = $value; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | // Write the new translation to the database |
||
| 183 | $sitelang = Translatable::get_current_locale(); |
||
| 184 | Translatable::set_current_locale($newtrans->Locale); |
||
| 185 | $newtrans->writeToStage($stage); |
||
| 186 | Translatable::set_current_locale($sitelang); |
||
| 187 | |||
| 188 | $newtrans->addTranslationGroup($original->getTranslationGroup(), true); |
||
| 189 | |||
| 190 | |||
| 191 | if ($stage == 'Stage') { |
||
| 192 | $ids[$original->ID] = $newtrans->ID; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | echo "\n\n#################################\n"; |
||
| 198 | echo "Done!\n"; |
||
| 199 | } |
||
| 200 | } |
||
| 201 |