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