* Use wfMessage() instead of wfMsgGetKey()
[lhc/web/wiklou.git] / includes / api / ApiQueryAllmessages.php
1 <?php
2 /**
3 *
4 *
5 * Created on Dec 1, 2007
6 *
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * A query action to return messages from site message cache
34 *
35 * @ingroup API
36 */
37 class ApiQueryAllmessages extends ApiQueryBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'am' );
41 }
42
43 public function execute() {
44 $params = $this->extractRequestParams();
45
46 if ( is_null( $params['lang'] ) ) {
47 global $wgLang;
48 $langObj = $wgLang;
49 } else {
50 $langObj = Language::factory( $params['lang'] );
51 }
52
53 $prop = array_flip( (array)$params['prop'] );
54
55 // Determine which messages should we print
56 if ( in_array( '*', $params['messages'] ) ) {
57 $message_names = array_keys( Language::getMessagesFor( 'en' ) );
58 sort( $message_names );
59 $messages_target = $message_names;
60 } else {
61 $messages_target = $params['messages'];
62 }
63
64 // Filter messages
65 if ( isset( $params['filter'] ) ) {
66 $messages_filtered = array();
67 foreach ( $messages_target as $message ) {
68 // !== is used because filter can be at the beginning of the string
69 if ( strpos( $message, $params['filter'] ) !== false ) {
70 $messages_filtered[] = $message;
71 }
72 }
73 $messages_target = $messages_filtered;
74 }
75
76 // Get all requested messages and print the result
77 $skip = !is_null( $params['from'] );
78 $useto = !is_null( $params['to'] );
79 $result = $this->getResult();
80 foreach ( $messages_target as $message ) {
81 // Skip all messages up to $params['from']
82 if ( $skip && $message === $params['from'] ) {
83 $skip = false;
84 }
85
86 if ( $useto && $message > $params['to'] ) {
87 break;
88 }
89
90 if ( !$skip ) {
91 $a = array( 'name' => $message );
92 $args = array();
93 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
94 $args = $params['args'];
95 }
96
97 $msg = wfMessage( $message, $args )->inLanguage( $langObj );
98
99 if ( !$msg->exists() ) {
100 $a['missing'] = '';
101 } else {
102 // Check if the parser is enabled:
103 if ( $params['enableparser'] ) {
104 $msgString = $msg->text();
105 } else {
106 $msgString = $msg->plain();
107 }
108 ApiResult::setContent( $a, $msgString );
109 if ( isset( $prop['default'] ) ) {
110 $default = wfMessage( $message )->inLanguage( $langObj )->useDatabase( false );
111 if ( !$default->exists() ) {
112 $a['defaultmissing'] = '';
113 } elseif ( $default->plain() != $msgString ) {
114 $a['default'] = $default->plain();
115 }
116 }
117 }
118 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
119 if ( !$fit ) {
120 $this->setContinueEnumParameter( 'from', $message );
121 break;
122 }
123 }
124 }
125 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
126 }
127
128 public function getCacheMode( $params ) {
129 if ( is_null( $params['lang'] ) ) {
130 // Language not specified, will be fetched from preferences
131 return 'anon-public-user-private';
132 } elseif ( $params['enableparser'] ) {
133 // User-specific parser options will be used
134 return 'anon-public-user-private';
135 } else {
136 // OK to cache
137 return 'public';
138 }
139 }
140
141 public function getAllowedParams() {
142 return array(
143 'messages' => array(
144 ApiBase::PARAM_DFLT => '*',
145 ApiBase::PARAM_ISMULTI => true,
146 ),
147 'prop' => array(
148 ApiBase::PARAM_ISMULTI => true,
149 ApiBase::PARAM_TYPE => array(
150 'default'
151 )
152 ),
153 'enableparser' => false,
154 'args' => array(
155 ApiBase::PARAM_ISMULTI => true
156 ),
157 'filter' => array(),
158 'lang' => null,
159 'from' => null,
160 'to' => null,
161 );
162 }
163
164 public function getParamDescription() {
165 return array(
166 'messages' => 'Which messages to output. "*" means all messages',
167 'prop' => 'Which properties to get',
168 'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
169 'Will substitute magic words, handle templates etc.' ),
170 'args' => 'Arguments to be substituted into message',
171 'filter' => 'Return only messages that contain this string',
172 'lang' => 'Return messages in this language',
173 'from' => 'Return messages starting at this message',
174 'to' => 'Return messages ending at this message',
175 );
176 }
177
178 public function getDescription() {
179 return 'Return messages from this site';
180 }
181
182 protected function getExamples() {
183 return array(
184 'api.php?action=query&meta=allmessages&amfilter=ipb-',
185 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
186 );
187 }
188
189 public function getVersion() {
190 return __CLASS__ . ': $Id$';
191 }
192 }