Code Duplication    Length = 181-182 lines in 2 locations

includes/api/ApiQueryIWBacklinks.php 1 location

@@ 32-213 (lines=182) @@
29
 * This gives links pointing to the given interwiki
30
 * @ingroup API
31
 */
32
class ApiQueryIWBacklinks extends ApiQueryGeneratorBase {
33
34
	public function __construct( ApiQuery $query, $moduleName ) {
35
		parent::__construct( $query, $moduleName, 'iwbl' );
36
	}
37
38
	public function execute() {
39
		$this->run();
40
	}
41
42
	public function executeGenerator( $resultPageSet ) {
43
		$this->run( $resultPageSet );
44
	}
45
46
	/**
47
	 * @param ApiPageSet $resultPageSet
48
	 * @return void
49
	 */
50
	public function run( $resultPageSet = null ) {
51
		$params = $this->extractRequestParams();
52
53
		if ( isset( $params['title'] ) && !isset( $params['prefix'] ) ) {
54
			$this->dieUsageMsg( [ 'missingparam', 'prefix' ] );
55
		}
56
57
		if ( !is_null( $params['continue'] ) ) {
58
			$cont = explode( '|', $params['continue'] );
59
			$this->dieContinueUsageIf( count( $cont ) != 3 );
60
61
			$db = $this->getDB();
62
			$op = $params['dir'] == 'descending' ? '<' : '>';
63
			$prefix = $db->addQuotes( $cont[0] );
64
			$title = $db->addQuotes( $cont[1] );
65
			$from = intval( $cont[2] );
66
			$this->addWhere(
67
				"iwl_prefix $op $prefix OR " .
68
				"(iwl_prefix = $prefix AND " .
69
				"(iwl_title $op $title OR " .
70
				"(iwl_title = $title AND " .
71
				"iwl_from $op= $from)))"
72
			);
73
		}
74
75
		$prop = array_flip( $params['prop'] );
76
		$iwprefix = isset( $prop['iwprefix'] );
77
		$iwtitle = isset( $prop['iwtitle'] );
78
79
		$this->addTables( [ 'iwlinks', 'page' ] );
80
		$this->addWhere( 'iwl_from = page_id' );
81
82
		$this->addFields( [ 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
83
			'iwl_from', 'iwl_prefix', 'iwl_title' ] );
84
85
		$sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
86
		if ( isset( $params['prefix'] ) ) {
87
			$this->addWhereFld( 'iwl_prefix', $params['prefix'] );
88
			if ( isset( $params['title'] ) ) {
89
				$this->addWhereFld( 'iwl_title', $params['title'] );
90
				$this->addOption( 'ORDER BY', 'iwl_from' . $sort );
91
			} else {
92
				$this->addOption( 'ORDER BY', [
93
					'iwl_title' . $sort,
94
					'iwl_from' . $sort
95
				] );
96
			}
97
		} else {
98
			$this->addOption( 'ORDER BY', [
99
				'iwl_prefix' . $sort,
100
				'iwl_title' . $sort,
101
				'iwl_from' . $sort
102
			] );
103
		}
104
105
		$this->addOption( 'LIMIT', $params['limit'] + 1 );
106
107
		$res = $this->select( __METHOD__ );
108
109
		$pages = [];
110
111
		$count = 0;
112
		$result = $this->getResult();
113
		foreach ( $res as $row ) {
114
			if ( ++$count > $params['limit'] ) {
115
				// We've reached the one extra which shows that there are
116
				// additional pages to be had. Stop here...
117
				// Continue string preserved in case the redirect query doesn't
118
				// pass the limit
119
				$this->setContinueEnumParameter(
120
					'continue',
121
					"{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}"
122
				);
123
				break;
124
			}
125
126
			if ( !is_null( $resultPageSet ) ) {
127
				$pages[] = Title::newFromRow( $row );
128
			} else {
129
				$entry = [ 'pageid' => $row->page_id ];
130
131
				$title = Title::makeTitle( $row->page_namespace, $row->page_title );
132
				ApiQueryBase::addTitleInfo( $entry, $title );
133
134
				if ( $row->page_is_redirect ) {
135
					$entry['redirect'] = true;
136
				}
137
138
				if ( $iwprefix ) {
139
					$entry['iwprefix'] = $row->iwl_prefix;
140
				}
141
142
				if ( $iwtitle ) {
143
					$entry['iwtitle'] = $row->iwl_title;
144
				}
145
146
				$fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry );
147
				if ( !$fit ) {
148
					$this->setContinueEnumParameter(
149
						'continue',
150
						"{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}"
151
					);
152
					break;
153
				}
154
			}
155
		}
156
157
		if ( is_null( $resultPageSet ) ) {
158
			$result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'iw' );
159
		} else {
160
			$resultPageSet->populateFromTitles( $pages );
161
		}
