Don't duplicate code, use wfAppendQuery
[lhc/web/wiklou.git] / includes / api / ApiQueryInfo.php
index 55e59dc..1715103 100644 (file)
@@ -63,12 +63,13 @@ class ApiQueryInfo extends ApiQueryBase {
                        return array();
 
                $this->tokenFunctions = array(
-                       'edit' => 'ApiQueryInfo::getEditToken',
-                       'delete' => 'ApiQueryInfo::getDeleteToken',
-                       'protect' => 'ApiQueryInfo::getProtectToken',
-                       'move' => 'ApiQueryInfo::getMoveToken',
-                       'block' => 'ApiQueryInfo::getBlockToken',
-                       'unblock' => 'ApiQueryInfo::getUnblockToken'
+                       'edit' => array( 'ApiQueryInfo', 'getEditToken' ),
+                       'delete' => array( 'ApiQueryInfo', 'getDeleteToken' ),
+                       'protect' => array( 'ApiQueryInfo', 'getProtectToken' ),
+                       'move' => array( 'ApiQueryInfo', 'getMoveToken' ),
+                       'block' => array( 'ApiQueryInfo', 'getBlockToken' ),
+                       'unblock' => array( 'ApiQueryInfo', 'getUnblockToken' ),
+                       'email' => array( 'ApiQueryInfo', 'getEmailToken' ),
                );
                wfRunHooks('APIQueryInfoTokens', array(&$this->tokenFunctions));
                return $this->tokenFunctions;
@@ -153,17 +154,33 @@ class ApiQueryInfo extends ApiQueryBase {
                return self::getBlockToken($pageid, $title);
        }
 
+       public static function getEmailToken($pageid, $title)
+       {
+               global $wgUser;
+               if(!$wgUser->canSendEmail() || $wgUser->isBlockedFromEmailUser())
+                       return false;
+
+               static $cachedEmailToken = null;
+               if(!is_null($cachedEmailToken))
+                       return $cachedEmailToken;
+
+               $cachedEmailToken = $wgUser->editToken();
+               return $cachedEmailToken;
+       }
+
        public function execute() {
 
                global $wgUser;
 
                $params = $this->extractRequestParams();
-               $fld_protection = $fld_talkid = $fld_subjectid = false;
+               $fld_protection = $fld_talkid = $fld_subjectid = $fld_url = $fld_readable = false;
                if(!is_null($params['prop'])) {
                        $prop = array_flip($params['prop']);
                        $fld_protection = isset($prop['protection']);
                        $fld_talkid = isset($prop['talkid']);
                        $fld_subjectid = isset($prop['subjectid']);
+                       $fld_url = isset($prop['url']);
+                       $fld_readable = isset($prop['readable']);
                }
 
                $pageSet = $this->getPageSet();
@@ -397,46 +414,51 @@ class ApiQueryInfo extends ApiQueryBase {
                        }
 
                        if($fld_protection) {
+                               $pageInfo['protection'] = array();
                                if (isset($protections[$pageid])) {
                                        $pageInfo['protection'] = $protections[$pageid];
                                        $result->setIndexedTagName($pageInfo['protection'], 'pr');
-                               } else {
-                                       # Also check old restrictions
-                                       if( $pageRestrictions[$pageid] ) {
-                                               foreach( explode( ':', trim( $pageRestrictions[$pageid] ) ) as $restrict ) {
-                                                       $temp = explode( '=', trim( $restrict ) );
-                                                       if(count($temp) == 1) {
-                                                               // old old format should be treated as edit/move restriction
-                                                               $restriction = trim( $temp[0] );
-                                                               $pageInfo['protection'][] = array(
-                                                                       'type' => 'edit',
-                                                                       'level' => $restriction,
-                                                                       'expiry' => 'infinity',
-                                                               );
-                                                               $pageInfo['protection'][] = array(
-                                                                       'type' => 'move',
-                                                                       'level' => $restriction,
-                                                                       'expiry' => 'infinity',
-                                                               );
-                                                       } else {
-                                                               $restriction = trim( $temp[1] );
-                                                               $pageInfo['protection'][] = array(
-                                                                       'type' => $temp[0],
-                                                                       'level' => $restriction,
-                                                                       'expiry' => 'infinity',
-                                                               );
-                                                       }
+                               }
+                               # Also check old restrictions
+                               if( $pageRestrictions[$pageid] ) {
+                                       foreach( explode( ':', trim( $pageRestrictions[$pageid] ) ) as $restrict ) {
+                                               $temp = explode( '=', trim( $restrict ) );
+                                               if(count($temp) == 1) {
+                                                       // old old format should be treated as edit/move restriction
+                                                       $restriction = trim( $temp[0] );
+                                                       $pageInfo['protection'][] = array(
+                                                               'type' => 'edit',
+                                                               'level' => $restriction,
+                                                               'expiry' => 'infinity',
+                                                       );
+                                                       $pageInfo['protection'][] = array(
+                                                               'type' => 'move',
+                                                               'level' => $restriction,
+                                                               'expiry' => 'infinity',
+                                                       );
+                                               } else {
+                                                       $restriction = trim( $temp[1] );
+                                                       $pageInfo['protection'][] = array(
+                                                               'type' => $temp[0],
+                                                               'level' => $restriction,
+                                                               'expiry' => 'infinity',
+                                                       );
                                                }
-                                               $result->setIndexedTagName($pageInfo['protection'], 'pr');
-                                       } else {
-                                               $pageInfo['protection'] = array();
                                        }
+                                       $result->setIndexedTagName($pageInfo['protection'], 'pr');
                                }
                        }
                        if($fld_talkid && isset($talkids[$title->getNamespace()][$title->getDbKey()]))
                                $pageInfo['talkid'] = $talkids[$title->getNamespace()][$title->getDbKey()];
                        if($fld_subjectid && isset($subjectids[$title->getNamespace()][$title->getDbKey()]))
                                $pageInfo['subjectid'] = $subjectids[$title->getNamespace()][$title->getDbKey()];
+                       if($fld_url) {
+                               $pageInfo['fullurl'] = $title->getFullURL();
+                               $pageInfo['editurl'] = $title->getFullURL('action=edit');
+                       }
+                       if($fld_readable)
+                               if($title->userCanRead())
+                                       $pageInfo['readable'] = '';
 
                        $result->addValue(array (
                                'query',
@@ -444,9 +466,9 @@ class ApiQueryInfo extends ApiQueryBase {
                        ), $pageid, $pageInfo);
                }
 
-               // Get edit/protect tokens and protection data for missing titles if requested
-               // Delete and move tokens are N/A for missing titles anyway
-               if(!is_null($params['token']) || $fld_protection || $fld_talkid || $fld_subjectid)
+               // Get properties for missing titles if requested
+               if(!is_null($params['token']) || $fld_protection || $fld_talkid || $fld_subjectid ||
+                                                       $fld_url || $fld_readable)
                {
                        $res = &$result->getData();
                        foreach($missing as $pageid => $title) {
@@ -474,6 +496,13 @@ class ApiQueryInfo extends ApiQueryBase {
                                        $res['query']['pages'][$pageid]['talkid'] = $talkids[$title->getNamespace()][$title->getDbKey()];
                                if($fld_subjectid && isset($subjectids[$title->getNamespace()][$title->getDbKey()]))
                                        $res['query']['pages'][$pageid]['subjectid'] = $subjectids[$title->getNamespace()][$title->getDbKey()];
+                               if($fld_url) {
+                                       $res['query']['pages'][$pageid]['fullurl'] = $title->getFullURL();
+                                       $res['query']['pages'][$pageid]['editurl'] = $title->getFullURL('action=edit');
+                               }
+                               if($fld_readable)
+                                       if($title->userCanRead())
+                                               $res['query']['pages'][$pageid]['readable'] = '';
                        }
                }
        }
@@ -486,7 +515,9 @@ class ApiQueryInfo extends ApiQueryBase {
                                ApiBase :: PARAM_TYPE => array (
                                        'protection',
                                        'talkid',
-                                       'subjectid'
+                                       'subjectid',
+                                       'url',
+                                       'readable',
                                )),
                        'token' => array (
                                ApiBase :: PARAM_DFLT => NULL,