package votorola.a.web.gwt; // Copyright 2010-2013, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import com.google.gwt.dom.client.*; import com.google.gwt.http.client.URL; import com.google.gwt.regexp.shared.RegExp; import java.util.regex.MatchResult; import votorola.a.*; import votorola.a.position.*; import votorola.a.voter.*; import votorola.g.*; import votorola.g.web.gwt.*; /** A pollwiki from the viewpoint of a GWT client. * * @see App#pollwiki() */ public final class PollwikiG implements Pollwiki { /** Does nothing itself but the call forces static initialization of this class. */ static void forceInitClass() {} // ------------------------------------------------------------------------------------ /** Encodes a page name and appends it to the wiki {@linkplain #getLocation() base * URL}. Usage example:
      *
      *     String href = voteServer.pollwiki().encodePageSpecifier(
      *       "Main page" ).build().toASCIIString();
* * @see #appendPageSpecifier(String) */ public StringBuilder encodePageSpecifier( String pageName ) { // cf. MediaWiki.encodePageSpecifier final StringBuilder b = new StringBuilder( wikiLocation ); pageName = MediaWiki.demiEncodedPageName( pageName, maybeUgly ); // fully encoded here: if( maybeUgly ) { b.append( "?title=" ); b.append( URL.encodeQueryString( pageName )); } else { b.append( '/' ); b.append( URLX.encodePath( pageName )); } return b; } /** The base URL for requesting pages from the pollwiki, without a trailing slash (/). * This is either the standard access URL ending in "index.php" for example, or a * pretty alias per $wgUsePathInfo. * * @see #appendPageSpecifier(String) * @see #maybeUgly() * @see #setLocation(String) */ public static @GWTConfigCallback String getLocation() { return wikiLocation; } // cf. votorola.a.PollwikiVS.uri() private static String wikiLocation; private static native void exposeLocation() /*-{ $wnd.a_web_gwt_PollwikiG_getLocation = $entry( @votorola.a.web.gwt.PollwikiG::getLocation() ); $wnd.a_web_gwt_PollwikiG_setLocation = $entry( @votorola.a.web.gwt.PollwikiG::setLocation(Ljava/lang/String;) ); }-*/; static { assert AMod.isForcedInit(): "forced init " + PollwikiG.class.getName(); exposeLocation(); } /** Sets the base URL for requesting pages from the pollwiki. Call it from the * global configuration function {@linkplain AMod voGWTConfig.a} in this * manner:
   a_web_gwt_PollwikiG_setLocation( 'http://YOUR.DOM/wiki' );
          *     // defaults to http://SCRIPT-LOCATION/index.php
* * @throws IllegalArgumentException if the location ends with a slash '/' * character. * @see #getLocation() * @see #setScriptLocation(String) */ public static @GWTConfigCallback void setLocation( final String s ) { if( s.endsWith( "/" )) throw new IllegalArgumentException( "location ends with '/'" ); wikiLocation = s; maybeUgly = MAYBE_UGLY_URL_PATTERN.exec(s) != null; } /** The base URL for script execution in the pollwiki, without a trailing slash (/). * For example http://reluk.ca/mediawiki. Script requests may be * constructed by appending the script path and parameters:
    getScriptLocation() + "/index.php?oldid=1138"
* * @see #setScriptLocation(String) */ public static @GWTConfigCallback String getScriptLocation() { return wikiScriptLocation; } // cf. votorola.a.PollwikiVS.wikiScriptURI() private static String wikiScriptLocation; private static native void exposeScriptLocation() /*-{ $wnd.a_web_gwt_PollwikiG_getScriptLocation = $entry( @votorola.a.web.gwt.PollwikiG::getScriptLocation() ); $wnd.a_web_gwt_PollwikiG_setScriptLocation = $entry( @votorola.a.web.gwt.PollwikiG::setScriptLocation(Ljava/lang/String;) ); }-*/; static { assert AMod.isForcedInit(): "forced init " + PollwikiG.class.getName(); exposeScriptLocation(); } /** Sets base URL for for script execution in the pollwiki. Call it from the * global configuration function {@linkplain AMod voGWTConfig.a} in this * manner:
   a_web_gwt_PollwikiG_setScriptLocation( 'http://YOUR.DOM/mediawiki' );
          *     // defaults to the standard location http://SERVER.DOM/mediawiki
          *     // where SERVER.DOM is a calculated guess at the web server name
