Movable Type 備忘録

 ダイナミック・パブリッシングで sort_method を使用する

こんばんわ^^

Ogawa::Memoranda さんとこでダイナミック・パブリッシングでも sort_method を使用可能にするパッチが公開されてました。

ダイナミック・パブリッシングを利用してない人にとってはあまり関係ないかな ( ̄∇ ̄)b

sort_method って何!? って人のために・・・。
sort_method は MTTopLevelCategories ブロックタグや MTSubCategories タグなどで使用するモディファイアです。 このモディファイアを使うことで好きな順番でカテゴリを並べ替えることができます。
それには CategorySort.pm というファイルを用意して、そのファイル内でカテゴリの順番を明示してあげる必要があります。

sort_method を使ってみる

  1. CategorySort.pm 作成

    sort_method モディファイアを使う前に以下の手順で CategorySort.pm ファイルを作成します。
    たとえば、わたしのサイトで使ってる CategorySort.pm を以下に記します。

    CategorySort.pm

    1. package CategorySort;
    2. my %category_priorities = (
    3.     'MovableType( ムーバブルタイプ )' => 100,
    4.     'Template( テンプレート )' => 110,
    5.     'Customize( カスタマイズ )' => 120,
    6.     'Tips( 小技 )' => 121,
    7.     'Plugins( プラグイン )' => 122,
    8.     'Install( インストール )' => 130,
    9.     'Troubleshooting( トラブルシューティング )' => 140,
    10.     'Tools( ツール )' => 200,
    11.     'JavaScript( スクリプト )' => 210,
    12.     'jQuery( jQuery )' => 211,
    13.     'prototype( prototype )' => 212,
    14.     'mootools( mootools )' => 213,
    15.     'Rental Server( レンタルサーバ )' => 300,
    16.     'Moblog( 雑記 )' => 400,
    17.     'Topic( トピック )' => 410,
    18.     'Analytics( アクセス解析 )' => 411,
    19.     'Books( 書籍 )' => 500,
    20. );
    21. sub cat_sort ($$) {
    22.     my ($a, $b) = @_;
    23.     my $a_pri = $category_priorities{$a->label} || 9999;
    24.     my $b_pri = $category_priorities{$b->label} || 9999;
    25.     unless ($a_pri == 9999 && $b_pri == 9999) {
    26.         return $a_pri <=> $b_pri;
    27.     }
    28.     return $a->label cmp $b->label;
    29. }

    青字の部分には各自のカテゴリ名称を明記します。
    ここで注意することは誤字脱字です。正しくカテゴリ名称を明示してください。そうしないと期待する動作しなくなります。

    赤字の部分にはカテゴリの表示順( 優先度 )を明示します。 小さい値程優先度が高くなります。
    カテゴリの階層をツリー表示すると以下のようなかんじになります。

     Movable Type MEMO
    |
    100 +-- MovableType( ムーバブルタイプ )
    |     |
    110 |     +-- Template( テンプレート )
    120 |     +-- Customize( カスタマイズ )
    121 |     |    +-- Tips( 小技 )
    122 |     |    +-- Plugins( プラグイン )
    |     |
    130 |     +-- Install( インストール )
    140 |     +-- Troubleshooting( トラブルシューティング )
    |
    200 +-- Tools( ツール )
    |     |
    210 |     +-- JavaScript( スクリプト )
    211 |          +-- jQuery( jQuery )
    212 |          +-- prototype( prototype )
    213 |          +-- mootools( mootools )
    |
    300 +-- Rental Server( レンタルサーバ )
    400 +-- Moblog( 雑記 )
    |     |
    410 |     +-- Topic( トピック )
    420 |     +-- Analytics( アクセス解析 )
    |
    500 +-- Books( 書籍 )
    
  2. CategorySort.pm のアップロード

    作成したら CategorySort.pm をサーバにアップロードします。
    アップロード先は mt/extlib/ フォルダ配下にアップロードしてください。

  3. テンプレートの修正

    前準備が完了したらテンプレートを修正します。
    MTTopLevelCategories ブロックタグや MTSubCategories ブロックタグを使用してるカテゴリ一覧を表示するテンプレートを以下のように変更します。

    カテゴリ一覧

    1. <MTTopLevelCategories sort_method="CategorySort::cat_sort">

    青字の部分を追加します。

