mediawiki

package module
v0.0.0-...-0ac1a9d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2021 License: MIT Imports: 11 Imported by: 0

README

go-mediawiki | CircleCI | GoDoc | Coverage Status

A MediaWiki API wrapper for Go.

Documentation

Documentation specific to this implementation is located on GoDoc.

Documentation specific to the MediaWiki API in general is located at their own wiki.

Examples are located in the examples subdirectory.

Supported features

  • Login/Logout
  • Edit Pages
  • Read Pages
  • Upload
  • Download
  • Generic API Interface
  • Unit tests

License

This software is licensed under the MIT license. Pull requests and issues are welcome.

Documentation

Overview

Package mediawiki provides a wrapper for interacting with the Mediawiki API

Please see https://www.mediawiki.org/wiki/API:Main_page for any API specific information or refer to any of the functions defined for the MWApi struct for information regarding this specific implementation.

The examples/ subdirectory contains an example application that uses this API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MWApi

type MWApi struct {
	Domain string

	UseBasicAuth  bool
	BasicAuthUser string
	BasicAuthPass string
	// contains filtered or unexported fields
}

MWApi is used to interact with the MediaWiki server.

func New

func New(wikiURL, userAgent string) (*MWApi, error)

New generates a new MediaWiki API (MWApi) struct.

Example: mediawiki.New("https://en.wikipedia.org/w/api.php", "My Mediawiki Bot") Returns errors if the URL is invalid

func (*MWApi) API

func (m *MWApi) API(values ...map[string]string) ([]byte, error)

API is a generic interface to the Mediawiki API. Refer to the MediaWiki API reference for details.

This is used by all internal functions to interact with the API.

func (*MWApi) Download

func (m *MWApi) Download(filename string) (io.ReadCloser, error)

Download a file.

Returns a readcloser that must be closed manually. Refer to the example app for additional usage.

func (*MWApi) Edit

func (m *MWApi) Edit(values map[string]string) error

Edit a page.

This function will request an edit token if the MWApi struct doesn't already contain one.

Example:

editConfig := map[string]string{
    "title":   "SOME PAGE",
    "summary": "THIS IS WHAT SHOWS UP IN THE LOG",
    "text":    "THE ENTIRE TEXT OF THE PAGE",
}
err = client.Edit(editConfig)

func (*MWApi) GetEditToken

func (m *MWApi) GetEditToken() error

GetEditToken retrieves an edit token from the MediaWiki site and saves it.

This is necessary for editing any page.

The Edit() function will call this automatically but it is available if you want to make direct calls to API().

func (*MWApi) Login

func (m *MWApi) Login(username, password string) error

Login to the Mediawiki Website.

func (*MWApi) Logout

func (m *MWApi) Logout()

Logout of the MediaWiki website

func (*MWApi) Read

func (m *MWApi) Read(pageName string) (*Page, error)

Read returns the most recent revision of a Page. If an error occurs, nil is returned.

func (*MWApi) Upload

func (m *MWApi) Upload(dstFilename string, file io.Reader, ignorewarnings bool) error

Upload a file

This does a simple, but more error-prone upload. Mediawiki has a chunked upload version but it is only available in newer versions of the API.

Automatically retrieves an edit token if necessary.

type Page

type Page struct {
	Pageid    int
	Ns        int
	Title     string
	Touched   string
	Lastrevid int
	// Mediawiki will return ” for zero, this makes me sad.
	// If for some reason you need this value you'll have to
	// do some type assertion sillyness.
	Counter   interface{}
	Length    int
	Edittoken string
	Revisions []struct {
		Revid         int       `json:"revid"`
		Parentid      int       `json:"parentid"`
		Minor         string    `json:"minor"`
		User          string    `json:"user"`
		Userid        int       `json:"userid"`
		Timestamp     time.Time `json:"timestamp"`
		Size          int       `json:"size"`
		Sha1          string    `json:"sha1"`
		ContentModel  string    `json:"contentmodel"`
		Comment       string    `json:"comment"`
		ParsedComment string    `json:"parsedcomment"`
		ContentFormat string    `json:"contentformat"`
		Body          string    `json:"*"` // Take note, MediaWiki literally returns { '*':
	}
	Imageinfo []struct {
		Url            string
		Descriptionurl string
	}
}

A Page represents a MediaWiki page and its metadata.

type Response

type Response struct {
	Query struct {
		// The JSON response for this part of the struct is dumb.
		// It will return something like { '23': { 'pageid': 23 ...
		//
		// As a workaround you can use PageSlice which will create
		// a list of pages from the map.
		Pages map[string]Page
	}
}

Response is a struct used for unmarshaling the MediaWiki JSON response.

func (*Response) PageSlice

func (r *Response) PageSlice() []Page

PageSlice generates a slice from Pages to work around the sillyness in the MediaWiki API.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL