ApiQueryMessageGroups.php

Go to the documentation of this file.
00001 <?php
00018 class ApiQueryMessageGroups extends ApiQueryBase {
00019 
00020     public function __construct( $query, $moduleName ) {
00021         parent::__construct( $query, $moduleName, 'mg' );
00022     }
00023 
00024     public function execute() {
00025         $params = $this->extractRequestParams();
00026         $filter = $params['filter'];
00027 
00028         $groups = array();
00029         if ( $params['format'] === 'flat' ) {
00030             $groups = MessageGroups::getAllGroups();
00031             foreach ( MessageGroups::getDynamicGroups() as $id => $unused ) {
00032                 // Do not list the sandbox group. The code that knows it
00033                 // exists can access it directly.
00034                 if ( $id === '!sandbox' ) {
00035                     continue;
00036                 }
00037                 $groups[$id] = MessageGroups::getGroup( $id );
00038             }
00039 
00040             // Not sorted by default, so do it now
00041             // Work around php bug: https://bugs.php.net/bug.php?id=50688
00042             wfSuppressWarnings();
00043             usort( $groups, array( 'MessageGroups', 'groupLabelSort' ) );
00044             wfRestoreWarnings();
00045         } elseif ( $params['root'] !== '' ) {
00046             // format=tree from now on, as it is the only other valid option
00047             $group = MessageGroups::getGroup( $params['root'] );
00048             if ( $group instanceof AggregateMessageGroup ) {
00049                 $groups = MessageGroups::subGroups( $group );
00050                 // The parent group is the first, ignore it
00051                 array_shift( $groups );
00052             }
00053         } else {
00054             $groups = MessageGroups::getGroupStructure();
00055             foreach ( MessageGroups::getDynamicGroups() as $id => $unused ) {
00056                 $groups[$id] = MessageGroups::getGroup( $id );
00057             }
00058         }
00059 
00060         $props = array_flip( $params['prop'] );
00061 
00062         $result = $this->getResult();
00063         $matcher = new StringMatcher( '', $filter );
00067         foreach ( $groups as $mixed ) {
00068             if ( $filter !== array() && !$matcher->match( $mixed->getId() ) ) {
00069                 continue;
00070             }
00071 
00072             $a = $this->formatGroup( $mixed, $props );
00073 
00074             $result->setIndexedTagName( $a, 'group' );
00075 
00076             // @todo Add a continue?
00077             $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
00078             if ( !$fit ) {
00079                 $this->setWarning( 'Could not fit all groups in the resultset.' );
00080                 // Even if we're not going to give a continue, no point carrying on
00081                 // if the result is full
00082                 break;
00083             }
00084         }
00085 
00086         $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'group' );
00087     }
00088 
00095     protected function formatGroup( $mixed, $props, $depth = 0 ) {
00096         $params = $this->extractRequestParams();
00097 
00098         // Default
00099         $g = $mixed;
00100         $subgroups = array();
00101 
00102         // Format = tree and has subgroups
00103         if ( is_array( $mixed ) ) {
00104             $g = array_shift( $mixed );
00105             $subgroups = $mixed;
00106         }
00107 
00108         $a = array();
00109 
00110         $groupId = $g->getId();
00111 
00112         if ( isset( $props['id'] ) ) {
00113             $a['id'] = $groupId;
00114         }
00115 
00116         if ( isset( $props['label'] ) ) {
00117             $a['label'] = $g->getLabel();
00118         }
00119 
00120         if ( isset( $props['description'] ) ) {
00121             $a['description'] = $g->getDescription();
00122         }
00123 
00124         if ( isset( $props['class'] ) ) {
00125             $a['class'] = get_class( $g );
00126         }
00127 
00128         if ( isset( $props['namespace'] ) ) {
00129             $a['namespace'] = $g->getNamespace();
00130         }
00131 
00132         if ( isset( $props['exists'] ) ) {
00133             $a['exists'] = $g->exists();
00134         }
00135 
00136         if ( isset( $props['icon'] ) ) {
00137             $formats = TranslateUtils::getIcon( $g, $params['iconsize'] );
00138             if ( $formats ) {
00139                 $a['icon'] = $formats;
00140             }
00141         }
00142 
00143         if ( isset( $props['priority'] ) ) {
00144             $priority = MessageGroups::getPriority( $g );
00145             $a['priority'] = $priority ? : 'default';
00146         }
00147 
00148         if ( isset( $props['prioritylangs'] ) ) {
00149             $prioritylangs = TranslateMetadata::get( $groupId, 'prioritylangs' );
00150             $a['prioritylangs'] = $prioritylangs ? explode( ',', $prioritylangs ) : false;
00151         }
00152 
00153         if ( isset( $props['priorityforce'] ) ) {
00154             $a['priorityforce'] = ( TranslateMetadata::get( $groupId, 'priorityforce' ) === 'on' );
00155         }
00156 
00157         if ( isset( $props['workflowstates'] ) ) {
00158             $a['workflowstates'] = $this->getWorkflowStates( $g );
00159         }
00160 
00161         wfRunHooks(
00162             'TranslateProcessAPIMessageGroupsProperties',
00163             array( &$a, $props, $params, $g )
00164         );
00165 
00166         // Depth only applies to tree format
00167         if ( $depth >= $params['depth'] && $params['format'] === 'tree' ) {
00168             $a['groupcount'] = count( $subgroups );
00169 
00170             // Prevent going further down in the three
00171             return $a;
00172         }
00173 
00174         // Always empty array for flat format, only sometimes for tree format
00175         if ( $subgroups !== array() ) {
00176             foreach ( $subgroups as $sg ) {
00177                 $a['groups'][] = $this->formatGroup( $sg, $props );
00178             }
00179             $result = $this->getResult();
00180             $result->setIndexedTagName( $a['groups'], 'group' );
00181         }
00182 
00183         return $a;
00184     }
00185 
00193     protected function getWorkflowStates( MessageGroup $group ) {
00194         if ( MessageGroups::isDynamic( $group ) ) {
00195             return false;
00196         }
00197 
00198         $stateConfig = $group->getMessageGroupStates()->getStates();
00199 
00200         if ( !is_array( $stateConfig ) || $stateConfig === array() ) {
00201             return false;
00202         }
00203 
00204         $user = $this->getUser();
00205 
00206         foreach ( $stateConfig as $state => $config ) {
00207             if ( is_array( $config ) ) {
00208                 // Check if user is allowed to change states generally
00209                 $allowed = $user->isAllowed( 'translate-groupreview' );
00210                 // Check further restrictions
00211                 if ( $allowed && isset( $config['right'] ) ) {
00212                     $allowed = $user->isAllowed( $config['right'] );
00213                 }
00214 
00215                 $stateConfig[$state]['_canchange'] = $allowed;
00216                 $stateConfig[$state]['_name'] =
00217                     $this->msg( "translate-workflow-state-$state" )->text();
00218             }
00219         }
00220 
00221         return $stateConfig;
00222     }
00223 
00224     public function getAllowedParams() {
00225         $allowedParams = array(
00226             'depth' => array(
00227                 ApiBase::PARAM_TYPE => 'integer',
00228                 ApiBase::PARAM_DFLT => '100',
00229             ),
00230             'filter' => array(
00231                 ApiBase::PARAM_TYPE => 'string',
00232                 ApiBase::PARAM_DFLT => '',
00233                 ApiBase::PARAM_ISMULTI => true,
00234             ),
00235             'format' => array(
00236                 ApiBase::PARAM_TYPE => array( 'flat', 'tree' ),
00237                 ApiBase::PARAM_DFLT => 'flat',
00238             ),
00239             'iconsize' => array(
00240                 ApiBase::PARAM_TYPE => 'integer',
00241                 ApiBase::PARAM_DFLT => 64,
00242             ),
00243             'prop' => array(
00244                 ApiBase::PARAM_TYPE => array_keys( self::getPropertyList() ),
00245                 ApiBase::PARAM_DFLT => 'id|label|description|class|exists',
00246                 ApiBase::PARAM_ISMULTI => true,
00247             ),
00248             'root' => array(
00249                 ApiBase::PARAM_TYPE => 'string',
00250                 ApiBase::PARAM_DFLT => '',
00251             ),
00252         );
00253         wfRunHooks( 'TranslateGetAPIMessageGroupsParameterList', array( &$allowedParams ) );
00254 
00255         return $allowedParams;
00256     }
00257 
00258     public function getParamDescription() {
00259         $depth = <<<TEXT
00260 When using the tree format, limit the depth to this many levels. Value 0 means
00261 that no subgroups are shown. If the limit is reached, a prop groupcount is
00262 added and it states the number of direct children.
00263 TEXT;
00264         $root = <<<TEXT
00265 When using the tree format, instead of starting from top level start from the
00266 given message group, which must be an aggregate message group.
00267 TEXT;
00268         $filter = <<<TEXT
00269 Only return messages with IDs that match one or more of the input(s) given
00270 (case-insensitive, separated by pipes, * wildcard).
00271 TEXT;
00272 
00273         $propIntro = array( 'What translation-related information to get:' );
00274 
00275         $paramDescs = array(
00276             'depth' => $depth,
00277             'format' => 'In a tree format message groups can exist multiple places in the tree.',
00278             'iconsize' => 'Preferred size of rasterised group icon',
00279             'root' => $root,
00280             'filter' => $filter,
00281             'prop' => array_merge( $propIntro, self::getPropertyList() ),
00282         );
00283 
00284         $p = $this->getModulePrefix(); // Can be useful for documentation
00285         wfRunHooks( 'TranslateGetAPIMessageGroupsParameterDescs', array( &$paramDescs, $p ) );
00286 
00287         $indent = "\n" . str_repeat( ' ', 24 );
00288         $wrapWidth = 104 - 24;
00289         foreach ( $paramDescs as &$val ) {
00290             if ( is_string( $val ) ) {
00291                 $val = wordwrap( str_replace( "\n", ' ', $val ), $wrapWidth, $indent );
00292             }
00293         }
00294 
00295         return $paramDescs;
00296     }
00297 
00303     protected static function getPropertyList() {
00304         $properties = array(
00305             'id'             => ' id             - Include id of the group',
00306             'label'          => ' label          - Include label of the group',
00307             'description'    => ' description    - Include description of the group',
00308             'class'          => ' class          - Include class name of the group',
00309             'namespace'      =>
00310                 ' namespace      - Include namespace of the group. Not all groups belong ' .
00311                     'to a single namespace.',
00312             'exists'         =>
00313                 ' exists         - Include self-calculated existence property of the group',
00314             'icon'           => ' icon           - Include urls to icon of the group',
00315             'priority'       => ' priority       - Include priority status like discouraged',
00316             'prioritylangs'  =>
00317                 ' prioritylangs  - Include prefered languages. If not set, this returns false',
00318             'priorityforce'  =>
00319                 ' priorityforce  - Include priority status - is the priority languages ' .
00320                     'setting forced',
00321             'workflowstates' =>
00322                 ' workflowstates - Include the workflow states for the message group',
00323         );
00324 
00325         wfRunHooks( 'TranslateGetAPIMessageGroupsPropertyDescs', array( &$properties ) );
00326 
00327         return $properties;
00328     }
00329 
00330     public function getDescription() {
00331         return 'Return information about message groups. Note that uselang parameter ' .
00332             'affects the output of language dependent parts.';
00333     }
00334 
00335     protected function getExamples() {
00336         return array(
00337             'api.php?action=query&meta=messagegroups',
00338         );
00339     }
00340 
00341     public function getVersion() {
00342         return __CLASS__ . ': ' . TRANSLATE_VERSION;
00343     }
00344 }
Generated on Tue Oct 29 00:00:23 2013 for MediaWiki Translate Extension by  doxygen 1.6.3