|
PmWiki / CustomPagelistSortOrder
PmWiki can have custom pagelist As an example we have a There are two ways to create a custom pagelist order criteria for the pagelists. Method 1If the custom sort-order desired is (:pagelist order=yearworkauthor:)
The array that maps order= parameters to custom comparison code to be called to perform comparisons is $PageListSortCmp['yearworkauthor'] = 'YearWorkAuthor'; # only a function name (recommended)
or previously $PageListSortCmp['yearworkauthor'] = 'YearWorkAuthor($x, $y)'; # deprecated since PHP 7.2
If you use a function name only, that function will be called with the order as a third argument. The Of course, in this scenario, the pages given by function YearWorkAuthor($x, $y) { ## first, get the Data- versions of the pagenames $datax = 'Data-' . PageVar($x, '$BaseName'); $datay = 'Data-' . PageVar($y, '$BaseName'); ## compare the $:Year values $c = strcmp(PageVar($datax, '$:Year'), PageVar($datay, '$:Year')); if ($c != 0) return $c; ## compare the $:Work values $c = strcmp(PageVar($datax, '$:Work'), PageVar($datay, '$:Work')); if ($c != 0) return $c; ## compare the $:Author values $c = strcmp(PageVar($datax, '$:Author'), PageVar($datay, '$:Author')); return $c; } In the function above, the first two lines figure out the names of the As written there's a slight bit of overhead in the repeated calls
to Method 2To give the wiki user more flexibility, another approach would be to create a generic function DataCompare($x, $y, $order) { $var = "$:" . ucfirst($order); # year -> $:Year ## get the Data- versions of the pagenames $datax = 'Data-' . PageVar($x, '$BaseName'); $datay = 'Data-' . PageVar($y, '$BaseName'); ## perform the requested comparison $c = strcmp(PageVar($datax, $var), PageVar($datay, $var)); return $c; } $PageListSortCmp['year'] = 'DataCompare'; $PageListSortCmp['work'] = 'DataCompare'; $PageListSortCmp['author'] = 'DataCompare'; Note, the following code was previously valid but will raise Deprecated warnings in PHP 7.2. See above how to update it. function DataCompare($x, $y, $var) { ## get the Data- versions of the pagenames $datax = 'Data-' . PageVar($x, '$BaseName'); $datay = 'Data-' . PageVar($y, '$BaseName'); ## perform the requested comparison $c = strcmp(PageVar($datax, $var), PageVar($datay, $var)); return $c; } $PageListSortCmp['year'] = 'DataCompare($x, $y, "$:Year")'; $PageListSortCmp['work'] = 'DataCompare($x, $y, "$:Work")'; $PageListSortCmp['author'] = 'DataCompare($x, $y, "$:Author")'; Then one can do any number of pagelist order=year # sort by $:Year from the Data- pages order=year,work # sort by $:Year, then $:Work order=year,-author # sort by $:Year, reverse by $:Author order=author # sort by $:Author only This is more in keeping with what an author would expect, given that other sort criteria are malleable and nestable by the end user. If you want your users to be able to customize the sort order without requiring custom re-programming in Alternative wayConsidering that page variables are (or should be) the general PmWiki hook for doing custom things with attributes and properties of pages, in whatever form. In fact, here's *another* way to handle the sort/group/display
problem by defining custom page variables that have exactly what
you want, and without needing to define any custom sort features
for Let's define Here are the definitions: $FmtPV['$Year'] = "PageTextVar('Data-'.MakeBaseName(\$pn), 'Year')"; $FmtPV['$Work'] = "PageTextVar('Data-'.MakeBaseName(\$pn), 'Work')"; $FmtPV['$Author'] = "PageTextVar('Data-'.MakeBaseName(\$pn), 'Author')"; Okay, so what does this buy us? Well, the value of So, what we've done is to move all of the issues of relating
pages to (:pagelist group=Group order=$Year,$Work,$Author:) (:pagelist group=Data-Group order=$Year,$Work,$Author:) The specification of order= Note that we also don't have to worry about whether the
pagelist is running through the pages of the This also greatly simplifies the pagelist template, because we can now write: (:if ! equal {<$Year} {=$Year}:) !! {=$Year} (:ifend:) Again, the ' See AlsoThis page may have a more recent version on pmwiki.org: PmWiki:CustomPagelistSortOrder, and a talk page: PmWiki:CustomPagelistSortOrder-Talk. |