162
	}
163
164
	public function getCacheMode( $params ) {
165
		return 'public';
166
	}
167
168
	public function getAllowedParams() {
169
		return [
170
			'prefix' => null,
171
			'title' => null,
172
			'continue' => [
173
				ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
174
			],
175
			'limit' => [
176
				ApiBase::PARAM_DFLT => 10,
177
				ApiBase::PARAM_TYPE => 'limit',
178
				ApiBase::PARAM_MIN => 1,
179
				ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
180
				ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
181
			],
182
			'prop' => [
183
				ApiBase::PARAM_ISMULTI => true,
184
				ApiBase::PARAM_DFLT => '',
185
				ApiBase::PARAM_TYPE => [
186
					'iwprefix',
187
					'iwtitle',
188
				],
189
				ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
190
			],
191
			'dir' => [
192
				ApiBase::PARAM_DFLT => 'ascending',
193
				ApiBase::PARAM_TYPE => [
194
					'ascending',
195
					'descending'
196
				]
197
			],
198
		];
199
	}
200
201
	protected function getExamplesMessages() {
202
		return [
203
			'action=query&list=iwbacklinks&iwbltitle=Test&iwblprefix=wikibooks'
204
				=> 'apihelp-query+iwbacklinks-example-simple',
205
			'action=query&generator=iwbacklinks&giwbltitle=Test&giwblprefix=wikibooks&prop=info'
206
				=> 'apihelp-query+iwbacklinks-example-generator',
207
		];
208
	}
209
210
	public function getHelpUrls() {
211
		return 'https://www.mediawiki.org/wiki/API:Iwbacklinks';
212
	}
213
}
214

includes/api/ApiQueryLangBacklinks.php 1 location

@@ 32-212 (lines=181) @@
29
 * This gives links pointing to the given interwiki
30
 * @ingroup API
31
 */
