]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryAllmessages.php
MediaWiki 1.16.0
[autoinstalls/mediawiki.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 (C) 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                 {
47                         global $wgLang;
48                         $wgLang = Language::factory( $params['lang'] );
49                 }
50                 
51                 $prop = array_flip( (array)$params['prop'] );
52
53                 // Determine which messages should we print
54                 $messages_target = array();
55                 if ( in_array( '*', $params['messages'] ) ) {
56                         $message_names = array_keys( Language::getMessagesFor( 'en' ) );
57                         sort( $message_names );
58                         $messages_target = $message_names;
59                 } else {
60                         $messages_target = $params['messages'];
61                 }
62
63                 // Filter messages
64                 if ( isset( $params['filter'] ) ) {
65                         $messages_filtered = array();
66                         foreach ( $messages_target as $message ) {
67                                 if ( strpos( $message, $params['filter'] ) !== false ) {        // !== is used because filter can be at the beginnig of the string
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                         if ( !$skip ) {
84                                 $a = array( 'name' => $message );
85                                 $args = null;
86                                 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
87                                         $args = $params['args'];
88                                 }
89                                 // Check if the parser is enabled:
90                                 if ( $params['enableparser'] ) {
91                                         $msg = wfMsgExt( $message, array( 'parsemag' ), $args );
92                                 } else if ( $args ) {
93                                         $msgString = wfMsgGetKey( $message, true, false, false );
94                                         $msg = wfMsgReplaceArgs( $msgString, $args );
95                                 } else {
96                                         $msg = wfMsgGetKey( $message, true, false, false );
97                                 }
98
99                                 if ( wfEmptyMsg( $message, $msg ) )
100                                         $a['missing'] = '';
101                                 else {
102                                         ApiResult::setContent( $a, $msg );
103                                         if ( isset( $prop['default'] ) ) {
104                                                 $default = wfMsgGetKey( $message, false, false, false );
105                                                 if ( $default !== $msg ) {
106                                                         if ( wfEmptyMsg( $message, $default ) )
107                                                                 $a['defaultmissing'] = '';
108                                                         else
109                                                                 $a['default'] = $default;
110                                                 }
111                                         }
112                                 }
113                                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
114                                 if ( !$fit ) {
115                                         $this->setContinueEnumParameter( 'from', $name );
116                                         break;
117                                 }
118                         }
119                 }
120                 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
121         }
122
123         public function getCacheMode( $params ) {
124                 if ( is_null( $params['lang'] ) ) {
125                         // Language not specified, will be fetched from preferences
126                         return 'anon-public-user-private';
127                 } elseif ( $params['enableparser'] ) {
128                         // User-specific parser options will be used
129                         return 'anon-public-user-private';
130                 } else {
131                         // OK to cache
132                         return 'public';
133                 }
134         }
135
136         public function getAllowedParams() {
137                 return array (
138                         'messages' => array (
139                                 ApiBase :: PARAM_DFLT => '*',
140                                 ApiBase :: PARAM_ISMULTI => true,
141                         ),
142                         'prop' => array(
143                                 ApiBase :: PARAM_ISMULTI => true,
144                                 ApiBase :: PARAM_TYPE => array(
145                                         'default'
146                                 )
147                         ),
148                         'enableparser' => false,
149                         'args' => array(
150                                 ApiBase :: PARAM_ISMULTI => true
151                         ),
152                         'filter' => array(),
153                         'lang' => null,
154                         'from' => null,
155                 );
156         }
157
158         public function getParamDescription() {
159                 return array (
160                         'messages' => 'Which messages to output. "*" means all messages',
161                         'prop' => 'Which properties to get',
162                         'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
163                                                           'Will substitute magic words, handle templates etc.' ),
164                         'args' => 'Arguments to be substituted into message',
165                         'filter' => 'Return only messages that contain this string',
166                         'lang' => 'Return messages in this language',
167                         'from' => 'Return messages starting at this message',
168                 );
169         }
170
171         public function getDescription() {
172                 return 'Return messages from this site.';
173         }
174
175         protected function getExamples() {
176                 return array(
177                         'api.php?action=query&meta=allmessages&amfilter=ipb-',
178                         'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
179                         );
180         }
181
182         public function getVersion() {
183                 return __CLASS__ . ': $Id: ApiQueryAllmessages.php 69932 2010-07-26 08:03:21Z tstarling $';
184         }
185 }