4 * Created on Dec 1, 2007
6 * API for MediaWiki 1.8+
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
32 * A query action to return messages from site message cache
36 class ApiQueryAllmessages
extends ApiQueryBase
{
38 public function __construct( $query, $moduleName ) {
39 parent
::__construct( $query, $moduleName, 'am' );
42 public function execute() {
43 $params = $this->extractRequestParams();
48 if ( !is_null( $params['lang'] ) && $params['lang'] != $wgLang->getCode() ) {
49 $oldLang = $wgLang; // Keep $wgLang for restore later
50 $wgLang = Language
::factory( $params['lang'] );
51 } else if ( is_null( $params['lang'] ) ) {
52 // Language not determined by URL but by user preferences, so don't cache
53 $this->getMain()->setVaryCookie();
56 $prop = array_flip( (array)$params['prop'] );
58 // Determine which messages should we print
59 $messages_target = array();
60 if ( in_array( '*', $params['messages'] ) ) {
61 $message_names = array_keys( Language
::getMessagesFor( 'en' ) );
62 sort( $message_names );
63 $messages_target = $message_names;
65 $messages_target = $params['messages'];
69 if ( isset( $params['filter'] ) ) {
70 $messages_filtered = array();
71 foreach ( $messages_target as $message ) {
72 // !== is used because filter can be at the beginning of the string
73 if ( strpos( $message, $params['filter'] ) !== false ) {
74 $messages_filtered[] = $message;
77 $messages_target = $messages_filtered;
80 // Get all requested messages and print the result
81 $skip = !is_null( $params['from'] );
82 $result = $this->getResult();
83 foreach ( $messages_target as $message ) {
84 // Skip all messages up to $params['from']
85 if ( $skip && $message === $params['from'] ) {
90 $a = array( 'name' => $message );
92 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
93 $args = $params['args'];
95 // Check if the parser is enabled:
96 if ( $params['enableparser'] ) {
97 $msg = wfMsgExt( $message, array( 'parsemag' ), $args );
99 $msgString = wfMsgGetKey( $message, true, false, false );
100 $msg = wfMsgReplaceArgs( $msgString, $args );
102 $msg = wfMsgGetKey( $message, true, false, false );
105 if ( wfEmptyMsg( $message, $msg ) ) {
108 ApiResult
::setContent( $a, $msg );
109 if ( isset( $prop['default'] ) ) {
110 $default = wfMsgGetKey( $message, false, false, false );
111 if ( $default !== $msg ) {
112 if ( wfEmptyMsg( $message, $default ) ) {
113 $a['defaultmissing'] = '';
115 $a['default'] = $default;
120 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
122 $this->setContinueEnumParameter( 'from', $name );
127 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
129 if ( !is_null( $oldLang ) ) {
130 $wgLang = $oldLang; // Restore $oldLang
134 public function getAllowedParams() {
137 ApiBase
::PARAM_DFLT
=> '*',
138 ApiBase
::PARAM_ISMULTI
=> true,
141 ApiBase
::PARAM_ISMULTI
=> true,
142 ApiBase
::PARAM_TYPE
=> array(
146 'enableparser' => false,
148 ApiBase
::PARAM_ISMULTI
=> true
156 public function getParamDescription() {
158 'messages' => 'Which messages to output. "*" means all messages',
159 'prop' => 'Which properties to get',
160 'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
161 'Will substitute magic words, handle templates etc.' ),
162 'args' => 'Arguments to be substituted into message',
163 'filter' => 'Return only messages that contain this string',
164 'lang' => 'Return messages in this language',
165 'from' => 'Return messages starting at this message',
169 public function getDescription() {
170 return 'Return messages from this site';
173 protected function getExamples() {
175 'api.php?action=query&meta=allmessages&amfilter=ipb-',
176 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
180 public function getVersion() {
181 return __CLASS__
. ': $Id$';