/** * Handle sortby requests for the DB iterator and table header links. * Prefix the column with + or - like "+pagename","-mtime", ... * supported actions: 'flip_order' "mtime" => "+mtime" => "-mtime" ... * 'db' "-pagename" => "pagename DESC" * In PageList all columns are sortable. (patch by DanFr) * Here with the backend only some, the rest is delayed to PageList. * (some kind of DumbIter) * Duplicate the PageList function here to avoid loading the whole * PageList.php, and it forces the backend specific sortable_columns() */ function sortby($column, $action, $sortable_columns = false) { if (empty($column)) { return ''; } //support multiple comma-delimited sortby args: "+hits,+pagename" if (strstr($column, ',')) { $result = array(); foreach (explode(',', $column) as $col) { if (empty($this)) { $result[] = WikiDB_backend::sortby($col, $action); } else { $result[] = $this->sortby($col, $action); } } return join(",", $result); } if (substr($column, 0, 1) == '+') { $order = '+'; $column = substr($column, 1); } elseif (substr($column, 0, 1) == '-') { $order = '-'; $column = substr($column, 1); } // default order: +pagename, -mtime, -hits if (empty($order)) { if (in_array($column, array('mtime', 'hits'))) { $order = '-'; } else { $order = '+'; } } if ($action == 'flip_order') { return ($order == '+' ? '-' : '+') . $column; } elseif ($action == 'init') { $this->_sortby[$column] = $order; return $order . $column; } elseif ($action == 'check') { return !empty($this->_sortby[$column]) or $GLOBALS['request']->getArg('sortby') and strstr($GLOBALS['request']->getArg('sortby'), $column); } elseif ($action == 'db') { // native sort possible? if (!empty($this) and !$sortable_columns) { $sortable_columns = $this->sortable_columns(); } if (in_array($column, $sortable_columns)) { // asc or desc: +pagename, -pagename return $column . ($order == '+' ? ' ASC' : ' DESC'); } else { return ''; } } return ''; }
function WikiDB_backend_dbaBase_pageiter(&$backend, &$pages, $options = false) { $this->_backend = $backend; $this->_options = $options; if ($pages) { if (!empty($options['sortby'])) { $sortby = WikiDB_backend::sortby($options['sortby'], 'db', array('pagename', 'mtime')); // check for which column to sortby if ($sortby and !strstr($sortby, "hits ")) { usort($pages, 'WikiDB_backend_dbaBase_sortby_' . str_replace(' ', '_', $sortby)); } } if (!empty($options['limit'])) { list($offset, $limit) = WikiDB_backend::limit($options['limit']); $pages = array_slice($pages, $offset, $limit); } $this->_pages = $pages; } else { $this->_pages = array(); } }