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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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'] );
53 $prop = array_flip( (array)$params['prop'] );
55 // Determine which messages should we print
56 $messages_target = array();
57 if ( in_array( '*', $params['messages'] ) ) {
58 $message_names = array_keys( Language
::getMessagesFor( 'en' ) );
59 sort( $message_names );
60 $messages_target = $message_names;
62 $messages_target = $params['messages'];
66 if ( isset( $params['filter'] ) ) {
67 $messages_filtered = array();
68 foreach ( $messages_target as $message ) {
69 // !== is used because filter can be at the beginning of the string
70 if ( strpos( $message, $params['filter'] ) !== false ) {
71 $messages_filtered[] = $message;
74 $messages_target = $messages_filtered;
77 // Get all requested messages and print the result
79 $skip = !is_null( $params['from'] );
80 $result = $this->getResult();
81 foreach ( $messages_target as $message ) {
82 // Skip all messages up to $params['from']
83 if ( $skip && $message === $params['from'] ) {
88 $a = array( 'name' => $message );
90 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
91 $args = $params['args'];
93 // Check if the parser is enabled:
94 if ( $params['enableparser'] ) {
95 $msg = wfMsgExt( $message, array( 'parsemag' ), $args );
97 $msgString = wfMsgGetKey( $message, true, false, false );
98 $msg = wfMsgReplaceArgs( $msgString, $args );
100 $msg = wfMsgGetKey( $message, true, false, false );
103 if ( wfEmptyMsg( $message, $msg ) ) {
106 ApiResult
::setContent( $a, $msg );
107 if ( isset( $prop['default'] ) ) {
108 $default = wfMsgGetKey( $message, false, false, false );
109 if ( $default !== $msg ) {
110 if ( wfEmptyMsg( $message, $default ) ) {
111 $a['defaultmissing'] = '';
113 $a['default'] = $default;
118 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
120 $this->setContinueEnumParameter( 'from', $name );
125 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
127 if ( !is_null( $oldLang ) ) {
128 $wgLang = $oldLang; // Restore $oldLang
132 public function getAllowedParams() {
135 ApiBase
::PARAM_DFLT
=> '*',
136 ApiBase
::PARAM_ISMULTI
=> true,
139 ApiBase
::PARAM_ISMULTI
=> true,
140 ApiBase
::PARAM_TYPE
=> array(
144 'enableparser' => false,
146 ApiBase
::PARAM_ISMULTI
=> true
154 public function getParamDescription() {
156 'messages' => 'Which messages to output. "*" means all messages',
157 'prop' => 'Which properties to get',
158 'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
159 'Will substitute magic words, handle templates etc.' ),
160 'args' => 'Arguments to be substituted into message',
161 'filter' => 'Return only messages that contain this string',
162 'lang' => 'Return messages in this language',
163 'from' => 'Return messages starting at this message',
167 public function getDescription() {
168 return 'Return messages from this site';
171 protected function getExamples() {
173 'api.php?action=query&meta=allmessages&amfilter=ipb-',
174 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
178 public function getVersion() {
179 return __CLASS__
. ': $Id$';