$sources = [["url" => "https://es.wikipedia.org/wiki/Anexo:Se%C3%B1ales_de_tr%C3%A1fico_de_reglamentaci%C3%B3n_de_Espa%C3%B1a", "text_query" => "//ul[@class='gallery mw-gallery-traditional'][1]/li[@class='gallerybox']/div/div[@class='gallerytext']", "image_query" => "//ul[@class='gallery mw-gallery-traditional'][1]/li[@class='gallerybox']/div/div[@class='thumb']/div/a/img/@src", "subclass" => 1], ["url" => "https://es.wikipedia.org/wiki/Anexo:Se%C3%B1ales_de_tr%C3%A1fico_de_reglamentaci%C3%B3n_de_Espa%C3%B1a", "text_query" => "//ul[@class='gallery mw-gallery-traditional'][2]/li[@class='gallerybox']/div/div[@class='gallerytext']", "image_query" => "//ul[@class='gallery mw-gallery-traditional'][2]/li[@class='gallerybox']/div/div[@class='thumb']/div/a/img/@src", "subclass" => 2], ["url" => "https://es.wikipedia.org/wiki/Anexo:Se%C3%B1ales_de_tr%C3%A1fico_de_reglamentaci%C3%B3n_de_Espa%C3%B1a", "text_query" => "//ul[@class='gallery mw-gallery-traditional'][3]/li[@class='gallerybox']/div/div[@class='gallerytext']", "image_query" => "//ul[@class='gallery mw-gallery-traditional'][3]/li[@class='gallerybox']/div/div[@class='thumb']/div/a/img/@src", "subclass" => 2], ["url" => "https://es.wikipedia.org/wiki/Anexo:Se%C3%B1ales_de_tr%C3%A1fico_de_reglamentaci%C3%B3n_de_Espa%C3%B1a", "text_query" => "//ul[@class='gallery mw-gallery-traditional'][4]/li[@class='gallerybox']/div/div[@class='gallerytext']", "image_query" => "//ul[@class='gallery mw-gallery-traditional'][4]/li[@class='gallerybox']/div/div[@class='thumb']/div/a/img/@src", "subclass" => 2], ["url" => "https://es.wikipedia.org/wiki/Anexo:Se%C3%B1ales_de_tr%C3%A1fico_de_reglamentaci%C3%B3n_de_Espa%C3%B1a", "text_query" => "//ul[@class='gallery mw-gallery-traditional'][5]/li[@class='gallerybox']/div/div[@class='gallerytext']", "image_query" => "//ul[@class='gallery mw-gallery-traditional'][5]/li[@class='gallerybox']/div/div[@class='thumb']/div/a/img/@src", "subclass" => 4], ["url" => "https://es.wikipedia.org/wiki/Anexo:Se%C3%B1ales_de_tr%C3%A1fico_de_reglamentaci%C3%B3n_de_Espa%C3%B1a", "text_query" => "//ul[@class='gallery mw-gallery-traditional'][6]/li[@class='gallerybox']/div/div[@class='gallerytext']", "image_query" => "//ul[@class='gallery mw-gallery-traditional'][6]/li[@class='gallerybox']/div/div[@class='thumb']/div/a/img/@src", "subclass" => 6], ["url" => "https://es.wikipedia.org/wiki/Anexo:Señales_de_tráfico_de_peligro_de_España", "text_query" => "//ul[@class='gallery mw-gallery-traditional'][1]/li[@class='gallerybox']/div/div[@class='gallerytext']", "image_query" => "//ul[@class='gallery mw-gallery-traditional'][1]/li[@class='gallerybox']/div/div[@class='thumb']/div/a/img/@src", "subclass" => 3], ["url" => "https://es.wikipedia.org/wiki/Anexo:Se%C3%B1ales_de_tr%C3%A1fico_de_indicaci%C3%B3n_de_Espa%C3%B1a", "text_query" => "//table[@class='wikitable'][1]/tr[position()>1]/td[2]", "image_query" => "//table[@class='wikitable'][1]/tr/td[1]//a/img/@src", "subclass" => 5, "is_table" => true], ["url" => "https://es.wikipedia.org/wiki/Anexo:Se%C3%B1ales_de_tr%C3%A1fico_de_indicaci%C3%B3n_de_Espa%C3%B1a", "text_query" => "//table[@class='wikitable'][2]/tr[position()>1]/td[2]", "image_query" => "//table[@class='wikitable'][2]/tr/td[1]//a/img/@src", "subclass" => 5, "is_table" => true], ["url" => "https://es.wikipedia.org/wiki/Anexo:Se%C3%B1ales_de_tr%C3%A1fico_de_indicaci%C3%B3n_de_Espa%C3%B1a", "text_query" => "//table[@class='wikitable'][3]/tr[position()>1]/td[2]", "image_query" => "//table[@class='wikitable'][3]/tr/td[1]//a/img/@src", "subclass" => 5, "is_table" => true]]; foreach ($sources as $source) { $html = file_get_contents($source['url']); $dom = new DOMDocument(); @$dom->loadHtml($html); $xpath = new DOMXPath($dom); $classes = array(); $texts = $xpath->query($source['text_query']); $images = $xpath->query($source['image_query']); for ($i = 0; $i < $images->length; $i++) { if (isset($source['is_table']) && $source['is_table']) { $sign_id = trim(strip_tags(html_entity_decode(scrape_between(getInnerHTML($texts->item($i)), '<br>', '</b>')))); $sign_text = trim(strip_tags(html_entity_decode(scrape_between(getInnerHTML($texts->item($i)), '<b>', '<br>')))); } else { $sign_id = trim(strip_tags(html_entity_decode(scrape_between(getInnerHTML($texts->item($i)), '<b>', '<br>')))); $sign_text = trim(strip_tags(html_entity_decode(scrape_between(getInnerHTML($texts->item($i)), '<br>', '</center>')))); } $sign_id = str_replace(' ', '', $sign_id); if (empty($sign_id)) { $sign_id = NULL; } $image = "https:" . $images->item($i)->nodeValue; $classes[$i]['spain_id'] = $sign_id; $classes[$i]['text'] = $sign_text; $classes[$i]['image'] = $image; } saveClasses($classes, $source['subclass']); } function saveClasses($classes, $subclass) { foreach ($classes as $class) {
function onAfterDispatch() { global $offlajnParams, $offlajnDashboard; $app = JFactory::getApplication(); if (!defined('OFFLAJNADMIN') || isset($_REQUEST['output']) && $_REQUEST['output'] == 'json') { return; } $doc = JFactory::getDocument(); $c = $doc->getBuffer('component'); $dom = new DomDocument(); if (function_exists("mb_convert_encoding")) { @$dom->loadHtml('<?xml encoding="UTF-8"><div>' . mb_convert_encoding($c, 'HTML-ENTITIES', "UTF-8") . '</div>'); } else { @$dom->loadHtml('<?xml encoding="UTF-8"><div>' . htmlspecialchars_decode(utf8_decode(htmlentities($c, ENT_COMPAT, 'utf-8', false))) . '</div>'); } $lis = array(); $moduleparams = ""; $advanced = JRequest::getCmd('option') == 'com_advancedmodules'; if (version_compare(JVERSION, '3.0.0', 'ge') && !$this->getElementById($dom, 'module-sliders')) { // Joomla 3.0.3 fix if (version_compare(JVERSION, '3.1.99', 'ge')) { $moduleparams = $this->getElementByClass($dom, 'span9'); } elseif (version_compare(JVERSION, '3.0.3', 'ge')) { $moduleparams = $this->getElementById($dom, 'collapse0'); } else { $moduleparams = $this->getElementById($dom, 'options-basic'); } if ($advanced) { $moduleparams = version_compare(JVERSION, '3.2.2', 'ge') ? $this->getElementByClass($dom, 'span9') : $this->getElementByClass($dom, 'span6', 1); } if ($moduleparams) { $element = $dom->createElement('div'); $element->setAttribute('id', 'content-box'); $moduleparams->appendChild($element); $moduleparams = $element; $element = $dom->createElement('div'); $element->setAttribute('id', 'module-sliders'); $element->setAttribute('class', 'pane-sliders'); $moduleparams->appendChild($element); $moduleparams = $element; } } elseif (version_compare(JVERSION, '1.6.0', 'ge')) { $moduleparams = $this->getElementById($dom, 'module-sliders'); } else { $moduleparams = $this->getElementById($dom, 'menu-pane'); } if ($moduleparams) { $removed = array(); while ($cNode = $moduleparams->firstChild) { $removed[] = $moduleparams->removeChild($cNode); } if (version_compare(JVERSION, '1.6.0', 'ge')) { array_splice($removed, 0, 2); } else { array_splice($removed, 0, 1); } $html = '<div>'; $html .= isset($offlajnDashboard) ? $offlajnDashboard : ''; $html .= isset($offlajnParams['first']) && is_array($offlajnParams['first']) ? implode("\n", $offlajnParams['first']) : ''; $html .= isset($offlajnParams['last']) && is_array($offlajnParams['last']) ? implode("\n", $offlajnParams['last']) : ''; $html .= '</div>'; $tabsDom = new DomDocument(); if (function_exists("mb_convert_encoding")) { @$tabsDom->loadHtml('<?xml encoding="UTF-8">' . mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8")); } else { @$tabsDom->loadHtml('<?xml encoding="UTF-8">' . htmlspecialchars_decode(utf8_decode(htmlentities($html, ENT_COMPAT, 'utf-8', false)))); } $node = $dom->importNode($tabsDom->getElementsByTagName('div')->item(0), true); while ($cNode = $node->firstChild) { if (@$cNode->tagName == 'div') { $moduleparams->appendChild($cNode); } else { $node->removeChild($cNode); } } if (count($removed) > 0) { foreach ($removed as $r) { if ($r instanceof DOMElement) { $r->setAttribute("class", $r->getAttribute("class") . " legacy"); $moduleparams->appendChild($r); } } } if (!version_compare(JVERSION, '1.6.0', 'ge')) { $tables = $dom->getElementsByTagName('table'); foreach ($tables as $table) { $table->setAttribute("cellspacing", "0"); } } $params = $moduleparams->getElementsByTagName('h3'); foreach ($params as $param) { $span = $param->getElementsByTagName('span')->item(0); $titleWords = explode(" ", $span->textContent); $titleWords[count($titleWords) - 1] = "<b>" . $titleWords[count($titleWords) - 1] . "</b>"; $newTitle = implode(' ', $titleWords); $span->removeChild($span->firstChild); $newText = $dom->createCDATASection($newTitle); $span->appendChild($newText); } $j = 0; foreach ($moduleparams->childNodes as $param) { $param->setAttribute("id", "offlajnpanel-" . $j); $j++; } } if (!isset($doc->_script['text/javascript'])) { $doc->_script['text/javascript'] = array(); } $doc->_script['text/javascript'] = preg_replace("/window.addEvent.*?pane-toggler.*?\\}\\);.*?\\}\\);/i", '', $doc->_script['text/javascript']); $doc->_script['text/javascript'] .= ' window.addEvent("domready", function(){ if(document.formvalidator) document.formvalidator.isValid = function() {return true;}; });'; if (version_compare(JVERSION, '3.0.0', 'ge')) { if ($moduleparams && $moduleparams->parentNode) { function getInnerHTML($Node) { $Document = new DOMDocument(); $Document->appendChild($Document->importNode($Node, true)); return $Document->saveHTML(); } $nc = getInnerHTML($moduleparams->parentNode); } else { $nc = $dom->saveHTML(); } $nc = preg_replace("/.*?<body>/si", '', $nc, 1); $nc = preg_replace("/<\\/body>.*/si", '', $nc, 1); $pattern = '/<div\\s*class="tab-pane"\\s*id="options-basic".*?>/'; if (version_compare(JVERSION, '3.1.99', 'ge')) { $pattern = '/<div\\s*class="span9".*?>/'; } elseif (version_compare(JVERSION, '3.0.3', 'ge')) { $pattern = '/<div\\s*class="accordion-body collapse in"\\s*id="collapse0".*?>/'; } if ($advanced) { $pattern = version_compare(JVERSION, '3.2.2', 'ge') ? '/<div\\s*class="span9".*?>/' : '/<\\/div>\\s*<div\\s*class="span6".*?>/'; } preg_match($pattern, $c, $matches); if (count($matches) > 0) { $c = str_replace($matches[0], $matches[0] . $nc, $c); } else { $c = $nc; } } else { $c = $dom->saveHtml(); $c = preg_replace("/.*?<body><div>/si", '', $c, 1); $c = preg_replace("/<\\/div><\\/body>.*/si", '', $c, 1); } $doc->setBuffer($c, 'component'); }
function extractTextFromNode($node, &$strings) { global $file; $attrs = array('span' => array('text', 'head', 'title')); if ($node instanceof DOMText) { $text = checkExtractedString($node->textContent); if (strlen($text) == 0) { return; } $line_no = null; if (version_compare(PHP_VERSION, '5.3.0') >= 0) { $line_no = $node->getLineNo(); } setSourceComment($strings, $text, $line_no); return; } if ($node instanceof DOMElement) { $check_children = true; if (strtolower($node->tagName) == 'script' || strtolower($node->tagName) == 'style') { //skip a script node: Thanks to Federico Vera for pointing this out. return; } if ($node->hasAttribute('printf')) { $text = checkExtractedPrintF($node->getAttribute('printf')); if (strlen($text) == 0) { continue; } if ($node->hasAttribute('printf_plural') && $node->hasAttribute('printf_form')) { $text_form = checkExtractedString($node->getAttribute('printf_form')); if (strlen($text_form) == 0) { continue; } $text_plural = checkExtractedString($node->getAttribute('printf_plural')); if (strlen($text_plural) == 0) { continue; } addPluralString($strings, $text, $text_plural); } else { //assume it is only the singular form addString($strings, $text); } setFormatComment($strings, $text, 'c-format'); $line_no = null; if (version_compare(PHP_VERSION, '5.3.0') >= 0) { $line_no = $node->getLineNo(); } setSourceComment($strings, $text, $line_no); if ($node->hasAttribute('translator_comment')) { setExtractedComment($strings, $text, $node->getAttribute('translator_comment')); } return; } else { if ($node->hasAttribute('lang')) { $lang = $node->getAttribute('lang'); if (strtr($lang, '-', '_') == I2CE_Locales::DEFAULT_LOCALE) { //if we have lang='en-US' of lang='en_US' attribute, treat this as a block $block = $node->tagName; $text = checkExtractedString(getInnerHTML($node)); if (strlen($text) == 0) { return; } $line_no = null; if (version_compare(PHP_VERSION, '5.3.0') >= 0) { $line_no = $node->getLineNo(); } setSourceComment($strings, $text, $line_no); if ($node->hasAttribute('translator_comment')) { setExtractedComment($strings, $text, $node->getAttribute('translator_comment')); } $check_children = false; } } } if (array_key_exists($node->tagName, $attrs)) { foreach ($attrs[$node->tagName] as $attr) { if ($node->hasAttribute($attr)) { $text = checkExtractedString($node->getAttribute($attr)); if (strlen($text) == 0) { continue; } $line_no = null; if (version_compare(PHP_VERSION, '5.3.0') >= 0) { $line_no = $node->getLineNo(); } setSourceComment($strings, $text, $line_no); } } } if ($check_children) { for ($i = 0; $i < $node->childNodes->length; $i++) { extractTextFromNode($node->childNodes->item($i), $strings); } } } }
function translateNode($node) { if (!$node instanceof DOMNode) { var_dump($node); die; return; } global $locale_dashed; global $translations; global $changed_text; global $rtl; $attrs = array('span' => array('text', 'head', 'title')); if ($node instanceof DOMText) { $trans = translate($node->textContent); if ($trans !== false) { $node->deleteData(0, $node->length); $node->appendData($trans); if ($rtl && $node->parentNode instanceof DOMElement) { $node->parentNode->setAttribute('dir', 'rtl'); } } } $block = false; if ($node instanceof DOMElement) { if (strtolower($node->tagName) == 'script' || strtolower($node->tagName) == 'style') { return; } if ($node->hasAttribute('translator_comment')) { $node->removeAttribute('translator_comment'); } if ($node->hasAttribute('printf')) { list($trans_sing, $trans_remainder) = extractPrintF($node->getAttribute('printf')); if ($node->hasAttribute('printf_plural') && $node->hasAttribute('printf_form')) { if (array_key_exists($trans_sing, $translations) && is_array($translations[$trans_sing])) { $changed_text = true; //we have translated plural information. $node->removeAttribute('printf_plural'); $node->removeAttribute('printf_sing'); foreach ($translations[$trans_sing] as $n => $trans) { $node->setAttribute('printf_' . $n, "'" . $trans . "'"); } $node->setAttribute('lang', $locale_dashed); if ($rtl) { $node->setAttribute('dir', 'rtl'); } } } else { $trans_sing = translate($trans_sing); if ($trans_sing !== false) { $node->setAttribute('printf', "'" . $trans_sing . "'" . $trans_remainder); if ($rtl) { $node->setAttribute('dir', 'rtl'); } } } return; } else { if ($node->hasAttribute('lang')) { $lang = $node->getAttribute('lang'); if (strtr($lang, '-', '-') == I2CE_Locales::DEFAULT_LOCALE) { //if we have lang='en-US' of lang='en_US' attribute, treat this as a block $block = $node->tagName; } } else { if (array_key_exists($node->tagName, $attrs)) { foreach ($attrs[$node->tagName] as $attr) { if ($node->hasAttribute($attr)) { $trans = translate($node->getAttribute($attr)); if ($trans !== false) { $node->setAttribute($attr, $trans); } } } } } } } if (!$block) { if ($node->childNodes instanceof DOMNodeList) { for ($i = 0; $i < $node->childNodes->length; $i++) { translateNode($node->childNodes->item($i)); } } } else { $text = checkExtractedString(getInnerHTML($node)); if (strlen($text) > 0 && ($trans = translate($text)) !== false) { $html = false; $body = false; $head = false; $wrapped = "<{$block}>{$trans}</{$block}>"; $wrapped = wrapHTMLinUTF8($wrapped); $import_dom = new DOMDocument(); $orig_dom = $node->ownerDocument; libxml_clear_errors(); $success = $import_dom->loadHTML($wrapped); $success = $success && count(libxml_get_errors()) == 0; if (!$success) { $tmp_dom = new DOMDocument(); if (!$tmp_dom->loadHTML($wrapped)) { die("Problem importing translated block"); } else { die("Yell at carl -- problem with wrapping"); } } $xpath = new DOMXpath($import_dom); $nodeList = $xpath->query('//' . $block . '[1]'); if ($nodeList->length != 1) { die("Yell at carl about finding blocks //{$block}" . "[1]" . " -- " . $nodeList->length . "\n"); } $new_node = $orig_dom->importNode($nodeList->item(0), true); // var_dump($orig_dom); // var_dump($import_dom); // var_dump($new_node->ownerDocument); $new_node->setAttribute('lang', $locale_dashed); if ($rtl) { $new_node->setAttribute('dir', 'rtl'); } $node->parentNode->replaceChild($new_node, $node); } } }