http://www.mediawiki.org/wiki/Special:Code/MediaWiki/95963

Revision: 95963
Author:   catrope
Date:     2011-09-01 10:55:45 +0000 (Thu, 01 Sep 2011)
Log Message:
-----------
RL2: Add API module for prefix matches on CSS/JS pages, will be used for search 
suggestions in the gadget manager UI

Modified Paths:
--------------
    branches/RL2/extensions/Gadgets/Gadgets.php

Added Paths:
-----------
    branches/RL2/extensions/Gadgets/api/ApiQueryGadgetPages.php

Modified: branches/RL2/extensions/Gadgets/Gadgets.php
===================================================================
--- branches/RL2/extensions/Gadgets/Gadgets.php 2011-09-01 10:54:12 UTC (rev 
95962)
+++ branches/RL2/extensions/Gadgets/Gadgets.php 2011-09-01 10:55:45 UTC (rev 
95963)
@@ -116,6 +116,7 @@
 
 $wgAutoloadClasses['ApiGadgetManager'] = $dir . 'api/ApiGadgetManager.php';
 $wgAutoloadClasses['ApiQueryGadgetCategories'] = $dir . 
'api/ApiQueryGadgetCategories.php';
+$wgAutoloadClasses['ApiQueryGadgetPages'] = $dir . 
'api/ApiQueryGadgetPages.php';
 $wgAutoloadClasses['ApiQueryGadgets'] = $dir . 'api/ApiQueryGadgets.php';
 $wgAutoloadClasses['ForeignDBGadgetRepo'] = $dir . 
'backend/ForeignDBGadgetRepo.php';
 $wgAutoloadClasses['Gadget'] = $dir . 'backend/Gadget.php';
@@ -135,6 +136,7 @@
 $wgAPIModules['gadgetmanager'] = 'ApiGadgetManager';
 $wgAPIListModules['gadgetcategories'] = 'ApiQueryGadgetCategories';
 $wgAPIListModules['gadgets'] = 'ApiQueryGadgets';
