Code Duplication    Length = 74-74 lines in 2 locations

includes/libs/virtualrest/ParsoidVirtualRESTService.php 1 location

@@ 151-224 (lines=74) @@
148
	 * ParsoidVirtualRESTService translated these to the "real" Parsoid v1
149
	 * API.  We now translate these to the "real" Parsoid v3 API.
150
	 */
151
	public function onParsoid1Request( array $req, Closure $idGeneratorFunc ) {
152
		$parts = explode( '/', $req['url'] );
153
		list(
154
			$targetWiki, // 'local'
155
			$version, // 'v1'
156
			$reqType // 'page' or 'transform'
157
		) = $parts;
158
		if ( $targetWiki !== 'local' ) {
159
			throw new Exception( "Only 'local' target wiki is currently supported" );
160
		} elseif ( $version !== 'v1' ) {
161
			throw new Exception( "Only v1 and v3 are supported." );
162
		} elseif ( $reqType !== 'page' && $reqType !== 'transform' ) {
163
			throw new Exception( "Request type must be either 'page' or 'transform'" );
164
		}
165
		$req['url'] = $this->params['url'] . $this->params['domain'] . '/v3/';
166
		if ( $reqType === 'page' ) {
167
			$title = $parts[3];
168
			if ( $parts[4] !== 'html' ) {
169
				throw new Exception( "Only 'html' output format is currently supported" );
170
			}
171
			$req['url'] .= 'page/html/' . $title;
172
			if ( isset( $parts[5] ) ) {
173
				$req['url'] .= '/' . $parts[5];
174
			} elseif ( isset( $req['query']['oldid'] ) && $req['query']['oldid'] ) {
175
				$req['url'] .= '/' . $req['query']['oldid'];
176
				unset( $req['query']['oldid'] );
177
			}
178
		} elseif ( $reqType === 'transform' ) {
179
			$req['url'] .= 'transform/'. $parts[3] . '/to/' . $parts[5];
180
			// the title
181
			if ( isset( $parts[6] ) ) {
182
				$req['url'] .= '/' . $parts[6];
183
			}
184
			// revision id
185
			if ( isset( $parts[7] ) ) {
186
				$req['url'] .= '/' . $parts[7];
187
			} elseif ( isset( $req['body']['oldid'] ) && $req['body']['oldid'] ) {
188
				$req['url'] .= '/' . $req['body']['oldid'];
189
				unset( $req['body']['oldid'] );
190
			}
191
			if ( $parts[4] !== 'to' ) {
192
				throw new Exception( "Part index 4 is not 'to'" );
193
			}
194
			if ( $parts[3] === 'html' && $parts[5] === 'wikitext' ) {
195
				if ( !isset( $req['body']['html'] ) ) {
196
					throw new Exception( "You must set an 'html' body key for this request" );
197
				}
198
			} elseif ( $parts[3] == 'wikitext' && $parts[5] == 'html' ) {
199
				if ( !isset( $req['body']['wikitext'] ) ) {
200
					throw new Exception( "You must set a 'wikitext' body key for this request" );
201
				}
202
				if ( isset( $req['body']['body'] ) ) {
203
					$req['body']['body_only'] = $req['body']['body'];
204
					unset( $req['body']['body'] );
205
				}
206
			} else {
207
				throw new Exception( "Transformation unsupported" );
208
			}
209
		}
210
		// set the appropriate proxy, timeout and headers
211
		if ( $this->params['HTTPProxy'] ) {
212
			$req['proxy'] = $this->params['HTTPProxy'];
213
		}
214
		if ( $this->params['timeout'] != null ) {
215
			$req['reqTimeout'] = $this->params['timeout'];
216
		}
217
		if ( $this->params['forwardCookies'] ) {
218
			$req['headers']['Cookie'] = $this->params['forwardCookies'];
219
		}
220
221
		return $req;
222
	}
223
224
}
225

