|
PmWiki /
CustomPagelistSortOrderPmWiki can have custom pagelist order= values pre-set by the wiki admin in config.php. First, we have to tell PmWiki which function to call in response to the custom order= parameter. As an example we have a Data- page that contains page text variables storing data about books. The Data-Group.PageName page contains the colon-delimited values $:Year (year of publication), $:Work (title of the book), and $:WAuthor (author of the book). In some cases the sort-order data for Group.PageName needs to come from these corresponding Data- pages. There are two ways to create a custom pagelist order criteria for the pagelists. Method 1If the custom sort-order desired is $:Year,$:Work,$:WAuthor, let's use 'yearworkwauthor' as the custom function order parameter, in which case the pagelist criteria would be:
The array that maps order= parameters to custom comparison code to be called to perform comparisons is $PageListSortCmp:
$PageListSortComp is an array of page list functions. Each function listed (after the =) expects two parameters -- each contains the pagenames for a page to be sorted; only two pages are compared at a time. Thus, this says that to perform a comparison between two pages in the pagelist (given by $x and $y), call the function YearWorkWAuthor() and pass those pagenames as arguments. The YearWorkWAuthor() function should return a value that is less than zero if $x should be before $y in the list, greater than zero if $x should come after $y, and zero if they're "equivalent" for the purposes of this comparison. Of course, in this scenario, the pages given by $x and $y don't contain the values we want to sort by -- those values are in the corresponding Data-* pages for $x and $y -- otherwise we wouldn't need to customize the sort order. Thus, we figure out the names of the "Data-" pages, and then test the page text variables from those pages:
In the function above, the first two lines figure out the names of the Data-* pages corresponding to $x and $y, and store them in $datax and $datay. The next two lines grab the $:Year page text variables for $datax and $datay, and return a negative or positive value if they're different. "strcmp" is a built-in PHP function aka "string compare" and it returns a numeric value that represents how different two pieces of data (text) are. If they're the same (i.e., $c == 0), we fall through to test the $:Work page text variables, by similar logic, and if those are also the same we test the $:WAuthor page text variables and return that. As written there's a slight bit of overhead in the repeated calls to PageVar() that we can avoid if speed becomes an issue, but the above code illustrates the basic concept behind the custom sort. Method 2To give the wiki user more flexibility, another approach would be to create a generic DataCompare() function for comparing page text variables in Data-* pages, and then define separate "year", "work", and "wauthor" options for the order= parameter that pass an appropriate argument to DataCompare():
Then one can do any number of pagelist order= combinations, such as:
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 config.php when new needs arise, this is probably the better model. 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 $Year, $Work, and $WAuthor page variables for
every page, such that the values of $Year, $Work, and $WAuthor for any
page Group.XYZ are always the $:Year, $:Work, and $:WAuthor page
text variable from Group.XYZ's corresponding Data-* page. In
other words, Here are the definitions:
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 Data- pages out of the pagelist template and into some relatively simple page variable definitions. With this in place, our pagelist directives then look like:
The specification of order=$Year,$Work,$WAuthor (page variables) means that we will sort the list of pages based on the $:Year, $:Work, and $:WAuthor page text variables of the corresponding pages in the Data-Group group. Note that we also don't have to worry about whether the pagelist is running through the pages of the Simile or Data-Simile groups, because our custom page variables always map the pagename into the Data- form of the group. This also greatly simplifies the pagelist template, because we can now write:
Again, the '$Year' page variable is taking care of the
details of obtaining $:Year from the correct See AlsoThis page may have a more recent version on pmwiki.org: PmWiki:CustomPagelistSortOrder, and a talk page: PmWiki:CustomPagelistSortOrder-Talk. |