Перайсьці да зьместу

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

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

Код клясы bot.categories.CategoryManager
Файл CategoryManager.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.categories;

import net.sourceforge.jwbf.bots.MediaWikiBot;
import net.sourceforge.jwbf.contentRep.mw.SimpleArticle;
import java.util.Iterator;

public class CategoryManager 
{
	private MediaWikiBot b;
	
	// private final String teplateContents = "{{болей}}";
	private final String categoryPrefix = "Катэгорыя:";
	
	public CategoryManager(MediaWikiBot b)
	{
		this.b = b;
	}


	public void fireCategoryTemplateMoreCorrection() throws Exception
	{
		fireCategoryTemplateMoreCorrection(new String(""));
	}
	
	public void fireCategoryTemplateMoreCorrection(String startArticle) throws Exception
	{
		//Iterable<String> categories = b.getAllPageTitles("", "", true, true);
		int[] n = new int[1];
		n[0] = 14; // Namespace of all the categories
		
		String articleToStart = null;
		if (startArticle.length() > 0)
		{
			articleToStart = startArticle; 
		}
		
		Iterator<String> categories = b.getAllPageTitles(articleToStart, null, false, true, n).iterator();
		
		while (categories.hasNext())
		{
			String categoryName = categories.next();
			unifyTemplateMore(categoryName);
		}
	}
	
	
	public void unifyTemplateMore(String categoryName) throws Exception
	{
		// quotes correction
		categoryName = categoryName.replace(new String("&#039;"), new String("'"));
		
		System.out.print(categoryName + " ");
		
		// stripping initial "Category:"...
		int prefixIndex = categoryName.indexOf(':');
		categoryName = categoryName.substring(prefixIndex + 1);
		
		
		SimpleArticle sa = new SimpleArticle(b.readContent(categoryPrefix + categoryName));
				
		String contents = sa.getText();
		
		boolean categoryHasMore = categoryHasMore(contents);
		String moreCustomName = "";
		if (categoryHasMore)
		{
			moreCustomName = getMoreCustomName(contents);
		}
		
		
		boolean articleExists;
		
		if (!moreCustomName.equalsIgnoreCase(""))
			articleExists = articleExists(moreCustomName);
		else
			articleExists = articleExists(categoryName);
		
		boolean contentChanged = false;
		String summary = "";
		
		if (categoryHasMore && !articleExists)
		{
			// remove more
			// contents = contents.replaceAll(teplateContents, "");
			contents = contents.replaceAll("\\{\\{болей\\}\\}\\n", "");
			contents = contents.replaceAll("\\{\\{болей\\}\\}", "");
			contents = contents.replaceAll("\\{\\{болей\\|(.*){1,200}\\}\\}\\n", "");
			contents = contents.replaceAll("\\{\\{болей\\|(.*){1,200}\\}\\}", "");
			contents = contents.replaceAll("\\{\\{Болей\\}\\}\\n", "");
			contents = contents.replaceAll("\\{\\{Болей\\}\\}", "");
			contents = contents.replaceAll("\\{\\{Болей\\|(.*){1,200}\\}\\}\\n", "");
			contents = contents.replaceAll("\\{\\{Болей\\|(.*){1,200}\\}\\}", "");

			while (contents.startsWith("\n") || contents.startsWith("\r"))
			{
				contents = contents.substring(1);
			}
			
			summary = "выдаліў шаблён «болей»";
			contentChanged = true;
			System.out.println("removed [more]");
		}
		else if (!categoryHasMore && articleExists)
		{
			// add more
			while (contents.startsWith("\n") || contents.startsWith("\r"))
			{
				contents = contents.substring(1);
			}
			
			contents = "{{болей}}" + "\r\n\r\n" + contents;
			summary = "дадаў шаблён «болей»";
			contentChanged = true;
			System.out.println("added [more]");
		}
		
		if (contentChanged)
		{
			// write category

			sa.setText(contents);
			// sa.setLabel("Удзельнік:Zedlik/Пясочніца/Магадзішу");
			sa.setLabel(categoryPrefix + categoryName);
			
			sa.setEditSummary(new String("Робат " + summary));
			sa.setMinorEdit(true);
			
			b.writeContent(sa);
			
			// b.wait(5);
			Thread.sleep(5000);
		}
		else
		{
			Thread.sleep(400);
			System.out.println("");
		}
	}
	
	
	private boolean articleExists(String articleName) throws Exception
	{
		SimpleArticle sa = new SimpleArticle(b.readContent(articleName));
		String text = sa.getText();
		
		return !text.equals(new String(""));
	}
	
	private boolean categoryHasMore(String contents)
	{
		return (contents.indexOf("{{болей}}") >= 0) || (contents.indexOf("{{болей|") >= 0) ||
			(contents.indexOf("{{Болей}}") >= 0) || (contents.indexOf("{{Болей|") >= 0);
	}
	
	private String getMoreCustomName(String contents)
	{
		int b = contents.indexOf("{{болей|");
		if (b < 0)
		{
			b = contents.indexOf("{{Болей|");
		}
		int e = contents.indexOf("}}", b);

		if (b >= 0 && e >= b)
		{
			return contents.substring(b + 8, e);
		}
		else
		{
			return new String("");
		}
	}
	

}