Standardised file description headers, added @file
[lhc/web/wiklou.git] / includes / api / ApiQueryAllmessages.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
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 global $wgLang;
47
48 $oldLang = null;
49 if ( !is_null( $params['lang'] ) ) {
50 $oldLang = $wgLang; // Keep $wgLang for restore later
51 $wgLang = Language::factory( $params['lang'] );
52 }
53
54 $prop = array_flip( (array)$params['prop'] );
55
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;
62 } else {
63 $messages_target = $params['messages'];
64 }
65
66 // Filter 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;
73 }
74 }
75 $messages_target = $messages_filtered;
76 }
77
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'] ) {
85 $skip = false;
86 }
87
88 if( $useto && $message > $params['to'] ) {
89 break;
90 }
91
92 if ( !$skip ) {
93 $a = array( 'name' => $message );
94 $args = null;
95 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
96 $args = $params['args'];
97 }
98 // Check if the parser is enabled:
99 if ( $params['enableparser'] ) {
100 $msg = wfMsgExt( $message, array( 'parsemag' ), $args );
101 } elseif ( $args ) {
102 $msgString = wfMsgGetKey( $message, true, false, false );
103 $msg = wfMsgReplaceArgs( $msgString, $args );
104 } else {
105 $msg = wfMsgGetKey( $message, true, false, false );
106 }
107
108 if ( wfEmptyMsg( $message, $msg ) ) {
109 $a['missing'] = '';
110 } else {
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'] = '';
117 } else {
118 $a['default'] = $default;
119 }
120 }
121 }
122 }
123 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
124 if ( !$fit ) {
125 $this->setContinueEnumParameter( 'from', $name );
126 break;
127 }
128 }
129 }
130 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
131
132 if ( !is_null( $oldLang ) ) {
133 $wgLang = $oldLang; // Restore $oldLang
134 }
135 }
136
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';
144 } else {
145 // OK to cache
146 return 'public';
147 }
148 }
149
150 public function getAllowedParams() {
151 return array(
152 'messages' => array(
153 ApiBase::PARAM_DFLT => '*',
154 ApiBase::PARAM_ISMULTI => true,
155 ),
156 'prop' => array(
157 ApiBase::PARAM_ISMULTI => true,
158 ApiBase::PARAM_TYPE => array(
159 'default'
160 )
161 ),
162 'enableparser' => false,
163 'args' => array(
164 ApiBase::PARAM_ISMULTI => true
165 ),
166 'filter' => array(),
167 'lang' => null,
168 'from' => null,
169 'to' => null,
170 );
171 }
172
173 public function getParamDescription() {
174 return array(
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',
184 );
185 }
186
187 public function getDescription() {
188 return 'Return messages from this site';
189 }
190
191 protected function getExamples() {
192 return array(
193 'api.php?action=query&meta=allmessages&amfilter=ipb-',
194 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
195 );
196 }
197
198 public function getVersion() {
199 return __CLASS__ . ': $Id$';
200 }
201 }