1 | <?php |
||
15 | class MediawikiSession implements LoggerAwareInterface { |
||
16 | |||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | private $tokens = []; |
||
21 | |||
22 | /** |
||
23 | * @var MediawikiApi |
||
24 | */ |
||
25 | private $api; |
||
26 | |||
27 | /** |
||
28 | * @var bool if this session is running against mediawiki version pre 1.25 |
||
29 | */ |
||
30 | private $usePre125TokensModule = false; |
||
31 | |||
32 | /** |
||
33 | * @var LoggerInterface |
||
34 | */ |
||
35 | private $logger; |
||
36 | |||
37 | /** |
||
38 | * @param MediawikiApi $api |
||
39 | */ |
||
40 | 5 | public function __construct( MediawikiApi $api ) { |
|
44 | |||
45 | /** |
||
46 | * Sets a logger instance on the object |
||
47 | * |
||
48 | * @since 1.1 |
||
49 | * |
||
50 | * @param LoggerInterface $logger |
||
51 | * |
||
52 | * @return null |
||
53 | */ |
||
54 | public function setLogger( LoggerInterface $logger ) { |
||
57 | |||
58 | /** |
||
59 | * Tries to get the specified token from the API |
||
60 | * |
||
61 | * @since 0.1 |
||
62 | * |
||
63 | * @param string $type |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | 6 | public function getToken( $type = 'csrf' ) { |
|
84 | |||
85 | 2 | private function reallyGetPre125Token( $type ) { |
|
94 | |||
95 | 4 | 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 | 4 | $result = @$this->api->postRequest( // @codingStandardsIgnoreLine |
|
98 | 4 | new SimpleRequest( 'query', [ |
|
99 | 4 | 'meta' => 'tokens', |
|
100 | 4 | 'type' => $this->getNewTokenType( $type ), |
|
101 | 4 | 'continue' => '', |
|
102 | ] ) |
||
103 | ); |
||
104 | // If mw<1.25 (no new module) |
||
105 | 4 | $metaWarning = "Unrecognized value for parameter 'meta': tokens"; |
|
106 | 4 | if ( isset( $result['warnings']['query']['*'] ) |
|
107 | 4 | && false !== strpos( $result['warnings']['query']['*'], $metaWarning ) ) { |
|
108 | 2 | $this->usePre125TokensModule = true; |
|
109 | 2 | $this->logger->log( LogLevel::DEBUG, 'Falling back to pre 1.25 token system' ); |
|
110 | 2 | $this->tokens[$type] = $this->reallyGetPre125Token( $type ); |
|
111 | } else { |
||
112 | 2 | $this->tokens[$type] = array_pop( $result['query']['tokens'] ); |
|
113 | } |
||
114 | |||
115 | 4 | return $this->tokens[$type]; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * Tries to guess a new token type from an old token type |
||
120 | * |
||
121 | * @param string $type |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | 4 | private function getNewTokenType( $type ) { |
|
141 | |||
142 | /** |
||
143 | * Tries to guess an old token type from a new token type |
||
144 | * |
||
145 | * @param $type |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | 2 | private function getOldTokenType( $type ) { |
|
158 | |||
159 | /** |
||
160 | * Clears all tokens stored by the api |
||
161 | * |
||
162 | * @since 0.2 |
||
163 | */ |
||
164 | 2 | public function clearTokens() { |
|
168 | |||
169 | } |
||
170 |