summaryrefslogtreecommitdiff
path: root/bin/wiki/vendor/addwiki/mediawiki-api-base/src/MediawikiSession.php
blob: c430695ca3b0674930a3c5cac3bddead54f1ef4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php

namespace Mediawiki\Api;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;

/**
 * @since 0.1
 *
 * @author Addshore
 */
class MediawikiSession implements LoggerAwareInterface {

	/**
	 * @var array
	 */
	private $tokens = [];

	/**
	 * @var MediawikiApi
	 */
	private $api;

	/**
	 * @var bool if this session is running against mediawiki version pre 1.25
	 */
	private $usePre125TokensModule = false;

	/**
	 * @var LoggerInterface
	 */
	private $logger;

	/**
	 * @param MediawikiApi $api The API object to use for this session.
	 */
	public function __construct( MediawikiApi $api ) {
		$this->api = $api;
		$this->logger = new NullLogger();
	}

	/**
	 * Sets a logger instance on the object
	 *
	 * @since 1.1
	 *
	 * @param LoggerInterface $logger The new Logger object.
	 *
	 * @return null
	 */
	public function setLogger( LoggerInterface $logger ) {
		$this->logger = $logger;
	}

	/**
	 * Tries to get the specified token from the API
	 *
	 * @since 0.1
	 *
	 * @param string $type The type of token to get.
	 *
	 * @return string
	 */
	public function getToken( $type = 'csrf' ) {
		// If we don't already have the token that we want
		if ( !array_key_exists( $type, $this->tokens ) ) {
			$this->logger->log( LogLevel::DEBUG, 'Getting fresh token', [ 'type' => $type ] );

			// If we know that we don't have the new module mw<1.25
			if ( $this->usePre125TokensModule ) {
				return $this->reallyGetPre125Token( $type );
			} else {
				return $this->reallyGetToken( $type );
			}

		}

		return $this->tokens[$type];
	}

	private function reallyGetPre125Token( $type ) {
		// Suppress deprecation warning
		$result = @$this->api->postRequest( // @codingStandardsIgnoreLine
			new SimpleRequest( 'tokens', [ 'type' => $this->getOldTokenType( $type ) ] )
		);
		$this->tokens[$type] = array_pop( $result['tokens'] );

		return $this->tokens[$type];
	}

	private function reallyGetToken( $type ) {
		// We suppress errors on this call so the user doesn't get get a warning that isn't their fault.
		$result = @$this->api->postRequest( // @codingStandardsIgnoreLine
			new SimpleRequest( 'query', [
				'meta' => 'tokens',
				'type' => $this->getNewTokenType( $type ),
				'continue' => '',
			] )
		);
		// If mw<1.25 (no new module)
		$metaWarning = "Unrecognized value for parameter 'meta': tokens";
		if ( isset( $result['warnings']['query']['*'] )
			&& false !== strpos( $result['warnings']['query']['*'], $metaWarning ) ) {
			$this->usePre125TokensModule = true;
			$this->logger->log( LogLevel::DEBUG, 'Falling back to pre 1.25 token system' );
			$this->tokens[$type] = $this->reallyGetPre125Token( $type );
		} else {
			$this->tokens[$type] = array_pop( $result['query']['tokens'] );
		}

		return $this->tokens[$type];
	}

	/**
	 * Tries to guess a new token type from an old token type
	 *
	 * @param string $type
	 *
	 * @return string
	 */
	private function getNewTokenType( $type ) {
		switch ( $type ) {
			case 'edit':
			case 'delete':
			case 'protect':
			case 'move':
			case 'block':
			case 'unblock':
			case 'email':
			case 'import':
			case 'options':
				return 'csrf';
		}
		// Return the same type, don't know what to do with this..
		return $type;
	}

	/**
	 * Tries to guess an old token type from a new token type
	 *
	 * @param $type
	 *
	 * @return string
	 */
	private function getOldTokenType( $type ) {
		switch ( $type ) {
			// Guess that we want an edit token, this may not always work as we might be trying to
			// use it for something else...
			case 'csrf':
				return 'edit';
		}
		return $type;
	}

	/**
	 * Clears all tokens stored by the api
	 *
	 * @since 0.2
	 */
	public function clearTokens() {
		$this->logger->log( LogLevel::DEBUG, 'Clearing session tokens', [ 'tokens' => $this->tokens ] );
		$this->tokens = [];
	}

}