Удзельнік:Zedlik/Код робата/bot.interwiki.InterwikiManager

Зьвесткі зь Вікіпэдыі — вольнай энцыкляпэдыі

Код клясы bot.interwiki.InterwikiManager
Файл InterwikiManager.java

/*
 * Copyright (c) 2008 zedlik.
 * jz53@zedlik.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the code author nor the 
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ZEDLIK ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL ZEDLIK BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package bot.interwiki;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sourceforge.jwbf.bots.MediaWikiBot;
import net.sourceforge.jwbf.contentRep.mw.SimpleArticle;

public class InterwikiManager 
{
	private String lastInterwikiUsed;
	private String lastInterwikiLinkUsed;
	
	public InterwikiManager()
	{
		lastInterwikiUsed = "";
	}
	
	public String getInterwikiLanguageLineIfExists(String language, String articleTitle) throws Exception
	{
		if (interwikiArticleExists(articleTitle, language))
		{
			return "[[" + language + ":" + articleTitle + "]]\r\n";
		}
		
		return "";
	}
	
	public boolean interwikiArticleExists(String articleTitle, String language) throws Exception
	{
		return getInterwikiArticleContentsByInterwikiTitle(articleTitle, language).length() > 0;
	}
	
	
	public String getInterwikiArticleContentsByInterwikiTitle(String articleTitle, String language) throws Exception
	{
		String customArticleTitle = articleTitle;		
		lastInterwikiUsed = language;
		lastInterwikiLinkUsed = customArticleTitle; 
		
		if (customArticleTitle.length() > 0)
		{
			MediaWikiBot bIW = new MediaWikiBot("http://" + language + ".wikipedia.org/w/");
			SimpleArticle saIW = new SimpleArticle(bIW.readContent(customArticleTitle));
			
			return saIW.getText();
		}
		
		return "";
	}
	
	
	public String getInterwikiArticleContents(String contents, String language) throws Exception
	{
		String customArticleTitle = getInterwikiArticleTitle(contents, language);
		return getInterwikiArticleContentsByInterwikiTitle(customArticleTitle, language);
	}

	public String getInterwikiArticleTitle(String contents, String language) throws Exception
	{
		InterwikiParser ip = new InterwikiParser(contents);
		ip.parse();

		return ip.getTitle(language);		
	}

	
	public String getInterwikiCommonsTitle(String contents, String articleTitle)
	{
		if (contents.length() > 0)
		{
			String commonsTitle = getInterwikiPageCommonsTitle(contents, articleTitle);
			if (commonsTitle.length() == 0) 
				commonsTitle = getInterwikiCategoryCommonsTitle(contents, articleTitle);
			
			return commonsTitle;
		}
		
		return "";
	}
	
	private String getInterwikiCategoryCommonsTitle(String contents, String articleTitle)
	{
		Pattern p = Pattern.compile("\\{\\{Commons(?: )?cat\\|(.*)\\|(.*)\\}\\}", Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(contents);
		
		if (m.find())
		{
			String commonsTitle = m.group(1);
			return "Category:" + commonsTitle;
		}
		else
		{
			p = Pattern.compile("\\{\\{Commons(?: )?cat\\|(.*)\\}\\}", Pattern.CASE_INSENSITIVE);
			m = p.matcher(contents);
			
			if (m.find())
			{
				String commonsTitle = m.group(1);
				return "Category:" + commonsTitle;
			}
			else
			{
				p = Pattern.compile("\\{\\{Commons(?: )?cat\\}\\}", Pattern.CASE_INSENSITIVE);
				m = p.matcher(contents);
				
				if (m.find())
				{
					// String commonsTitle = m.group(1);
					// return "Category:" + commonsTitle;
					return "Category:" + articleTitle;
				}
			}
		}
		
		return "";
	}
	

	private String getInterwikiPageCommonsTitle(String contents, String articleTitle)
	{
		Pattern p = Pattern.compile("\\{\\{Commons\\|(.*)\\|(.*)\\}\\}", Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(contents);
		
		if (m.find())
		{
			String commonsTitle = m.group(1);
			return commonsTitle;
		}
		else
		{
			p = Pattern.compile("\\{\\{Commons\\|(.*)\\}\\}", Pattern.CASE_INSENSITIVE);
			m = p.matcher(contents);
			
			if (m.find())
			{
				String commonsTitle = m.group(1);
				return commonsTitle;
			}
			else
			{
				p = Pattern.compile("\\{\\{Commons\\}\\}", Pattern.CASE_INSENSITIVE);
				m = p.matcher(contents);
				
				if (m.find())
				{
					//String commonsTitle = m.group(1);
					// return commonsTitle;
					return articleTitle;
				}			
			}
		}
		
		return "";
	}
	
	
	public String getLastInterwikiLinkUsed() 
	{
		return lastInterwikiLinkUsed;
	}

	public String getLastInterwikiUsed() 
	{
		return lastInterwikiUsed;
	}	

}