Category Archives: software & technology - Page 2

HowTo: Bugfix file too large for wordpress importer

During the update to WordPress 3.2, I encountered (like many other people) the problem with limitation of the filesize for file uploads. Per default PHP will set it to 2MB whereas my wordpress export backup file (XML) has already 4MB. No, reconfiguring PHP’s upload_max_filesize was no option for me as far as the backup was already stored as a dump at the server. So my only thing to do was to replace the uploaded file with the already existing at the harddisk and make WordPress recognizing it. And this was not that difficult if you know where to look for. So here is my patch to get the wordpress-importer import a file already stored at the server hard disk. The wordpress export file to load has to be stored at wp-content/uploads/wordpress.import.xml.

diff --git a/wp-content/plugins/wordpress-importer/wordpress-importer.php b/wp-content/plugins/wordpress-importer/wordpress-importer.php
index 5e38484..e0cace0 100644
--- a/wp-content/plugins/wordpress-importer/wordpress-importer.php
+++ b/wp-content/plugins/wordpress-importer/wordpress-importer.php
@@ -102,6 +102,7 @@ class WP_Import extends WP_Importer {
         * @param string $file Path to the WXR file for importing
         */
        function import( $file ) {
+               $file = ABSPATH . 'wp-content/uploads/wordpress.import.xml'; #wp_import_handle_upload();
                add_filter( 'import_post_meta_key', array( $this, 'is_valid_meta_key' ) );
                add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );

@@ -132,7 +133,7 @@ class WP_Import extends WP_Importer {
        function import_start( $file ) {
                if ( ! is_file($file) ) {
                        echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
-                       echo __( 'The file does not exist, please try again.', 'wordpress-importer' ) . '</p>';
+                       echo __( 'The file '.htmlspecialchars($file).' does not exist, please try again.', 'wordpress-importer' ) . '</p>';
                        $this->footer();
                        die();
                }
@@ -188,7 +189,7 @@ class WP_Import extends WP_Importer {
         * @return bool False if error uploading or invalid file, true otherwise
         */
        function handle_upload() {
-               $file = wp_import_handle_upload();
+               $file = array('id' => 4869, 'file' => ABSPATH . 'wp-content/uploads/wordpress.import.xml'); #wp_import_handle_upload();

                if ( isset( $file['error'] ) ) {
                        echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';

I did not put any further (compatibility) research into that issue. Worked for me™ with WordPress Version 3.2 and PHP Version 5.3.

WordPress FAQ: Import and Export

Python wants to become one-based?

Yesterday (or today in my timezone) Guido wrote a reply, which indicated that he wants python to become one-based. I have written recently about my WTF-situation when I realized that SQL is one-based (and also Lua which comes up in the python’s mailing list discussion). Personally speaking I think that python is that damn intuitive because it’s based on mathematical principles (which Guido as mathematician is aware of). But math is not that consistent and beautiful that it solves all problems.

A sequence of numbers is a range of numbers from x to y with x included but y excluded. To get all indizes of a list, you can use range(0, len(seq)). This is damn readable and does not include a nasty -1 like in most other languages. Well… life is not that beautiful. If we change to a one-based numbering system, what will happen? range(1, len(seq)+1)? Seriously? Lua does not exclude y and therefore does not have a nasty +1.

Lists start with 0, because 0 is the firstzeroth number. In a decimal system, the firstzeroth number with two digits is 10. Three digits: 100. The least-significant number is always zero. The firstzeroth number with one digit is 1? This only comes from an exception in mathematics (102..0).

If I want to have all numbers in a list at 2*n indizes in python I will use …

>>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>> [a[2 * index] for index in xrange(0, len(a) / 2)]
[1, 3, 5, 7, 9]

Again: No nasty +1. Just like Wikipedia points out, it’s about congruence.

I understand that in certain situations it can be useful. If you want to get all numbers at 2n indizes, you probably want to have the mathematical base0=1 and you would be fine with one-based systems. You probably want to get the real firstzeroth number:

>>> import math
>>> a = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
>>> [a[2 ** index - 1] for index in xrange(0, int(math.log(len(a), 2)) + 1)]
[0, 1, 2, 13]

For Lua it just says:

a = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34}
for index=1,math.log(#a+1, 2)+1 do
  print(a[2 ^ index])
end

I have shown only mathematical examples, but science is want I consider to be one of the most important fields for python. Other (especially old-school) languages really suck at this and it’s one of python’s strengths to be fine with scientists from other fields. In a mathematical context one-based systems it might be okay (see MATLAB), but for me a list starting with 1 is unpythonic and not useful. Yes, just like the Lua community I can live with a one-based languages, but I generally consider it to be a design fault.

Thanks to Bruce Leban for this awesome funny reply.

Update: Guido, you got me. It was a joke ;-)

When MySQL substr does not work

So let’s start with a simple MySQL setup:

mysql> CREATE TABLE example (prefix VARCHAR(50),
domain VARCHAR(30), PRIMARY KEY(prefix, domain));
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO example VALUES
("hello", "world.org"), ("foo", "bar"),  ("Foot","ball");
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

Alright… so we have some sort of “split up” email addresses. It’s not a problem to combine them together on the fly as far as MySQL provides basic string operations.

mysql> SELECT CONCAT(prefix, "@", domain) as email_addr FROM example;
+-----------------+
| email_addr      |
+-----------------+
| foo@bar         |
| Foot@ball       |
| hello@world.org |
+-----------------+
3 rows in set (0.00 sec)

So now let’s say, we want to remove the Top Level Domain from the domain (“world” instead of “world.org”) [1].

mysql> SELECT SUBSTR(domain, 0, LOCATE('.', domain)-1) as tld FROM example;
+-----+
| tld |
+-----+
|     |
|     |
|     |
+-----+
3 rows in set (0.00 sec)

What? Let’s slow down… take a substring of domain starting at position zero and with length of the position of the “.” (dot character) minus 1 (before that dot character). Okay… so something has to be wrong about it.

mysql> SELECT domain as tld FROM example;
+-----------+
| tld       |
+-----------+
| bar       |
| ball      |
| world.org |
+-----------+
3 rows in set (0.00 sec)

A quarter of an hour later I realized the problem with the help of a colleague:

mysql> SELECT SUBSTR(domain, 1, LOCATE('.', domain)-1) as tld FROM example;
+-------+
| tld   |
+-------+
|       |
|       |
| world |
+-------+
3 rows in set (0.00 sec)

SQL (and therefore the substr function) is one-based. So to address the first character you have to specify it using “1″. MySQL uses 0 to tell “no match”.

Stupid world. Why do we have conventions? To break them? I was aware that MATLAB sucks in this regard too, but I was shocked, when I heard about Lua (both languages one-based).

Note [1]. If you really have such an usecase, please refer to SUBSTRING_INDEX.

The XOR issue

In discrete mathematics, you often define chains of logical operations. XOR has an issue, not everybody is aware of. If it’s used with 3 variables, it loses its most important behaviour: Indicating that only one value is set to True.

A B C A ⊕ B ⊕ C
True True True True
True True False False
True False True False
True False False True
False True True False
False True False True
False False True True
False False False False

In the end, the XOR keeps associative for 3 variables: (A ⊕ B) ⊕ C = A ⊕ (B ⊕ C). For any greater number of variables the behaviour is in no intuitive relation with 2-var-XOR any longer:

A B C D A ⊕ B ⊕ C ⊕ D
True True True True False
True True True False True
True True False True True
True True False False False
True False True True True
True False True False False
True False False True False
True False False False True
False True True True True
False True True False False
False True False True False
False True False False True
False False True True False
False False True False True
False False False True True
False False False False False

If you want to experiment with XOR, I want to refer to my article Truthtable with python and XOR is in python a caret “^”.

Advertisments not for everybody

Advertisement showing source code instead of content

Probably such advertisements are not attractive for everybody :-/

HowTo subscribe to the (non-existent) twitter user RSS feed

I will explain it for Mozilla Thunderbird since this is my current feed reader. Just leave out steps 3, 4, 6 and 7 for any other feed reader.

  1. Get the name of the twitter user.
  2. Get the ID of the twitter user by name.
  3. Open Thunderbird, select the top-level element containing your RSS feeds.
  4. Select “Manage subscriptions” and click “Add”.
  5. The feed URL is http://twitter.com/statuses/user_timeline/<userID>.rss (with the placeholder replaced)
  6. Click “Ok” to subscribe and move the RSS-Feed to any subfolder you would like to place it.
  7. Prefer “View” > “Feed Message Body As” > “Plain Text” and “Summary”

Why “non-existent”? Because Twitter has dropped support for user RSS feeds some time ago. The feeds still exist, but should not be used. However I like RSS and was using this feature for the last 2 years continuously. This decision annoys me :-(

Verilog assign command

I was unhappy the way Verilog’s assign command was described online. Therefore I would like to share my way to explain it. It’s actually damn simple

assign is described as ‘continuous assignment statement’. Therefore, when you assign a register to a wire, it keeps bind forever. Assigning a to b and changing b means changing a. Be aware, that the left term has to be a wire and the right one has to be some sort of value container (eg. reg). So assigning a value to a is not possible.

module assign_test();
  wire [7:0] a;
  reg [15:0] b = 'h3;
 
  // a reads bits 7 to 0 of b
  assign a = b[7:0];
  // modify b 5 time units later
  always b = #5 b + 'hA;
 
  always #10 $display("reg b is %h", b);
  always #10 $display("wire a is %h", a);
  initial #21 $finish();
endmodule

The output is:

reg b is 000d
wire a is 0d
reg b is 0021
wire a is 21

PHP Accessing elements by reference

Input:
$a = array();
$b = $a['a'];
var_dump($a);

Output:
array(0) { }

Input:
$a = array();
$b = &$a['a'];
var_dump($a);

Output:
array(1) { ["a"]=> &NULL }

Accessing elements modifies state. I hate this.

Sagemath plot fail


Sagemath Plot fail

I think mathlibplot is responsible :-(

Happy Easteregg

Milestones in learning a programming language :-) … at least my experience.


Steps in learning a programming language

[SVG-Version]