]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryAllmessages.php
MediaWiki 1.17.4
[autoinstalls/mediawiki.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                 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                 $skip = !is_null( $params['from'] );
79                 $useto = !is_null( $params['to'] );
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( $useto && $message > $params['to'] ) {
88                                 break;
89                         }
90
91                         if ( !$skip ) {
92                                 $a = array( 'name' => $message );
93                                 $args = null;
94                                 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
95                                         $args = $params['args'];
96                                 }
97                                 // Check if the parser is enabled:
98                                 if ( $params['enableparser'] ) {
99                                         $msg = wfMsgExt( $message, array( 'parsemag' ), $args );
100                                 } elseif ( $args ) {
101                                         $msgString = wfMsgGetKey( $message, true, false, false );
102                                         $msg = wfMsgReplaceArgs( $msgString, $args );
103                                 } else {
104                                         $msg = wfMsgGetKey( $message, true, false, false );
105                                 }
106
107                                 if ( wfEmptyMsg( $message, $msg ) ) {
108                                         $a['missing'] = '';
109                                 } else {
110                                         ApiResult::setContent( $a, $msg );
111                                         if ( isset( $prop['default'] ) ) {
112                                                 $default = wfMsgGetKey( $message, false, false, false );
113                                                 if ( $default !== $msg ) {
114                                                         if ( wfEmptyMsg( $message, $default ) ) {
115                                                                 $a['defaultmissing'] = '';
116                                                         } else {
117                                                                 $a['default'] = $default;
118                                                         }
119                                                 }
120                                         }
121                                 }
122                                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
123                                 if ( !$fit ) {
124                                         $this->setContinueEnumParameter( 'from', $message );
125                                         break;
126                                 }
127                         }
128                 }
129                 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
130
131                 if ( !is_null( $oldLang ) ) {
132                         $wgLang = $oldLang; // Restore $oldLang
133                 }
134         }
135
136         public function getCacheMode( $params ) {
137                 if ( is_null( $params['lang'] ) ) {
138                         // Language not specified, will be fetched from preferences
139                         return 'anon-public-user-private';
140                 } elseif ( $params['enableparser'] ) {
141                         // User-specific parser options will be used
142                         return 'anon-public-user-private';
143                 } else {
144                         // OK to cache
145                         return 'public';
146                 }
147         }
148
149         public function getAllowedParams() {
150                 return array(
151                         'messages' => array(
152                                 ApiBase::PARAM_DFLT => '*',
153                                 ApiBase::PARAM_ISMULTI => true,
154                         ),
155                         'prop' => array(
156                                 ApiBase::PARAM_ISMULTI => true,
157                                 ApiBase::PARAM_TYPE => array(
158                                         'default'
159                                 )
160                         ),
161                         'enableparser' => false,
162                         'args' => array(
163                                 ApiBase::PARAM_ISMULTI => true
164                         ),
165                         'filter' => array(),
166                         'lang' => null,
167                         'from' => null,
168                         'to' => null,
169                 );
170         }
171
172         public function getParamDescription() {
173                 return array(
174                         'messages' => 'Which messages to output. "*" means all messages',
175                         'prop' => 'Which properties to get',
176                         'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
177                                                           'Will substitute magic words, handle templates etc.' ),
178                         'args' => 'Arguments to be substituted into message',
179                         'filter' => 'Return only messages that contain this string',
180                         'lang' => 'Return messages in this language',
181                         'from' => 'Return messages starting at this message',
182                         'to' => 'Return messages ending at this message',
183                 );
184         }
185
186         public function getDescription() {
187                 return 'Return messages from this site';
188         }
189
190         protected function getExamples() {
191                 return array(
192                         'api.php?action=query&meta=allmessages&amfilter=ipb-',
193                         'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
194                 );
195         }
196
197         public function getVersion() {
198                 return __CLASS__ . ': $Id$';
199         }
200 }