Merge "Allow setting the ID of the main table in HTMLForm"
[lhc/web/wiklou.git] / includes / api / ApiQueryInfo.php
index 7797205..5f8c497 100644 (file)
@@ -33,7 +33,8 @@ class ApiQueryInfo extends ApiQueryBase {
 
        private $fld_protection = false, $fld_talkid = false,
                $fld_subjectid = false, $fld_url = false,
-               $fld_readable = false, $fld_watched = false, $fld_notificationtimestamp = false,
+               $fld_readable = false, $fld_watched = false, $fld_watchers = false,
+               $fld_notificationtimestamp = false,
                $fld_preload = false, $fld_displaytitle = false;
 
        private $params, $titles, $missing, $everything, $pageCounter;
@@ -41,7 +42,8 @@ class ApiQueryInfo extends ApiQueryBase {
        private $pageRestrictions, $pageIsRedir, $pageIsNew, $pageTouched,
                $pageLatest, $pageLength;
 
-       private $protections, $watched, $notificationtimestamps, $talkids, $subjectids, $displaytitles;
+       private $protections, $watched, $watchers, $notificationtimestamps, $talkids, $subjectids, $displaytitles;
+       private $showZeroWatchers = false;
 
        private $tokenFunctions;
 
@@ -54,11 +56,11 @@ class ApiQueryInfo extends ApiQueryBase {
         * @return void
         */
        public function requestExtraData( $pageSet ) {
-               global $wgDisableCounters;
+               global $wgDisableCounters, $wgContentHandlerUseDB;
 
                $pageSet->requestField( 'page_restrictions' );
                // when resolving redirects, no page will have this field
-               if( !$pageSet->isResolvingRedirects() ) {
+               if ( !$pageSet->isResolvingRedirects() ) {
                        $pageSet->requestField( 'page_is_redirect' );
                }
                $pageSet->requestField( 'page_is_new' );
@@ -68,6 +70,9 @@ class ApiQueryInfo extends ApiQueryBase {
                $pageSet->requestField( 'page_touched' );
                $pageSet->requestField( 'page_latest' );
                $pageSet->requestField( 'page_len' );
+               if ( $wgContentHandlerUseDB ) {
+                       $pageSet->requestField( 'page_content_model' );
+               }
        }
 
        /**
@@ -248,6 +253,7 @@ class ApiQueryInfo extends ApiQueryBase {
                        $prop = array_flip( $this->params['prop'] );
                        $this->fld_protection = isset( $prop['protection'] );
                        $this->fld_watched = isset( $prop['watched'] );
+                       $this->fld_watchers = isset( $prop['watchers'] );
                        $this->fld_notificationtimestamp = isset( $prop['notificationtimestamp'] );
                        $this->fld_talkid = isset( $prop['talkid'] );
                        $this->fld_subjectid = isset( $prop['subjectid'] );
@@ -305,6 +311,10 @@ class ApiQueryInfo extends ApiQueryBase {
                        $this->getWatchedInfo();
                }
 
+               if ( $this->fld_watchers ) {
+                       $this->getWatcherInfo();
+               }
+
                // Run the talkid/subjectid query if requested
                if ( $this->fld_talkid || $this->fld_subjectid ) {
                        $this->getTSIDs();
@@ -314,6 +324,7 @@ class ApiQueryInfo extends ApiQueryBase {
                        $this->getDisplayTitle();
                }
 
+               /** @var $title Title */
                foreach ( $this->everything as $pageid => $title ) {
                        $pageInfo = $this->extractPageInfo( $pageid, $title );
                        $fit = $result->addValue( array(
@@ -331,7 +342,7 @@ class ApiQueryInfo extends ApiQueryBase {
 
        /**
         * Get a result array with information about a title
-        * @param $pageid int Page ID (negative for missing titles)
+        * @param int $pageid Page ID (negative for missing titles)
         * @param $title Title object
         * @return array
         */
@@ -340,6 +351,10 @@ class ApiQueryInfo extends ApiQueryBase {
                $titleExists = $pageid > 0; //$title->exists() needs pageid, which is not set for all title objects
                $ns = $title->getNamespace();
                $dbkey = $title->getDBkey();
+
+               $pageInfo['contentmodel'] = $title->getContentModel();
+               $pageInfo['pagelanguage'] = $title->getPageLanguage()->getCode();
+
                if ( $titleExists ) {
                        global $wgDisableCounters;
 
@@ -384,6 +399,14 @@ class ApiQueryInfo extends ApiQueryBase {
                        $pageInfo['watched'] = '';
                }
 
+               if ( $this->fld_watchers ) {
+                       if ( isset( $this->watchers[$ns][$dbkey] ) ) {
+                               $pageInfo['watchers'] = $this->watchers[$ns][$dbkey];
+                       } elseif ( $this->showZeroWatchers ) {
+                               $pageInfo['watchers'] = 0;
+                       }
+               }
+
                if ( $this->fld_notificationtimestamp ) {
                        $pageInfo['notificationtimestamp'] = '';
                        if ( isset( $this->notificationtimestamps[$ns][$dbkey] ) ) {
@@ -447,6 +470,7 @@ class ApiQueryInfo extends ApiQueryBase {
 
                        $res = $this->select( __METHOD__ );
                        foreach ( $res as $row ) {
+                               /** @var $title Title */
                                $title = $this->titles[$row->pr_page];
                                $a = array(
                                        'type' => $row->pr_type,
@@ -459,7 +483,7 @@ class ApiQueryInfo extends ApiQueryBase {
                                $this->protections[$title->getNamespace()][$title->getDBkey()][] = $a;
                        }
                        // Also check old restrictions
-                       foreach( $this->titles as $pageId => $title ) {
+                       foreach ( $this->titles as $pageId => $title ) {
                                if ( $this->pageRestrictions[$pageId] ) {
                                        $namespace = $title->getNamespace();
                                        $dbKey = $title->getDBkey();
@@ -582,6 +606,7 @@ class ApiQueryInfo extends ApiQueryBase {
        private function getTSIDs() {
                $getTitles = $this->talkids = $this->subjectids = array();
 
+               /** @var $t Title */
                foreach ( $this->everything as $t ) {
                        if ( MWNamespace::isTalk( $t->getNamespace() ) ) {
                                if ( $this->fld_subjectid ) {
@@ -675,6 +700,46 @@ class ApiQueryInfo extends ApiQueryBase {
                }
        }
 
+       /**
+        * Get the count of watchers and put it in $this->watchers
+        */
+       private function getWatcherInfo() {
+               global $wgUnwatchedPageThreshold;
+
+               if ( count( $this->everything ) == 0 ) {
+                       return;
+               }
+
+               $user = $this->getUser();
+               $canUnwatchedpages = $user->isAllowed( 'unwatchedpages' );
+               if ( !$canUnwatchedpages && !is_int( $wgUnwatchedPageThreshold ) ) {
+                       return;
+               }
+
+               $this->watchers = array();
+               $this->showZeroWatchers = $canUnwatchedpages;
+               $db = $this->getDB();
+
+               $lb = new LinkBatch( $this->everything );
+
+               $this->resetQueryParams();
+               $this->addTables( array( 'watchlist' ) );
+               $this->addFields( array( 'wl_title', 'wl_namespace', 'count' => 'COUNT(*)' ) );
+               $this->addWhere( array(
+                       $lb->constructSet( 'wl', $db )
+               ) );
+               $this->addOption( 'GROUP BY', array( 'wl_namespace', 'wl_title' ) );
+               if ( !$canUnwatchedpages ) {
+                       $this->addOption( 'HAVING', "COUNT(*) >= $wgUnwatchedPageThreshold" );
+               }
+
+               $res = $this->select( __METHOD__ );
+
+               foreach ( $res as $row ) {
+                       $this->watchers[$row->wl_namespace][$row->wl_title] = (int)$row->count;
+               }
+       }
+
        public function getCacheMode( $params ) {
                $publicProps = array(
                        'protection',
@@ -706,6 +771,7 @@ class ApiQueryInfo extends ApiQueryBase {
                                        'protection',
                                        'talkid',
                                        'watched', # private
+                                       'watchers', # private
                                        'notificationtimestamp', # private
                                        'subjectid',
                                        'url',
@@ -731,6 +797,7 @@ class ApiQueryInfo extends ApiQueryBase {
                                ' protection            - List the protection level of each page',
                                ' talkid                - The page ID of the talk page for each non-talk page',
                                ' watched               - List the watched status of each page',
+                               ' watchers              - The number of watchers, if allowed',
                                ' notificationtimestamp - The watchlist notification timestamp of each page',
                                ' 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',
@@ -759,11 +826,18 @@ class ApiQueryInfo extends ApiQueryBase {
                                'starttimestamp' => array(
                                        ApiBase::PROP_TYPE => 'timestamp',
                                        ApiBase::PROP_NULLABLE => true
-                               )
+                               ),
+                               'contentmodel' => 'string',
                        ),
                        'watched' => array(
                                'watched' => 'boolean'
                        ),
+                       'watchers' => array(
+                               'watchers' => array(
+                                       ApiBase::PROP_TYPE => 'integer',
+                                       ApiBase::PROP_NULLABLE => true
+                               )
+                       ),
                        'notificationtimestamp' => array(
                                'notificationtimestamp' => array(
                                        ApiBase::PROP_TYPE => 'timestamp',