Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permalink broken when both 'folder' and 'basename' are set in API Page creation #150

Open
Kagetsuki opened this issue Feb 22, 2018 · 7 comments

Comments

@Kagetsuki
Copy link

Kagetsuki commented Feb 22, 2018

There's some sort of conflicting condition that occurs when creating a page through the API. For example, say we have a folder with ID 1 with a path a/b/ and we want to create "index.html" in this folder through the API. The following patterns will produce different results:

folder basename resulting folder resulting permalink expected result
a/b index a/b a/b/post-0.html a/b/index.html
a/b a/b a/b/post-0.html a/b/post-0.html
index index.html index.html
a/b a/b/index a/b a/b/a/b/index.html a/b/index.html
a/b/index a/b/index.html a/b/index.html

Ideally the first pattern should work, but it does not. The only pattern that works is if you do not set the folder and set the base name as the full path without the .html extension, but then you can't sort the file into a folder in the admin screen.

NOTE
I'm putting almost 3000 pages into a fresh instance (with a license!), so not using the API is not an option, and the site is going to be intensely difficult to manage without folders.

@Kagetsuki Kagetsuki changed the title Permalink broken when both 'folder' and 'base' are set Permalink broken when both 'folder' and 'basename' are set in API Page creation Feb 22, 2018
@yuji
Copy link
Contributor

yuji commented Feb 25, 2018

@Kagetsuki

Hello,

Do you mean API as Data API? If yes, please tell me Movable Type Version.

@Kagetsuki
Copy link
Author

Yup, the Data API. I'm testing on 6.3.6. I have a feeling the internal code works something like this:

  1. if folder is set, manage file under folder (automatically generate name from title, if no title or title contains no ASCII characters set title to "post-##") ⇒ set file path and name based on folder and title (basename is not used? and automatically generated)
  2. if basename is set, check to make sure the base name does not conflict with folder path (path portion of base name matches folder path) ⇒ if base name conflicts with folder path ignore base name / set it to null
  3. permalink is generated as folder path + base name

The logic here probably made sense, because if you had folder path a/b and base name c/d/e.html, folder sorting wouldn't work correctly. The issue is there's no other attribute but base name to set the file name, and in the case of using the API you'd need to set a base name as "index" to get the apex of a folder or you'd need to set manually to some name to fit with links in other pages you're posting.

Ideally if MT would extract folders automatically from the base name, and create folders if they didn't already exist, it would not only solve this issue but would also eliminate the need to pass folder arguments (which require folder ID, meaning you need to get folders from API first and then extrapolate the ID before you can even post a page!).

Thanks for looking into this - please tell me if you need any more info.

@Kagetsuki
Copy link
Author

I hate to bump but we're looking to finish a site in the next 10 days and without this working it's going to be a bit of a mess. If there's some alternate solution I'd love to hear about it - otherwise do you think we can expect a patch at some point?

@dphillips
Copy link
Contributor

I don't work with the Data API, but I did look at the Data API code and see if anything jumped out at me. I think I discovered a bug in the basename uniqueness checking when a new page is created through the Data API.

In theory, you should be able to create multiple pages with the same basename (file name) in the same site/blog as long as each page is in a different folder. In practice, the Data API checks the entire site/blog for unique basenames without looking at any folder assignment.

From lib/MT/DataAPI/Endpoint/v2/Page.pm, lines 89-94 (MT 6.3.6):

if (  !$new_page->basename
    || $app->model('page')
    ->exist( { blog_id => $site->id, basename => $new_page->basename } ) )
{
    $new_page->basename( MT::Util::make_unique_basename($new_page) );
}

Once a page has been created with a basename of 'index' for example, the above code does not allow for any other pages to have the same 'index' basename, even if the page is in a different folder. In this case, the Data API generates a unique basename ("post-##") instead of allowing "index".

When creating new pages in Movable Type, the MT code loads all pages with the same basename as the new page being created, constructs the permalinks to each page, and compares the existing page permalinks to the new page permalink to determine whether a duplicate basename has been used. A similar logic should be implemented in the Data API for uniqueness checking of basenames of new pages.

I don't see any workarounds for this issue in the Data API. The Data API code will need to be updated to address this issue.

@Kagetsuki - Does this seem to explain the behavior you are encountering with the Data API?

@Kagetsuki
Copy link
Author

Once a page has been created with a basename of 'index' for example, the above code does not allow for any other pages to have the same 'index' basename, even if the page is in a different folder. In this case, the Data API generates a unique basename ("post-##") instead of allowing "index"

I'd have to double check but that sounds dead on! Specifically line 4 of the table I made in the first post would seem to confirm this:

folder basename resulting folder resulting permalink expected result
a/b a/b/index a/b a/b/a/b/index.html a/b/index.html

But, as I noted, permalink generation is folder + basename, so the "resulting permalink" above has the path twice, and sending the permalink as a parameter to the API just gets ignored.

Definitely hoping for a quick fix on the API. The site we're doing is a monster and importing all the pages through the admin panel by hand would probably take months... with the API we have it down to about 10 minutes!

@Kagetsuki
Copy link
Author

何度も投稿して大変申し訳ありませんが、ライセンス費を払っているお客様に綺麗に整理されているサイトの納品がしたいのです。 @dphillips 様が述べているようにそこまで複雑な問題ではなさそうなので、早めに対応して貰えれば大変ありがたいです。
よろしくお願い致します。

@yuji
Copy link
Contributor

yuji commented Mar 6, 2018

@Kagetsuki
大変ご迷惑をお掛けしております。できる限り、早急の対応を検討します。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants