1 | <?php |
||
16 | class MediawikiSession implements LoggerAwareInterface { |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | private $tokens = []; |
||
22 | |||
23 | /** |
||
24 | * @var MediawikiApi |
||
25 | */ |
||
26 | private $api; |
||
27 | |||
28 | /** |
||
29 | * @var bool if this session is running against mediawiki version pre 1.25 |
||
30 | */ |
||
31 | private $usePre125TokensModule = false; |
||
32 | |||
33 | /** |
||
34 | * @var LoggerInterface |
||
35 | */ |
||
36 | private $logger; |
||
37 | |||
38 | /** |
||
39 | * @param MediawikiApi $api The API object to use for this session. |
||
40 | */ |
||
41 | 6 | public function __construct( MediawikiApi $api ) { |
|
42 | 6 | $this->api = $api; |
|
43 | 6 | $this->logger = new NullLogger(); |
|
44 | 6 | } |
|
45 | |||
46 | /** |
||
47 | * Sets a logger instance on the object |
||
48 | * |
||
49 | * @since 1.1 |
||
50 | * |
||
51 | * @param LoggerInterface $logger The new Logger object. |
||
52 | * |
||
53 | * @return null |
||
54 | */ |
||
55 | public function setLogger( LoggerInterface $logger ) { |
||
56 | $this->logger = $logger; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Tries to get the specified token from the API |
||
61 | * |
||
62 | * @since 0.1 |
||
63 | * |
||
64 | * @param string $type The type of token to get. |
||
65 | * |
||
66 | * @return string |
||
67 | */ |
||
68 | 7 | public function getToken( $type = 'csrf' ) { |
|
69 | // If we don't already have the token that we want |
||
70 | 7 | if ( !array_key_exists( $type, $this->tokens ) ) { |
|
71 | 7 | $this->logger->log( LogLevel::DEBUG, 'Getting fresh token', [ 'type' => $type ] ); |
|
72 | |||
73 | // If we know that we don't have the new module mw<1.25 |
||
74 | 7 | if ( $this->usePre125TokensModule ) { |
|
75 | return $this->reallyGetPre125Token( $type ); |
||
76 | } else { |
||
77 | 7 | return $this->reallyGetToken( $type ); |
|
78 | } |
||
79 | |||
80 | } |
||
81 | |||
82 | 4 | return $this->tokens[$type]; |
|
83 | } |
||
84 | |||
85 | 2 | private function reallyGetPre125Token( $type ) { |
|
94 | |||
95 | 5 | private function reallyGetToken( $type ) { |
|
96 | // We suppress errors on this call so the user doesn't get get a warning that isn't their fault. |
||
97 | 5 | $result = @$this->api->postRequest( // @codingStandardsIgnoreLine |
|
120 | |||
121 | /** |
||
122 | * Tries to guess a new token type from an old token type |
||
123 | * |
||
124 | * @param string $type |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | 5 | private function getNewTokenType( $type ) { |
|
144 | |||
145 | /** |
||
146 | * Tries to guess an old token type from a new token type |
||
147 | * |
||
148 | * @param $type |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | 2 | private function getOldTokenType( $type ) { |
|
161 | |||
162 | /** |
||
163 | * Clears all tokens stored by the api |
||
164 | * |
||
165 | * @since 0.2 |
||
166 | */ |
||
167 | 2 | public function clearTokens() { |
|
171 | |||
172 | } |
||
173 |