* * @throws IllegalArgumentException if the location ends with a slash '/' * character. * @see #getScriptLocation() */ public static @GWTConfigCallback void setScriptLocation( final String s ) { if( s.endsWith( "/" )) throw new IllegalArgumentException( "location ends with '/'" ); wikiScriptLocation = s; } /** Returns the position identifier of the loaded page, or null if the page is not a * position page. */ public PositionID identifyAsPosition( final Document page ) { final String pageName = WindowX.js()._get( "wgPageName" ); if( pageName == null ) return null; // not a MediaWiki page return identifyAsPosition( pageName ); } /** Returns the position identifier of the loaded page, or null if the page is not a * position page. */ public PositionID identifyAsPosition( final String pageName ) { final MatchResult m = MediaWiki.parsePageNameS( pageName ); if( m == null ) return null; // not a MediaWiki page final String personName = IDPair.normalUsername( m.group( 2 )); final String pollName; if( pipeRecognizer.isPipeName( personName )) { pollName = datumValue( "pipePoll", Document.get() ); // per http://havoc.zelea.com/w/Category:Pipe } else pollName = m.group( 3 ); // subpage path if any if( pollName == null ) return null; return new PositionID( personName, pollName ); } // - P o l l w i k i ------------------------------------------------------------------ /** @see #getLocation() * @see #encodePageSpecifier(String) */ public StringBuilder appendPageSpecifier( final String encodedPageName ) { return MediaWiki.appendPageSpecifier( new StringBuilder(wikiLocation), maybeUgly, encodedPageName ); } /** @see #getLocation() */ public boolean maybeUgly() { return maybeUgly; } private static boolean maybeUgly; /** @see #setPipeRecognizer(String,String) */ public PipeRecognizer pipeRecognizer() { return pipeRecognizer; } private static PipeRecognizer pipeRecognizer = new PipeRecognizer0(); // by default private static native void exposePipeRecognizer() /*-{ $wnd.a_web_gwt_PollwikiG_setPipeRecognizer = $entry( @votorola.a.web.gwt.PollwikiG::setPipeRecognizer(Ljava/lang/String;Ljava/lang/String;) ); }-*/; static { assert AMod.isForcedInit(): "forced init " + PollwikiG.class.getName(); exposePipeRecognizer(); } /** Configures the pipe recognizer. Call this method from the global * configuration function {@linkplain AMod voGWTConfig.a} in this manner:
   a_web_gwt_PollwikiG_setPipeRecognizer( 'PREFIX', 'SUFFIX' );
          *     // there are no dependable defaults, you should set values if pipes are enabled
* * @see #pipeRecognizer() * @see Category:Pipe#Enabling pipes in the wiki */ public static @GWTConfigCallback void setPipeRecognizer( final String pipePrefix, final String pipeSuffix ) { pipeRecognizer = new PipeRecognizer1( pipePrefix, pipeSuffix ); } public String positionPageName( final String personName, final String pollName ) { return PositionID.pageName( personName, pollName, pipeRecognizer() ); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// /** Returns the value of a datum encoded in the loaded pollwiki page, or null if none * is encoded. * * @see Template:Data encoded */// per INLDOC private String datumValue( final String name, final Document page ) { final Element span = page.getElementById( name ); return span == null? null: span.getTitle(); } private static final RegExp MAYBE_UGLY_URL_PATTERN = RegExp.compile( ".+/index\\.php[0-9]*.*" ); // changing? change also in g.MediaWiki }