Repository: getnikola/nikola

Issue:
Wordpress importer broke on REDIRECTION
I was following the manual:

``` bash
$ nikola import_wordpress thenanbanjinnikki.wordpress.2016-01-21.xml
$ cd new_site
$ nikola build
Scanning posts........done!
ERROR: Task generation 'render_site' has duplicated definition of 'redirect:output/nikki/index.html'

$ nikola version
Nikola v7.7.4
$ grep xmlns:excerpt thenanbanjinnikki.wordpress.2016-01-21.xml 
        xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/"
```

The problem seems to be that `conf.py` had a lot (23) of duplicated  keys of the form `["nikki/index.html", $something]` on `REDIRECTIONS =`.  I've manually commented those, and then the build did work.



Discussion:
We need to see your urlmap.csv (should be created by the importer).

Also, how did your post URLs look on WordPress?

## 

Chris Warrick https://chriswarrick.com/
Sent from my Galaxy S3.
On 21 Jan 2016 20:56, "Leonardo Boiko" notifications@github.com wrote:

> I was following the manual:
> 
> $ nikola import_wordpress thenanbanjinnikki.wordpress.2016-01-21.xml
> $ cd new_site
> $ nikola build
> Scanning posts........done!
> ERROR: Task generation 'render_site' has duplicated definition of 'redirect:output/nikki/index.html'
> 
> $ nikola version
> Nikola v7.7.4
> $ grep xmlns:excerpt thenanbanjinnikki.wordpress.2016-01-21.xml
>         xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/"
> 
> The problem seems to be that conf.py had a lot (23) of duplicated keys of
> the form ["nikki/index.html", $something] on REDIRECTIONS =. I've
> manually commented those, and then the build did work.
> 
> —
> Reply to this email directly or view it on GitHub
> https://github.com/getnikola/nikola/issues/2224.

[Here’s `url_map.csv`](https://github.com/getnikola/nikola/files/99888/url_map.csv.txt) (renamed to .txt to make github happy).

The post URLs are title slugs; you can see the original site at http://namakajiri.net/nikki/ . However, I note that most of the duplicated REDIRECTIONS (18 out of 23) point to number-based targets, like this:

``` python
["nikki/index.html",
 "/nikki/posts/191.html"],
```

And they all seem to be post drafts.

We will probably need to ignore URLs with query strings when creating
redirections. I'll look into it tomorrow.

## 

Chris Warrick https://chriswarrick.com/
Sent from my Galaxy S3.
On 21 Jan 2016 22:04, "Leonardo Boiko" notifications@github.com wrote:

> Here’s url_map.csv
> https://github.com/getnikola/nikola/files/99888/url_map.csv.txt
> (renamed to .txt to make github happy).
> 
> The post URLs are title slugs; you can see the original site at
> http://namakajiri.net/nikki/ . However, I note that most of the
> duplicated REDIRECTIONS (17 out of 23) point to number-based targets, like
> this:
> 
> ["nikki/index.html",
>  "/nikki/posts/191.html"],
> 
> And they all seem to be post drafts.
> 
> —
> Reply to this email directly or view it on GitHub
> https://github.com/getnikola/nikola/issues/2224#issuecomment-173707231.

We already handle this for pages that go in the web server root. But if your WordPress blog lives in a subdirectory, you get issues like this. I think I have a fix.


Resolving patch:
diff --git a/nikola/plugins/basic_import.py b/nikola/plugins/basic_import.py
--- a/nikola/plugins/basic_import.py
+++ b/nikola/plugins/basic_import.py
@@ -76,8 +76,11 @@ def get_channel_from_file(cls, filename):
         return channel
 
     @staticmethod
-    def configure_redirections(url_map):
+    def configure_redirections(url_map, base_dir=''):
         """Configure redirections from an url_map."""
+        index = base_dir + 'index.html'
+        if index.startswith('/'):
+            index = index[1:]
         redirections = []
         for k, v in url_map.items():
             if not k[-1] == '/':
@@ -86,11 +89,10 @@ def configure_redirections(url_map):
             # remove the initial "/" because src is a relative file path
             src = (urlparse(k).path + 'index.html')[1:]
             dst = (urlparse(v).path)
-            if src == 'index.html':
+            if src == index:
                 utils.LOGGER.warn("Can't do a redirect for: {0!r}".format(k))
             else:
                 redirections.append((src, dst))
-
         return redirections
 
     def generate_base_site(self):
diff --git a/nikola/plugins/command/import_wordpress.py b/nikola/plugins/command/import_wordpress.py
--- a/nikola/plugins/command/import_wordpress.py
+++ b/nikola/plugins/command/import_wordpress.py
@@ -334,7 +334,7 @@ def show_info_about_mising_module(modulename):
         self.context['TRANSLATIONS'] = format_default_translations_config(
             self.extra_languages)
         self.context['REDIRECTIONS'] = self.configure_redirections(
-            self.url_map)
+            self.url_map, self.base_dir)
         if self.timezone:
             self.context['TIMEZONE'] = self.timezone
         if self.export_categories_as_categories:


Test patch: