Wikipedia:Scripts/ImageFileMigrator/Wiki.py

#!/usr/bin/env python
import urllib2
import MultipartPostHandler
import cStringIO
#From http://fabien.seisen.org/python/urllib2_multipart.html
from urllib import urlencode
import cookielib
import re
import os
 
class Wiki:
    def __init__(self, domain, path = '/index.php'):
    #def __init__(self, domain, path = '/index.php5'):
        self.domain = domain
        self.path = self.domain + path
        self.token = ''
        self.cookie_processor = urllib2.HTTPCookieProcessor()
        self.opener = urllib2.build_opener(self.cookie_processor, MultipartPostHandler.MultipartPostHandler())
 
    def login(self, user, password):
        #call the login page with no data to get the token, if there is one
        loginpage = self.opener.open(self.path + "?title=Special:UserLogin")
        matches = re.findall('<input type="hidden" name="wpLoginToken" value="(\w*)" /></form>', loginpage.read())
        if matches: self.token = matches[0]
        data = {'wpName': user,
                'wpPassword': password,
                'wpLoginattempt': 'Log in'}
        if self.token: data['wpLoginToken'] = self.token
        url = self.path  + "?title=Special:UserLogin&action=submitlogin&type=login"
        response = self.opener.open(url, urlencode(data))
        return response.read()
 
    def get_image_list(self):
        # Setting to 100,000 by default to try to get all images.
        # Older versions of MediaWiki:
        #image_request = self.opener.open(self.path + "/Special:Imagelist?limit=100000")
        image_request = self.opener.open(self.path + "?limit=100000&ilsearch=&title=Special:ImageList")
        html = image_request.read()
        matches = re.findall("<td class=\"TablePager_col_img_name\"><a href=\".*\" title=\"(.*)\">.*</a> \(<a href=\"(.*)\">file</a>\)</td>", html)
        images = [(match[0],match[1]) for match in matches]
        return images
 
    def get_page_export(self, pages):
        text = "\n".join(pages)
        data = {
            'curonly': 'on',
            'pages': text,
            #'templates': '',
            #'wpDownload': '',
            'submit': 'Export'
        }
        # Older versions of MediaWiki:
        #url = self.path + "/Special:Export"
        url = self.path + "?title=Special:Export"
        result = self.opener.open((url), urlencode(data))
        return result.read()
    def import_pages(self, xml):
        xml_file = open("import.xml", "w")
        xml_file.write(xml)
        xml_file.close()
        xml_file = open("import.xml", "r")
        data = {
            'action': 'submit',
            'xmlimport': xml_file,
            'source': 'upload',
            'submit': 'Upload File'
        }
        url = self.path + "?title=Special:Import&action=submit"
        result = self.opener.open((url), data)
        xml_file.close()
        os.remove("import.xml")
        return result.read()
    def upload_image(self, filename):
        name = filename.split("/")[-1]
        image_file = open(filename, "rb")
        data = {
            'wpUploadFile': image_file,
            'wpDestFile': name,
            'wpUpload': 'Upload File',
            'wpIgnoreWarning': 'off'
        }
        url = self.path + "?title=Special:Upload&action=submit"
        result = self.opener.open((url), data)
        return result.read()
 
 
    def logout(self):
        self.cookie_processor.cookiejar.clear()

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.