001/*
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.
018 */
019package org.apache.wiki.providers;
020
021import org.apache.log4j.Logger;
022import org.apache.wiki.InternalWikiException;
023import org.apache.wiki.api.core.Engine;
024import org.apache.wiki.api.core.Page;
025import org.apache.wiki.api.exceptions.NoRequiredPropertyException;
026import org.apache.wiki.api.exceptions.ProviderException;
027import org.apache.wiki.api.providers.PageProvider;
028import org.apache.wiki.api.providers.WikiProvider;
029import org.apache.wiki.api.spi.Wiki;
030import org.apache.wiki.util.FileUtil;
031
032import java.io.BufferedInputStream;
033import java.io.BufferedOutputStream;
034import java.io.File;
035import java.io.FileInputStream;
036import java.io.FileOutputStream;
037import java.io.IOException;
038import java.io.InputStream;
039import java.io.OutputStream;
040import java.util.ArrayList;
041import java.util.Collection;
042import java.util.Date;
043import java.util.List;
044import java.util.Properties;
045
046/**
047 *  Provides a simple directory based repository for Wiki pages.
048 *  Pages are held in a directory structure:
049 *  <PRE>
050 *    Main.txt
051 *    Foobar.txt
052 *    OLD/
053 *       Main/
054 *          1.txt
055 *          2.txt
056 *          page.properties
057 *       Foobar/
058 *          page.properties
059 *  </PRE>
060 *
061 *  In this case, "Main" has three versions, and "Foobar" just one version.
062 *  <P>
063 *  The properties file contains the necessary metainformation (such as author)
064 *  information of the page.  DO NOT MESS WITH IT!
065 *
066 *  <P>
067 *  All files have ".txt" appended to make life easier for those
068 *  who insist on using Windows or other software which makes assumptions
069 *  on the files contents based on its name.
070 *
071 */
072public class VersioningFileProvider extends AbstractFileProvider {
073
074    private static final Logger     log = Logger.getLogger(VersioningFileProvider.class);
075
076    /** Name of the directory where the old versions are stored. */
077    public static final String      PAGEDIR      = "OLD";
078
079    /** Name of the property file which stores the metadata. */
080    public static final String      PROPERTYFILE = "page.properties";
081
082    private CachedProperties        m_cachedProperties;
083
084    /**
085     *  {@inheritDoc}
086     */
087    @Override
088    public void initialize( final Engine engine, final Properties properties ) throws NoRequiredPropertyException, IOException {
089        super.initialize( engine, properties );
090        // some additional sanity checks :
091        final File oldpages = new File( getPageDirectory(), PAGEDIR );
092        if( !oldpages.exists() ) {
093            if( !oldpages.mkdirs() ) {
094                throw new IOException( "Failed to create page version directory " + oldpages.getAbsolutePath() );
095            }
096        } else {
097            if( !oldpages.isDirectory() ) {
098                throw new IOException( "Page version directory is not a directory: " + oldpages.getAbsolutePath() );
099            }
100            if( !oldpages.canWrite() ) {
101                throw new IOException( "Page version directory is not writable: " + oldpages.getAbsolutePath() );
102            }
103        }
104        log.info( "Using directory " + oldpages.getAbsolutePath() + " for storing old versions of pages" );
105    }
106
107    /**
108     *  Returns the directory where the old versions of the pages
109     *  are being kept.
110     */
111    private File findOldPageDir( final String page ) {
112        if( page == null ) {
113            throw new InternalWikiException( "Page may NOT be null in the provider!" );
114        }
115        final File oldpages = new File( getPageDirectory(), PAGEDIR );
116        return new File( oldpages, mangleName( page ) );
117    }
118
119    /**
120     *  Goes through the repository and decides which version is the newest one in that directory.
121     *
122     *  @return Latest version number in the repository, or -1, if there is no page in the repository.
123     */
124
125    // FIXME: This is relatively slow.
126    /*
127    private int findLatestVersion( String page )
128    {
129        File pageDir = findOldPageDir( page );
130
131        String[] pages = pageDir.list( new WikiFileFilter() );
132
133        if( pages == null )
134        {
135            return -1; // No such thing found.
136        }
137
138        int version = -1;
139
140        for( int i = 0; i < pages.length; i++ )
141        {
142            int cutpoint = pages[i].indexOf( '.' );
143            if( cutpoint > 0 )
144            {
145                String pageNum = pages[i].substring( 0, cutpoint );
146
147                try
148                {
149                    int res = Integer.parseInt( pageNum );
150
151                    if( res > version )
152                    {
153                        version = res;
154                    }
155                }
156                catch( NumberFormatException e ) {} // It's okay to skip these.
157            }
158        }
159
160        return version;
161    }
162*/
163    private int findLatestVersion( final String page ) {
164        int version = -1;
165
166        try {
167            final Properties props = getPageProperties( page );
168
169            for( final Object o : props.keySet() ) {
170                final String key = ( String )o;
171                if( key.endsWith( ".author" ) ) {
172                    final int cutpoint = key.indexOf( '.' );
173                    if( cutpoint > 0 ) {
174                        final String pageNum = key.substring( 0, cutpoint );
175
176                        try {
177                            final int res = Integer.parseInt( pageNum );
178
179                            if( res > version ) {
180                                version = res;
181                            }
182                        } catch( final NumberFormatException e ) {
183                        } // It's okay to skip these.
184                    }
185                }
186            }
187        } catch( final IOException e ) {
188            log.error( "Unable to figure out latest version - dying...", e );
189        }
190
191        return version;
192    }
193
194    /**
195     *  Reads page properties from the file system.
196     */
197    private Properties getPageProperties( final String page ) throws IOException {
198        final File propertyFile = new File( findOldPageDir(page), PROPERTYFILE );
199        if( propertyFile.exists() ) {
200            final long lastModified = propertyFile.lastModified();
201
202            //
203            //   The profiler showed that when calling the history of a page the propertyfile
204            //   was read just as much times as there were versions of that file. The loading
205            //   of a propertyfile is a cpu-intensive job. So now hold on to the last propertyfile
206            //   read because the next method will with a high probability ask for the same propertyfile.
207            //   The time it took to show a historypage with 267 versions dropped with 300%.
208            //
209
210            CachedProperties cp = m_cachedProperties;
211
212            if( cp != null && cp.m_page.equals( page ) && cp.m_lastModified == lastModified ) {
213                return cp.m_props;
214            }
215
216            try( final InputStream in = new BufferedInputStream(new FileInputStream( propertyFile ) ) ) {
217                final Properties props = new Properties();
218                props.load( in );
219                cp = new CachedProperties( page, props, lastModified );
220                m_cachedProperties = cp; // Atomic
221
222                return props;
223            }
224        }
225
226        return new Properties(); // Returns an empty object
227    }
228
229    /**
230     *  Writes the page properties back to the file system.
231     *  Note that it WILL overwrite any previous properties.
232     */
233    private void putPageProperties( final String page, final Properties properties ) throws IOException {
234        final File propertyFile = new File( findOldPageDir(page), PROPERTYFILE );
235        try( final OutputStream out = new FileOutputStream( propertyFile ) ) {
236            properties.store( out, " JSPWiki page properties for "+page+". DO NOT MODIFY!" );
237        }
238
239        // The profiler showed the probability was very high that when  calling for the history of
240        // a page the propertyfile would be read as much times as there were versions of that file.
241        // It is statistically likely the propertyfile will be examined many times before it is updated.
242        final CachedProperties cp = new CachedProperties( page, properties, propertyFile.lastModified() );
243        m_cachedProperties = cp; // Atomic
244    }
245
246    /**
247     *  Figures out the real version number of the page and also checks for its existence.
248     *
249     *  @throws NoSuchVersionException if there is no such version.
250     */
251    private int realVersion( final String page, final int requestedVersion ) throws NoSuchVersionException {
252        //  Quickly check for the most common case.
253        if( requestedVersion == WikiProvider.LATEST_VERSION ) {
254            return -1;
255        }
256
257        final int latest = findLatestVersion(page);
258
259        if( requestedVersion == latest || (requestedVersion == 1 && latest == -1 ) ) {
260            return -1;
261        } else if( requestedVersion <= 0 || requestedVersion > latest ) {
262            throw new NoSuchVersionException( "Requested version " + requestedVersion + ", but latest is " + latest );
263        }
264
265        return requestedVersion;
266    }
267
268    /**
269     *  {@inheritDoc}
270     */
271    @Override
272    public synchronized String getPageText( final String page, int version ) throws ProviderException {
273        final File dir = findOldPageDir( page );
274
275        version = realVersion( page, version );
276        if( version == -1 ) {
277            // We can let the FileSystemProvider take care of these requests.
278            return super.getPageText( page, PageProvider.LATEST_VERSION );
279        }
280
281        final File pageFile = new File( dir, ""+version+FILE_EXT );
282        if( !pageFile.exists() ) {
283            throw new NoSuchVersionException("Version "+version+"does not exist.");
284        }
285
286        return readFile( pageFile );
287    }
288
289
290    // FIXME: Should this really be here?
291    private String readFile( final File pagedata ) throws ProviderException {
292        String result = null;
293        if( pagedata.exists() ) {
294            if( pagedata.canRead() ) {
295                try( final InputStream in = new FileInputStream( pagedata ) ) {
296                    result = FileUtil.readContents( in, m_encoding );
297                } catch( final IOException e ) {
298                    log.error("Failed to read", e);
299                    throw new ProviderException("I/O error: "+e.getMessage());
300                }
301            } else {
302                log.warn("Failed to read page from '"+pagedata.getAbsolutePath()+"', possibly a permissions problem");
303                throw new ProviderException("I cannot read the requested page.");
304            }
305        } else {
306            // This is okay.
307            // FIXME: is it?
308            log.info("New page");
309        }
310
311        return result;
312    }
313
314    // FIXME: This method has no rollback whatsoever.
315
316    /*
317      This is how the page directory should look like:
318
319         version    pagedir       olddir
320          none       empty         empty
321           1         Main.txt (1)  empty
322           2         Main.txt (2)  1.txt
323           3         Main.txt (3)  1.txt, 2.txt
324    */
325    /**
326     *  {@inheritDoc}
327     */
328    @Override
329    public synchronized void putPageText( final Page page, final String text ) throws ProviderException {
330        // This is a bit complicated.  We'll first need to copy the old file to be the newest file.
331        final int  latest  = findLatestVersion( page.getName() );
332        final File pageDir = findOldPageDir( page.getName() );
333        if( !pageDir.exists() ) {
334            pageDir.mkdirs();
335        }
336
337        try {
338            // Copy old data to safety, if one exists.
339            final File oldFile = findPage( page.getName() );
340
341            // Figure out which version should the old page be? Numbers should always start at 1.
342            // "most recent" = -1 ==> 1
343            // "first"       = 1  ==> 2
344            int versionNumber = (latest > 0) ? latest : 1;
345            final boolean firstUpdate = (versionNumber == 1);
346
347            if( oldFile != null && oldFile.exists() ) {
348                final File pageFile = new File( pageDir, versionNumber + FILE_EXT );
349                try( final InputStream in = new BufferedInputStream( new FileInputStream( oldFile ) );
350                     final OutputStream out = new BufferedOutputStream( new FileOutputStream( pageFile ) ) ) {
351                    FileUtil.copyContents( in, out );
352
353                    // We need also to set the date, since we rely on this.
354                    pageFile.setLastModified( oldFile.lastModified() );
355
356                    // Kludge to make the property code to work properly.
357                    versionNumber++;
358                }
359            }
360
361            //  Let superclass handler writing data to a new version.
362            super.putPageText( page, text );
363
364            //  Finally, write page version data.
365            // FIXME: No rollback available.
366            final Properties props = getPageProperties( page.getName() );
367
368            String authorFirst = null;
369            // if the following file exists, we are NOT migrating from FileSystemProvider
370            final File pagePropFile = new File(getPageDirectory() + File.separator + PAGEDIR + File.separator + mangleName(page.getName()) + File.separator + "page" + FileSystemProvider.PROP_EXT);
371            if( firstUpdate && ! pagePropFile.exists() ) {
372                // we might not yet have a versioned author because the old page was last maintained by FileSystemProvider
373                final Properties props2 = getHeritagePageProperties( page.getName() );
374
375                // remember the simulated original author (or something) in the new properties
376                authorFirst = props2.getProperty( "1.author", "unknown" );
377                props.setProperty( "1.author", authorFirst );
378            }
379
380            String newAuthor = page.getAuthor();
381            if ( newAuthor == null ) {
382                newAuthor = ( authorFirst != null ) ? authorFirst : "unknown";
383            }
384            page.setAuthor(newAuthor);
385            props.setProperty( versionNumber + ".author", newAuthor );
386
387            final String changeNote = page.getAttribute( Page.CHANGENOTE );
388            if( changeNote != null ) {
389                props.setProperty( versionNumber + ".changenote", changeNote );
390            }
391
392            // Get additional custom properties from page and add to props
393            getCustomProperties( page, props );
394            putPageProperties( page.getName(), props );
395        } catch( final IOException e ) {
396            log.error( "Saving failed", e );
397            throw new ProviderException("Could not save page text: "+e.getMessage());
398        }
399    }
400
401    /**
402     *  {@inheritDoc}
403     */
404    @Override
405    public Page getPageInfo( final String page, final int version ) throws ProviderException {
406        final int latest = findLatestVersion( page );
407        final int realVersion;
408
409        Page p = null;
410
411        if( version == PageProvider.LATEST_VERSION || version == latest || (version == 1 && latest == -1) ) {
412            //
413            // Yes, we need to talk to the top level directory to get this version.
414            //
415            // I am listening to Press Play On Tape's guitar version of the good old C64 "Wizardry" -tune at this moment.
416            // Oh, the memories...
417            //
418            realVersion = (latest >= 0) ? latest : 1;
419
420            p = super.getPageInfo( page, PageProvider.LATEST_VERSION );
421
422            if( p != null ) {
423                p.setVersion( realVersion );
424            }
425        } else {
426            // The file is not the most recent, so we'll need to find it from the deep trenches of the "OLD" directory structure.
427            realVersion = version;
428            final File dir = findOldPageDir( page );
429            if( !dir.exists() || !dir.isDirectory() ) {
430                return null;
431            }
432
433            final File file = new File( dir, version + FILE_EXT );
434            if( file.exists() ) {
435                p = Wiki.contents().page( m_engine, page );
436
437                p.setLastModified( new Date( file.lastModified() ) );
438                p.setVersion( version );
439            }
440        }
441
442        //  Get author and other metadata information (Modification date has already been set.)
443        if( p != null ) {
444            try {
445                final Properties props = getPageProperties( page );
446                String author = props.getProperty( realVersion + ".author" );
447                if( author == null ) {
448                    // we might not have a versioned author because the old page was last maintained by FileSystemProvider
449                    final Properties props2 = getHeritagePageProperties( page );
450                    author = props2.getProperty( Page.AUTHOR );
451                }
452                if( author != null ) {
453                    p.setAuthor( author );
454                }
455
456                final String changenote = props.getProperty( realVersion + ".changenote" );
457                if( changenote != null ) {
458                    p.setAttribute( Page.CHANGENOTE, changenote );
459                }
460
461                // Set the props values to the page attributes
462                setCustomProperties( p, props );
463            } catch( final IOException e ) {
464                log.error( "Cannot get author for page" + page + ": ", e );
465            }
466        }
467
468        return p;
469    }
470
471    /**
472     *  {@inheritDoc}
473     */
474    @Override
475    public boolean pageExists( final String pageName, final int version ) {
476        if (version == PageProvider.LATEST_VERSION || version == findLatestVersion( pageName ) ) {
477            return pageExists(pageName);
478        }
479
480        final File dir = findOldPageDir( pageName );
481        if( !dir.exists() || !dir.isDirectory() ) {
482            return false;
483        }
484
485        return new File( dir, version + FILE_EXT ).exists();
486    }
487
488    /**
489     *  {@inheritDoc}
490     */
491     // FIXME: Does not get user information.
492    @Override
493    public List< Page > getVersionHistory( final String page ) throws ProviderException {
494        final ArrayList< Page > list = new ArrayList<>();
495        final int latest = findLatestVersion( page );
496        for( int i = latest; i > 0; i-- ) {
497            final Page info = getPageInfo( page, i );
498            if( info != null ) {
499                list.add( info );
500            }
501        }
502
503        return list;
504    }
505
506    /*
507     * Support for migration of simple properties created by the FileSystemProvider when coming under Versioning management.
508     * Simulate an initial version.
509     */
510    private Properties getHeritagePageProperties( final String page ) throws IOException {
511        final File propertyFile = new File( getPageDirectory(), mangleName( page ) + FileSystemProvider.PROP_EXT );
512        if ( propertyFile.exists() ) {
513            final long lastModified = propertyFile.lastModified();
514
515            CachedProperties cp = m_cachedProperties;
516            if ( cp != null && cp.m_page.equals(page) && cp.m_lastModified == lastModified ) {
517                return cp.m_props;
518            }
519
520            try( final InputStream in = new BufferedInputStream( new FileInputStream( propertyFile ) ) ) {
521                final Properties props = new Properties();
522                props.load( in );
523
524                final String originalAuthor = props.getProperty( Page.AUTHOR );
525                if ( originalAuthor.length() > 0 ) {
526                    // simulate original author as if already versioned but put non-versioned property in special cache too
527                    props.setProperty( "1.author", originalAuthor );
528
529                    // The profiler showed the probability was very high that when calling for the history of a page the
530                    // propertyfile would be read as much times as there were versions of that file. It is statistically
531                    // likely the propertyfile will be examined many times before it is updated.
532                    cp = new CachedProperties( page, props, propertyFile.lastModified() );
533                    m_cachedProperties = cp; // Atomic
534                }
535
536                return props;
537            }
538        }
539
540        return new Properties(); // Returns an empty object
541    }
542
543    /**
544     *  Removes the relevant page directory under "OLD" -directory as well, but does not remove any extra subdirectories from it.
545     *  It will only touch those files that it thinks to be WikiPages.
546     *
547     *  @param page {@inheritDoc}
548     *  @throws {@inheritDoc}
549     */
550    // FIXME: Should log errors.
551    @Override
552    public void deletePage( final String page ) throws ProviderException {
553        super.deletePage( page );
554        final File dir = findOldPageDir( page );
555        if( dir.exists() && dir.isDirectory() ) {
556            final File[] files = dir.listFiles( new WikiFileFilter() );
557            for( int i = 0; i < files.length; i++ ) {
558                files[ i ].delete();
559            }
560
561            final File propfile = new File( dir, PROPERTYFILE );
562            if( propfile.exists() ) {
563                propfile.delete();
564            }
565
566            dir.delete();
567        }
568    }
569
570    /**
571     *  {@inheritDoc}
572     *
573     *  Deleting versions has never really worked, JSPWiki assumes that version histories are "not gappy". Using deleteVersion() is
574     *  definitely not recommended.
575     */
576    @Override
577    public void deleteVersion( final String page, final int version ) throws ProviderException {
578        final File dir = findOldPageDir( page );
579        int latest = findLatestVersion( page );
580        if( version == PageProvider.LATEST_VERSION ||
581            version == latest ||
582            (version == 1 && latest == -1) ) {
583            //  Delete the properties
584            try {
585                final Properties props = getPageProperties( page );
586                props.remove( ((latest > 0) ? latest : 1)+".author" );
587                putPageProperties( page, props );
588            } catch( final IOException e ) {
589                log.error("Unable to modify page properties",e);
590                throw new ProviderException("Could not modify page properties: " + e.getMessage());
591            }
592
593            // We can let the FileSystemProvider take care of the actual deletion
594            super.deleteVersion( page, PageProvider.LATEST_VERSION );
595
596            //  Copy the old file to the new location
597            latest = findLatestVersion( page );
598
599            final File pageDir = findOldPageDir( page );
600            final File previousFile = new File( pageDir, latest + FILE_EXT );
601            final File pageFile = findPage(page);
602            try( final InputStream in = new BufferedInputStream( new FileInputStream( previousFile ) );
603                 final OutputStream out = new BufferedOutputStream( new FileOutputStream( pageFile ) ) ) {
604                if( previousFile.exists() ) {
605                    FileUtil.copyContents( in, out );
606                    // We need also to set the date, since we rely on this.
607                    pageFile.setLastModified( previousFile.lastModified() );
608                }
609            } catch( final IOException e ) {
610                log.fatal("Something wrong with the page directory - you may have just lost data!",e);
611            }
612
613            return;
614        }
615
616        final File pageFile = new File( dir, ""+version+FILE_EXT );
617        if( pageFile.exists() ) {
618            if( !pageFile.delete() ) {
619                log.error("Unable to delete page." + pageFile.getPath() );
620            }
621        } else {
622            throw new NoSuchVersionException("Page "+page+", version="+version);
623        }
624    }
625
626    /**
627     *  {@inheritDoc}
628     */
629    // FIXME: This is kinda slow, we should need to do this only once.
630    @Override
631    public Collection< Page > getAllPages() throws ProviderException {
632        final Collection< Page > pages = super.getAllPages();
633        final Collection< Page > returnedPages = new ArrayList<>();
634        for( final Page page : pages ) {
635            final Page info = getPageInfo( page.getName(), WikiProvider.LATEST_VERSION );
636            returnedPages.add( info );
637        }
638
639        return returnedPages;
640    }
641
642    /**
643     *  {@inheritDoc}
644     */
645    @Override
646    public String getProviderInfo()
647    {
648        return "";
649    }
650
651    /**
652     *  {@inheritDoc}
653     */
654    @Override
655    public void movePage( final String from, final String to ) {
656        // Move the file itself
657        final File fromFile = findPage( from );
658        final File toFile = findPage( to );
659        fromFile.renameTo( toFile );
660
661        // Move any old versions
662        final File fromOldDir = findOldPageDir( from );
663        final File toOldDir = findOldPageDir( to );
664        fromOldDir.renameTo( toOldDir );
665    }
666
667    /*
668     * The profiler showed that when calling the history of a page, the propertyfile was read just as many
669     * times as there were versions of that file. The loading of a propertyfile is a cpu-intensive job.
670     * This Class holds onto the last propertyfile read, because the probability is high that the next call
671     * will with ask for the same propertyfile. The time it took to show a historypage with 267 versions dropped
672     * by 300%. Although each propertyfile in a history could be cached, there is likely to be little performance
673     * gain over simply keeping the last one requested.
674     */
675    private static class CachedProperties {
676        String m_page;
677        Properties m_props;
678        long m_lastModified;
679
680        /**
681         * Because a Constructor is inherently synchronised, there is no need to synchronise the arguments.
682         *
683         * @param pageName page name
684         * @param props  Properties to use for initialization
685         * @param lastModified last modified date
686         */
687        public CachedProperties( final String pageName, final Properties props, final long lastModified ) {
688            if ( pageName == null ) {
689                throw new NullPointerException ( "pageName must not be null!" );
690            }
691            this.m_page = pageName;
692            if ( props == null ) {
693                throw new NullPointerException ( "properties must not be null!" );
694            }
695            m_props = props;
696            this.m_lastModified = lastModified;
697        }
698    }
699
700}