以上で、任意の順番にカテゴリを並べ替えることができます。

ダイナミック・パブリッシングで sort_method を使ってみる

で、ここからが本題なのですが、sort_method モディファイアによるカテゴリの並べ替えはダイナミック・パブリッシングでは使用できません。

Perl 版ダイナミック・パブリッシングをご利用の場合には使えます。

ダイナミック・パブリッシングでも sort_method モディファイアを使えるようにするのが Ogawa::Memoranda さんが公開されてるパッチになります ( ̄∇ ̄)b

パッチをあてる( 修正される )ファイルは以下の通りです。

  • mt/php/lib/block.mtsubcategories.php
  • mt/php/lib/function.mtsubcatsrecurse.php
  • mt/php/lib/MTUtil.php

パッチのあて方が分からない人は以下のように修正してください。

mt/php/lib/block.mtsubcategories.php / 59 行目

  1.             $repeat = false;
  2.             return '';
  3.         }
  4. ### [2009.02.25] bzbell 追加
  5.         sort_cats($ctx, $sort_method, $sort_order, $cats);
  6. ### [2009.02.25] bzbell 追加
  7.  
  8.         $ctx->stash('__categories', $cats);
  9.         # Be sure the regular MT tags know we're in a category context

mt/php/lib/function.mtsubcatsrecurse.php / 31 行目

  1.     $cats =& $ctx->mt->db->fetch_categories(array('blog_id' => $ctx->stash('blog_id'), 'category_id' => $cat['category_id'], 'children' => 1, 'show_empty' => 1, 'class' => $class));
  2.  
  3. ### [2009.02.25] bzbell 削除
  4.     #$cats = sort_cats($ctx, $sort_method, $sort_order, $child_cats);
  5. ### [2009.02.25] bzbell 削除
  6.     if (!$cats) {
  7.         return ''; #$ctx->error("No sub categories!");
  8.     }
  9. ### [2009.02.25] bzbell 追加
  10.     sort_cats($ctx, $sort_method, $sort_order, $cats);
  11. ### [2009.02.25] bzbell 追加
  12.  
  13.     $count = 0;
  14.     $res = '';

mt/php/lib/MTUtil.php / 1146 行目

  1.     return null;
  2. }
  3.  
  4. ### [2009.02.25] bzbell 追加
  5. function sort_cats(&$ctx, $sort_method, $sort_order, &$cats) {
  6. if (!is_array($cats)) {
  7. return;
  8. }
  9. if ($sort_method) {
  10. # Convert "FooClass::BarMethod" -> "fooclass_barmethod"
  11. $sort_method = strtolower($sort_method);
  12. $sort_method = str_replace('::', '_', $sort_method);
  13. }
  14. else {
  15. $sort_method = (strtolower($sort_order) == 'descend') ?
  16. 'sort_cats_descend': 'sort_cats_ascend';
  17. }
  18. if (function_exists($sort_method)) {
  19. $ret = usort($cats, $sort_method);
  20. if (!$ret) {
  21. $this_tag = $ctx->this_tag();
  22. if (preg_match('/folder/', $this_tag)) {
  23. return $ctx->error("Error sorting folders");
  24. } else {
  25. return $ctx->error("Error sorting categories");
  26. }
  27. }
  28. }
  29. }
  30. function sort_cats_ascend($a, $b) {
  31. return strcmp($a['category_label'], $b['category_label']);
  32. }
  33. function sort_cats_descend($a, $b) {
  34. return strcmp($b['category_label'], $a['category_label']);
  35. }
  36. ### [2009.02.25] bzbell 追加
  37.  
  38. # sorts by length of tag name, from longest to shortest
  39. function tagarray_name_sort($a, $b) {
  40.     return strcmp($a['tag_name'], $b['tag_name']);

以上です。
以下の場所に Movable Type 4.23 用の上記ファイルを修正したものを置いておきますので参考にしてください。

前準備が必要だけど sort_method モディファイアによるカテゴリ並べ替えは便利なので、ダイナミック・パブリッシングしてる人は試してみるといいのココロ。

 Trackback Pings(0)

No trackbacks found.

 Comments(0)

No comments found.

 Post a Comment

 

コメント用フィード