Page MenuHomeDevCentral

D307.diff
No OneTemporary

D307.diff

diff --git a/page.php b/page.php
--- a/page.php
+++ b/page.php
@@ -108,7 +108,7 @@
$this->get_data();
if ($this->data) {
$this->analyse();
- }
+ }
}
function get_data () {
@@ -121,11 +121,14 @@
return;
}
}
- $encoding = mb_detect_encoding($data, "ISO-8859-15, ISO-8859-1, UTF-8, ASCII, auto");
+ $this->data = $data;
+ $this->encodeData();
+ }
+
+ function encodeData () {
+ $encoding = mb_detect_encoding($this->data, "ISO-8859-15, ISO-8859-1, UTF-8, ASCII, auto");
if ($encoding && $encoding != 'UTF-8') {
- $this->data = Encoding::toUTF8($data);
- } else {
- $this->data = $data;
+ $this->data = Encoding::toUTF8($this->data);
}
}
diff --git a/pages/DownloadWithWget.php b/pages/DownloadWithWget.php
new file mode 100644
--- /dev/null
+++ b/pages/DownloadWithWget.php
@@ -0,0 +1,37 @@
+<?php
+
+trait DownloadWithWget {
+
+ /**
+ * @return string
+ */
+ private function getTemporaryFilename () {
+ $dir = sys_get_temp_dir();
+ return tempnam($dir, "http-client-wget-");
+ }
+
+ /**
+ * Gets the content of the specified URL, using wget to download it
+ *
+ * @return string
+ */
+ function getFileContents ($url) {
+ $file = $this->getTemporaryFilename();
+ $url = escapeshellarg($url);
+
+ system("wget -q -O $file $url");
+ $data = file_get_contents($file);
+ unlink($file);
+
+ return $data;
+ }
+
+ /**
+ * Downloads the URL through wget and fill data properties
+ */
+ function get_data () {
+ $this->data = $this->getFileContents($this->url);
+ $this->encodeData();
+ }
+
+}
diff --git a/tests/DownloadWithWgetTest.php b/tests/DownloadWithWgetTest.php
new file mode 100644
--- /dev/null
+++ b/tests/DownloadWithWgetTest.php
@@ -0,0 +1,34 @@
+<?php
+
+require 'pages/DownloadWithWget.php';
+
+class DownloadWithWgetTest extends \PHPUnit_Framework_TestCase {
+
+ /**
+ * The object under test.
+ *
+ * @var object
+ */
+ private $instance;
+
+ /**
+ * Sets up the fixture.
+ *
+ * This method is called before a test is executed.
+ *
+ * @return void
+ */
+ public function setUp () {
+ $this->instance = $this->getObjectForTrait('DownloadWithWget');
+ }
+
+ /**
+ * Tests getFileContents method
+ */
+ public function testGetFileContents () {
+ $this->assertContains(
+ "* <----- vous &ecirc;tes ici",
+ $this->instance->getFileContents("http://www.perdu.com")
+ );
+ }
+}

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 23, 09:24 (18 h, 31 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2255844
Default Alt Text
D307.diff (2 KB)

Event Timeline