2011-05-14 11:29:45 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* API for MediaWiki 1.17+
|
|
|
|
|
*
|
|
|
|
|
* Created on May 14, 2011
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2011 Sam Reed
|
2012-07-15 20:13:02 +00:00
|
|
|
* Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
|
2011-05-14 11:29:45 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This gives links pointing to the given interwiki
|
|
|
|
|
* @ingroup API
|
|
|
|
|
*/
|
|
|
|
|
class ApiQueryLangBacklinks extends ApiQueryGeneratorBase {
|
|
|
|
|
|
2014-03-25 17:22:11 +00:00
|
|
|
public function __construct( ApiQuery $query, $moduleName ) {
|
2011-05-14 11:29:45 +00:00
|
|
|
parent::__construct( $query, $moduleName, 'lbl' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
$this->run();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function executeGenerator( $resultPageSet ) {
|
|
|
|
|
$this->run( $resultPageSet );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-04-15 18:12:09 +00:00
|
|
|
* @param ApiPageSet $resultPageSet
|
2012-01-12 19:41:18 +00:00
|
|
|
* @return void
|
2011-05-14 11:29:45 +00:00
|
|
|
*/
|
|
|
|
|
public function run( $resultPageSet = null ) {
|
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
|
|
|
|
|
if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError(
|
|
|
|
|
[
|
|
|
|
|
'apierror-invalidparammix-mustusewith',
|
|
|
|
|
$this->encodeParamName( 'title' ),
|
|
|
|
|
$this->encodeParamName( 'lang' )
|
|
|
|
|
],
|
|
|
|
|
'nolang'
|
|
|
|
|
);
|
2011-05-14 11:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !is_null( $params['continue'] ) ) {
|
|
|
|
|
$cont = explode( '|', $params['continue'] );
|
2013-01-15 02:19:16 +00:00
|
|
|
$this->dieContinueUsageIf( count( $cont ) != 3 );
|
2011-05-14 11:29:45 +00:00
|
|
|
|
2012-05-16 17:22:36 +00:00
|
|
|
$db = $this->getDB();
|
2012-06-17 07:59:19 +00:00
|
|
|
$op = $params['dir'] == 'descending' ? '<' : '>';
|
2012-05-16 17:22:36 +00:00
|
|
|
$prefix = $db->addQuotes( $cont[0] );
|
2012-05-21 17:07:37 +00:00
|
|
|
$title = $db->addQuotes( $cont[1] );
|
2011-05-14 11:29:45 +00:00
|
|
|
$from = intval( $cont[2] );
|
|
|
|
|
$this->addWhere(
|
2012-06-17 07:59:19 +00:00
|
|
|
"ll_lang $op $prefix OR " .
|
2012-05-16 17:22:36 +00:00
|
|
|
"(ll_lang = $prefix AND " .
|
2012-06-17 07:59:19 +00:00
|
|
|
"(ll_title $op $title OR " .
|
2012-05-16 17:22:36 +00:00
|
|
|
"(ll_title = $title AND " .
|
2012-06-17 07:59:19 +00:00
|
|
|
"ll_from $op= $from)))"
|
2011-05-14 11:29:45 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$prop = array_flip( $params['prop'] );
|
2011-05-19 21:13:03 +00:00
|
|
|
$lllang = isset( $prop['lllang'] );
|
2011-05-14 11:29:45 +00:00
|
|
|
$lltitle = isset( $prop['lltitle'] );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->addTables( [ 'langlinks', 'page' ] );
|
2011-05-14 11:29:45 +00:00
|
|
|
$this->addWhere( 'll_from = page_id' );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->addFields( [ 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
|
|
|
|
|
'll_from', 'll_lang', 'll_title' ] );
|
2011-05-14 11:29:45 +00:00
|
|
|
|
2012-06-17 07:59:19 +00:00
|
|
|
$sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
|
2011-05-14 11:29:45 +00:00
|
|
|
if ( isset( $params['lang'] ) ) {
|
|
|
|
|
$this->addWhereFld( 'll_lang', $params['lang'] );
|
|
|
|
|
if ( isset( $params['title'] ) ) {
|
|
|
|
|
$this->addWhereFld( 'll_title', $params['title'] );
|
2012-06-17 07:59:19 +00:00
|
|
|
$this->addOption( 'ORDER BY', 'll_from' . $sort );
|
2011-05-14 11:29:45 +00:00
|
|
|
} else {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->addOption( 'ORDER BY', [
|
2012-06-17 07:59:19 +00:00
|
|
|
'll_title' . $sort,
|
|
|
|
|
'll_from' . $sort
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2011-05-14 11:29:45 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->addOption( 'ORDER BY', [
|
2012-06-17 07:59:19 +00:00
|
|
|
'll_lang' . $sort,
|
|
|
|
|
'll_title' . $sort,
|
|
|
|
|
'll_from' . $sort
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2011-05-14 11:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->addOption( 'LIMIT', $params['limit'] + 1 );
|
|
|
|
|
|
|
|
|
|
$res = $this->select( __METHOD__ );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$pages = [];
|
2011-05-14 11:29:45 +00:00
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
|
$result = $this->getResult();
|
|
|
|
|
foreach ( $res as $row ) {
|
2013-11-14 12:56:06 +00:00
|
|
|
if ( ++$count > $params['limit'] ) {
|
2013-11-14 14:18:38 +00:00
|
|
|
// We've reached the one extra which shows that there are
|
|
|
|
|
// additional pages to be had. Stop here... Continue string
|
|
|
|
|
// preserved in case the redirect query doesn't pass the limit.
|
|
|
|
|
$this->setContinueEnumParameter(
|
|
|
|
|
'continue',
|
|
|
|
|
"{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}"
|
|
|
|
|
);
|
2011-05-14 11:29:45 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !is_null( $resultPageSet ) ) {
|
|
|
|
|
$pages[] = Title::newFromRow( $row );
|
|
|
|
|
} else {
|
2016-02-17 09:09:32 +00:00
|
|
|
$entry = [ 'pageid' => $row->page_id ];
|
2011-05-14 11:29:45 +00:00
|
|
|
|
|
|
|
|
$title = Title::makeTitle( $row->page_namespace, $row->page_title );
|
|
|
|
|
ApiQueryBase::addTitleInfo( $entry, $title );
|
|
|
|
|
|
|
|
|
|
if ( $row->page_is_redirect ) {
|
2015-01-16 19:00:07 +00:00
|
|
|
$entry['redirect'] = true;
|
2011-05-14 11:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $lllang ) {
|
2011-05-14 12:14:25 +00:00
|
|
|
$entry['lllang'] = $row->ll_lang;
|
2011-05-14 11:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $lltitle ) {
|
2011-05-14 12:14:25 +00:00
|
|
|
$entry['lltitle'] = $row->ll_title;
|
2011-05-14 11:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry );
|
2011-05-14 11:29:45 +00:00
|
|
|
if ( !$fit ) {
|
2013-11-14 14:18:38 +00:00
|
|
|
$this->setContinueEnumParameter(
|
|
|
|
|
'continue',
|
|
|
|
|
"{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}"
|
|
|
|
|
);
|
2011-05-14 11:29:45 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( is_null( $resultPageSet ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'll' );
|
2011-05-14 11:29:45 +00:00
|
|
|
} else {
|
|
|
|
|
$resultPageSet->populateFromTitles( $pages );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getCacheMode( $params ) {
|
|
|
|
|
return 'public';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2011-05-19 21:13:03 +00:00
|
|
|
'lang' => null,
|
2011-05-14 11:29:45 +00:00
|
|
|
'title' => null,
|
2016-02-17 09:09:32 +00:00
|
|
|
'continue' => [
|
2014-09-18 17:38:23 +00:00
|
|
|
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'limit' => [
|
2011-05-14 11:29:45 +00:00
|
|
|
ApiBase::PARAM_DFLT => 10,
|
|
|
|
|
ApiBase::PARAM_TYPE => 'limit',
|
|
|
|
|
ApiBase::PARAM_MIN => 1,
|
|
|
|
|
ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
|
|
|
|
|
ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'prop' => [
|
2011-05-14 11:29:45 +00:00
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
|
ApiBase::PARAM_DFLT => '',
|
2016-02-17 09:09:32 +00:00
|
|
|
ApiBase::PARAM_TYPE => [
|
2011-05-14 11:29:45 +00:00
|
|
|
'lllang',
|
|
|
|
|
'lltitle',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
|
|
|
|
|
],
|
|
|
|
|
'dir' => [
|
2012-06-17 07:59:19 +00:00
|
|
|
ApiBase::PARAM_DFLT => 'ascending',
|
2016-02-17 09:09:32 +00:00
|
|
|
ApiBase::PARAM_TYPE => [
|
2012-06-17 07:59:19 +00:00
|
|
|
'ascending',
|
|
|
|
|
'descending'
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
];
|
2011-05-14 11:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
2014-10-28 17:17:02 +00:00
|
|
|
protected function getExamplesMessages() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2014-09-18 17:38:23 +00:00
|
|
|
'action=query&list=langbacklinks&lbltitle=Test&lbllang=fr'
|
|
|
|
|
=> 'apihelp-query+langbacklinks-example-simple',
|
|
|
|
|
'action=query&generator=langbacklinks&glbltitle=Test&glbllang=fr&prop=info'
|
|
|
|
|
=> 'apihelp-query+langbacklinks-example-generator',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-05-14 11:29:45 +00:00
|
|
|
}
|
2013-05-16 07:08:18 +00:00
|
|
|
|
|
|
|
|
public function getHelpUrls() {
|
2017-04-04 22:52:57 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Langbacklinks';
|
2013-05-16 07:08:18 +00:00
|
|
|
}
|
2011-05-14 11:29:45 +00:00
|
|
|
}
|