[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/api/ -> ApiQueryAllMessages.php (source)

   1  <?php
   2  /**
   3   *
   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  /**
  28   * A query action to return messages from site message cache
  29   *
  30   * @ingroup API
  31   */
  32  class ApiQueryAllMessages extends ApiQueryBase {
  33  
  34  	public function __construct( ApiQuery $query, $moduleName ) {
  35          parent::__construct( $query, $moduleName, 'am' );
  36      }
  37  
  38  	public function execute() {
  39          $params = $this->extractRequestParams();
  40  
  41          if ( is_null( $params['lang'] ) ) {
  42              $langObj = $this->getLanguage();
  43          } elseif ( !Language::isValidCode( $params['lang'] ) ) {
  44              $this->dieUsage( 'Invalid language code for parameter lang', 'invalidlang' );
  45          } else {
  46              $langObj = Language::factory( $params['lang'] );
  47          }
  48  
  49          if ( $params['enableparser'] ) {
  50              if ( !is_null( $params['title'] ) ) {
  51                  $title = Title::newFromText( $params['title'] );
  52                  if ( !$title || $title->isExternal() ) {
  53                      $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
  54                  }
  55              } else {
  56                  $title = Title::newFromText( 'API' );
  57              }
  58          }
  59  
  60          $prop = array_flip( (array)$params['prop'] );
  61  
  62          // Determine which messages should we print
  63          if ( in_array( '*', $params['messages'] ) ) {
  64              $message_names = Language::getMessageKeysFor( $langObj->getCode() );
  65              if ( $params['includelocal'] ) {
  66                  $message_names = array_unique( array_merge(
  67                      $message_names,
  68                      // Pass in the content language code so we get local messages that have a
  69                      // MediaWiki:msgkey page. We might theoretically miss messages that have no
  70                      // MediaWiki:msgkey page but do have a MediaWiki:msgkey/lang page, but that's
  71                      // just a stupid case.
  72                      MessageCache::singleton()->getAllMessageKeys( $this->getConfig()->get( 'LanguageCode' ) )
  73                  ) );
  74              }
  75              sort( $message_names );
  76              $messages_target = $message_names;
  77          } else {
  78              $messages_target = $params['messages'];
  79          }
  80  
  81          // Filter messages that have the specified prefix
  82          // Because we sorted the message array earlier, they will appear in a clump:
  83          if ( isset( $params['prefix'] ) ) {
  84              $skip = false;
  85              $messages_filtered = array();
  86              foreach ( $messages_target as $message ) {
  87                  // === 0: must be at beginning of string (position 0)
  88                  if ( strpos( $message, $params['prefix'] ) === 0 ) {
  89                      if ( !$skip ) {
  90                          $skip = true;
  91                      }
  92                      $messages_filtered[] = $message;
  93                  } elseif ( $skip ) {
  94                      break;
  95                  }
  96              }
  97              $messages_target = $messages_filtered;
  98          }
  99  
 100          // Filter messages that contain specified string
 101          if ( isset( $params['filter'] ) ) {
 102              $messages_filtered = array();
 103              foreach ( $messages_target as $message ) {
 104                  // !== is used because filter can be at the beginning of the string
 105                  if ( strpos( $message, $params['filter'] ) !== false ) {
 106                      $messages_filtered[] = $message;
 107                  }
 108              }
 109              $messages_target = $messages_filtered;
 110          }
 111  
 112          // Whether we have any sort of message customisation filtering
 113          $customiseFilterEnabled = $params['customised'] !== 'all';
 114          if ( $customiseFilterEnabled ) {
 115              global $wgContLang;
 116              $lang = $langObj->getCode();
 117  
 118              $customisedMessages = AllMessagesTablePager::getCustomisedStatuses(
 119                  array_map( array( $langObj, 'ucfirst' ), $messages_target ), $lang, $lang != $wgContLang->getCode() );
 120  
 121              $customised = $params['customised'] === 'modified';
 122          }
 123  
 124          // Get all requested messages and print the result
 125          $skip = !is_null( $params['from'] );
 126          $useto = !is_null( $params['to'] );
 127          $result = $this->getResult();
 128          foreach ( $messages_target as $message ) {
 129              // Skip all messages up to $params['from']
 130              if ( $skip && $message === $params['from'] ) {
 131                  $skip = false;
 132              }
 133  
 134              if ( $useto && $message > $params['to'] ) {
 135                  break;
 136              }
 137  
 138              if ( !$skip ) {
 139                  $a = array( 'name' => $message );
 140                  $args = array();
 141                  if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
 142                      $args = $params['args'];
 143                  }
 144  
 145                  if ( $customiseFilterEnabled ) {
 146                      $messageIsCustomised = isset( $customisedMessages['pages'][$langObj->ucfirst( $message )] );
 147                      if ( $customised === $messageIsCustomised ) {
 148                          if ( $customised ) {
 149                              $a['customised'] = '';
 150                          }
 151                      } else {
 152                          continue;
 153                      }
 154                  }
 155  
 156                  $msg = wfMessage( $message, $args )->inLanguage( $langObj );
 157  
 158                  if ( !$msg->exists() ) {
 159                      $a['missing'] = '';
 160                  } else {
 161                      // Check if the parser is enabled:
 162                      if ( $params['enableparser'] ) {
 163                          $msgString = $msg->title( $title )->text();
 164                      } else {
 165                          $msgString = $msg->plain();
 166                      }
 167                      if ( !$params['nocontent'] ) {
 168                          ApiResult::setContent( $a, $msgString );
 169                      }
 170                      if ( isset( $prop['default'] ) ) {
 171                          $default = wfMessage( $message )->inLanguage( $langObj )->useDatabase( false );
 172                          if ( !$default->exists() ) {
 173                              $a['defaultmissing'] = '';
 174                          } elseif ( $default->plain() != $msgString ) {
 175                              $a['default'] = $default->plain();
 176                          }
 177                      }
 178                  }
 179                  $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
 180                  if ( !$fit ) {
 181                      $this->setContinueEnumParameter( 'from', $message );
 182                      break;
 183                  }
 184              }
 185          }
 186          $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
 187      }
 188  
 189  	public function getCacheMode( $params ) {
 190          if ( is_null( $params['lang'] ) ) {
 191              // Language not specified, will be fetched from preferences
 192              return 'anon-public-user-private';
 193          } elseif ( $params['enableparser'] ) {
 194              // User-specific parser options will be used
 195              return 'anon-public-user-private';
 196          } else {
 197              // OK to cache
 198              return 'public';
 199          }
 200      }
 201  
 202  	public function getAllowedParams() {
 203          return array(
 204              'messages' => array(
 205                  ApiBase::PARAM_DFLT => '*',
 206                  ApiBase::PARAM_ISMULTI => true,
 207              ),
 208              'prop' => array(
 209                  ApiBase::PARAM_ISMULTI => true,
 210                  ApiBase::PARAM_TYPE => array(
 211                      'default'
 212                  )
 213              ),
 214              'enableparser' => false,
 215              'nocontent' => false,
 216              'includelocal' => false,
 217              'args' => array(
 218                  ApiBase::PARAM_ISMULTI => true,
 219                  ApiBase::PARAM_ALLOW_DUPLICATES => true,
 220              ),
 221              'filter' => array(),
 222              'customised' => array(
 223                  ApiBase::PARAM_DFLT => 'all',
 224                  ApiBase::PARAM_TYPE => array(
 225                      'all',
 226                      'modified',
 227                      'unmodified'
 228                  )
 229              ),
 230              'lang' => null,
 231              'from' => null,
 232              'to' => null,
 233              'title' => null,
 234              'prefix' => null,
 235          );
 236      }
 237  
 238  	public function getParamDescription() {
 239          return array(
 240              'messages' => 'Which messages to output. "*" (default) means all messages',
 241              'prop' => 'Which properties to get',
 242              'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
 243                  'Will substitute magic words, handle templates etc.' ),
 244              'nocontent' => 'If set, do not include the content of the messages in the output.',
 245              'includelocal' => array( "Also include local messages, i.e. messages that don't exist in the software but do exist as a MediaWiki: page.",
 246                  "This lists all MediaWiki: pages, so it will also list those that aren't 'really' messages such as Common.js",
 247              ),
 248              'title' => 'Page name to use as context when parsing message (for enableparser option)',
 249              'args' => 'Arguments to be substituted into message',
 250              'prefix' => 'Return messages with this prefix',
 251              'filter' => 'Return only messages with names that contain this string',
 252              'customised' => 'Return only messages in this customisation state',
 253              'lang' => 'Return messages in this language',
 254              'from' => 'Return messages starting at this message',
 255              'to' => 'Return messages ending at this message',
 256          );
 257      }
 258  
 259  	public function getDescription() {
 260          return 'Return messages from this site.';
 261      }
 262  
 263  	public function getExamples() {
 264          return array(
 265              'api.php?action=query&meta=allmessages&amprefix=ipb-',
 266              'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
 267          );
 268      }
 269  
 270  	public function getHelpUrls() {
 271          return 'https://www.mediawiki.org/wiki/API:Meta#allmessages_.2F_am';
 272      }
 273  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1