Big blitz on unused variables (a lot of $db = $this->getDb() )
[lhc/web/wiklou.git] / includes / api / ApiQueryInfo.php
index 88d2166..896e200 100644 (file)
@@ -19,7 +19,7 @@
  *
  * 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.,
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
  */
 
@@ -38,7 +38,7 @@ class ApiQueryInfo extends ApiQueryBase {
        private $fld_protection = false, $fld_talkid = false,
                $fld_subjectid = false, $fld_url = false,
                $fld_readable = false, $fld_watched = false,
-               $fld_preload = false;
+               $fld_preload = false, $fld_displaytitle = false;
 
        public function __construct( $query, $moduleName ) {
                parent::__construct( $query, $moduleName, 'in' );
@@ -210,6 +210,7 @@ class ApiQueryInfo extends ApiQueryBase {
                        $this->fld_url = isset( $prop['url'] );
                        $this->fld_readable = isset( $prop['readable'] );
                        $this->fld_preload = isset( $prop['preload'] );
+                       $this->fld_displaytitle = isset( $prop['displaytitle'] );
                }
 
                $pageSet = $this->getPageSet();
@@ -246,13 +247,13 @@ class ApiQueryInfo extends ApiQueryBase {
                $this->pageLatest = $pageSet->getCustomField( 'page_latest' );
                $this->pageLength = $pageSet->getCustomField( 'page_len' );
 
-               $db = $this->getDB();
                // Get protection info if requested
                if ( $this->fld_protection ) {
                        $this->getProtectionInfo();
                }
 
                if ( $this->fld_watched ) {
+                       $this->getMain()->setVaryCookie();
                        $this->getWatchedInfo();
                }
 
@@ -260,6 +261,10 @@ class ApiQueryInfo extends ApiQueryBase {
                if ( $this->fld_talkid || $this->fld_subjectid ) {
                        $this->getTSIDs();
                }
+               
+               if ( $this->fld_displaytitle ) {
+                       $this->getDisplayTitle();
+               }
 
                foreach ( $this->everything as $pageid => $title ) {
                        $pageInfo = $this->extractPageInfo( $pageid, $title );
@@ -289,6 +294,7 @@ class ApiQueryInfo extends ApiQueryBase {
                        $pageInfo['lastrevid'] = intval( $this->pageLatest[$pageid] );
                        $pageInfo['counter'] = intval( $this->pageCounter[$pageid] );
                        $pageInfo['length'] = intval( $this->pageLength[$pageid] );
+
                        if ( $this->pageIsRedir[$pageid] ) {
                                $pageInfo['redirect'] = '';
                        }
@@ -298,6 +304,9 @@ class ApiQueryInfo extends ApiQueryBase {
                }
 
                if ( !is_null( $this->params['token'] ) ) {
+                       // Don't cache tokens
+                       $this->getMain()->setCachePrivate();
+                       
                        $tokenFunctions = $this->getTokenFunctions();
                        $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601, time() );
                        foreach ( $this->params['token'] as $t ) {
@@ -343,11 +352,21 @@ class ApiQueryInfo extends ApiQueryBase {
                        if ( $title->exists() ) {
                                $pageInfo['preload'] = '';
                        } else {
+                               $text = null;
                                wfRunHooks( 'EditFormPreloadText', array( &$text, &$title ) );
 
                                $pageInfo['preload'] = $text;
                        }
                }
+               
+               if ( $this->fld_displaytitle ) {
+                       if ( isset( $this->displaytitles[$title->getArticleId()] ) ) {
+                               $pageInfo['displaytitle'] = $this->displaytitles[$title->getArticleId()];
+                       } else {
+                               $pageInfo['displaytitle'] = $title->getPrefixedText();
+                       }
+               }
+
                return $pageInfo;
        }
 
@@ -369,7 +388,7 @@ class ApiQueryInfo extends ApiQueryBase {
                        $this->addWhereFld( 'pr_page', array_keys( $this->titles ) );
 
                        $res = $this->select( __METHOD__ );
-                       while ( $row = $db->fetchObject( $res ) ) {
+                       foreach ( $res as $row ) {
                                $a = array(
                                        'type' => $row->pr_type,
                                        'level' => $row->pr_level,
@@ -416,7 +435,6 @@ class ApiQueryInfo extends ApiQueryBase {
                                        }
                                }
                        }
-                       $db->freeResult( $res );
                }
 
                // Get protections for missing titles
@@ -427,14 +445,13 @@ class ApiQueryInfo extends ApiQueryBase {
                        $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
                        $this->addWhere( $lb->constructSet( 'pt', $db ) );
                        $res = $this->select( __METHOD__ );
-                       while ( $row = $db->fetchObject( $res ) ) {
+                       foreach ( $res as $row ) {
                                $this->protections[$row->pt_namespace][$row->pt_title][] = array(
                                        'type' => 'create',
                                        'level' => $row->pt_create_perm,
                                        'expiry' => Block::decodeExpiry( $row->pt_expiry, TS_ISO_8601 )
                                );
                        }
-                       $db->freeResult( $res );
                }
 
                // Cascading protections
@@ -461,7 +478,7 @@ class ApiQueryInfo extends ApiQueryBase {
                        $this->addWhereFld( 'pr_cascade', 1 );
 
                        $res = $this->select( __METHOD__ );
-                       while ( $row = $db->fetchObject( $res ) ) {
+                       foreach ( $res as $row ) {
                                $source = Title::makeTitle( $row->page_namespace, $row->page_title );
                                $this->protections[$row->tl_namespace][$row->tl_title][] = array(
                                        'type' => $row->pr_type,
@@ -470,7 +487,6 @@ class ApiQueryInfo extends ApiQueryBase {
                                        'source' => $source->getPrefixedText()
                                );
                        }
-                       $db->freeResult( $res );
                }
 
                if ( count( $images ) ) {
@@ -485,7 +501,7 @@ class ApiQueryInfo extends ApiQueryBase {
                        $this->addWhereFld( 'il_to', $images );
 
                        $res = $this->select( __METHOD__ );
-                       while ( $row = $db->fetchObject( $res ) ) {
+                       foreach ( $res as $row ) {
                                $source = Title::makeTitle( $row->page_namespace, $row->page_title );
                                $this->protections[NS_FILE][$row->il_to][] = array(
                                        'type' => $row->pr_type,
@@ -494,7 +510,6 @@ class ApiQueryInfo extends ApiQueryBase {
                                        'source' => $source->getPrefixedText()
                                );
                        }
-                       $db->freeResult( $res );
                }
        }
 
@@ -504,7 +519,7 @@ class ApiQueryInfo extends ApiQueryBase {
         */
        private function getTSIDs() {
                $getTitles = $this->talkids = $this->subjectids = array();
-               $db = $this->getDB();
+
                foreach ( $this->everything as $t ) {
                        if ( MWNamespace::isTalk( $t->getNamespace() ) ) {
                                if ( $this->fld_subjectid ) {
@@ -517,6 +532,8 @@ class ApiQueryInfo extends ApiQueryBase {
                if ( !count( $getTitles ) ) {
                        return;
                }
+               
+               $db = $this->getDB();
 
                // Construct a custom WHERE clause that matches
                // all titles in $getTitles
@@ -526,7 +543,7 @@ class ApiQueryInfo extends ApiQueryBase {
                $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
                $this->addWhere( $lb->constructSet( 'page', $db ) );
                $res = $this->select( __METHOD__ );
-               while ( $row = $db->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        if ( MWNamespace::isTalk( $row->page_namespace ) ) {
                                $this->talkids[MWNamespace::getSubject( $row->page_namespace )][$row->page_title] =
                                                intval( $row->page_id );
@@ -536,9 +553,30 @@ class ApiQueryInfo extends ApiQueryBase {
                        }
                }
        }
+       
+       private function getDisplayTitle() {
+               $this->displaytitles = array();
+               
+               $pageIds = array_keys( $this->titles );
+       
+               if ( !count( $pageIds ) ) {
+                       return;
+               }
+
+               $this->resetQueryParams();
+               $this->addTables( 'page_props' );
+               $this->addFields( array( 'pp_page', 'pp_value' ) );
+               $this->addWhereFld( 'pp_page', $pageIds );
+               $this->addWhereFld( 'pp_propname', 'displaytitle' );
+               $res = $this->select( __METHOD__ );
+               
+               foreach ( $res as $row ) {
+                       $this->displaytitles[$row->pp_page] = $row->pp_value;
+               }
+       }
 
        /**
-        * Get information about watched status and put it in $watched
+        * Get information about watched status and put it in $this->watched
         */
        private function getWatchedInfo() {
                global $wgUser;
@@ -564,7 +602,7 @@ class ApiQueryInfo extends ApiQueryBase {
 
                $res = $this->select( __METHOD__ );
 
-               while ( $row = $db->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        $this->watched[$row->page_namespace][$row->page_title] = true;
                }
        }
@@ -581,7 +619,8 @@ class ApiQueryInfo extends ApiQueryBase {
                                        'subjectid',
                                        'url',
                                        'readable',
-                                       'preload'
+                                       'preload',
+                                       'displaytitle',
                                ) ),
                        'token' => array(
                                ApiBase::PARAM_DFLT => null,
@@ -602,7 +641,8 @@ class ApiQueryInfo extends ApiQueryBase {
                                ' subjectid    - The page ID of the parent page for each talk page',
                                ' url          - Gives a full URL to the page, and also an edit URL',
                                ' readable     - Whether the user can read this page',
-                               ' preload      - Gives the text returned by EditFormPreloadText'
+                               ' preload      - Gives the text returned by EditFormPreloadText',
+                               ' displaytitle - Gives the way the page title is actually displayed',
                        ),
                        'token' => 'Request a token to perform a data-modifying action on a page',
                        'continue' => 'When more results are available, use this to continue',