+$wgAPIListModules['gadgetpages'] = 'ApiQueryGadgetPages';
 
 $gadResourceTemplate = array(
        'localBasePath' => $dir . 'modules',

Added: branches/RL2/extensions/Gadgets/api/ApiQueryGadgetPages.php
===================================================================
--- branches/RL2/extensions/Gadgets/api/ApiQueryGadgetPages.php                 
        (rev 0)
+++ branches/RL2/extensions/Gadgets/api/ApiQueryGadgetPages.php 2011-09-01 
10:55:45 UTC (rev 95963)
@@ -0,0 +1,156 @@
+<?php
+/**
+ * Created on 15 April 2011
+ * API for Gadgets extension
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+
+class ApiQueryGadgetPages extends ApiQueryGeneratorBase {
+       public function __construct( $query, $moduleName ) {
+               parent::__construct( $query, $moduleName, 'gp' );
+       }
+
+       public function getCacheMode( $params ) {
+               return 'public';
+       }
+
+       public function execute() {
+               $this->run();
+       }
+
+       /**
+        * @param $resultPageSet ApiPageSet
+        * @return void
+        */
+       public function executeGenerator( $resultPageSet ) {
+               $this->run( $resultPageSet );
+       }
+
+       /**
+        * @param $resultPageSet ApiPageSet
+        * @return void
+        */
+       private function run( $resultPageSet = null ) {
+               $params = $this->extractRequestParams();
+               $db = $this->getDB();
+               
+               $this->addTables( 'gadgetpagelist' );
+               $this->addFields( array( 'gpl_namespace', 'gpl_title' ) );
+               $this->addWhereFld( 'gpl_extension', $params['extension'] );
+               $this->addWhereFld( 'gpl_namespace', $params['namespace'] );
+               if ( $params['prefix'] !== null ) {
+                       $this->addWhere( 'gpl_title' . $db->buildLike( 
$this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
+               }
+               $limit = $params['limit'];
+               $this->addOption( 'LIMIT', $limit + 1 );
+               $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
+               $from = $params['from'] !== null ? $this->titlePartToKey( 
$params['from'] ) : null;
+               $this->addWhereRange( 'gpl_title', $dir, $from, null );
+               
+               $res = $this->select( __METHOD__ );
+
+               $count = 0;
+               $titles = array();
+               $result = $this->getResult();
+               foreach ( $res as $row ) {
+                       if ( ++$count > $limit ) {
+                               // We've reached the one extra which shows that 
there are additional pages to be had. Stop here...
+                               $this->setContinueEnumParameter( 'from', 
$this->keyToTitle( $row->gpl_title ) );
+                               break;
+                       }
+                       
+                       $title = Title::makeTitle( $row->gpl_namespace, 
$row->gpl_title );
+                       if ( is_null( $resultPageSet ) ) {
+                               $vals = array();
+                               self::addTitleInfo( $vals, $title );
+                               $fit = $result->addValue( array( 'query', 
$this->getModuleName() ), null, $vals );
+                               if ( !$fit ) {
+                                       $this->setContinueEnumParameter( 
'from', $this->keyToTitle( $row->gpl_title ) );
+                                       break;
+                               }
+                       } else {
+                               $titles[] = $title;
+                       }
+               }
+
+               if ( is_null( $resultPageSet ) ) {
+                       $result->setIndexedTagName_internal( array( 'query', 
$this->getModuleName() ), 'p' );
+               } else {
+                       $resultPageSet->populateFromTitles( $titles );
+               }
+       }
+
+       public function getAllowedParams() {
+               return array(
+                       'extension' => array(
+                               ApiBase::PARAM_DFLT => 'js',
+                               ApiBase::PARAM_TYPE => array( 'js', 'css' ),
+                       ),
+                       'namespace' => array(
+                               ApiBase::PARAM_DFLT => NS_GADGET,
+                               ApiBase::PARAM_TYPE => 'namespace',
+                       ),
+                       'prefix' => null,
+                       'from' => null,
+                       'limit' => array(
+                               ApiBase::PARAM_DFLT => 10,
+                               ApiBase::PARAM_TYPE => 'limit',
+                               ApiBase::PARAM_MIN => 1,
+                               ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
+                               ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
+                       ),
+                       'dir' => array(
+                               ApiBase::PARAM_DFLT => 'ascending',
+                               ApiBase::PARAM_TYPE => array(
+                                       'ascending',
+                                       'descending'
+                               )
+                       ),
+               );
+       }
+
+       public function getDescription() {
+               return 'Returns a list of .js/.css pages';
+       }
+
+       public function getParamDescription() {
+               return array(
+                       'extension' => 'Search for pages with this extension.',
+                       'namespace' => 'Search for pages in this namespace.',
+                       'prefix' => array( 'Search for pages with this prefix 
(optional).',
+                                          'NOTE: Prefix does not include the 
namespace prefix',
+                       ),
+                       'from' => 'Start at this page title. NOTE: Does not 
include the namespace prefix',
+                       'limit' => 'How many total pages to return.',
+                       'dir' => 'The direction in which to list',
+               );
+       }
+
+       protected function getExamples() {
+               return array(
+                       'Get a list of .js pages in the MediaWiki namespace:',
+                       '    
api.php?action=query&list=gadgetpages&gpextension=js&gpnamespace=8',
+                       "Get a list of .css pages in the Catrope's user space:",
+                       '    
api.php?action=query&list=gadgetpages&gpextension=css&gpnamespace=2&gpprefix=Catrope/',
+               );
+       }
+
+       public function getVersion() {
+               return __CLASS__ . ': $Id$';
+       }
+
+}


Property changes on: branches/RL2/extensions/Gadgets/api/ApiQueryGadgetPages.php
___________________________________________________________________
Added: svn:keywords
   + Id
Added: svn:eol-style
   + native


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to