3 * API for MediaWiki 1.8+
5 * Created on Dec 1, 2007
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * A query action to return messages from site message cache
37 class ApiQueryAllmessages
extends ApiQueryBase
{
39 public function __construct( $query, $moduleName ) {
40 parent
::__construct( $query, $moduleName, 'am' );
43 public function execute() {
44 $params = $this->extractRequestParams();
49 if ( !is_null( $params['lang'] ) ) {
50 $oldLang = $wgLang; // Keep $wgLang for restore later
51 $wgLang = Language
::factory( $params['lang'] );
54 $prop = array_flip( (array)$params['prop'] );
56 // Determine which messages should we print
57 $messages_target = array();
58 if ( in_array( '*', $params['messages'] ) ) {
59 $message_names = array_keys( Language
::getMessagesFor( 'en' ) );
60 sort( $message_names );
61 $messages_target = $message_names;
63 $messages_target = $params['messages'];
67 if ( isset( $params['filter'] ) ) {
68 $messages_filtered = array();
69 foreach ( $messages_target as $message ) {
70 // !== is used because filter can be at the beginning of the string
71 if ( strpos( $message, $params['filter'] ) !== false ) {
72 $messages_filtered[] = $message;
75 $messages_target = $messages_filtered;
78 // Get all requested messages and print the result
79 $skip = !is_null( $params['from'] );
80 $useto = !is_null( $params['to'] );
81 $result = $this->getResult();
82 foreach ( $messages_target as $message ) {
83 // Skip all messages up to $params['from']
84 if ( $skip && $message === $params['from'] ) {
88 if( $useto && $message > $params['to'] ) {
93 $a = array( 'name' => $message );
95 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
96 $args = $params['args'];
98 // Check if the parser is enabled:
99 if ( $params['enableparser'] ) {
100 $msg = wfMsgExt( $message, array( 'parsemag' ), $args );
102 $msgString = wfMsgGetKey( $message, true, false, false );
103 $msg = wfMsgReplaceArgs( $msgString, $args );
105 $msg = wfMsgGetKey( $message, true, false, false );
108 if ( wfEmptyMsg( $message, $msg ) ) {
111 ApiResult
::setContent( $a, $msg );
112 if ( isset( $prop['default'] ) ) {
113 $default = wfMsgGetKey( $message, false, false, false );
114 if ( $default !== $msg ) {
115 if ( wfEmptyMsg( $message, $default ) ) {
116 $a['defaultmissing'] = '';
118 $a['default'] = $default;
123 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
125 $this->setContinueEnumParameter( 'from', $name );
130 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
132 if ( !is_null( $oldLang ) ) {
133 $wgLang = $oldLang; // Restore $oldLang
137 public function getCacheMode( $params ) {
138 if ( is_null( $params['lang'] ) ) {
139 // Language not specified, will be fetched from preferences
140 return 'anon-public-user-private';
141 } elseif ( $params['enableparser'] ) {
142 // User-specific parser options will be used
143 return 'anon-public-user-private';
150 public function getAllowedParams() {
153 ApiBase
::PARAM_DFLT
=> '*',
154 ApiBase
::PARAM_ISMULTI
=> true,
157 ApiBase
::PARAM_ISMULTI
=> true,
158 ApiBase
::PARAM_TYPE
=> array(
162 'enableparser' => false,
164 ApiBase
::PARAM_ISMULTI
=> true
173 public function getParamDescription() {
175 'messages' => 'Which messages to output. "*" means all messages',
176 'prop' => 'Which properties to get',
177 'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
178 'Will substitute magic words, handle templates etc.' ),
179 'args' => 'Arguments to be substituted into message',
180 'filter' => 'Return only messages that contain this string',
181 'lang' => 'Return messages in this language',
182 'from' => 'Return messages starting at this message',
183 'to' => 'Return messages ending at this message',
187 public function getDescription() {
188 return 'Return messages from this site';
191 protected function getExamples() {
193 'api.php?action=query&meta=allmessages&amfilter=ipb-',
194 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
198 public function getVersion() {
199 return __CLASS__
. ': $Id$';