Merge "Making listToText() not break if passed a 1-item list."
[lhc/web/wiklou.git] / includes / api / ApiQueryInfo.php
index a6c0ed5..4a85b0b 100644 (file)
@@ -4,7 +4,7 @@
  *
  * Created on Sep 25, 2006
  *
- * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
+ * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
  *
  * 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
@@ -57,7 +57,10 @@ class ApiQueryInfo extends ApiQueryBase {
                global $wgDisableCounters;
 
                $pageSet->requestField( 'page_restrictions' );
-               $pageSet->requestField( 'page_is_redirect' );
+               // when resolving redirects, no page will have this field
+               if( !$pageSet->isResolvingRedirects() ) {
+                       $pageSet->requestField( 'page_is_redirect' );
+               }
                $pageSet->requestField( 'page_is_new' );
                if ( !$wgDisableCounters ) {
                        $pageSet->requestField( 'page_counter' );
@@ -99,6 +102,12 @@ class ApiQueryInfo extends ApiQueryBase {
                return $this->tokenFunctions;
        }
 
+       static $cachedTokens = array();
+
+       public static function resetTokenCache() {
+               ApiQueryInfo::$cachedTokens = array();
+       }
+
        public static function getEditToken( $pageid, $title ) {
                // We could check for $title->userCan('edit') here,
                // but that's too expensive for this purpose
@@ -108,14 +117,12 @@ class ApiQueryInfo extends ApiQueryBase {
                        return false;
                }
 
-               // The edit token is always the same, let's exploit that
-               static $cachedEditToken = null;
-               if ( !is_null( $cachedEditToken ) ) {
-                       return $cachedEditToken;
+               // The token is always the same, let's exploit that
+               if ( !isset( ApiQueryInfo::$cachedTokens[ 'edit' ] ) ) {
+                       ApiQueryInfo::$cachedTokens[ 'edit' ] = $wgUser->getEditToken();
                }
 
-               $cachedEditToken = $wgUser->getEditToken();
-               return $cachedEditToken;
+               return ApiQueryInfo::$cachedTokens[ 'edit' ];
        }
 
        public static function getDeleteToken( $pageid, $title ) {
@@ -124,13 +131,12 @@ class ApiQueryInfo extends ApiQueryBase {
                        return false;
                }
 
-               static $cachedDeleteToken = null;
-               if ( !is_null( $cachedDeleteToken ) ) {
-                       return $cachedDeleteToken;
+               // The token is always the same, let's exploit that
+               if ( !isset( ApiQueryInfo::$cachedTokens[ 'delete' ] ) ) {
+                       ApiQueryInfo::$cachedTokens[ 'delete' ] = $wgUser->getEditToken();
                }
 
-               $cachedDeleteToken = $wgUser->getEditToken();
-               return $cachedDeleteToken;
+               return ApiQueryInfo::$cachedTokens[ 'delete' ];
        }
 
        public static function getProtectToken( $pageid, $title ) {
@@ -139,13 +145,12 @@ class ApiQueryInfo extends ApiQueryBase {
                        return false;
                }
 
-               static $cachedProtectToken = null;
-               if ( !is_null( $cachedProtectToken ) ) {
-                       return $cachedProtectToken;
+               // The token is always the same, let's exploit that
+               if ( !isset( ApiQueryInfo::$cachedTokens[ 'protect' ] ) ) {
+                       ApiQueryInfo::$cachedTokens[ 'protect' ] = $wgUser->getEditToken();
                }
 
-               $cachedProtectToken = $wgUser->getEditToken();
-               return $cachedProtectToken;
+               return ApiQueryInfo::$cachedTokens[ 'protect' ];
        }
 
        public static function getMoveToken( $pageid, $title ) {
@@ -154,13 +159,12 @@ class ApiQueryInfo extends ApiQueryBase {
                        return false;
                }
 
-               static $cachedMoveToken = null;
-               if ( !is_null( $cachedMoveToken ) ) {
-                       return $cachedMoveToken;
+               // The token is always the same, let's exploit that
+               if ( !isset( ApiQueryInfo::$cachedTokens[ 'move' ] ) ) {
+                       ApiQueryInfo::$cachedTokens[ 'move' ] = $wgUser->getEditToken();
                }
 
-               $cachedMoveToken = $wgUser->getEditToken();
-               return $cachedMoveToken;
+               return ApiQueryInfo::$cachedTokens[ 'move' ];
        }
 
        public static function getBlockToken( $pageid, $title ) {
@@ -169,13 +173,12 @@ class ApiQueryInfo extends ApiQueryBase {
                        return false;
                }
 
-               static $cachedBlockToken = null;
-               if ( !is_null( $cachedBlockToken ) ) {
-                       return $cachedBlockToken;
+               // The token is always the same, let's exploit that
+               if ( !isset( ApiQueryInfo::$cachedTokens[ 'block' ] ) ) {
+                       ApiQueryInfo::$cachedTokens[ 'block' ] = $wgUser->getEditToken();
                }
 
-               $cachedBlockToken = $wgUser->getEditToken();
-               return $cachedBlockToken;
+               return ApiQueryInfo::$cachedTokens[ 'block' ];
        }
 
        public static function getUnblockToken( $pageid, $title ) {
@@ -189,13 +192,12 @@ class ApiQueryInfo extends ApiQueryBase {
                        return false;
                }
 
-               static $cachedEmailToken = null;
-               if ( !is_null( $cachedEmailToken ) ) {
-                       return $cachedEmailToken;
+               // The token is always the same, let's exploit that
+               if ( !isset( ApiQueryInfo::$cachedTokens[ 'email' ] ) ) {
+                       ApiQueryInfo::$cachedTokens[ 'email' ] = $wgUser->getEditToken();
                }
 
-               $cachedEmailToken = $wgUser->getEditToken();
-               return $cachedEmailToken;
+               return ApiQueryInfo::$cachedTokens[ 'email' ];
        }
 
        public static function getImportToken( $pageid, $title ) {
@@ -204,13 +206,12 @@ class ApiQueryInfo extends ApiQueryBase {
                        return false;
                }
 
-               static $cachedImportToken = null;
-               if ( !is_null( $cachedImportToken ) ) {
-                       return $cachedImportToken;
+               // The token is always the same, let's exploit that
+               if ( !isset( ApiQueryInfo::$cachedTokens[ 'import' ] ) ) {
+                       ApiQueryInfo::$cachedTokens[ 'import' ] = $wgUser->getEditToken();
                }
 
-               $cachedImportToken = $wgUser->getEditToken();
-               return $cachedImportToken;
+               return ApiQueryInfo::$cachedTokens[ 'import' ];
        }
 
        public static function getWatchToken( $pageid, $title ) {
@@ -219,13 +220,12 @@ class ApiQueryInfo extends ApiQueryBase {
                        return false;
                }
 
-               static $cachedWatchToken = null;
-               if ( !is_null( $cachedWatchToken ) ) {
-                       return $cachedWatchToken;
+               // The token is always the same, let's exploit that
+               if ( !isset( ApiQueryInfo::$cachedTokens[ 'watch' ] ) ) {
+                       ApiQueryInfo::$cachedTokens[ 'watch' ] = $wgUser->getEditToken( 'watch' );
                }
 
-               $cachedWatchToken = $wgUser->getEditToken( 'watch' );
-               return $cachedWatchToken;
+               return ApiQueryInfo::$cachedTokens[ 'watch' ];
        }
 
        public static function getOptionsToken( $pageid, $title ) {
@@ -234,13 +234,12 @@ class ApiQueryInfo extends ApiQueryBase {
                        return false;
                }
 
-               static $cachedOptionsToken = null;
-               if ( !is_null( $cachedOptionsToken ) ) {
-                       return $cachedOptionsToken;
+               // The token is always the same, let's exploit that
+               if ( !isset( ApiQueryInfo::$cachedTokens[ 'options' ] ) ) {
+                       ApiQueryInfo::$cachedTokens[ 'options' ] = $wgUser->getEditToken();
                }
 
-               $cachedOptionsToken = $wgUser->getEditToken();
-               return $cachedOptionsToken;
+               return ApiQueryInfo::$cachedTokens[ 'options' ];
        }
 
        public function execute() {
@@ -284,7 +283,10 @@ class ApiQueryInfo extends ApiQueryBase {
                }
 
                $this->pageRestrictions = $pageSet->getCustomField( 'page_restrictions' );
-               $this->pageIsRedir = $pageSet->getCustomField( 'page_is_redirect' );
+               // when resolving redirects, no page will have this field
+               $this->pageIsRedir = !$pageSet->isResolvingRedirects()
+                       ? $pageSet->getCustomField( 'page_is_redirect' )
+                       : array();
                $this->pageIsNew = $pageSet->getCustomField( 'page_is_new' );
 
                global $wgDisableCounters;
@@ -350,7 +352,7 @@ class ApiQueryInfo extends ApiQueryBase {
                                : intval( $this->pageCounter[$pageid] );
                        $pageInfo['length'] = intval( $this->pageLength[$pageid] );
 
-                       if ( $this->pageIsRedir[$pageid] ) {
+                       if ( isset( $this->pageIsRedir[$pageid] ) && $this->pageIsRedir[$pageid] ) {
                                $pageInfo['redirect'] = '';
                        }
                        if ( $this->pageIsNew[$pageid] ) {
@@ -433,15 +435,14 @@ class ApiQueryInfo extends ApiQueryBase {
                // Get normal protections for existing titles
                if ( count( $this->titles ) ) {
                        $this->resetQueryParams();
-                       $this->addTables( array( 'page_restrictions', 'page' ) );
-                       $this->addWhere( 'page_id=pr_page' );
+                       $this->addTables( 'page_restrictions' );
                        $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
-                                       'pr_expiry', 'pr_cascade', 'page_namespace',
-                                       'page_title' ) );
+                                       'pr_expiry', 'pr_cascade' ) );
                        $this->addWhereFld( 'pr_page', array_keys( $this->titles ) );
 
                        $res = $this->select( __METHOD__ );
                        foreach ( $res as $row ) {
+                               $title = $this->titles[$row->pr_page];
                                $a = array(
                                        'type' => $row->pr_type,
                                        'level' => $row->pr_level,
@@ -450,11 +451,14 @@ class ApiQueryInfo extends ApiQueryBase {
                                if ( $row->pr_cascade ) {
                                        $a['cascade'] = '';
                                }
-                               $this->protections[$row->page_namespace][$row->page_title][] = $a;
-
-                               // Also check old restrictions
-                               if ( $this->pageRestrictions[$row->pr_page] ) {
-                                       $restrictions = explode( ':', trim( $this->pageRestrictions[$row->pr_page] ) );
+                               $this->protections[$title->getNamespace()][$title->getDBkey()][] = $a;
+                       }
+                       // Also check old restrictions
+                       foreach( $this->titles as $pageId => $title ) {
+                               if ( $this->pageRestrictions[$pageId] ) {
+                                       $namespace = $title->getNamespace();
+                                       $dbKey = $title->getDBkey();
+                                       $restrictions = explode( ':', trim( $this->pageRestrictions[$pageId] ) );
                                        foreach ( $restrictions as $restrict ) {
                                                $temp = explode( '=', trim( $restrict ) );
                                                if ( count( $temp ) == 1 ) {
@@ -464,12 +468,12 @@ class ApiQueryInfo extends ApiQueryBase {
                                                        if ( $restriction == '' ) {
                                                                continue;
                                                        }
-                                                       $this->protections[$row->page_namespace][$row->page_title][] = array(
+                                                       $this->protections[$namespace][$dbKey][] = array(
                                                                'type' => 'edit',
                                                                'level' => $restriction,
                                                                'expiry' => 'infinity',
                                                        );
-                                                       $this->protections[$row->page_namespace][$row->page_title][] = array(
+                                                       $this->protections[$namespace][$dbKey][] = array(
                                                                'type' => 'move',
                                                                'level' => $restriction,
                                                                'expiry' => 'infinity',
@@ -479,7 +483,7 @@ class ApiQueryInfo extends ApiQueryBase {
                                                        if ( $restriction == '' ) {
                                                                continue;
                                                        }
-                                                       $this->protections[$row->page_namespace][$row->page_title][] = array(
+                                                       $this->protections[$namespace][$dbKey][] = array(
                                                                'type' => $temp[0],
                                                                'level' => $restriction,
                                                                'expiry' => 'infinity',