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
82 $skip = !is_null( $params['from'] );
83 $result = $this->getResult();
84 foreach ( $messages_target as $message ) {
85 // Skip all messages up to $params['from']
86 if ( $skip && $message === $params['from'] ) {
91 $a = array( 'name' => $message );
93 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
94 $args = $params['args'];
96 // Check if the parser is enabled:
97 if ( $params['enableparser'] ) {
98 $msg = wfMsgExt( $message, array( 'parsemag' ), $args );
100 $msgString = wfMsgGetKey( $message, true, false, false );
101 $msg = wfMsgReplaceArgs( $msgString, $args );
103 $msg = wfMsgGetKey( $message, true, false, false );
106 if ( wfEmptyMsg( $message, $msg ) ) {
109 ApiResult
::setContent( $a, $msg );
110 if ( isset( $prop['default'] ) ) {
111 $default = wfMsgGetKey( $message, false, false, false );
112 if ( $default !== $msg ) {
113 if ( wfEmptyMsg( $message, $default ) ) {
114 $a['defaultmissing'] = '';
116 $a['default'] = $default;
121 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
123 $this->setContinueEnumParameter( 'from', $name );
128 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
130 if ( !is_null( $oldLang ) ) {
131 $wgLang = $oldLang; // Restore $oldLang
135 public function getAllowedParams() {
138 ApiBase
::PARAM_DFLT
=> '*',
139 ApiBase
::PARAM_ISMULTI
=> true,
142 ApiBase
::PARAM_ISMULTI
=> true,
143 ApiBase
::PARAM_TYPE
=> array(
147 'enableparser' => false,
149 ApiBase
::PARAM_ISMULTI
=> true
157 public function getParamDescription() {
159 'messages' => 'Which messages to output. "*" means all messages',
160 'prop' => 'Which properties to get',
161 'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
162 'Will substitute magic words, handle templates etc.' ),
163 'args' => 'Arguments to be substituted into message',
164 'filter' => 'Return only messages that contain this string',
165 'lang' => 'Return messages in this language',
166 'from' => 'Return messages starting at this message',
170 public function getDescription() {
171 return 'Return messages from this site';
174 protected function getExamples() {
176 'api.php?action=query&meta=allmessages&amfilter=ipb-',
177 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
181 public function getVersion() {
182 return __CLASS__
. ': $Id$';