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();
|
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 );
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-22 20:27:06 +00:00
|
|
|
if ( $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();
|
|
|
|
|
}
|
|
|
|
|
$xml_result = array();
|
2013-04-30 18:16:36 +00:00
|
|
|
ApiResult::setContent( $xml_result, $xml );
|
2010-01-11 15:55:52 +00:00
|
|
|
$result->addValue( null, 'parsetree', $xml_result );
|
2008-05-13 09:03:23 +00:00
|
|
|
}
|
2008-12-17 16:34:01 +00:00
|
|
|
$retval = $wgParser->preprocess( $params['text'], $title_obj, $options );
|
2007-10-10 00:15:51 +00:00
|
|
|
|
|
|
|
|
// Return result
|
|
|
|
|
$retval_array = array();
|
2013-04-30 18:16:36 +00:00
|
|
|
ApiResult::setContent( $retval_array, $retval );
|
2007-11-03 15:50:11 +00:00
|
|
|
$result->addValue( null, $this->getModuleName(), $retval_array );
|
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,
|
|
|
|
|
),
|
2008-05-13 09:03:23 +00:00
|
|
|
'generatexml' => false,
|
2011-05-01 22:11:05 +00:00
|
|
|
'includecomments' => false,
|
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',
|
2008-05-13 09:03:23 +00:00
|
|
|
'generatexml' => 'Generate XML parse tree',
|
2011-05-01 22:11:05 +00:00
|
|
|
'includecomments' => 'Whether to include HTML comments in the output',
|
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(
|
|
|
|
|
'' => array(
|
|
|
|
|
'*' => 'string'
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|