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