2007-12-01 13:37:02 +00:00
|
|
|
<?php
|
2010-02-23 18:05:46 +00:00
|
|
|
/**
|
2010-08-07 19:59:42 +00:00
|
|
|
* Created on Dec 01, 2007
|
|
|
|
|
*
|
2010-02-23 18:05:46 +00:00
|
|
|
* Copyright © 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
|
2007-12-01 13:37:02 +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-12-01 13:37:02 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-07 19:59:42 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
2007-12-01 13:37:02 +00:00
|
|
|
*/
|
|
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
if ( !defined( 'MEDIAWIKI' ) ) {
|
2007-12-01 13:37:02 +00:00
|
|
|
// Eclipse helper - will be ignored in production
|
2010-02-23 18:05:46 +00:00
|
|
|
require_once( "ApiBase.php" );
|
2007-12-01 13:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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-12-01 13:37:02 +00:00
|
|
|
*/
|
|
|
|
|
class ApiParse extends ApiBase {
|
2011-01-28 01:47:08 +00:00
|
|
|
private $section, $text, $pstText = null;
|
2010-10-20 18:50:33 +00:00
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
public function __construct( $main, $action ) {
|
2010-02-23 18:05:46 +00:00
|
|
|
parent::__construct( $main, $action );
|
2007-12-01 13:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
2010-07-23 07:17:56 +00:00
|
|
|
// The data is hot but user-dependent, like page views, so we set vary cookies
|
|
|
|
|
$this->getMain()->setCacheMode( 'anon-public-user-private' );
|
|
|
|
|
|
2007-12-01 13:37:02 +00:00
|
|
|
// Get parameters
|
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
$text = $params['text'];
|
|
|
|
|
$title = $params['title'];
|
2008-01-29 14:47:27 +00:00
|
|
|
$page = $params['page'];
|
2010-05-10 14:17:21 +00:00
|
|
|
$pageid = $params['pageid'];
|
2008-04-10 10:51:40 +00:00
|
|
|
$oldid = $params['oldid'];
|
2010-05-10 18:27:58 +00:00
|
|
|
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( !is_null( $page ) && ( !is_null( $text ) || $title != 'API' ) ) {
|
|
|
|
|
$this->dieUsage( 'The page parameter cannot be used together with the text and title parameters', 'params' );
|
|
|
|
|
}
|
2010-01-11 15:55:52 +00:00
|
|
|
$prop = array_flip( $params['prop'] );
|
2010-05-25 13:47:22 +00:00
|
|
|
|
2010-05-10 18:27:58 +00:00
|
|
|
if ( isset( $params['section'] ) ) {
|
|
|
|
|
$this->section = $params['section'];
|
|
|
|
|
} else {
|
|
|
|
|
$this->section = false;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2008-12-20 20:00:07 +00:00
|
|
|
// The parser needs $wgTitle to be set, apparently the
|
|
|
|
|
// $title parameter in Parser::parse isn't enough *sigh*
|
2011-01-05 23:59:04 +00:00
|
|
|
global $wgParser, $wgUser, $wgTitle, $wgLang;
|
2010-05-10 14:17:21 +00:00
|
|
|
|
2010-06-23 19:36:26 +00:00
|
|
|
// Currently unnecessary, code to act as a safeguard against any change in current behaviour of uselang breaks
|
2010-03-09 11:52:43 +00:00
|
|
|
$oldLang = null;
|
2010-03-08 22:32:17 +00:00
|
|
|
if ( isset( $params['uselang'] ) && $params['uselang'] != $wgLang->getCode() ) {
|
2010-04-17 20:58:04 +00:00
|
|
|
$oldLang = $wgLang; // Backup wgLang
|
2010-03-08 22:32:17 +00:00
|
|
|
$wgLang = Language::factory( $params['uselang'] );
|
|
|
|
|
}
|
2010-05-10 14:17:21 +00:00
|
|
|
|
2008-06-12 11:50:01 +00:00
|
|
|
$popts = new ParserOptions();
|
2010-01-11 15:55:52 +00:00
|
|
|
$popts->setTidy( true );
|
2010-08-04 07:44:23 +00:00
|
|
|
$popts->enableLimitReport( !$params['disablepp'] );
|
2011-01-06 23:35:59 +00:00
|
|
|
|
2008-10-24 12:28:14 +00:00
|
|
|
$redirValues = null;
|
2011-01-06 23:35:59 +00:00
|
|
|
|
2011-06-30 01:06:17 +00:00
|
|
|
// Return result
|
|
|
|
|
$result = $this->getResult();
|
|
|
|
|
|
2010-05-10 14:17:21 +00:00
|
|
|
if ( !is_null( $oldid ) || !is_null( $pageid ) || !is_null( $page ) ) {
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( !is_null( $oldid ) ) {
|
2010-01-23 22:52:40 +00:00
|
|
|
// Don't use the parser cache
|
2010-01-11 15:55:52 +00:00
|
|
|
$rev = Revision::newFromID( $oldid );
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( !$rev ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
if ( !$rev->userCan( Revision::DELETED_TEXT ) ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2010-01-23 22:47:49 +00:00
|
|
|
|
2008-04-10 10:51:40 +00:00
|
|
|
$titleObj = $rev->getTitle();
|
2010-10-24 19:16:46 +00:00
|
|
|
|
2008-12-20 20:00:07 +00:00
|
|
|
$wgTitle = $titleObj;
|
2010-05-10 18:27:58 +00:00
|
|
|
|
2010-12-30 17:06:09 +00:00
|
|
|
// If for some reason the "oldid" is actually the current revision, it may be cached
|
2011-01-01 22:42:53 +00:00
|
|
|
if ( $titleObj->getLatestRevID() === intval( $oldid ) ) {
|
2011-01-06 17:06:05 +00:00
|
|
|
$articleObj = new Article( $titleObj, 0 );
|
2011-01-01 22:35:16 +00:00
|
|
|
|
2011-01-28 01:47:08 +00:00
|
|
|
$p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid,
|
|
|
|
|
isset( $prop['wikitext'] ) ) ;
|
2011-01-06 23:35:59 +00:00
|
|
|
} else { // This is an old revision, so get the text differently
|
2011-01-28 01:47:08 +00:00
|
|
|
$this->text = $rev->getText( Revision::FOR_THIS_USER );
|
2010-10-24 19:16:46 +00:00
|
|
|
|
|
|
|
|
$wgTitle = $titleObj;
|
2010-05-10 18:27:58 +00:00
|
|
|
|
2010-10-24 19:16:46 +00:00
|
|
|
if ( $this->section !== false ) {
|
2011-01-28 01:47:08 +00:00
|
|
|
$this->text = $this->getSectionText( $this->text, 'r' . $rev->getId() );
|
2010-10-24 19:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
2011-01-28 01:47:08 +00:00
|
|
|
$p_result = $wgParser->parse( $this->text, $titleObj, $popts );
|
2010-10-24 19:16:46 +00:00
|
|
|
}
|
2011-01-06 23:19:19 +00:00
|
|
|
} else { // Not $oldid
|
2011-06-17 15:57:00 +00:00
|
|
|
if ( $params['redirects'] ) {
|
|
|
|
|
$reqParams = array(
|
|
|
|
|
'action' => 'query',
|
|
|
|
|
'redirects' => '',
|
|
|
|
|
);
|
|
|
|
|
if ( !is_null ( $pageid ) ) {
|
|
|
|
|
$reqParams['pageids'] = $pageid;
|
|
|
|
|
} else { // $page
|
|
|
|
|
$reqParams['titles'] = $page;
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2011-06-17 15:57:00 +00:00
|
|
|
$req = new FauxRequest( $reqParams );
|
|
|
|
|
$main = new ApiMain( $req );
|
|
|
|
|
$main->execute();
|
|
|
|
|
$data = $main->getResultData();
|
|
|
|
|
$redirValues = isset( $data['query']['redirects'] )
|
|
|
|
|
? $data['query']['redirects']
|
|
|
|
|
: array();
|
|
|
|
|
$to = $page;
|
|
|
|
|
foreach ( (array)$redirValues as $r ) {
|
|
|
|
|
$to = $r['to'];
|
2010-05-10 14:17:21 +00:00
|
|
|
}
|
|
|
|
|
$titleObj = Title::newFromText( $to );
|
2011-06-17 15:57:00 +00:00
|
|
|
} else {
|
|
|
|
|
if ( !is_null ( $pageid ) ) {
|
|
|
|
|
$reqParams['pageids'] = $pageid;
|
|
|
|
|
$titleObj = Title::newFromID( $pageid );
|
|
|
|
|
} else { // $page
|
|
|
|
|
$to = $page;
|
|
|
|
|
$titleObj = Title::newFromText( $to );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( !is_null ( $pageid ) ) {
|
|
|
|
|
if ( !$titleObj ) {
|
|
|
|
|
// Still throw nosuchpageid error if pageid was provided
|
|
|
|
|
$this->dieUsageMsg( array( 'nosuchpageid', $pageid ) );
|
2010-05-10 14:17:21 +00:00
|
|
|
}
|
2011-06-17 15:57:00 +00:00
|
|
|
} elseif ( !$titleObj || !$titleObj->exists() ) {
|
|
|
|
|
$this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2010-05-10 14:17:21 +00:00
|
|
|
$wgTitle = $titleObj;
|
2007-12-01 13:37:02 +00:00
|
|
|
|
2011-01-06 17:06:05 +00:00
|
|
|
$articleObj = new Article( $titleObj, 0 );
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( isset( $prop['revid'] ) ) {
|
2008-04-10 10:51:40 +00:00
|
|
|
$oldid = $articleObj->getRevIdFetched();
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
|
2011-01-28 01:47:08 +00:00
|
|
|
$p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid,
|
|
|
|
|
isset( $prop['wikitext'] ) ) ;
|
2008-01-29 14:47:27 +00:00
|
|
|
}
|
2011-01-06 23:35:59 +00:00
|
|
|
} else { // Not $oldid, $pageid, $page. Hence based on $text
|
2011-01-06 23:19:19 +00:00
|
|
|
|
2011-01-28 01:47:08 +00:00
|
|
|
$this->text = $text;
|
2010-01-11 15:55:52 +00:00
|
|
|
$titleObj = Title::newFromText( $title );
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( !$titleObj ) {
|
2011-05-20 17:19:06 +00:00
|
|
|
$this->dieUsageMsg( array( 'invalidtitle', $title ) );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2008-12-20 20:00:07 +00:00
|
|
|
$wgTitle = $titleObj;
|
2010-05-10 18:27:58 +00:00
|
|
|
|
|
|
|
|
if ( $this->section !== false ) {
|
2011-01-28 01:47:08 +00:00
|
|
|
$this->text = $this->getSectionText( $this->text, $titleObj->getText() );
|
2010-05-10 18:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( $params['pst'] || $params['onlypst'] ) {
|
2011-01-28 01:47:08 +00:00
|
|
|
$this->pstText = $wgParser->preSaveTransform( $this->text, $titleObj, $wgUser, $popts );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
if ( $params['onlypst'] ) {
|
2008-12-04 15:51:39 +00:00
|
|
|
// Build a result and bail out
|
|
|
|
|
$result_array['text'] = array();
|
2011-06-30 01:06:17 +00:00
|
|
|
$result->setContent( $result_array['text'], $this->pstText );
|
2011-01-28 01:47:08 +00:00
|
|
|
if ( isset( $prop['wikitext'] ) ) {
|
|
|
|
|
$result_array['wikitext'] = array();
|
2011-06-30 01:06:17 +00:00
|
|
|
$result->setContent( $result_array['wikitext'], $this->text );
|
2011-01-28 01:47:08 +00:00
|
|
|
}
|
2011-06-30 01:06:17 +00:00
|
|
|
$result->addValue( null, $this->getModuleName(), $result_array );
|
2008-12-04 15:51:39 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2011-02-02 19:16:05 +00:00
|
|
|
$p_result = $wgParser->parse( $params['pst'] ? $this->pstText : $this->text, $titleObj, $popts );
|
2008-01-29 14:47:27 +00:00
|
|
|
}
|
2007-12-01 13:37:02 +00:00
|
|
|
|
2008-01-15 21:33:08 +00:00
|
|
|
$result_array = array();
|
2011-01-04 21:37:18 +00:00
|
|
|
|
|
|
|
|
$result_array['title'] = $titleObj->getPrefixedText();
|
|
|
|
|
|
|
|
|
|
if ( !is_null( $oldid ) ) {
|
|
|
|
|
$result_array['revid'] = intval( $oldid );
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( $params['redirects'] && !is_null( $redirValues ) ) {
|
2008-10-24 12:28:14 +00:00
|
|
|
$result_array['redirects'] = $redirValues;
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
if ( isset( $prop['text'] ) ) {
|
2008-01-15 21:33:08 +00:00
|
|
|
$result_array['text'] = array();
|
2010-01-11 15:55:52 +00:00
|
|
|
$result->setContent( $result_array['text'], $p_result->getText() );
|
2008-01-15 21:33:08 +00:00
|
|
|
}
|
2010-02-23 18:05:46 +00:00
|
|
|
|
2010-01-31 17:45:51 +00:00
|
|
|
if ( !is_null( $params['summary'] ) ) {
|
|
|
|
|
$result_array['parsedsummary'] = array();
|
|
|
|
|
$result->setContent( $result_array['parsedsummary'], $wgUser->getSkin()->formatComment( $params['summary'], $titleObj ) );
|
|
|
|
|
}
|
2010-02-23 18:05:46 +00:00
|
|
|
|
|
|
|
|
if ( isset( $prop['langlinks'] ) ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$result_array['langlinks'] = $this->formatLangLinks( $p_result->getLanguageLinks() );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2010-12-06 18:31:09 +00:00
|
|
|
if ( isset( $prop['languageshtml'] ) ) {
|
|
|
|
|
$languagesHtml = $this->languagesHtml( $p_result->getLanguageLinks() );
|
|
|
|
|
$result_array['languageshtml'] = array();
|
|
|
|
|
$result->setContent( $result_array['languageshtml'], $languagesHtml );
|
|
|
|
|
}
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( isset( $prop['categories'] ) ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2010-12-06 18:31:09 +00:00
|
|
|
if ( isset( $prop['categorieshtml'] ) ) {
|
|
|
|
|
$categoriesHtml = $this->categoriesHtml( $p_result->getCategories() );
|
|
|
|
|
$result_array['categorieshtml'] = array();
|
|
|
|
|
$result->setContent( $result_array['categorieshtml'], $categoriesHtml );
|
|
|
|
|
}
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( isset( $prop['links'] ) ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$result_array['links'] = $this->formatLinks( $p_result->getLinks() );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
if ( isset( $prop['templates'] ) ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
if ( isset( $prop['images'] ) ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$result_array['images'] = array_keys( $p_result->getImages() );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
if ( isset( $prop['externallinks'] ) ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
if ( isset( $prop['sections'] ) ) {
|
2008-01-15 21:33:08 +00:00
|
|
|
$result_array['sections'] = $p_result->getSections();
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2010-05-25 13:47:22 +00:00
|
|
|
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( isset( $prop['displaytitle'] ) ) {
|
2009-01-31 16:35:23 +00:00
|
|
|
$result_array['displaytitle'] = $p_result->getDisplayTitle() ?
|
|
|
|
|
$p_result->getDisplayTitle() :
|
|
|
|
|
$titleObj->getPrefixedText();
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
|
2010-05-24 16:35:23 +00:00
|
|
|
if ( isset( $prop['headitems'] ) || isset( $prop['headhtml'] ) ) {
|
2011-07-22 10:45:07 +00:00
|
|
|
$context = $this->createContext();
|
|
|
|
|
$context->setTitle( $titleObj );
|
2011-06-03 11:04:49 +00:00
|
|
|
$context->getOutput()->addParserOutputNoText( $p_result );
|
2010-05-24 16:35:23 +00:00
|
|
|
|
2011-02-24 23:03:55 +00:00
|
|
|
if ( isset( $prop['headitems'] ) ) {
|
|
|
|
|
$headItems = $this->formatHeadItems( $p_result->getHeadItems() );
|
2010-05-24 16:35:23 +00:00
|
|
|
|
2011-06-03 11:04:49 +00:00
|
|
|
$css = $this->formatCss( $context->getOutput()->buildCssLinksArray() );
|
2010-05-24 16:35:23 +00:00
|
|
|
|
2011-07-22 10:45:07 +00:00
|
|
|
$scripts = array( $context->getOutput()->getHeadScripts() );
|
2010-05-24 16:35:23 +00:00
|
|
|
|
2011-02-24 23:03:55 +00:00
|
|
|
$result_array['headitems'] = array_merge( $headItems, $css, $scripts );
|
|
|
|
|
}
|
2010-02-23 18:05:46 +00:00
|
|
|
|
2011-02-24 23:03:55 +00:00
|
|
|
if ( isset( $prop['headhtml'] ) ) {
|
|
|
|
|
$result_array['headhtml'] = array();
|
2011-06-03 11:04:49 +00:00
|
|
|
$result->setContent( $result_array['headhtml'], $context->getOutput()->headElement( $context->getSkin() ) );
|
2011-02-24 23:03:55 +00:00
|
|
|
}
|
2010-02-09 20:47:35 +00:00
|
|
|
}
|
2010-07-23 07:33:40 +00:00
|
|
|
|
2010-05-25 13:47:22 +00:00
|
|
|
if ( isset( $prop['iwlinks'] ) ) {
|
|
|
|
|
$result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
|
|
|
|
|
}
|
2011-05-17 08:32:10 +00:00
|
|
|
|
2011-01-28 01:47:08 +00:00
|
|
|
if ( isset( $prop['wikitext'] ) ) {
|
|
|
|
|
$result_array['wikitext'] = array();
|
|
|
|
|
$result->setContent( $result_array['wikitext'], $this->text );
|
|
|
|
|
if ( !is_null( $this->pstText ) ) {
|
|
|
|
|
$result_array['psttext'] = array();
|
|
|
|
|
$result->setContent( $result_array['psttext'], $this->pstText );
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-02-23 18:05:46 +00:00
|
|
|
|
2007-12-01 13:37:02 +00:00
|
|
|
$result_mapping = array(
|
2008-10-24 12:28:14 +00:00
|
|
|
'redirects' => 'r',
|
2007-12-01 13:37:02 +00:00
|
|
|
'langlinks' => 'll',
|
|
|
|
|
'categories' => 'cl',
|
|
|
|
|
'links' => 'pl',
|
|
|
|
|
'templates' => 'tl',
|
|
|
|
|
'images' => 'img',
|
|
|
|
|
'externallinks' => 'el',
|
2010-05-25 13:47:22 +00:00
|
|
|
'iwlinks' => 'iw',
|
2007-12-27 20:14:07 +00:00
|
|
|
'sections' => 's',
|
2010-05-24 16:35:23 +00:00
|
|
|
'headitems' => 'hi',
|
2007-12-01 13:37:02 +00:00
|
|
|
);
|
|
|
|
|
$this->setIndexedTagNames( $result_array, $result_mapping );
|
|
|
|
|
$result->addValue( null, $this->getModuleName(), $result_array );
|
2010-05-10 14:17:21 +00:00
|
|
|
|
2010-03-09 11:52:43 +00:00
|
|
|
if ( !is_null( $oldLang ) ) {
|
2010-04-17 20:58:04 +00:00
|
|
|
$wgLang = $oldLang; // Reset $wgLang to $oldLang
|
2010-03-08 22:32:17 +00:00
|
|
|
}
|
2007-12-01 13:37:02 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2011-01-01 23:01:24 +00:00
|
|
|
/**
|
2011-06-05 16:34:09 +00:00
|
|
|
* @param $articleObj Article
|
|
|
|
|
* @param $titleObj Title
|
|
|
|
|
* @param $popts ParserOptions
|
|
|
|
|
* @param $pageId Int
|
|
|
|
|
* @param $getWikitext Bool
|
2011-01-01 23:01:24 +00:00
|
|
|
* @return ParserOutput
|
|
|
|
|
*/
|
2011-01-28 01:47:08 +00:00
|
|
|
private function getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageId = null, $getWikitext = false ) {
|
2011-01-01 23:01:24 +00:00
|
|
|
if ( $this->section !== false ) {
|
2011-01-06 23:35:59 +00:00
|
|
|
global $wgParser;
|
|
|
|
|
|
2011-01-28 01:47:08 +00:00
|
|
|
$this->text = $this->getSectionText( $articleObj->getRawText(), !is_null ( $pageId )
|
2011-01-01 23:01:24 +00:00
|
|
|
? 'page id ' . $pageId : $titleObj->getText() );
|
|
|
|
|
|
2011-01-28 01:47:08 +00:00
|
|
|
return $wgParser->parse( $this->text, $titleObj, $popts );
|
2011-01-01 23:01:24 +00:00
|
|
|
} else {
|
|
|
|
|
// Try the parser cache first
|
2011-01-28 01:47:08 +00:00
|
|
|
$pout = $articleObj->getParserOutput();
|
|
|
|
|
if ( $getWikitext ) {
|
|
|
|
|
$rev = Revision::newFromTitle( $titleObj );
|
|
|
|
|
if ( $rev ) {
|
|
|
|
|
$this->text = $rev->getText();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $pout;
|
2011-01-01 23:01:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-10 18:27:58 +00:00
|
|
|
private function getSectionText( $text, $what ) {
|
|
|
|
|
global $wgParser;
|
|
|
|
|
$text = $wgParser->getSection( $text, $this->section, false );
|
|
|
|
|
if ( $text === false ) {
|
|
|
|
|
$this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
|
|
|
|
|
}
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-01 13:37:02 +00:00
|
|
|
private function formatLangLinks( $links ) {
|
2007-12-01 13:49:39 +00:00
|
|
|
$result = array();
|
2010-01-11 15:55:52 +00:00
|
|
|
foreach ( $links as $link ) {
|
2007-12-01 13:37:02 +00:00
|
|
|
$entry = array();
|
2009-09-28 18:36:15 +00:00
|
|
|
$bits = explode( ':', $link, 2 );
|
2011-01-28 01:09:47 +00:00
|
|
|
$title = Title::newFromText( $link );
|
2011-05-17 08:32:10 +00:00
|
|
|
|
2007-12-01 13:37:02 +00:00
|
|
|
$entry['lang'] = $bits[0];
|
2011-01-28 01:09:47 +00:00
|
|
|
if ( $title ) {
|
2011-08-03 07:05:21 +00:00
|
|
|
$entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_HTTP );
|
2011-01-28 01:09:47 +00:00
|
|
|
}
|
2007-12-01 13:37:02 +00:00
|
|
|
$this->getResult()->setContent( $entry, $bits[1] );
|
|
|
|
|
$result[] = $entry;
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-12-01 13:37:02 +00:00
|
|
|
private function formatCategoryLinks( $links ) {
|
2007-12-01 13:49:39 +00:00
|
|
|
$result = array();
|
2010-01-11 15:55:52 +00:00
|
|
|
foreach ( $links as $link => $sortkey ) {
|
2007-12-01 13:37:02 +00:00
|
|
|
$entry = array();
|
|
|
|
|
$entry['sortkey'] = $sortkey;
|
|
|
|
|
$this->getResult()->setContent( $entry, $link );
|
|
|
|
|
$result[] = $entry;
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-12-06 18:31:09 +00:00
|
|
|
private function categoriesHtml( $categories ) {
|
2011-07-08 16:18:31 +00:00
|
|
|
$context = $this->createContext();
|
|
|
|
|
$context->getOutput()->addCategoryLinks( $categories );
|
|
|
|
|
return $context->getSkin()->getCategories();
|
2010-12-06 18:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
2011-02-05 20:16:42 +00:00
|
|
|
/**
|
2011-05-17 08:46:29 +00:00
|
|
|
* @deprecated since 1.18 No modern skin generates language links this way, please use language links
|
|
|
|
|
* data to generate your own HTML.
|
2011-02-05 20:16:42 +00:00
|
|
|
*/
|
2010-12-06 18:31:09 +00:00
|
|
|
private function languagesHtml( $languages ) {
|
2011-02-08 22:10:34 +00:00
|
|
|
global $wgContLang, $wgHideInterlanguageLinks;
|
2011-02-05 20:16:42 +00:00
|
|
|
|
|
|
|
|
if ( $wgHideInterlanguageLinks || count( $languages ) == 0 ) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$s = htmlspecialchars( wfMsg( 'otherlanguages' ) . wfMsg( 'colon-separator' ) );
|
|
|
|
|
|
|
|
|
|
$langs = array();
|
|
|
|
|
foreach ( $languages as $l ) {
|
|
|
|
|
$nt = Title::newFromText( $l );
|
|
|
|
|
$text = $wgContLang->getLanguageName( $nt->getInterwiki() );
|
|
|
|
|
|
2011-02-05 23:06:36 +00:00
|
|
|
$langs[] = Html::element( 'a',
|
|
|
|
|
array( 'href' => $nt->getFullURL(), 'title' => $nt->getText(), 'class' => "external" ),
|
|
|
|
|
$text == '' ? $l : $text );
|
2011-02-05 20:16:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$s .= implode( htmlspecialchars( wfMsgExt( 'pipe-separator', 'escapenoentities' ) ), $langs );
|
|
|
|
|
|
|
|
|
|
if ( $wgContLang->isRTL() ) {
|
|
|
|
|
$s = Html::rawElement( 'span', array( 'dir' => "LTR" ), $s );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $s;
|
2010-12-06 18:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
2007-12-01 13:37:02 +00:00
|
|
|
private function formatLinks( $links ) {
|
2007-12-01 13:49:39 +00:00
|
|
|
$result = array();
|
2010-01-11 15:55:52 +00:00
|
|
|
foreach ( $links as $ns => $nslinks ) {
|
|
|
|
|
foreach ( $nslinks as $title => $id ) {
|
2007-12-01 13:37:02 +00:00
|
|
|
$entry = array();
|
|
|
|
|
$entry['ns'] = $ns;
|
|
|
|
|
$this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( $id != 0 ) {
|
2007-12-01 13:37:02 +00:00
|
|
|
$entry['exists'] = '';
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2007-12-01 13:37:02 +00:00
|
|
|
$result[] = $entry;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-05-25 13:47:22 +00:00
|
|
|
private function formatIWLinks( $iw ) {
|
|
|
|
|
$result = array();
|
|
|
|
|
foreach ( $iw as $prefix => $titles ) {
|
2010-11-06 16:11:19 +00:00
|
|
|
foreach ( array_keys( $titles ) as $title ) {
|
2010-05-25 13:47:22 +00:00
|
|
|
$entry = array();
|
|
|
|
|
$entry['prefix'] = $prefix;
|
2010-07-23 07:33:40 +00:00
|
|
|
|
2010-05-25 18:37:55 +00:00
|
|
|
$title = Title::newFromText( "{$prefix}:{$title}" );
|
|
|
|
|
if ( $title ) {
|
2011-08-03 07:05:21 +00:00
|
|
|
$entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_HTTP );
|
2010-05-25 18:37:55 +00:00
|
|
|
}
|
2010-07-23 07:33:40 +00:00
|
|
|
|
2010-05-25 18:37:55 +00:00
|
|
|
$this->getResult()->setContent( $entry, $title->getFullText() );
|
2010-05-25 13:47:22 +00:00
|
|
|
$result[] = $entry;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-16 13:07:58 +00:00
|
|
|
private function formatHeadItems( $headItems ) {
|
|
|
|
|
$result = array();
|
2010-01-23 15:25:14 +00:00
|
|
|
foreach ( $headItems as $tag => $content ) {
|
2010-01-16 13:07:58 +00:00
|
|
|
$entry = array();
|
|
|
|
|
$entry['tag'] = $tag;
|
|
|
|
|
$this->getResult()->setContent( $entry, $content );
|
|
|
|
|
$result[] = $entry;
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-24 16:35:23 +00:00
|
|
|
private function formatCss( $css ) {
|
|
|
|
|
$result = array();
|
|
|
|
|
foreach ( $css as $file => $link ) {
|
|
|
|
|
$entry = array();
|
|
|
|
|
$entry['file'] = $file;
|
|
|
|
|
$this->getResult()->setContent( $entry, $link );
|
|
|
|
|
$result[] = $entry;
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-01 13:37:02 +00:00
|
|
|
private function setIndexedTagNames( &$array, $mapping ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
foreach ( $mapping as $key => $name ) {
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( isset( $array[$key] ) ) {
|
2007-12-01 13:49:39 +00:00
|
|
|
$this->getResult()->setIndexedTagName( $array[$key], $name );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2007-12-01 13:37:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getAllowedParams() {
|
2010-02-23 18:05:46 +00:00
|
|
|
return array(
|
2008-04-14 07:45:50 +00:00
|
|
|
'title' => array(
|
2010-02-23 18:05:46 +00:00
|
|
|
ApiBase::PARAM_DFLT => 'API',
|
2007-12-01 13:37:02 +00:00
|
|
|
),
|
2008-01-15 21:33:08 +00:00
|
|
|
'text' => null,
|
2010-01-31 17:45:51 +00:00
|
|
|
'summary' => null,
|
2008-01-29 14:47:27 +00:00
|
|
|
'page' => null,
|
2011-04-04 20:51:41 +00:00
|
|
|
'pageid' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'integer',
|
|
|
|
|
),
|
2008-10-24 12:28:14 +00:00
|
|
|
'redirects' => false,
|
2011-04-04 20:51:41 +00:00
|
|
|
'oldid' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'integer',
|
|
|
|
|
),
|
2008-01-15 21:33:08 +00:00
|
|
|
'prop' => array(
|
2010-02-23 18:05:46 +00:00
|
|
|
ApiBase::PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle',
|
|
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
|
ApiBase::PARAM_TYPE => array(
|
2008-01-15 21:33:08 +00:00
|
|
|
'text',
|
|
|
|
|
'langlinks',
|
2010-12-06 18:31:09 +00:00
|
|
|
'languageshtml',
|
2008-01-15 21:33:08 +00:00
|
|
|
'categories',
|
2010-12-06 18:31:09 +00:00
|
|
|
'categorieshtml',
|
2008-01-15 21:33:08 +00:00
|
|
|
'links',
|
|
|
|
|
'templates',
|
|
|
|
|
'images',
|
|
|
|
|
'externallinks',
|
2008-03-28 13:27:44 +00:00
|
|
|
'sections',
|
2009-01-31 16:29:15 +00:00
|
|
|
'revid',
|
|
|
|
|
'displaytitle',
|
2010-02-09 20:47:35 +00:00
|
|
|
'headitems',
|
2010-05-25 13:47:22 +00:00
|
|
|
'headhtml',
|
|
|
|
|
'iwlinks',
|
2011-01-28 01:47:08 +00:00
|
|
|
'wikitext',
|
2008-01-15 21:33:08 +00:00
|
|
|
)
|
2008-12-04 15:51:39 +00:00
|
|
|
),
|
|
|
|
|
'pst' => false,
|
|
|
|
|
'onlypst' => false,
|
2010-05-10 18:27:58 +00:00
|
|
|
'uselang' => null,
|
|
|
|
|
'section' => null,
|
2010-08-04 07:44:23 +00:00
|
|
|
'disablepp' => false,
|
2007-12-01 13:37:02 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getParamDescription() {
|
2010-05-11 22:30:18 +00:00
|
|
|
$p = $this->getModulePrefix();
|
2010-02-23 18:05:46 +00:00
|
|
|
return array(
|
2010-05-11 22:30:18 +00:00
|
|
|
'text' => 'Wikitext to parse',
|
|
|
|
|
'summary' => 'Summary to parse',
|
2011-06-17 15:57:00 +00:00
|
|
|
'redirects' => "If the {$p}page or the {$p}pageid parameter is set to a redirect, resolve it",
|
2010-05-11 22:30:18 +00:00
|
|
|
'title' => 'Title of page the text belongs to',
|
|
|
|
|
'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
|
|
|
|
|
'pageid' => "Parse the content of this page. Overrides {$p}page",
|
|
|
|
|
'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
|
2010-06-23 19:36:26 +00:00
|
|
|
'prop' => array(
|
|
|
|
|
'Which pieces of information to get',
|
|
|
|
|
' text - Gives the parsed text of the wikitext',
|
2010-12-14 11:47:13 +00:00
|
|
|
' langlinks - Gives the language links in the parsed wikitext',
|
|
|
|
|
' categories - Gives the categories in the parsed wikitext',
|
|
|
|
|
' categorieshtml - Gives the HTML version of the categories',
|
|
|
|
|
' languageshtml - Gives the HTML version of the language links',
|
2010-06-23 19:36:26 +00:00
|
|
|
' links - Gives the internal links in the parsed wikitext',
|
|
|
|
|
' templates - Gives the templates in the parsed wikitext',
|
|
|
|
|
' images - Gives the images in the parsed wikitext',
|
|
|
|
|
' externallinks - Gives the external links in the parsed wikitext',
|
|
|
|
|
' sections - Gives the sections in the parsed wikitext',
|
2010-12-14 11:47:13 +00:00
|
|
|
' revid - Adds the revision ID of the parsed page',
|
2010-06-23 19:36:26 +00:00
|
|
|
' displaytitle - Adds the title of the parsed wikitext',
|
|
|
|
|
' headitems - Gives items to put in the <head> of the page',
|
|
|
|
|
' headhtml - Gives parsed <head> of the page',
|
|
|
|
|
' iwlinks - Gives interwiki links in the parsed wikitext',
|
2011-01-28 01:47:08 +00:00
|
|
|
' wikitext - Gives the original wikitext that was parsed',
|
2008-01-29 14:47:27 +00:00
|
|
|
),
|
2010-06-23 19:36:26 +00:00
|
|
|
'pst' => array(
|
|
|
|
|
'Do a pre-save transform on the input before parsing it',
|
|
|
|
|
'Ignored if page, pageid or oldid is used'
|
2008-12-04 15:51:39 +00:00
|
|
|
),
|
2010-06-23 19:36:26 +00:00
|
|
|
'onlypst' => array(
|
|
|
|
|
'Do a pre-save transform (PST) on the input, but don\'t parse it',
|
|
|
|
|
'Returns the same wikitext, after a PST has been applied. Ignored if page, pageid or oldid is used'
|
2008-12-04 15:51:39 +00:00
|
|
|
),
|
2010-05-11 22:30:18 +00:00
|
|
|
'uselang' => 'Which language to parse the request in',
|
2010-05-10 18:27:58 +00:00
|
|
|
'section' => 'Only retrieve the content of this section number',
|
2010-08-04 07:44:23 +00:00
|
|
|
'disablepp' => 'Disable the PP Report from the parser output',
|
2007-12-01 13:37:02 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getDescription() {
|
2011-04-25 14:05:57 +00:00
|
|
|
return 'Parses wikitext and returns parser output';
|
2007-12-01 13:37:02 +00:00
|
|
|
}
|
2010-02-23 18:05:46 +00:00
|
|
|
|
2010-02-13 01:05:14 +00:00
|
|
|
public function getPossibleErrors() {
|
|
|
|
|
return array_merge( parent::getPossibleErrors(), array(
|
|
|
|
|
array( 'code' => 'params', 'info' => 'The page parameter cannot be used together with the text and title parameters' ),
|
|
|
|
|
array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
|
|
|
|
|
array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revisions' ),
|
|
|
|
|
array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
|
2010-07-06 13:15:59 +00:00
|
|
|
array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ),
|
2010-05-10 14:17:21 +00:00
|
|
|
array( 'nosuchpageid' ),
|
2011-05-20 17:19:06 +00:00
|
|
|
array( 'invalidtitle', 'title' ),
|
2010-02-13 01:05:14 +00:00
|
|
|
) );
|
|
|
|
|
}
|
2007-12-01 13:37:02 +00:00
|
|
|
|
|
|
|
|
protected function getExamples() {
|
2010-02-23 18:05:46 +00:00
|
|
|
return array(
|
2007-12-01 13:37:02 +00:00
|
|
|
'api.php?action=parse&text={{Project:Sandbox}}'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-17 16:38:24 +00:00
|
|
|
public function getHelpUrls() {
|
2011-07-17 16:51:11 +00:00
|
|
|
return 'http://www.mediawiki.org/wiki/API:Parsing_wikitext#parse';
|
2011-07-17 16:38:24 +00:00
|
|
|
}
|
|
|
|
|
|
2007-12-01 13:37:02 +00:00
|
|
|
public function getVersion() {
|
|
|
|
|
return __CLASS__ . ': $Id$';
|
|
|
|
|
}
|
2010-07-23 07:17:56 +00:00
|
|
|
}
|