32
class ApiQueryLangBacklinks extends ApiQueryGeneratorBase {
33
34
	public function __construct( ApiQuery $query, $moduleName ) {
35
		parent::__construct( $query, $moduleName, 'lbl' );
36
	}
37
38
	public function execute() {
39
		$this->run();
40
	}
41
42
	public function executeGenerator( $resultPageSet ) {
43
		$this->run( $resultPageSet );
44
	}
45
46
	/**
47
	 * @param ApiPageSet $resultPageSet
48
	 * @return void
49
	 */
50
	public function run( $resultPageSet = null ) {
51
		$params = $this->extractRequestParams();
52
53
		if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
54
			$this->dieUsageMsg( [ 'missingparam', 'lang' ] );
55
		}
56
57
		if ( !is_null( $params['continue'] ) ) {
58
			$cont = explode( '|', $params['continue'] );
59
			$this->dieContinueUsageIf( count( $cont ) != 3 );
60
61
			$db = $this->getDB();
62
			$op = $params['dir'] == 'descending' ? '<' : '>';
63
			$prefix = $db->addQuotes( $cont[0] );
64
			$title = $db->addQuotes( $cont[1] );
65
			$from = intval( $cont[2] );
66
			$this->addWhere(
67
				"ll_lang $op $prefix OR " .
68
				"(ll_lang = $prefix AND " .
69
				"(ll_title $op $title OR " .
70
				"(ll_title = $title AND " .
71
				"ll_from $op= $from)))"
72
			);
73
		}
74
75
		$prop = array_flip( $params['prop'] );
76
		$lllang = isset( $prop['lllang'] );
77
		$lltitle = isset( $prop['lltitle'] );
78
79
		$this->addTables( [ 'langlinks', 'page' ] );
80
		$this->addWhere( 'll_from = page_id' );
81
82
		$this->addFields( [ 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
83
			'll_from', 'll_lang', 'll_title' ] );
84
85
		$sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
86
		if ( isset( $params['lang'] ) ) {
87
			$this->addWhereFld( 'll_lang', $params['lang'] );
88
			if ( isset( $params['title'] ) ) {
89
				$this->addWhereFld( 'll_title', $params['title'] );
90
				$this->addOption( 'ORDER BY', 'll_from' . $sort );
91
			} else {
92
				$this->addOption( 'ORDER BY', [
93
					'll_title' . $sort,
94
					'll_from' . $sort
95
				] );
96
			}
97
		} else {
98
			$this->addOption( 'ORDER BY', [
99
				'll_lang' . $sort,
100
				'll_title' . $sort,
101
				'll_from' . $sort
102
			] );
103
		}
104
105
		$this->addOption( 'LIMIT', $params['limit'] + 1 );
106
107
		$res = $this->select( __METHOD__ );
108
109
		$pages = [];
110
111
		$count = 0;
112
		$result = $this->getResult();
113
		foreach ( $res as $row ) {
114
			if ( ++$count > $params['limit'] ) {
115
				// We've reached the one extra which shows that there are
116
				// additional pages to be had. Stop here... Continue string
117
				// preserved in case the redirect query doesn't pass the limit.
118
				$this->setContinueEnumParameter(
119
					'continue',
120
					"{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}"
121
				);
122
				break;
123
			}
124
125
			if ( !is_null( $resultPageSet ) ) {
126
				$pages[] = Title::newFromRow( $row );
127
			} else {
128
				$entry = [ 'pageid' => $row->page_id ];
129
130
				$title = Title::makeTitle( $row->page_namespace, $row->page_title );
131
				ApiQueryBase::addTitleInfo( $entry, $title );
132
133
				if ( $row->page_is_redirect ) {
134
					$entry['redirect'] = true;
135
				}
136
137
				if ( $lllang ) {
138
					$entry['lllang'] = $row->ll_lang;
139
				}
140
141
				if ( $lltitle ) {
142
					$entry['lltitle'] = $row->ll_title;
143
				}
144
145
				$fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry );
146
				if ( !$fit ) {
147
					$this->setContinueEnumParameter(
148
						'continue',
149
						"{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}"
150
					);
151
					break;
152
				}
153
			}
154
		}
155
156
		if ( is_null( $resultPageSet ) ) {
157
			$result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'll' );
158
		} else {
159
			$resultPageSet->populateFromTitles( $pages );
160
		}
161
	}
162
163
	public function getCacheMode( $params ) {
164
		return 'public';
165
	}
166
167
	public function getAllowedParams() {
168
		return [
169
			'lang' => null,
170
			'title' => null,
171
			'continue' => [
172
				ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
173
			],
174
			'limit' => [
175
				ApiBase::PARAM_DFLT => 10,
176
				ApiBase::PARAM_TYPE => 'limit',
177
				ApiBase::PARAM_MIN => 1,
178
				ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
179
				ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
180
			],
181
			'prop' => [
182
				ApiBase::PARAM_ISMULTI => true,
183
				ApiBase::PARAM_DFLT => '',
184
				ApiBase::PARAM_TYPE => [
185
					'lllang',
186
					'lltitle',
187
				],
188
				ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
189
			],
190
			'dir' => [
191
				ApiBase::PARAM_DFLT => 'ascending',
192
				ApiBase::PARAM_TYPE => [
193
					'ascending',
194
					'descending'
195
				]
196
			],
197
		];
198
	}
199
200
	protected function getExamplesMessages() {
201
		return [
202
			'action=query&list=langbacklinks&lbltitle=Test&lbllang=fr'
203
				=> 'apihelp-query+langbacklinks-example-simple',
204
			'action=query&generator=langbacklinks&glbltitle=Test&glbllang=fr&prop=info'
205
				=> 'apihelp-query+langbacklinks-example-generator',
206
		];
207
	}
208
209
	public function getHelpUrls() {
210
		return 'https://www.mediawiki.org/wiki/API:Langbacklinks';
211
	}
212
}
213