* (bug 27182) API: Add filter by prefix for meta=allmessages
[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 if ( $params['enableparser'] ) {
54 if ( !is_null( $params['title'] ) ) {
55 $title = Title::newFromText( $params['title'] );
56 if ( !$title ) {
57 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
58 }
59 } else {
60 $title = Title::newFromText( 'API' );
61 }
62 }
63
64 $prop = array_flip( (array)$params['prop'] );
65
66 // Determine which messages should we print
67 if ( in_array( '*', $params['messages'] ) ) {
68 $message_names = array_keys( Language::getMessagesFor( 'en' ) );
69 sort( $message_names );
70 $messages_target = $message_names;
71 } else {
72 $messages_target = $params['messages'];
73 }
74
75 // Filter messages that have the specified prefix
76 // Because we sorted the message array earlier, they will appear in a clump:
77 if ( isset( $params['prefix'] ) ) {
78 $skip = false;
79 $messages_filtered = array();
80 foreach ( $messages_target as $message ) {
81 // === 0: must be at beginning of string (position 0)
82 if ( strpos( $message, $params['prefix'] ) === 0 ) {
83 if( !$skip ) {
84 $skip = true;
85 }
86 $messages_filtered[] = $message;
87 } else if ( $skip ) {
88 break;
89 }
90 }
91 $messages_target = $messages_filtered;
92 }
93
94 // Filter messages that contain specified string
95 if ( isset( $params['filter'] ) ) {
96 $messages_filtered = array();
97 foreach ( $messages_target as $message ) {
98 // !== is used because filter can be at the beginning of the string
99 if ( strpos( $message, $params['filter'] ) !== false ) {
100 $messages_filtered[] = $message;
101 }
102 }
103 $messages_target = $messages_filtered;
104 }
105
106 // Get all requested messages and print the result
107 $skip = !is_null( $params['from'] );
108 $useto = !is_null( $params['to'] );
109 $result = $this->getResult();
110 foreach ( $messages_target as $message ) {
111 // Skip all messages up to $params['from']
112 if ( $skip && $message === $params['from'] ) {
113 $skip = false;
114 }
115
116 if ( $useto && $message > $params['to'] ) {
117 break;
118 }
119
120 if ( !$skip ) {
121 $a = array( 'name' => $message );
122 $args = array();
123 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
124 $args = $params['args'];
125 }
126
127 $msg = wfMessage( $message, $args )->inLanguage( $langObj );
128
129 if ( !$msg->exists() ) {
130 $a['missing'] = '';
131 } else {
132 // Check if the parser is enabled:
133 if ( $params['enableparser'] ) {
134 $msgString = $msg->title( $title )->text();
135 } else {
136 $msgString = $msg->plain();
137 }
138 ApiResult::setContent( $a, $msgString );
139 if ( isset( $prop['default'] ) ) {
140 $default = wfMessage( $message )->inLanguage( $langObj )->useDatabase( false );
141 if ( !$default->exists() ) {
142 $a['defaultmissing'] = '';
143 } elseif ( $default->plain() != $msgString ) {
144 $a['default'] = $default->plain();
145 }
146 }
147 }
148 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
149 if ( !$fit ) {
150 $this->setContinueEnumParameter( 'from', $message );
151 break;
152 }
153 }
154 }
155 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
156 }
157
158 public function getCacheMode( $params ) {
159 if ( is_null( $params['lang'] ) ) {
160 // Language not specified, will be fetched from preferences
161 return 'anon-public-user-private';
162 } elseif ( $params['enableparser'] ) {
163 // User-specific parser options will be used
164 return 'anon-public-user-private';
165 } else {
166 // OK to cache
167 return 'public';
168 }
169 }
170
171 public function getAllowedParams() {
172 return array(
173 'messages' => array(
174 ApiBase::PARAM_DFLT => '*',
175 ApiBase::PARAM_ISMULTI => true,
176 ),
177 'prop' => array(
178 ApiBase::PARAM_ISMULTI => true,
179 ApiBase::PARAM_TYPE => array(
180 'default'
181 )
182 ),
183 'enableparser' => false,
184 'args' => array(
185 ApiBase::PARAM_ISMULTI => true
186 ),
187 'filter' => array(),
188 'lang' => null,
189 'from' => null,
190 'to' => null,
191 'title' => null,
192 'prefix' => null,
193 );
194 }
195
196 public function getParamDescription() {
197 return array(
198 'messages' => 'Which messages to output. "*" (default) means all messages',
199 'prop' => 'Which properties to get',
200 'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
201 'Will substitute magic words, handle templates etc.' ),
202 'title' => 'Page name to use as context when parsing message (for enableparser option)',
203 'args' => 'Arguments to be substituted into message',
204 'prefix' => 'Return messages with this prefix',
205 'filter' => 'Return only messages with names that contain this string',
206 'lang' => 'Return messages in this language',
207 'from' => 'Return messages starting at this message',
208 'to' => 'Return messages ending at this message',
209 );
210 }
211
212 public function getDescription() {
213 return 'Return messages from this site';
214 }
215
216 protected function getExamples() {
217 return array(
218 'api.php?action=query&meta=allmessages&amprefix=ipb-',
219 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
220 );
221 }
222
223 public function getVersion() {
224 return __CLASS__ . ': $Id$';
225 }
226 }