includes/libs/virtualrest/RestbaseVirtualRESTService.php 1 location

@@ 151-224 (lines=74) @@
148
	 * Visual Editor "pretends" the V1 API is like.  (See
149
	 * ParsoidVirtualRESTService.)
150
	 */
151
	public function onParsoid1Request( array $req, Closure $idGeneratorFunc ) {
152
		$parts = explode( '/', $req['url'] );
153
		list(
154
			$targetWiki, // 'local'
155
			$version, // 'v1'
156
			$reqType // 'page' or 'transform'
157
		) = $parts;
158
		if ( $targetWiki !== 'local' ) {
159
			throw new Exception( "Only 'local' target wiki is currently supported" );
160
		} elseif ( $version !== 'v1' ) {
161
			throw new Exception( "Version mismatch: should not happen." );
162
		} elseif ( $reqType !== 'page' && $reqType !== 'transform' ) {
163
			throw new Exception( "Request type must be either 'page' or 'transform'" );
164
		}
165
		$req['url'] = $this->params['url'] . $this->params['domain'] . '/v1/' . $reqType . '/';
166
		if ( $reqType === 'page' ) {
167
			$title = $parts[3];
168
			if ( $parts[4] !== 'html' ) {
169
				throw new Exception( "Only 'html' output format is currently supported" );
170
			}
171
			$req['url'] .= 'html/' . $title;
172
			if ( isset( $parts[5] ) ) {
173
				$req['url'] .= '/' . $parts[5];
174
			} elseif ( isset( $req['query']['oldid'] ) && $req['query']['oldid'] ) {
175
				$req['url'] .= '/' . $req['query']['oldid'];
176
				unset( $req['query']['oldid'] );
177
			}
178
		} elseif ( $reqType === 'transform' ) {
179
			// from / to transform
180
			$req['url'] .= $parts[3] . '/to/' . $parts[5];
181
			// the title
182
			if ( isset( $parts[6] ) ) {
183
				$req['url'] .= '/' . $parts[6];
184
			}
185
			// revision id
186
			if ( isset( $parts[7] ) ) {
187
				$req['url'] .= '/' . $parts[7];
188
			} elseif ( isset( $req['body']['oldid'] ) && $req['body']['oldid'] ) {
189
				$req['url'] .= '/' . $req['body']['oldid'];
190
				unset( $req['body']['oldid'] );
191
			}
192
			if ( $parts[4] !== 'to' ) {
193
				throw new Exception( "Part index 4 is not 'to'" );
194
			}
195
			if ( $parts[3] === 'html' && $parts[5] === 'wikitext' ) {
196
				if ( !isset( $req['body']['html'] ) ) {
197
					throw new Exception( "You must set an 'html' body key for this request" );
198
				}
199
			} elseif ( $parts[3] == 'wikitext' && $parts[5] == 'html' ) {
200
				if ( !isset( $req['body']['wikitext'] ) ) {
201
					throw new Exception( "You must set a 'wikitext' body key for this request" );
202
				}
203
				if ( isset( $req['body']['body'] ) ) {
204
					$req['body']['body_only'] = $req['body']['body'];
205
					unset( $req['body']['body'] );
206
				}
207
			} else {
208
				throw new Exception( "Transformation unsupported" );
209
			}
210
		}
211
		// set the appropriate proxy, timeout and headers
212
		if ( $this->params['HTTPProxy'] ) {
213
			$req['proxy'] = $this->params['HTTPProxy'];
214
		}
215
		if ( $this->params['timeout'] != null ) {
216
			$req['reqTimeout'] = $this->params['timeout'];
217
		}
218
		if ( $this->params['forwardCookies'] ) {
219
			$req['headers']['Cookie'] = $this->params['forwardCookies'];
220
		}
221
222
		return $req;
223
	}
224
225
	/**
226
	 * Remap a Parsoid v3 request to a RESTBase v1 request.
227
	 *