Big blitz on unused variables (a lot of $db = $this->getDb() )
[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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 global $wgLang;
46
47 $oldLang = null;
48 if ( !is_null( $params['lang'] ) && $params['lang'] != $wgLang->getCode() ) {
49 $oldLang = $wgLang; // Keep $wgLang for restore later
50 $wgLang = Language::factory( $params['lang'] );
51 } else if ( is_null( $params['lang'] ) ) {
52 // Language not determined by URL but by user preferences, so don't cache
53 $this->getMain()->setVaryCookie();
54 }
55
56 $prop = array_flip( (array)$params['prop'] );
57
58 // Determine which messages should we print
59 $messages_target = array();
60 if ( in_array( '*', $params['messages'] ) ) {
61 $message_names = array_keys( Language::getMessagesFor( 'en' ) );
62 sort( $message_names );
63 $messages_target = $message_names;
64 } else {
65 $messages_target = $params['messages'];
66 }
67
68 // Filter messages
69 if ( isset( $params['filter'] ) ) {
70 $messages_filtered = array();
71 foreach ( $messages_target as $message ) {
72 // !== is used because filter can be at the beginning of the string
73 if ( strpos( $message, $params['filter'] ) !== false ) {
74 $messages_filtered[] = $message;
75 }
76 }
77 $messages_target = $messages_filtered;
78 }
79
80 // Get all requested messages and print the result
81 $skip = !is_null( $params['from'] );
82 $result = $this->getResult();
83 foreach ( $messages_target as $message ) {
84 // Skip all messages up to $params['from']
85 if ( $skip && $message === $params['from'] ) {
86 $skip = false;
87 }
88
89 if ( !$skip ) {
90 $a = array( 'name' => $message );
91 $args = null;
92 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
93 $args = $params['args'];
94 }
95 // Check if the parser is enabled:
96 if ( $params['enableparser'] ) {
97 $msg = wfMsgExt( $message, array( 'parsemag' ), $args );
98 } elseif ( $args ) {
99 $msgString = wfMsgGetKey( $message, true, false, false );
100 $msg = wfMsgReplaceArgs( $msgString, $args );
101 } else {
102 $msg = wfMsgGetKey( $message, true, false, false );
103 }
104
105 if ( wfEmptyMsg( $message, $msg ) ) {
106 $a['missing'] = '';
107 } else {
108 ApiResult::setContent( $a, $msg );
109 if ( isset( $prop['default'] ) ) {
110 $default = wfMsgGetKey( $message, false, false, false );
111 if ( $default !== $msg ) {
112 if ( wfEmptyMsg( $message, $default ) ) {
113 $a['defaultmissing'] = '';
114 } else {
115 $a['default'] = $default;
116 }
117 }
118 }
119 }
120 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
121 if ( !$fit ) {
122 $this->setContinueEnumParameter( 'from', $name );
123 break;
124 }
125 }
126 }
127 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
128
129 if ( !is_null( $oldLang ) ) {
130 $wgLang = $oldLang; // Restore $oldLang
131 }
132 }
133
134 public function getAllowedParams() {
135 return array(
136 'messages' => array(
137 ApiBase::PARAM_DFLT => '*',
138 ApiBase::PARAM_ISMULTI => true,
139 ),
140 'prop' => array(
141 ApiBase::PARAM_ISMULTI => true,
142 ApiBase::PARAM_TYPE => array(
143 'default'
144 )
145 ),
146 'enableparser' => false,
147 'args' => array(
148 ApiBase::PARAM_ISMULTI => true
149 ),
150 'filter' => array(),
151 'lang' => null,
152 'from' => null,
153 );
154 }
155
156 public function getParamDescription() {
157 return array(
158 'messages' => 'Which messages to output. "*" means all messages',
159 'prop' => 'Which properties to get',
160 'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
161 'Will substitute magic words, handle templates etc.' ),
162 'args' => 'Arguments to be substituted into message',
163 'filter' => 'Return only messages that contain this string',
164 'lang' => 'Return messages in this language',
165 'from' => 'Return messages starting at this message',
166 );
167 }
168
169 public function getDescription() {
170 return 'Return messages from this site';
171 }
172
173 protected function getExamples() {
174 return array(
175 'api.php?action=query&meta=allmessages&amfilter=ipb-',
176 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
177 );
178 }
179
180 public function getVersion() {
181 return __CLASS__ . ': $Id$';
182 }
183 }