# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution    
#    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time
import wizard
import osv
import pooler

section_form = '''<?xml version="1.0"?>
<form string="Create Menu">
    <separator string="Menu Information" colspan="4"/>
    <label string="Want to create a Index on Selected Pages ? "/>
</form>'''

def wiki_do_index(self, cr, uid, data, context):
    ids = data['ids']
    pool = pooler.get_pool(cr.dbname)
    wiki_pool = pool.get('wiki.wiki')
    
    iid = ','.join([str(x) for x in ids])
    sSQL = "Select id, section from wiki_wiki where id in (%s) order by section " % (iid)
    cr.execute(sSQL)
    wiki = cr.fetchall()
    i = 1
    for wk in wiki:
        wiki_pool.write(cr, uid, [wk[0]], {'section':i})
        i+=1
        
    return {}

class make_index(wizard.interface):
    states = {
        'init': {
            'actions': [], 
            'result': {'type':'form', 'arch':section_form, 'fields':{}, 'state':[('end','Cancel'),('yes','Create Index')]}
        },
        'yes': {
            'actions': [wiki_do_index], 
            'result': {
                'type':'state', 
                'state':'end'
            }
        }
    }
make_index('wiki.make.index')


@@ -1553,57 +1553,1065 @@
     
-wiki = cr.fetchall()%0A    i = 1%0A    for wk in wiki
+lst0 = cr.fetchall()%0A    lst = %5B%5D%0A    ids = %7B%7D%0A    for l in lst0:%0A        ids%5Bl%5B1%5D%5D = l%5B0%5D%0A        lst.append(l%5B1%5D)%0A%0A    lst.sort()%0A    val = None%0A    def toint(x):%0A        try:%0A            return int(x)%0A        except:%0A            return 1%0A    %0A    lst = map(lambda x: map(toint, x.split('.')), lst)%0A    %0A    result = %5B%5D%0A    current = %5B'0'%5D%0A    current2 = %5B%5D%0A%0A    for l in lst:%0A        for pos in range(len(l)):%0A            if pos %3E= len(current):%0A                current.append('1')%0A                continue%0A            if (pos == len(l) - 1) or (pos %3E= len(current2)) or (toint(l%5Bpos%5D) %3E toint(current2%5Bpos%5D)):%0A                 current%5Bpos%5D = str(toint(current%5Bpos%5D) + 1)%0A                 current = current%5B:pos + 1%5D%0A            if pos == len(l) - 1:%0A                 break%0A             %0A        key = ('.'.join(%5Bstr(x) for x in l%5D))%0A        id = ids%5Bkey%5D%0A        val = ('.'.join(%5Bstr(x) for x in current%5B:%5D%5D), id)%0A%0A        if val:%0A            result.append(val)%0A        current2 = l%0A    %0A    print 'OLD', lst%0A    print 'NEW', result%0A    for rs in result
 :%0A  
@@ -2646,12 +2646,12 @@
 d, %5B
-wk%5B0
+rs%5B1
 %5D%5D, 
@@ -2665,24 +2665,15 @@
 on':
-i%7D)%0A        i+=1
+rs%5B0%5D%7D)
 %0A   

write code for the reindexing : by fabien