Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
tao_helpers_translation_StructureExtractor
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 1
306
0.00% covered (danger)
0.00%
0 / 1
 extract
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 1
306
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung
19 *                         (under the project TAO-TRANSFER);
20 *               2009-2012 (update and modification) Public Research Centre Henri Tudor
21 *                         (under the project TAO-SUSTAIN & TAO-DEV);
22 *
23 */
24
25/**
26 * The StructureExtractor extracts translation units from structures.xml files.
27 *
28 * Some extracted translation units will be given a 'tao-public' flag. This flag indicates
29 * that the translation unit has to be included in every compiled messages.po file.
30 *
31 * @access public
32 * @author firstname and lastname of author, <author@example.org>
33 * @package tao
34
35 */
36class tao_helpers_translation_StructureExtractor extends tao_helpers_translation_TranslationExtractor
37{
38    /**
39     * Extracts the translation units from a structures.xml file.
40     * Translation Units can be retrieved after extraction by calling the getTranslationUnits
41     * method.
42     *
43     * @access public
44     * @author Jerome Bogaerts <jerome@taotesting.com>
45     * @throws tao_helpers_translation_TranslationException If an error occurs.
46     */
47    public function extract()
48    {
49        $translationUnits = [];
50
51        foreach ($this->getPaths() as $file) {
52            $xml = new \SimpleXMLElement($file, null, true);
53            if ($xml instanceof SimpleXMLElement) {
54                // look up for "name" attributes of structure elements.
55                $nodes = $xml->xpath("//structure[@name]|//section[@name]");
56                foreach ($nodes as $node) {
57                    if (isset($node['name'])) {
58                        $nodeName = (string)$node['name'];
59                        $newTranslationUnit = new tao_helpers_translation_POTranslationUnit();
60                        $newTranslationUnit->setSource($nodeName);
61                        $newTranslationUnit->addFlag('tao-public');
62                        $translationUnits[$nodeName] = $newTranslationUnit;
63                    }
64                }
65
66                // look up for "name" attributes of action elements.
67                $nodes = $xml->xpath("//action[@name]|//tree[@name]");
68                foreach ($nodes as $node) {
69                    if (isset($node['name'])) {
70                        $nodeName = (string)$node['name'];
71                        $newTranslationUnit = new tao_helpers_translation_POTranslationUnit();
72                        $newTranslationUnit->setSource($nodeName);
73                        $newTranslationUnit->addFlag('tao-public');
74                        $translationUnits[$nodeName] = $newTranslationUnit;
75                    }
76                }
77
78                // look up for "description" elements.
79                $nodes = $xml->xpath("//description");
80                foreach ($nodes as $node) {
81                    if ((string)$node != '') {
82                        $newTranslationUnit = new tao_helpers_translation_POTranslationUnit();
83                        $newTranslationUnit->setSource((string)$node);
84                        $newTranslationUnit->addFlag('tao-public');
85                        $translationUnits[(string)$node] = $newTranslationUnit;
86                    }
87                }
88
89                // look up for "action" elements inside a toolbar
90                $nodes = $xml->xpath("//toolbar/toolbaraction");
91                foreach ($nodes as $node) {
92                    $nodeValue = (string)$node;
93                    if (trim($nodeValue) != '' && !array_key_exists($nodeValue, $translationUnits)) {
94                        $newTranslationUnit = new tao_helpers_translation_POTranslationUnit();
95                        $newTranslationUnit->setSource($nodeValue);
96                        $newTranslationUnit->addFlag('tao-public');
97                        $translationUnits[$nodeValue] = $newTranslationUnit;
98                    }
99                }
100
101                // look up for the "title" attr of an "action" element inside a toolbar
102                $nodes = $xml->xpath("//toolbar/toolbaraction[@title]");
103                foreach ($nodes as $node) {
104                    if (isset($node['title'])) {
105                        $nodeTitle = (string)$node['title'];
106                        $newTranslationUnit = new tao_helpers_translation_POTranslationUnit();
107                        $newTranslationUnit->setSource($nodeTitle);
108                        $newTranslationUnit->addFlag('tao-public');
109                        $translationUnits[$nodeTitle] = $newTranslationUnit;
110                    }
111                }
112
113                // look up for the "entrypoint" elements
114                $nodes = $xml->xpath("//entrypoint");
115                foreach ($nodes as $node) {
116                    if (isset($node['title'])) {
117                        $nodeTitle = (string)$node['title'];
118                        $newTranslationUnit = new tao_helpers_translation_POTranslationUnit();
119                        $newTranslationUnit->setSource($nodeTitle);
120                        $newTranslationUnit->addFlag('tao-public');
121                        $translationUnits[$nodeTitle] = $newTranslationUnit;
122                    }
123                    if (isset($node['label'])) {
124                        $nodeLabel = (string)$node['label'];
125                        $newTranslationUnit = new tao_helpers_translation_POTranslationUnit();
126                        $newTranslationUnit->setSource($nodeLabel);
127                        $newTranslationUnit->addFlag('tao-public');
128                        $translationUnits[$nodeTitle] = $newTranslationUnit;
129                    }
130                }
131            }
132        }
133
134        $this->setTranslationUnits(array_values($translationUnits));
135    }
136}