[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/extensions/Gadgets/ -> GadgetHooks.php (source)

   1  <?php
   2  
   3  /**
   4   * Copyright © 2007 Daniel Kinzler
   5   *
   6   * This program is free software; you can redistribute it and/or modify
   7   * it under the terms of the GNU General Public License as published by
   8   * the Free Software Foundation; either version 2 of the License, or
   9   * (at your option) any later version.
  10   *
  11   * This program is distributed in the hope that it will be useful,
  12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14   * GNU General Public License for more details.
  15   *
  16   * You should have received a copy of the GNU General Public License along
  17   * with this program; if not, write to the Free Software Foundation, Inc.,
  18   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19   * http://www.gnu.org/copyleft/gpl.html
  20   *
  21   * @file
  22   */
  23  
  24  class GadgetHooks {
  25      /**
  26       * ArticleSaveComplete hook handler.
  27       *
  28       * @param $article Article
  29       * @param $user User
  30       * @param $text String: New page text
  31       * @return bool
  32       */
  33  	public static function articleSaveComplete( $article, $user, $text ) {
  34          // update cache if MediaWiki:Gadgets-definition was edited
  35          wfProfileIn( __METHOD__ );
  36          $title = $article->getTitle();
  37          if ( $title->getNamespace() == NS_MEDIAWIKI && $title->getText() == 'Gadgets-definition' ) {
  38              Gadget::loadStructuredList( $text );
  39          }
  40          wfProfileOut( __METHOD__ );
  41          return true;
  42      }
  43  
  44      /**
  45       * UserGetDefaultOptions hook handler
  46       * @param $defaultOptions Array of default preference keys and values
  47       * @return bool
  48       */
  49  	public static function userGetDefaultOptions( &$defaultOptions ) {
  50          $gadgets = Gadget::loadStructuredList();
  51          if ( !$gadgets ) {
  52              return true;
  53          }
  54  
  55          /**
  56           * @var $gadget Gadget
  57           */
  58          foreach ( $gadgets as $thisSection ) {
  59              foreach ( $thisSection as $gadgetId => $gadget ) {
  60                  if ( $gadget->isOnByDefault() ) {
  61                      $defaultOptions['gadget-' . $gadgetId] = 1;
  62                  }
  63              }
  64          }
  65  
  66          return true;
  67      }
  68  
  69      /**
  70       * GetPreferences hook handler.
  71       * @param $user User
  72       * @param $preferences Array: Preference descriptions
  73       * @return bool
  74       */
  75  	public static function getPreferences( $user, &$preferences ) {
  76          wfProfileIn( __METHOD__ );
  77          $gadgets = Gadget::loadStructuredList();
  78          if ( !$gadgets ) {
  79              wfProfileOut( __METHOD__ );
  80              return true;
  81          }
  82  
  83          $options = array();
  84          $default = array();
  85          foreach ( $gadgets as $section => $thisSection ) {
  86              $available = array();
  87  
  88              /**
  89               * @var $gadget Gadget
  90               */
  91              foreach ( $thisSection as $gadget ) {
  92                  if ( $gadget->isAllowed( $user ) ) {
  93                      $gname = $gadget->getName();
  94                      # bug 30182: dir="auto" because it's often not translated
  95                      $desc = '<span dir="auto">' . $gadget->getDescription() . '</span>';
  96                      $available[$desc] = $gname;
  97                      if ( $gadget->isEnabled( $user ) ) {
  98                          $default[] = $gname;
  99                      }
 100                  }
 101              }
 102  
 103              if ( $section !== '' ) {
 104                  $section = wfMessage( "gadget-section-$section" )->parse();
 105  
 106                  if ( count ( $available ) ) {
 107                      $options[$section] = $available;
 108                  }
 109              } else {
 110                  $options = array_merge( $options, $available );
 111              }
 112          }
 113  
 114          $preferences['gadgets-intro'] =
 115              array(
 116                  'type' => 'info',
 117                  'label' => '&#160;',
 118                  'default' => Xml::tags( 'tr', array(),
 119                      Xml::tags( 'td', array( 'colspan' => 2 ),
 120                          wfMessage( 'gadgets-prefstext' )->parseAsBlock() ) ),
 121                  'section' => 'gadgets',
 122                  'raw' => 1,
 123                  'rawrow' => 1,
 124              );
 125  
 126          $preferences['gadgets'] =
 127              array(
 128                  'type' => 'multiselect',
 129                  'options' => $options,
 130                  'section' => 'gadgets',
 131                  'label' => '&#160;',
 132                  'prefix' => 'gadget-',
 133                  'default' => $default,
 134              );
 135          wfProfileOut( __METHOD__ );
 136  
 137          return true;
 138      }
 139  
 140      /**
 141       * ResourceLoaderRegisterModules hook handler.
 142       * @param $resourceLoader ResourceLoader
 143       * @return bool
 144       */
 145  	public static function registerModules( &$resourceLoader ) {
 146          $gadgets = Gadget::loadList();
 147          if ( !$gadgets ) {
 148              return true;
 149          }
 150  
 151          /**
 152           * @var $g Gadget
 153           */
 154          foreach ( $gadgets as $g ) {
 155              $module = $g->getModule();
 156              if ( $module ) {
 157                  $resourceLoader->register( $g->getModuleName(), $module );
 158              }
 159          }
 160          return true;
 161  
 162      }
 163  
 164      /**
 165       * BeforePageDisplay hook handler.
 166       * @param $out OutputPage
 167       * @return bool
 168       */
 169  	public static function beforePageDisplay( $out ) {
 170          wfProfileIn( __METHOD__ );
 171  
 172          $gadgets = Gadget::loadList();
 173          if ( !$gadgets ) {
 174              wfProfileOut( __METHOD__ );
 175              return true;
 176          }
 177  
 178          $lb = new LinkBatch();
 179          $lb->setCaller( __METHOD__ );
 180          $pages = array();
 181  
 182          /**
 183           * @var $gadget Gadget
 184           */
 185          $user = $out->getUser();
 186          foreach ( $gadgets as $gadget ) {
 187              if ( $gadget->isEnabled( $user ) && $gadget->isAllowed( $user ) ) {
 188                  if ( $gadget->hasModule() ) {
 189                      $out->addModuleStyles( $gadget->getModuleName() );
 190                      $out->addModules( $gadget->getModuleName() );
 191                  }
 192  
 193                  foreach ( $gadget->getLegacyScripts() as $page ) {
 194                      $lb->add( NS_MEDIAWIKI, $page );
 195                      $pages[] = $page;
 196                  }
 197              }
 198          }
 199  
 200  
 201          // Allow other extensions, e.g. MobileFrontend, to disallow legacy gadgets
 202          if ( wfRunHooks( 'Gadgets::allowLegacy', array( $out->getContext() ) ) ) {
 203              $lb->execute( __METHOD__ );
 204  
 205              $done = array();
 206  
 207              foreach ( $pages as $page ) {
 208                  if ( isset( $done[$page] ) ) {
 209                      continue;
 210                  }
 211  
 212                  $done[$page] = true;
 213                  self::applyScript( $page, $out );
 214              }
 215          }
 216          wfProfileOut( __METHOD__ );
 217  
 218          return true;
 219      }
 220  
 221      /**
 222       * Adds one legacy script to output.
 223       *
 224       * @param string $page Unprefixed page title
 225       * @param OutputPage $out
 226       */
 227  	private static function applyScript( $page, $out ) {
 228          global $wgJsMimeType;
 229  
 230          # bug 22929: disable gadgets on sensitive pages.  Scripts loaded through the
 231          # ResourceLoader handle this in OutputPage::getModules()
 232          # TODO: make this extension load everything via RL, then we don't need to worry
 233          # about any of this.
 234          if ( $out->getAllowedModules( ResourceLoaderModule::TYPE_SCRIPTS ) < ResourceLoaderModule::ORIGIN_USER_SITEWIDE ) {
 235              return;
 236          }
 237  
 238          $t = Title::makeTitleSafe( NS_MEDIAWIKI, $page );
 239          if ( !$t ) {
 240              return;
 241          }
 242  
 243          $u = $t->getLocalURL( 'action=raw&ctype=' . $wgJsMimeType );
 244          $out->addScriptFile( $u, $t->getLatestRevID() );
 245      }
 246  
 247      /**
 248       * UnitTestsList hook handler
 249       * @param array $files
 250       * @return bool
 251       */
 252  	public static function onUnitTestsList( array &$files ) {
 253          $testDir = __DIR__ . '/tests/';
 254          $files = array_merge( $files, glob( "$testDir/*Test.php" ) );
 255          return true;
 256      }
 257  }


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