Conditions | 18 |
Paths | 222 |
Total Lines | 133 |
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 |
||
54 | public function run($request) |
||
55 | { |
||
56 | $ids = array(); |
||
57 | |||
58 | echo "#################################\n"; |
||
59 | echo "# Adding translation groups to existing records" . "\n"; |
||
60 | echo "#################################\n"; |
||
61 | |||
62 | $allSiteTreeIDs = DB::query('SELECT "ID" FROM "SiteTree"')->column(); |
||
63 | if ($allSiteTreeIDs) { |
||
64 | foreach ($allSiteTreeIDs as $id) { |
||
65 | $original = DataObject::get_by_id('SiteTree', $id); |
||
66 | $existingGroupID = $original->getTranslationGroup(); |
||
67 | if (!$existingGroupID) { |
||
68 | $original->addTranslationGroup($original->ID); |
||
69 | } |
||
70 | $original->destroy(); |
||
71 | unset($original); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | DataObject::flush_and_destroy_cache(); |
||
76 | |||
77 | echo sprintf("Created translation groups for %d records\n", count($allSiteTreeIDs)); |
||
78 | |||
79 | foreach (array('Stage', 'Live') as $stage) { |
||
80 | echo "\n\n#################################\n"; |
||
81 | echo "# Migrating stage $stage" . "\n"; |
||
82 | echo "#################################\n"; |
||
83 | |||
84 | $suffix = ($stage == 'Live') ? '_Live' : ''; |
||
85 | |||
86 | // First get all entries in SiteTree_lang |
||
87 | // This should be all translated pages |
||
88 | $trans = DB::query(sprintf('SELECT * FROM "_obsolete_SiteTree_lang%s"', $suffix)); |
||
89 | |||
90 | // Iterate over each translated pages |
||
91 | foreach ($trans as $oldtrans) { |
||
92 | $newLocale = i18n::get_locale_from_lang($oldtrans['Lang']); |
||
93 | |||
94 | echo sprintf( |
||
95 | "Migrating from %s to %s translation of '%s' (#%d)\n", |
||
96 | $oldtrans['Lang'], |
||
97 | $newLocale, |
||
98 | Convert::raw2xml($oldtrans['Title']), |
||
99 | $oldtrans['OriginalLangID'] |
||
100 | ); |
||
101 | |||
102 | // Get the untranslated page |
||
103 | |||
104 | $original = Versioned::get_one_by_stage( |
||
105 | $oldtrans['ClassName'], |
||
106 | $stage, |
||
107 | '"SiteTree"."ID" = ' . $oldtrans['OriginalLangID'] |
||
108 | ); |
||
109 | |||
110 | if (!$original) { |
||
111 | echo sprintf("Couldn't find original for #%d", $oldtrans['OriginalLangID']); |
||
112 | continue; |
||
113 | } |
||
114 | |||
115 | // write locale to $original |
||
116 | $original->Locale = i18n::get_locale_from_lang(Translatable::default_lang()); |
||
|
|||
117 | $original->writeToStage($stage); |
||
118 | |||
119 | // Clone the original, and set it up as a translation |
||
120 | $existingTrans = $original->getTranslation($newLocale, $stage); |
||
121 | |||
122 | if ($existingTrans) { |
||
123 | echo sprintf( |
||
124 | "Found existing new-style translation for #%d. Already merged? Skipping.\n", |
||
125 | $oldtrans['OriginalLangID'] |
||
126 | ); |
||
127 | continue; |
||
128 | } |
||
129 | |||
130 | // Doesn't work with stage/live split |
||
131 | //$newtrans = $original->createTranslation($newLocale); |
||
132 | |||
133 | $newtrans = $original->duplicate(false); |
||
134 | $newtrans->OriginalID = $original->ID; |
||
135 | // we have to "guess" a locale based on the language |
||
136 | $newtrans->Locale = $newLocale; |
||
137 | if ($stage == 'Live' && array_key_exists($original->ID, $ids)) { |
||
138 | $newtrans->ID = $ids[$original->ID]; |
||
139 | } |
||
140 | |||
141 | // Look at each class in the ancestry, and see if there is a _lang table for it |
||
142 | foreach (ClassInfo::ancestry($oldtrans['ClassName']) as $classname) { |
||
143 | $oldtransitem = false; |
||
144 | |||
145 | // If the class is SiteTree, we already have the DB record, |
||
146 | // else check for the table and get the record |
||
147 | if ($classname == 'SiteTree') { |
||
148 | $oldtransitem = $oldtrans; |
||
149 | } elseif (in_array(strtolower($classname) . '_lang', DB::tableList())) { |
||
150 | $oldtransitem = DB::query(sprintf( |
||
151 | 'SELECT * FROM "_obsolete_%s_lang%s" WHERE "OriginalLangID" = %d AND "Lang" = \'%s\'', |
||
152 | $classname, |
||
153 | $suffix, |
||
154 | $original->ID, |
||
155 | $oldtrans['Lang'] |
||
156 | ))->first(); |
||
157 | } |
||
158 | |||
159 | // Copy each translated field into the new translation |
||
160 | if ($oldtransitem) { |
||
161 | foreach ($oldtransitem as $key => $value) { |
||
162 | if (!in_array($key, array('ID', 'OriginalLangID'))) { |
||
163 | $newtrans->$key = $value; |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | } |
||
168 | |||
169 | // Write the new translation to the database |
||
170 | $sitelang = Translatable::get_current_locale(); |
||
171 | Translatable::set_current_locale($newtrans->Locale); |
||
172 | $newtrans->writeToStage($stage); |
||
173 | Translatable::set_current_locale($sitelang); |
||
174 | |||
175 | $newtrans->addTranslationGroup($original->getTranslationGroup(), true); |
||
176 | |||
177 | |||
178 | if ($stage == 'Stage') { |
||
179 | $ids[$original->ID] = $newtrans->ID; |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | |||
184 | echo "\n\n#################################\n"; |
||
185 | echo "Done!\n"; |
||
186 | } |
||
187 | } |
||
188 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.