API: Crusade against extract(). Left one extract() call alone in ApiQueryBacklinks...
authorRoan Kattouw <catrope@users.mediawiki.org>
Wed, 17 Dec 2008 16:34:01 +0000 (16:34 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Wed, 17 Dec 2008 16:34:01 +0000 (16:34 +0000)
includes/api/ApiBase.php
includes/api/ApiExpandTemplates.php
includes/api/ApiLogin.php
includes/api/ApiPageSet.php
includes/api/ApiQueryBacklinks.php
includes/api/ApiQueryRecentChanges.php
includes/api/ApiQueryRevisions.php
includes/api/ApiQueryWatchlist.php
includes/api/ApiRollback.php

index e8ac8c1..ff9129c 100644 (file)
@@ -369,7 +369,6 @@ abstract class ApiBase {
        /**
        * Using getAllowedParams(), makes an array of the values provided by the user,
        * with key being the name of the variable, and value - validated value from user or default.
-       * This method can be used to generate local variables using extract().
        * limit=max will not be parsed if $parseMaxLimit is set to false; use this
        * when the max limit is not definite, e.g. when getting revisions.
        */
index fa2070f..1fc1bfe 100644 (file)
@@ -43,23 +43,22 @@ class ApiExpandTemplates extends ApiBase {
 
        public function execute() {
                // Get parameters
-               extract( $this->extractRequestParams() );
-               $retval = '';
+               $params = $this->extractRequestParams();
 
                //Create title for parser
-               $title_obj = Title :: newFromText( $title );
+               $title_obj = Title :: newFromText( $params['title'] );
                if(!$title_obj)
-                       $title_obj = Title :: newFromText( "API" );     //  Default title is "API". For example, ExpandTemplates uses "ExpendTemplates" for it
+                       $title_obj = Title :: newFromText( "API" ); // default
 
                $result = $this->getResult();
 
                // Parse text
                global $wgParser;
                $options = new ParserOptions();
-               if ( $generatexml )
+               if ( $params['generatexml'] )
                {
                        $wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS );
-                       $dom = $wgParser->preprocessToDom( $text );
+                       $dom = $wgParser->preprocessToDom( $params['text'] );
                        if ( is_callable( array( $dom, 'saveXML' ) ) ) {
                                $xml = $dom->saveXML();
                        } else {
@@ -67,9 +66,9 @@ class ApiExpandTemplates extends ApiBase {
                        }
                        $xml_result = array();
                        $result->setContent( $xml_result, $xml );
-            $result->addValue( null, 'parsetree', $xml_result);
+                       $result->addValue( null, 'parsetree', $xml_result);
                }
-               $retval = $wgParser->preprocess( $text, $title_obj, $options );
+               $retval = $wgParser->preprocess( $params['text'], $title_obj, $options );
 
                // Return result
                $retval_array = array();
index 108ce81..1e6552b 100644 (file)
@@ -52,15 +52,14 @@ class ApiLogin extends ApiBase {
         * @access public
         */
        public function execute() {
-               $name = $password = $domain = null;
-               extract($this->extractRequestParams());
+               $params = $this->extractRequestParams();
 
                $result = array ();
 
-               $params = new FauxRequest(array (
-                       'wpName' => $name,
-                       'wpPassword' => $password,
-                       'wpDomain' => $domain,
+               $req = new FauxRequest(array (
+                       'wpName' => $params['name'],
+                       'wpPassword' => $params['password'],
+                       'wpDomain' => $params['domain'],
                        'wpRemember' => ''
                ));
 
@@ -69,7 +68,7 @@ class ApiLogin extends ApiBase {
                        wfSetupSession();
                }
 
-               $loginForm = new LoginForm($params);
+               $loginForm = new LoginForm($req);
                switch ($authRes = $loginForm->authenticateUserData()) {
                        case LoginForm :: SUCCESS :
                                global $wgUser, $wgCookiePrefix;
index e846f30..8fedca1 100644 (file)
@@ -228,19 +228,18 @@ class ApiPageSet extends ApiQueryBase {
         */
        public function execute() {
                $this->profileIn();
-               $titles = $pageids = $revids = null;
-               extract($this->extractRequestParams());
+               $params = $this->extractRequestParams();
 
                // Only one of the titles/pageids/revids is allowed at the same time
                $dataSource = null;
-               if (isset ($titles))
+               if (isset ($params['titles']))
                        $dataSource = 'titles';
-               if (isset ($pageids)) {
+               if (isset ($params['pageids'])) {
                        if (isset ($dataSource))
                                $this->dieUsage("Cannot use 'pageids' at the same time as '$dataSource'", 'multisource');
                        $dataSource = 'pageids';
                }
-               if (isset ($revids)) {
+               if (isset ($params['revids'])) {
                        if (isset ($dataSource))
                                $this->dieUsage("Cannot use 'revids' at the same time as '$dataSource'", 'multisource');
                        $dataSource = 'revids';
@@ -248,17 +247,17 @@ class ApiPageSet extends ApiQueryBase {
 
                switch ($dataSource) {
                        case 'titles' :
-                               $this->initFromTitles($titles);
+                               $this->initFromTitles($params['titles']);
                                break;
                        case 'pageids' :
-                               $this->initFromPageIds($pageids);
+                               $this->initFromPageIds($params['pageids']);
                                break;
                        case 'revids' :
                                if($this->mResolveRedirects)
                                        $this->setWarning('Redirect resolution cannot be used together with the revids= parameter. '.
                                        'Any redirects the revids= point to have not been resolved.');
                                $this->mResolveRedirects = false;
-                               $this->initFromRevIDs($revids);
+                               $this->initFromRevIDs($params['revids']);
                                break;
                        default :
                                // Do nothing - some queries do not need any of the data sources.
index 315cae9..f60e824 100644 (file)
@@ -60,7 +60,6 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
        );
 
        public function __construct($query, $moduleName) {
-               $code = $prefix = $linktbl = null;
                extract($this->backlinksSettings[$moduleName]);
 
                parent :: __construct($query, $moduleName, $code);
index c65c352..590cb26 100644 (file)
@@ -83,11 +83,8 @@ class ApiQueryRecentChanges extends ApiQueryBase {
         * Generates and outputs the result of this query based upon the provided parameters.
         */
        public function execute() {
-               /* Initialize vars */
-               $limit = $prop = $namespace = $titles = $show = $type = $dir = $start = $end = $token = null;
-
                /* Get the parameters of the request. */
-               extract($this->extractRequestParams());
+               $params = $this->extractRequestParams();
 
                /* Build our basic query. Namely, something along the lines of:
                 * SELECT * FROM recentchanges WHERE rc_timestamp > $start
@@ -97,13 +94,13 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                $db = $this->getDB();
                $this->addTables('recentchanges');
                $this->addOption('USE INDEX', array('recentchanges' => 'rc_timestamp'));
-               $this->addWhereRange('rc_timestamp', $dir, $start, $end);
-               $this->addWhereFld('rc_namespace', $namespace);
+               $this->addWhereRange('rc_timestamp', $params['dir'], $params['start'], $params['end']);
+               $this->addWhereFld('rc_namespace', $params['namespace']);
                $this->addWhereFld('rc_deleted', 0);
-               if($titles)
+               if($params['titles'])
                {
                        $lb = new LinkBatch;
-                       foreach($titles as $t)
+                       foreach($params['titles'] as $t)
                        {
                                $obj = Title::newFromText($t);
                                $lb->addObj($obj);
@@ -120,11 +117,11 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                                $this->addWhere($where);
                }
 
-               if(!is_null($type))
-                               $this->addWhereFld('rc_type', $this->parseRCType($type));
+               if(!is_null($params['type']))
+                               $this->addWhereFld('rc_type', $this->parseRCType($params['type']));
 
-               if (!is_null($show)) {
-                       $show = array_flip($show);
+               if (!is_null($params['show'])) {
+                       $show = array_flip($params['show']);
 
                        /* Check for conflicting parameters. */
                        if ((isset ($show['minor']) && isset ($show['!minor']))
@@ -167,8 +164,8 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                ));
 
                /* Determine what properties we need to display. */
-               if (!is_null($prop)) {
-                       $prop = array_flip($prop);
+               if (!is_null($params['prop'])) {
+                       $prop = array_flip($params['prop']);
 
                        /* Set up internal members based upon params. */
                        $this->fld_comment = isset ($prop['comment']);
@@ -210,10 +207,8 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                                $this->addFields('page_is_redirect');
                        }
                }
-               $this->token = $token;
-               /* Specify the limit for our query. It's $limit+1 because we (possibly) need to
-                * generate a "continue" parameter, to allow paging. */
-               $this->addOption('LIMIT', $limit +1);
+               $this->token = $params['token'];
+               $this->addOption('LIMIT', $params['limit'] +1);
 
                $data = array ();
                $count = 0;
@@ -224,7 +219,7 @@ class ApiQueryRecentChanges extends ApiQueryBase {
 
                /* Iterate through the rows, adding data extracted from them to our query result. */
                while ($row = $db->fetchObject($res)) {
-                       if (++ $count > $limit) {
+                       if (++ $count > $params['limit']) {
                                // We've reached the one extra which shows that there are additional pages to be had. Stop here...
                                $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rc_timestamp));
                                break;
index 91f3e4b..af2afdc 100644 (file)
@@ -74,14 +74,16 @@ class ApiQueryRevisions extends ApiQueryBase {
        }
 
        public function execute() {
-               $limit = $startid = $endid = $start = $end = $dir = $prop = $user = $excludeuser = $expandtemplates = $generatexml = $section = $token = null;
-               extract($this->extractRequestParams(false));
+               $params = $this->extractRequestParams(false);
 
                // If any of those parameters are used, work in 'enumeration' mode.
                // Enum mode can only be used when exactly one page is provided.
                // Enumerating revisions on multiple pages make it extremely
                // difficult to manage continuations and require additional SQL indexes
-               $enumRevMode = (!is_null($user) || !is_null($excludeuser) || !is_null($limit) || !is_null($startid) || !is_null($endid) || $dir === 'newer' || !is_null($start) || !is_null($end));
+               $enumRevMode = (!is_null($params['user']) || !is_null($params['excludeuser']) ||
+                               !is_null($params['limit']) || !is_null($params['startid']) ||
+                               !is_null($params['endid']) || $params['dir'] === 'newer' ||
+                               !is_null($params['start']) || !is_null($params['end']));
 
 
                $pageSet = $this->getPageSet();
@@ -103,7 +105,7 @@ class ApiQueryRevisions extends ApiQueryBase {
                $this->addTables( 'page' );
                $this->addWhere('page_id = rev_page');
 
-               $prop = array_flip($prop);
+               $prop = array_flip($params['prop']);
 
                // Optional fields
                $this->fld_ids = isset ($prop['ids']);
@@ -113,7 +115,7 @@ class ApiQueryRevisions extends ApiQueryBase {
                $this->fld_comment = isset ($prop['comment']);
                $this->fld_size = isset ($prop['size']);
                $this->fld_user = isset ($prop['user']);
-               $this->token = $token;
+               $this->token = $params['token'];
 
                if ( !is_null($this->token) || $pageCount > 0) {
                        $this->addFields( Revision::selectPageFields() );
@@ -136,16 +138,17 @@ class ApiQueryRevisions extends ApiQueryBase {
 
                        $this->fld_content = true;
 
-                       $this->expandTemplates = $expandtemplates;
-                       $this->generateXML = $generatexml;
-                       if(isset($section))
-                               $this->section = $section;
+                       $this->expandTemplates = $params['expandtemplates'];
+                       $this->generateXML = $params['generatexml'];
+                       if(isset($params['section']))
+                               $this->section = $params['section'];
                        else
                                $this->section = false;
                }
 
                $userMax = ( $this->fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1 );
                $botMax  = ( $this->fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2 );
+               $limit = $params['limit'];
                if( $limit == 'max' ) {
                        $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
                        $this->getResult()->addValue( 'limits', $this->getModuleName(), $limit );
@@ -154,13 +157,13 @@ class ApiQueryRevisions extends ApiQueryBase {
                if ($enumRevMode) {
 
                        // This is mostly to prevent parameter errors (and optimize SQL?)
-                       if (!is_null($startid) && !is_null($start))
+                       if (!is_null($params['startid']) && !is_null($params['start']))
                                $this->dieUsage('start and startid cannot be used together', 'badparams');
 
-                       if (!is_null($endid) && !is_null($end))
+                       if (!is_null($params['endid']) && !is_null($params['end']))
                                $this->dieUsage('end and endid cannot be used together', 'badparams');
 
-                       if(!is_null($user) && !is_null( $excludeuser))
+                       if(!is_null($params['user']) && !is_null($params['excludeuser']))
                                $this->dieUsage('user and excludeuser cannot be used together', 'badparams');
 
                        // This code makes an assumption that sorting by rev_id and rev_timestamp produces
@@ -170,10 +173,12 @@ class ApiQueryRevisions extends ApiQueryBase {
                        // one row with the same timestamp for the same page.
                        // The order needs to be the same as start parameter to avoid SQL filesort.
 
-                       if (is_null($startid) && is_null($endid))
-                               $this->addWhereRange('rev_timestamp', $dir, $start, $end);
+                       if (is_null($params['startid']) && is_null($params['endid']))
+                               $this->addWhereRange('rev_timestamp', $params['dir'],
+                                       $params['start'], $params['end']);
                        else
-                               $this->addWhereRange('rev_id', $dir, $startid, $endid);
+                               $this->addWhereRange('rev_id', $params['dir'],
+                                       $params['startid'], $params['endid']);
 
                        // must manually initialize unset limit
                        if (is_null($limit))
@@ -183,10 +188,11 @@ class ApiQueryRevisions extends ApiQueryBase {
                        // There is only one ID, use it
                        $this->addWhereFld('rev_page', current(array_keys($pageSet->getGoodTitles())));
 
-                       if(!is_null($user)) {
-                               $this->addWhereFld('rev_user_text', $user);
-                       } elseif (!is_null( $excludeuser)) {
-                               $this->addWhere('rev_user_text != ' . $this->getDB()->addQuotes($excludeuser));
+                       if(!is_null($params['user'])) {
+                               $this->addWhereFld('rev_user_text', $params['user']);
+                       } elseif (!is_null( $params['excludeuser'])) {
+                               $this->addWhere('rev_user_text != ' .
+                                       $this->getDB()->addQuotes($params['excludeuser']));
                        }
                }
                elseif ($revCount > 0) {
index a70ea49..7007a91 100644 (file)
@@ -59,12 +59,11 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                if (!$wgUser->isLoggedIn())
                        $this->dieUsage('You must be logged-in to have a watchlist', 'notloggedin');
 
-               $allrev = $start = $end = $namespace = $dir = $limit = $prop = $show = null;
-               extract($this->extractRequestParams());
+               $params = $this->extractRequestParams();
 
-               if (!is_null($prop) && is_null($resultPageSet)) {
+               if (!is_null($params['prop']) && is_null($resultPageSet)) {
 
-                       $prop = array_flip($prop);
+                       $prop = array_flip($params['prop']);
 
                        $this->fld_ids = isset($prop['ids']);
                        $this->fld_title = isset($prop['title']);
@@ -100,7 +99,7 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                        $this->addFieldsIf('rc_old_len', $this->fld_sizes);
                        $this->addFieldsIf('rc_new_len', $this->fld_sizes);
                }
-               elseif ($allrev) {
+               elseif ($params['allrev']) {
                        $this->addFields(array (
                                'rc_this_oldid',
                                'rc_namespace',
@@ -131,12 +130,12 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                        'rc_deleted' => 0,
                ));
 
-               $this->addWhereRange('rc_timestamp', $dir, $start, $end);
-               $this->addWhereFld('wl_namespace', $namespace);
-               $this->addWhereIf('rc_this_oldid=page_latest', !$allrev);
+               $this->addWhereRange('rc_timestamp', $params['dir'], $params['start'], $params['end']);
+               $this->addWhereFld('wl_namespace', $params['namespace']);
+               $this->addWhereIf('rc_this_oldid=page_latest', !$params['allrev']);
 
-               if (!is_null($show)) {
-                       $show = array_flip($show);
+               if (!is_null($params['show'])) {
+                       $show = array_flip($params['show']);
 
                        /* Check for conflicting parameters. */
                        if ((isset ($show['minor']) && isset ($show['!minor']))
@@ -165,9 +164,9 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
 
 
                # This is an index optimization for mysql, as done in the Special:Watchlist page
-               $this->addWhereIf("rc_timestamp > ''", !isset ($start) && !isset ($end) && $wgDBtype == 'mysql');
+               $this->addWhereIf("rc_timestamp > ''", !isset ($params['start']) && !isset ($params['end']) && $wgDBtype == 'mysql');
 
-               $this->addOption('LIMIT', $limit +1);
+               $this->addOption('LIMIT', $params['limit'] +1);
 
                $data = array ();
                $count = 0;
@@ -175,7 +174,7 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
 
                $db = $this->getDB();
                while ($row = $db->fetchObject($res)) {
-                       if (++ $count > $limit) {
+                       if (++ $count > $params['limit']) {
                                // We've reached the one extra which shows that there are additional pages to be had. Stop here...
                                $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rc_timestamp));
                                break;
@@ -186,7 +185,7 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                                if ($vals)
                                        $data[] = $vals;
                        } else {
-                               if ($allrev) {
+                               if ($params['allrev']) {
                                        $data[] = intval($row->rc_this_oldid);
                                } else {
                                        $data[] = intval($row->rc_cur_id);
@@ -200,7 +199,7 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                        $this->getResult()->setIndexedTagName($data, 'item');
                        $this->getResult()->addValue('query', $this->getModuleName(), $data);
                }
-               elseif ($allrev) {
+               elseif ($params['allrev']) {
                        $resultPageSet->populateFromRevisionIDs($data);
                } else {
                        $resultPageSet->populateFromPageIDs($data);
index 7a0573c..a7f0374 100644 (file)
@@ -70,16 +70,13 @@ class ApiRollback extends ApiBase {
                        // We don't care about multiple errors, just report one of them
                        $this->dieUsageMsg(current($retval));
 
-               $current = $target = $summary = NULL;
-               extract($details);
-
                $info = array(
                        'title' => $titleObj->getPrefixedText(),
-                       'pageid' => $current->getPage(),
-                       'summary' => $summary,
+                       'pageid' => $details['current']->getPage(),
+                       'summary' => $details['summary'],
                        'revid' => $titleObj->getLatestRevID(),
-                       'old_revid' => $current->getID(),
-                       'last_revid' => $target->getID()
+                       'old_revid' => $details['current']->getID(),
+                       'last_revid' => $details['target']->getID()
                );
 
                $this->getResult()->addValue(null, $this->getModuleName(), $info);