2007-10-10 00:15:51 +00:00
|
|
|
<?php
|
2010-02-22 20:27:06 +00:00
|
|
|
/**
|
2010-12-22 20:52:06 +00:00
|
|
|
*
|
2007-10-10 00:15:51 +00:00
|
|
|
*
|
2010-08-07 19:59:42 +00:00
|
|
|
* Created on Oct 05, 2007
|
|
|
|
|
*
|
2012-07-15 20:13:02 +00:00
|
|
|
* Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
|
2007-10-10 00:15:51 +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.,
|
2010-06-21 13:13:32 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2007-10-10 00:15:51 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-07 19:59:42 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
2007-10-10 00:15:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
2008-01-12 07:08:17 +00:00
|
|
|
* API module that functions as a shortcut to the wikitext preprocessor. Expands
|
|
|
|
|
* any templates in a provided string, and returns the result of this expansion
|
|
|
|
|
* to the caller.
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup API
|
2007-10-10 00:15:51 +00:00
|
|
|
*/
|
|
|
|
|
class ApiExpandTemplates extends ApiBase {
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
2010-07-23 07:17:56 +00:00
|
|
|
// Cache may vary on $wgUser because ParserOptions gets data from it
|
|
|
|
|
$this->getMain()->setCacheMode( 'anon-public-user-private' );
|
|
|
|
|
|
2007-10-10 00:15:51 +00:00
|
|
|
// Get parameters
|
2008-12-17 16:34:01 +00:00
|
|
|
$params = $this->extractRequestParams();
|
2014-06-02 18:53:38 +00:00
|
|
|
$this->requireMaxOneParameter( $params, 'prop', 'generatexml' );
|
|
|
|
|
|
|
|
|
|
if ( $params['prop'] === null ) {
|
|
|
|
|
$this->setWarning( 'Because no values have been specified for the prop parameter, a legacy format has been used for the output.'
|
|
|
|
|
. ' This format is deprecated, and in the future, a default value will be set for the prop parameter, causing the new format to always be used.' );
|
|
|
|
|
$prop = array();
|
|
|
|
|
} else {
|
|
|
|
|
$prop = array_flip( $params['prop'] );
|
|
|
|
|
}
|
2007-10-10 00:15:51 +00:00
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
// Create title for parser
|
2010-02-22 20:27:06 +00:00
|
|
|
$title_obj = Title::newFromText( $params['title'] );
|
2013-03-01 15:01:26 +00:00
|
|
|
if ( !$title_obj || $title_obj->isExternal() ) {
|
2011-08-04 20:04:09 +00:00
|
|
|
$this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
|
2010-02-22 20:27:06 +00:00
|
|
|
}
|
2008-05-13 09:03:23 +00:00
|
|
|
|
|
|
|
|
$result = $this->getResult();
|
2007-10-10 00:15:51 +00:00
|
|
|
|
|
|
|
|
// Parse text
|
|
|
|
|
global $wgParser;
|
2011-11-19 08:54:03 +00:00
|
|
|
$options = ParserOptions::newFromContext( $this->getContext() );
|
2010-02-22 20:27:06 +00:00
|
|
|
|
2011-05-01 22:11:05 +00:00
|
|
|
if ( $params['includecomments'] ) {
|
|
|
|
|
$options->setRemoveComments( false );
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-02 18:53:38 +00:00
|
|
|
$retval = array();
|
|
|
|
|
|
|
|
|
|
if ( isset( $prop['parsetree'] ) || $params['generatexml'] ) {
|
2008-05-13 09:03:23 +00:00
|
|
|
$wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS );
|
2008-12-17 16:34:01 +00:00
|
|
|
$dom = $wgParser->preprocessToDom( $params['text'] );
|
2008-05-13 09:03:23 +00:00
|
|
|
if ( is_callable( array( $dom, 'saveXML' ) ) ) {
|
|
|
|
|
$xml = $dom->saveXML();
|
|
|
|
|
} else {
|
|
|
|
|
$xml = $dom->__toString();
|
|
|
|
|
}
|
2014-06-02 18:53:38 +00:00
|
|
|
if ( isset( $prop['parsetree'] ) ) {
|
|
|
|
|
unset( $prop['parsetree'] );
|
|
|
|
|
$retval['parsetree'] = $xml;
|
|
|
|
|
} else {
|
|
|
|
|
// the old way
|
|
|
|
|
$xml_result = array();
|
|
|
|
|
ApiResult::setContent( $xml_result, $xml );
|
|
|
|
|
$result->addValue( null, 'parsetree', $xml_result );
|
2014-05-29 17:50:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-10-10 00:15:51 +00:00
|
|
|
|
2014-06-02 18:53:38 +00:00
|
|
|
// if they didn't want any output except (probably) the parse tree,
|
|
|
|
|
// then don't bother actually fully expanding it
|
|
|
|
|
if ( $prop || $params['prop'] === null ) {
|
2014-06-18 18:12:08 +00:00
|
|
|
$wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS );
|
2014-06-02 18:53:38 +00:00
|
|
|
$frame = $wgParser->getPreprocessor()->newFrame();
|
|
|
|
|
$wikitext = $wgParser->preprocess( $params['text'], $title_obj, $options, null, $frame );
|
|
|
|
|
if ( $params['prop'] === null ) {
|
|
|
|
|
// the old way
|
|
|
|
|
ApiResult::setContent( $retval, $wikitext );
|
|
|
|
|
} else {
|
|
|
|
|
if ( isset( $prop['categories'] ) ) {
|
|
|
|
|
$categories = $wgParser->getOutput()->getCategories();
|
|
|
|
|
if ( !empty( $categories ) ) {
|
|
|
|
|
$categories_result = array();
|
|
|
|
|
foreach ( $categories as $category => $sortkey ) {
|
|
|
|
|
$entry = array();
|
|
|
|
|
$entry['sortkey'] = $sortkey;
|
|
|
|
|
ApiResult::setContent( $entry, $category );
|
|
|
|
|
$categories_result[] = $entry;
|
|
|
|
|
}
|
|
|
|
|
$result->setIndexedTagName( $categories_result, 'category' );
|
|
|
|
|
$retval['categories'] = $categories_result;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-28 20:17:41 +00:00
|
|
|
if ( isset( $prop['volatile'] ) && $frame->isVolatile() ) {
|
2014-06-02 18:53:38 +00:00
|
|
|
$retval['volatile'] = '';
|
|
|
|
|
}
|
2014-05-28 20:17:41 +00:00
|
|
|
if ( isset( $prop['ttl'] ) && $frame->getTTL() !== null ) {
|
|
|
|
|
$retval['ttl'] = $frame->getTTL();
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $prop['wikitext'] ) ) {
|
2014-06-02 18:53:38 +00:00
|
|
|
$retval['wikitext'] = $wikitext;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-06 23:28:09 +00:00
|
|
|
}
|
2014-06-02 18:53:38 +00:00
|
|
|
$result->setSubelements( $retval, array( 'wikitext', 'parsetree' ) );
|
|
|
|
|
$result->addValue( null, $this->getModuleName(), $retval );
|
2007-10-10 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getAllowedParams() {
|
2010-02-22 20:27:06 +00:00
|
|
|
return array(
|
2008-04-14 07:45:50 +00:00
|
|
|
'title' => array(
|
2010-02-22 20:27:06 +00:00
|
|
|
ApiBase::PARAM_DFLT => 'API',
|
2007-10-10 00:15:51 +00:00
|
|
|
),
|
2011-08-05 14:55:53 +00:00
|
|
|
'text' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
|
|
|
|
),
|
2014-06-02 18:53:38 +00:00
|
|
|
'prop' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => array(
|
|
|
|
|
'wikitext',
|
|
|
|
|
'categories',
|
|
|
|
|
'volatile',
|
2014-05-28 20:17:41 +00:00
|
|
|
'ttl',
|
2014-06-02 18:53:38 +00:00
|
|
|
'parsetree',
|
|
|
|
|
),
|
|
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
|
),
|
2011-05-01 22:11:05 +00:00
|
|
|
'includecomments' => false,
|
2014-06-02 18:53:38 +00:00
|
|
|
'generatexml' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'boolean',
|
|
|
|
|
ApiBase::PARAM_DEPRECATED => true,
|
|
|
|
|
),
|
2007-10-10 00:15:51 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getParamDescription() {
|
2010-02-22 20:27:06 +00:00
|
|
|
return array(
|
2007-10-10 00:15:51 +00:00
|
|
|
'text' => 'Wikitext to convert',
|
|
|
|
|
'title' => 'Title of page',
|
2014-06-02 18:53:38 +00:00
|
|
|
'prop' => array(
|
|
|
|
|
'Which pieces of information to get',
|
|
|
|
|
' wikitext - The expanded wikitext',
|
|
|
|
|
' categories - Any categories present in the input that are not represented in the wikitext output',
|
|
|
|
|
' volatile - Whether the output is volatile and should not be reused elsewhere within the page',
|
2014-05-28 20:17:41 +00:00
|
|
|
' ttl - The maximum time after which caches of the result should be invalidated',
|
2014-06-02 18:53:38 +00:00
|
|
|
' parsetree - The XML parse tree of the input',
|
|
|
|
|
'Note that if no values are selected, the result will contain the wikitext,',
|
|
|
|
|
'but the output will be in a deprecated format.',
|
|
|
|
|
),
|
2011-05-01 22:11:05 +00:00
|
|
|
'includecomments' => 'Whether to include HTML comments in the output',
|
2014-06-02 18:53:38 +00:00
|
|
|
'generatexml' => 'Generate XML parse tree (replaced by prop=parsetree)',
|
2007-10-10 00:15:51 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
Added result properties to action=paraminfo
Added information about the properties of the results of API calls
to action=paraminfo, including information about "property groups":
what should the prop parameter be set to to get that property.
Uses the same format for types as parameters already do.
The output format of some modules doesn't fit this, so the result
properties for them weren't added, or only partially.
Partially implemented modules:
* expandtemplates:
parsetree is in its own tag
* protect, allusers, backlinks, deletedrevs, info, imageinfo,
logevents, querypage, recentchanges, revisions, searchinfo,
usercontribs, userinfo, users, watchlist, upload:
response with partially complex structure
Not implemented modules:
* feedcontributions, feedwatchlist, opensearch, rds:
non-standard reponse
* help:
error is normal response; not very useful for automated tools anyway
* paraminfo, parse, pageprops, siteinfo, userrights:
response with complex structure
Change-Id: Iff2a9bef79f994e73eef3062b4dd5461bff968ab
2012-05-02 15:00:30 +00:00
|
|
|
public function getResultProperties() {
|
|
|
|
|
return array(
|
2014-06-02 18:53:38 +00:00
|
|
|
'wikitext' => array(
|
|
|
|
|
'wikitext' => 'string',
|
|
|
|
|
),
|
|
|
|
|
'categories' => array(
|
|
|
|
|
'categories' => array(
|
|
|
|
|
ApiBase::PROP_TYPE => 'array',
|
|
|
|
|
ApiBase::PROP_NULLABLE => true,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
'volatile' => array(
|
|
|
|
|
'volatile' => array(
|
|
|
|
|
ApiBase::PROP_TYPE => 'boolean',
|
|
|
|
|
ApiBase::PROP_NULLABLE => true,
|
|
|
|
|
),
|
|
|
|
|
),
|
2014-05-28 20:17:41 +00:00
|
|
|
'ttl' => array(
|
|
|
|
|
'ttl' => array(
|
|
|
|
|
ApiBase::PROP_TYPE => 'integer',
|
|
|
|
|
ApiBase::PROP_NULLABLE => true,
|
|
|
|
|
),
|
|
|
|
|
),
|
2014-06-02 18:53:38 +00:00
|
|
|
'parsetree' => array(
|
|
|
|
|
'parsetree' => 'string',
|
|
|
|
|
),
|
Added result properties to action=paraminfo
Added information about the properties of the results of API calls
to action=paraminfo, including information about "property groups":
what should the prop parameter be set to to get that property.
Uses the same format for types as parameters already do.
The output format of some modules doesn't fit this, so the result
properties for them weren't added, or only partially.
Partially implemented modules:
* expandtemplates:
parsetree is in its own tag
* protect, allusers, backlinks, deletedrevs, info, imageinfo,
logevents, querypage, recentchanges, revisions, searchinfo,
usercontribs, userinfo, users, watchlist, upload:
response with partially complex structure
Not implemented modules:
* feedcontributions, feedwatchlist, opensearch, rds:
non-standard reponse
* help:
error is normal response; not very useful for automated tools anyway
* paraminfo, parse, pageprops, siteinfo, userrights:
response with complex structure
Change-Id: Iff2a9bef79f994e73eef3062b4dd5461bff968ab
2012-05-02 15:00:30 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getDescription() {
|
2014-03-09 20:22:47 +00:00
|
|
|
return 'Expands all templates in wikitext.';
|
2007-10-10 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
2011-08-04 20:04:09 +00:00
|
|
|
public function getPossibleErrors() {
|
|
|
|
|
return array_merge( parent::getPossibleErrors(), array(
|
|
|
|
|
array( 'invalidtitle', 'title' ),
|
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-17 22:24:21 +00:00
|
|
|
public function getExamples() {
|
2010-02-22 20:27:06 +00:00
|
|
|
return array(
|
2007-10-10 00:15:51 +00:00
|
|
|
'api.php?action=expandtemplates&text={{Project:Sandbox}}'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-17 16:38:24 +00:00
|
|
|
public function getHelpUrls() {
|
2011-11-28 15:43:11 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#expandtemplates';
|
2011-07-17 16:38:24 +00:00
|
|
|
}
|
2007-10-10 00:15:51 +00:00
|
|
|
}
|