| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Drupal\qa\Plugin\Qa\Control\I18n; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Drupal\qa\Plugin\Qa\Control\BaseControl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Find inconsistencies in {i18nvariables} and {languages} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class Variables extends BaseControl { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |    * {@inheritdoc] | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |    */ | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |   public function init() { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     $this->package_name = __NAMESPACE__; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     $this->title = t('Inconsistent variables translation'); | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     $this->description = t('In most scenarios, when a variable is translated at least once, it ought to be translated in every language on the site, not more, not less.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |    * Identify variables translations for languages not currently on site | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |   function checkExtra() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     $languages = array_keys(language_list()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     $ph = db_placeholders($languages, 'char'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     $sq = <<<sql | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | SELECT v.name, v.language | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | FROM {i18n_variable} v | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | WHERE v.language NOT IN ($ph) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | ORDER BY 1, 2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | sql; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     $q = db_query($sq, $languages); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     // No db_rewrite_sql: this needs to be unhindered by any access control | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     $vars = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     while ($o = db_fetch_object($q)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |       $vars[$o->name][] = $o->language; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     $items = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     foreach ($vars as $name => $var_languages) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |       $items[] = t('@var: @languages', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         ['@var' => $name, '@languages' => implode(', ', $var_languages)]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     $ret = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |       'name' => 'extra', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |       'status' => empty($vars) ? 1 : 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |       'result' => ['extra' => $items], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     return $ret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |    * Identify variables for which at least one translation is missing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |   function checkMissing() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     $languages = array_keys(language_list()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     $ph = db_placeholders($languages, 'char'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     $sq = <<<sql | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | SELECT v.name, v.language | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | FROM {i18n_variable} v | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | WHERE v.language IN ($ph) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | ORDER BY 1, 2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | sql; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     $q = db_query($sq, $languages); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     // No db_rewrite_sql: this needs to be unhindered by any access control | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     $vars = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     while ($o = db_fetch_object($q, $ph)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |       $vars[$o->name][] = $o->language; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     $items = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     foreach ($vars as $name => $var_languages) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |       $missing = array_diff($languages, $var_languages); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |       if (!empty($missing)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $items[] = t('@var: @languages', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |           ['@var' => $name, '@languages' => implode(', ', $missing)]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |       } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     $ret = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |       'name' => 'missing', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |       'status' => empty($items) ? 1 : 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |       'result' => ['missing' => $items], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     return $ret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |   static function getDependencies() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     $ret = parent::getDependencies(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     $ret = array_merge($ret, ['i18n']); // introduces {i18n_variable} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     return $ret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |   function run() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     $pass = parent::run(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     $pass->record($this->checkExtra()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     $pass->life->modify(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     $pass->record($this->checkMissing()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     $pass->life->end(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     $extra_row = empty($pass->result['extra']['extra']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |       ? [['data' => t('No extra translation found'), 'colspan' => 2,]] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |       : [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         t('Extra translations'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         theme('item_list', $pass->result['extra']['extra']), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |       ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     $missing_row = empty($pass->result['missing']['missing']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |       ? [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |           'data' => t('No missing translation found'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |           'colspan' => 2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |       ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |       : [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         t('Missing translations'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         theme('item_list', $pass->result['missing']['missing']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |       ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     $rows = [$extra_row, $missing_row]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     $pass->result = theme('table', NULL, $rows); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     return $pass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |   } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |  | 
            
                        
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.