SF.net SVN: postfixadmin:[1022] trunk/functions.inc.php
Brought to you by:
christian_boltz,
gingerdog
From: <chr...@us...> - 2011-04-09 22:37:18
|
Revision: 1022 http://postfixadmin.svn.sourceforge.net/postfixadmin/?rev=1022&view=rev Author: christian_boltz Date: 2011-04-09 22:37:11 +0000 (Sat, 09 Apr 2011) Log Message: ----------- functions.inc.php: - new function create_page_browser() Function to create the page browser index ("a-c, d-h, ...") with light speed (at least when compared with the current code that can take several minutes(!) for people with lots of mailboxes or aliases). At the moment, it only works with MySQL, has several big TODO notes (including notices how to implement the PostgreSQL query) and is not yet actively used. Modified Paths: -------------- trunk/functions.inc.php Modified: trunk/functions.inc.php =================================================================== --- trunk/functions.inc.php 2011-04-09 22:09:37 UTC (rev 1021) +++ trunk/functions.inc.php 2011-04-09 22:37:11 UTC (rev 1022) @@ -566,7 +566,57 @@ } +/** + * create_page_browser + * Action: Get page browser for a long list of mailboxes, aliases etc. + * Call: $pagebrowser = create_page_browser('table.field', 'query', 50) # replaces $param = $_GET['param'] + * + * @param String idxfield - database field name to use as title + * @param String query - core part of the query (starting at "FROM") + * @param Int item_count - number of total items (this function is lazy and doesn't want to count itsself ;-) + * @return String + */ +function create_page_browser($idxfield, $query, $item_count) { + global $CONF; + $page_size = $CONF['page_size']; + $label_len = 2; + $pagebrowser = array(); +# TODO: item_count is undefined on search results +# if ( $item_count <= $page_size ) return array(); # very short list - no pagebrowser needed + + $initcount = "SET @row=-1"; + $result = db_query($initcount); + + $last_in_page = $page_size - 1; + $query = "SELECT * FROM (SELECT $idxfield AS label, @row := @row + 1 AS row $query ) idx WHERE MOD(idx.row, $page_size) IN (0,$last_in_page)"; + +# TODO: $query is MySQL-specific + +# PostgreSQL: +# http://www.postgresql.org/docs/8.1/static/sql-createsequence.html +# http://www.postgresonline.com/journal/archives/79-Simulating-Row-Number-in-PostgreSQL-Pre-8.4.html +# http://www.pg-forum.de/sql/1518-nummerierung-der-abfrageergebnisse.html +# CREATE TEMPORARY SEQUENCE foo MINVALUE 0 MAXVALUE $page_size CYCLE +# afterwards: DROP SEQUENCE foo + + $result = db_query ($query); + if ($result['rows'] > 0) { + while ($row = db_array ($result['result'])) { + if ($row2 = db_array ($result['result'])) { + $label = substr($row['label'],0,$label_len) . '-' . substr($row2['label'],0,$label_len); + $pagebrowser[] = $label; + } else { + $label = substr($row['label'],0,$label_len) . '-' . 'ZZ'; + # TODO: separate query for the last row - or include it in the main query (... OR row = $item_count) + $pagebrowser[] = $label; + } + } + } + + return $pagebrowser; +} + // // get_mailbox_properties // Action: Get all the properties of a mailbox. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |