*
* @file
*/
+use MediaWiki\Linker\LinkTarget;
/**
* A query module to show basic page information.
// If we're in a mode that breaks the same-origin policy, no tokens can
// be obtained
if ( $this->lacksSameOriginSecurity() ) {
- return array();
- }
-
- $this->tokenFunctions = array(
- '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' ),
- 'import' => array( 'ApiQueryInfo', 'getImportToken' ),
- 'watch' => array( 'ApiQueryInfo', 'getWatchToken' ),
- );
- Hooks::run( 'APIQueryInfoTokens', array( &$this->tokenFunctions ) );
+ return [];
+ }
+
+ $this->tokenFunctions = [
+ 'edit' => [ 'ApiQueryInfo', 'getEditToken' ],
+ 'delete' => [ 'ApiQueryInfo', 'getDeleteToken' ],
+ 'protect' => [ 'ApiQueryInfo', 'getProtectToken' ],
+ 'move' => [ 'ApiQueryInfo', 'getMoveToken' ],
+ 'block' => [ 'ApiQueryInfo', 'getBlockToken' ],
+ 'unblock' => [ 'ApiQueryInfo', 'getUnblockToken' ],
+ 'email' => [ 'ApiQueryInfo', 'getEmailToken' ],
+ 'import' => [ 'ApiQueryInfo', 'getImportToken' ],
+ 'watch' => [ 'ApiQueryInfo', 'getWatchToken' ],
+ ];
+ Hooks::run( 'APIQueryInfoTokens', [ &$this->tokenFunctions ] );
return $this->tokenFunctions;
}
- static protected $cachedTokens = array();
+ static protected $cachedTokens = [];
/**
* @deprecated since 1.24
*/
public static function resetTokenCache() {
- ApiQueryInfo::$cachedTokens = array();
+ ApiQueryInfo::$cachedTokens = [];
}
/**
$this->everything = $this->titles + $this->missing;
$result = $this->getResult();
- uasort( $this->everything, array( 'Title', 'compare' ) );
+ uasort( $this->everything, [ 'Title', 'compare' ] );
if ( !is_null( $this->params['continue'] ) ) {
// Throw away any titles we're gonna skip so they don't
// clutter queries
// 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' );
$this->pageTouched = $pageSet->getCustomField( 'page_touched' );
/** @var $title Title */
foreach ( $this->everything as $pageid => $title ) {
$pageInfo = $this->extractPageInfo( $pageid, $title );
- $fit = $pageInfo !== null && $result->addValue( array(
+ $fit = $pageInfo !== null && $result->addValue( [
'query',
'pages'
- ), $pageid, $pageInfo );
+ ], $pageid, $pageInfo );
if ( !$fit ) {
$this->setContinueEnumParameter( 'continue',
$title->getNamespace() . '|' .
* @return array|null
*/
private function extractPageInfo( $pageid, $title ) {
- $pageInfo = array();
+ $pageInfo = [];
// $title->exists() needs pageid, which is not set for all title objects
$titleExists = $pageid > 0;
$ns = $title->getNamespace();
}
if ( $this->fld_protection ) {
- $pageInfo['protection'] = array();
+ $pageInfo['protection'] = [];
if ( isset( $this->protections[$ns][$dbkey] ) ) {
$pageInfo['protection'] =
$this->protections[$ns][$dbkey];
}
ApiResult::setIndexedTagName( $pageInfo['protection'], 'pr' );
- $pageInfo['restrictiontypes'] = array();
+ $pageInfo['restrictiontypes'] = [];
if ( isset( $this->restrictionTypes[$ns][$dbkey] ) ) {
$pageInfo['restrictiontypes'] =
$this->restrictionTypes[$ns][$dbkey];
ApiResult::setIndexedTagName( $pageInfo['restrictiontypes'], 'rt' );
}
- if ( $this->fld_watched ) {
- $pageInfo['watched'] = isset( $this->watched[$ns][$dbkey] );
+ if ( $this->fld_watched && $this->watched !== null ) {
+ $pageInfo['watched'] = $this->watched[$ns][$dbkey];
}
if ( $this->fld_watchers ) {
- if ( isset( $this->watchers[$ns][$dbkey] ) ) {
+ if ( $this->watchers !== null && $this->watchers[$ns][$dbkey] !== 0 ) {
$pageInfo['watchers'] = $this->watchers[$ns][$dbkey];
} elseif ( $this->showZeroWatchers ) {
$pageInfo['watchers'] = 0;
}
if ( $this->fld_visitingwatchers ) {
- if ( isset( $this->visitingwatchers[$ns][$dbkey] ) ) {
+ if ( $this->visitingwatchers !== null && $this->visitingwatchers[$ns][$dbkey] !== 0 ) {
$pageInfo['visitingwatchers'] = $this->visitingwatchers[$ns][$dbkey];
} elseif ( $this->showZeroWatchers ) {
$pageInfo['visitingwatchers'] = 0;
if ( $this->fld_notificationtimestamp ) {
$pageInfo['notificationtimestamp'] = '';
- if ( isset( $this->notificationtimestamps[$ns][$dbkey] ) ) {
+ if ( $this->notificationtimestamps[$ns][$dbkey] ) {
$pageInfo['notificationtimestamp'] =
wfTimestamp( TS_ISO_8601, $this->notificationtimestamps[$ns][$dbkey] );
}
$pageInfo['preload'] = '';
} else {
$text = null;
- Hooks::run( 'EditFormPreloadText', array( &$text, &$title ) );
+ Hooks::run( 'EditFormPreloadText', [ &$text, &$title ] );
$pageInfo['preload'] = $text;
}
}
$user = $this->getUser();
- $pageInfo['actions'] = array();
+ $pageInfo['actions'] = [];
foreach ( $this->params['testactions'] as $action ) {
$this->countTestedActions++;
$pageInfo['actions'][$action] = $title->userCan( $action, $user );
*/
private function getProtectionInfo() {
global $wgContLang;
- $this->protections = array();
+ $this->protections = [];
$db = $this->getDB();
// Get normal protections for existing titles
if ( count( $this->titles ) ) {
$this->resetQueryParams();
$this->addTables( 'page_restrictions' );
- $this->addFields( array( 'pr_page', 'pr_type', 'pr_level',
- 'pr_expiry', 'pr_cascade' ) );
+ $this->addFields( [ 'pr_page', 'pr_type', 'pr_level',
+ 'pr_expiry', 'pr_cascade' ] );
$this->addWhereFld( 'pr_page', array_keys( $this->titles ) );
$res = $this->select( __METHOD__ );
foreach ( $res as $row ) {
/** @var $title Title */
$title = $this->titles[$row->pr_page];
- $a = array(
+ $a = [
'type' => $row->pr_type,
'level' => $row->pr_level,
'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 )
- );
+ ];
if ( $row->pr_cascade ) {
$a['cascade'] = true;
}
if ( $restriction == '' ) {
continue;
}
- $this->protections[$namespace][$dbKey][] = array(
+ $this->protections[$namespace][$dbKey][] = [
'type' => 'edit',
'level' => $restriction,
'expiry' => 'infinity',
- );
- $this->protections[$namespace][$dbKey][] = array(
+ ];
+ $this->protections[$namespace][$dbKey][] = [
'type' => 'move',
'level' => $restriction,
'expiry' => 'infinity',
- );
+ ];
} else {
$restriction = trim( $temp[1] );
if ( $restriction == '' ) {
continue;
}
- $this->protections[$namespace][$dbKey][] = array(
+ $this->protections[$namespace][$dbKey][] = [
'type' => $temp[0],
'level' => $restriction,
'expiry' => 'infinity',
- );
+ ];
}
}
}
$this->resetQueryParams();
$lb = new LinkBatch( $this->missing );
$this->addTables( 'protected_titles' );
- $this->addFields( array( 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ) );
+ $this->addFields( [ 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ] );
$this->addWhere( $lb->constructSet( 'pt', $db ) );
$res = $this->select( __METHOD__ );
foreach ( $res as $row ) {
- $this->protections[$row->pt_namespace][$row->pt_title][] = array(
+ $this->protections[$row->pt_namespace][$row->pt_title][] = [
'type' => 'create',
'level' => $row->pt_create_perm,
'expiry' => $wgContLang->formatExpiry( $row->pt_expiry, TS_ISO_8601 )
- );
+ ];
}
}
// Separate good and missing titles into files and other pages
// and populate $this->restrictionTypes
- $images = $others = array();
+ $images = $others = [];
foreach ( $this->everything as $title ) {
if ( $title->getNamespace() == NS_FILE ) {
$images[] = $title->getDBkey();
// Non-images: check templatelinks
$lb = new LinkBatch( $others );
$this->resetQueryParams();
- $this->addTables( array( 'page_restrictions', 'page', 'templatelinks' ) );
- $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
+ $this->addTables( [ 'page_restrictions', 'page', 'templatelinks' ] );
+ $this->addFields( [ 'pr_type', 'pr_level', 'pr_expiry',
'page_title', 'page_namespace',
- 'tl_title', 'tl_namespace' ) );
+ 'tl_title', 'tl_namespace' ] );
$this->addWhere( $lb->constructSet( 'tl', $db ) );
$this->addWhere( 'pr_page = page_id' );
$this->addWhere( 'pr_page = tl_from' );
$res = $this->select( __METHOD__ );
foreach ( $res as $row ) {
$source = Title::makeTitle( $row->page_namespace, $row->page_title );
- $this->protections[$row->tl_namespace][$row->tl_title][] = array(
+ $this->protections[$row->tl_namespace][$row->tl_title][] = [
'type' => $row->pr_type,
'level' => $row->pr_level,
'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ),
'source' => $source->getPrefixedText()
- );
+ ];
}
}
if ( count( $images ) ) {
// Images: check imagelinks
$this->resetQueryParams();
- $this->addTables( array( 'page_restrictions', 'page', 'imagelinks' ) );
- $this->addFields( array( 'pr_type', 'pr_level', 'pr_expiry',
- 'page_title', 'page_namespace', 'il_to' ) );
+ $this->addTables( [ 'page_restrictions', 'page', 'imagelinks' ] );
+ $this->addFields( [ 'pr_type', 'pr_level', 'pr_expiry',
+ 'page_title', 'page_namespace', 'il_to' ] );
$this->addWhere( 'pr_page = page_id' );
$this->addWhere( 'pr_page = il_from' );
$this->addWhereFld( 'pr_cascade', 1 );
$res = $this->select( __METHOD__ );
foreach ( $res as $row ) {
$source = Title::makeTitle( $row->page_namespace, $row->page_title );
- $this->protections[NS_FILE][$row->il_to][] = array(
+ $this->protections[NS_FILE][$row->il_to][] = [
'type' => $row->pr_type,
'level' => $row->pr_level,
'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ),
'source' => $source->getPrefixedText()
- );
+ ];
}
}
}
* and put them in $talkids and $subjectids
*/
private function getTSIDs() {
- $getTitles = $this->talkids = $this->subjectids = array();
+ $getTitles = $this->talkids = $this->subjectids = [];
/** @var $t Title */
foreach ( $this->everything as $t ) {
$lb = new LinkBatch( $getTitles );
$this->resetQueryParams();
$this->addTables( 'page' );
- $this->addFields( array( 'page_title', 'page_namespace', 'page_id' ) );
+ $this->addFields( [ 'page_title', 'page_namespace', 'page_id' ] );
$this->addWhere( $lb->constructSet( 'page', $db ) );
$res = $this->select( __METHOD__ );
foreach ( $res as $row ) {
}
private function getDisplayTitle() {
- $this->displaytitles = array();
+ $this->displaytitles = [];
$pageIds = array_keys( $this->titles );
$this->resetQueryParams();
$this->addTables( 'page_props' );
- $this->addFields( array( 'pp_page', 'pp_value' ) );
+ $this->addFields( [ 'pp_page', 'pp_value' ] );
$this->addWhereFld( 'pp_page', $pageIds );
$this->addWhereFld( 'pp_propname', 'displaytitle' );
$res = $this->select( __METHOD__ );
return;
}
- $this->watched = array();
- $this->notificationtimestamps = array();
- $db = $this->getDB();
-
- $lb = new LinkBatch( $this->everything );
+ $this->watched = [];
+ $this->notificationtimestamps = [];
- $this->resetQueryParams();
- $this->addTables( array( 'watchlist' ) );
- $this->addFields( array( 'wl_title', 'wl_namespace' ) );
- $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp );
- $this->addWhere( array(
- $lb->constructSet( 'wl', $db ),
- 'wl_user' => $user->getId()
- ) );
-
- $res = $this->select( __METHOD__ );
+ $store = WatchedItemStore::getDefaultInstance();
+ $timestamps = $store->getNotificationTimestampsBatch( $user, $this->everything );
- foreach ( $res as $row ) {
- if ( $this->fld_watched ) {
- $this->watched[$row->wl_namespace][$row->wl_title] = true;
- }
- if ( $this->fld_notificationtimestamp ) {
- $this->notificationtimestamps[$row->wl_namespace][$row->wl_title] =
- $row->wl_notificationtimestamp;
+ if ( $this->fld_watched ) {
+ foreach ( $timestamps as $namespaceId => $dbKeys ) {
+ $this->watched[$namespaceId] = array_map(
+ function( $x ) {
+ return $x !== false;
+ },
+ $dbKeys
+ );
}
}
+ if ( $this->fld_notificationtimestamp ) {
+ $this->notificationtimestamps = $timestamps;
+ }
}
/**
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' ) );
+ $countOptions = [];
if ( !$canUnwatchedpages ) {
- $this->addOption( 'HAVING', "COUNT(*) >= $unwatchedPageThreshold" );
+ $countOptions['minimumWatchers'] = $unwatchedPageThreshold;
}
- $res = $this->select( __METHOD__ );
-
- foreach ( $res as $row ) {
- $this->watchers[$row->wl_namespace][$row->wl_title] = (int)$row->count;
- }
+ $this->watchers = WatchedItemStore::getDefaultInstance()->countWatchersMultiple(
+ $this->everything,
+ $countOptions
+ );
}
/**
$this->showZeroWatchers = $canUnwatchedpages;
- // Assemble a WHERE condition to find:
- // * if the page exists, number of users watching who have
- // visited the page recently
- // * if the page doesn't exist, number of users that have
- // the page on their watchlist
- $whereStrings = array();
-
- // For pages that exist
+ $titlesWithThresholds = [];
if ( $this->titles ) {
$lb = new LinkBatch( $this->titles );
// Fetch last edit timestamps for pages
$this->resetQueryParams();
- $this->addTables( array( 'page', 'revision' ) );
- $this->addFields( array( 'page_namespace', 'page_title', 'rev_timestamp' ) );
- $this->addWhere( array(
+ $this->addTables( [ 'page', 'revision' ] );
+ $this->addFields( [ 'page_namespace', 'page_title', 'rev_timestamp' ] );
+ $this->addWhere( [
'page_latest = rev_id',
$lb->constructSet( 'page', $db ),
- ) );
- $this->addOption( 'GROUP BY', array( 'page_namespace', 'page_title' ) );
+ ] );
+ $this->addOption( 'GROUP BY', [ 'page_namespace', 'page_title' ] );
$timestampRes = $this->select( __METHOD__ );
- // Assemble SQL WHERE condition to find number of page watchers who also
- // visited a "recent" edit (last visited about 26 weeks before latest edit)
$age = $config->get( 'WatchersMaxAge' );
- $timestamps = array();
+ $timestamps = [];
foreach ( $timestampRes as $row ) {
$revTimestamp = wfTimestamp( TS_UNIX, (int)$row->rev_timestamp );
- $threshold = $db->timestamp( $revTimestamp - $age );
- $timestamps[$row->page_namespace][$row->page_title] = $threshold;
- }
-
- foreach ( $timestamps as $ns_key => $namespace ) {
- $pageStrings = array();
- foreach ( $namespace as $pg_key => $threshold ) {
- $pageStrings[] = "wl_title = '$pg_key' AND" .
- ' (wl_notificationtimestamp >= ' .
- $db->addQuotes( $threshold ) .
- ' OR wl_notificationtimestamp IS NULL)';
- }
- $whereStrings[] = "wl_namespace = '$ns_key' AND (" .
- $db->makeList( $pageStrings, LIST_OR ) . ')';
+ $timestamps[$row->page_namespace][$row->page_title] = $revTimestamp - $age;
}
+ $titlesWithThresholds = array_map(
+ function( LinkTarget $target ) use ( $timestamps ) {
+ return [
+ $target, $timestamps[$target->getNamespace()][$target->getDBkey()]
+ ];
+ },
+ $this->titles
+ );
}
- // For nonexistant pages
if ( $this->missing ) {
- $lb = new LinkBatch( $this->missing );
- $whereStrings[] = $lb->constructSet( 'wl', $db );
- }
-
- // Make the actual string and do the query
- $whereString = $db->makeList( $whereStrings, LIST_OR );
-
- $this->resetQueryParams();
- $this->addTables( array( 'watchlist' ) );
- $this->addFields( array(
- 'wl_namespace',
- 'wl_title',
- 'count' => 'COUNT(*)'
- ) );
- $this->addWhere( array( $whereString ) );
- $this->addOption( 'GROUP BY', array( 'wl_namespace', 'wl_title' ) );
- if ( !$canUnwatchedpages ) {
- $this->addOption( 'HAVING', "COUNT(*) >= $unwatchedPageThreshold" );
- }
-
- $res = $this->select( __METHOD__ );
- foreach ( $res as $row ) {
- $this->visitingwatchers[$row->wl_namespace][$row->wl_title] = (int)$row->count;
- }
+ $titlesWithThresholds = array_merge(
+ $titlesWithThresholds,
+ array_map(
+ function( LinkTarget $target ) {
+ return [ $target, null ];
+ },
+ $this->missing
+ )
+ );
+ }
+
+ $this->visitingwatchers = WatchedItemStore::getDefaultInstance()->countVisitingWatchersMultiple(
+ $titlesWithThresholds,
+ !$canUnwatchedpages ? $unwatchedPageThreshold : null
+ );
}
public function getCacheMode( $params ) {
// Other props depend on something about the current user
- $publicProps = array(
+ $publicProps = [
'protection',
'talkid',
'subjectid',
'url',
'preload',
'displaytitle',
- );
+ ];
if ( array_diff( (array)$params['prop'], $publicProps ) ) {
return 'private';
}
}
public function getAllowedParams() {
- return array(
- 'prop' => array(
+ return [
+ 'prop' => [
ApiBase::PARAM_ISMULTI => true,
- ApiBase::PARAM_TYPE => array(
+ ApiBase::PARAM_TYPE => [
'protection',
'talkid',
'watched', # private
'displaytitle',
// If you add more properties here, please consider whether they
// need to be added to getCacheMode()
- ),
- ApiBase::PARAM_HELP_MSG_PER_VALUE => array(),
- ),
- 'testactions' => array(
+ ],
+ ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
+ ],
+ 'testactions' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_ISMULTI => true,
- ),
- 'token' => array(
+ ],
+ 'token' => [
ApiBase::PARAM_DEPRECATED => true,
ApiBase::PARAM_ISMULTI => true,
ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() )
- ),
- 'continue' => array(
+ ],
+ 'continue' => [
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
- ),
- );
+ ],
+ ];
}
protected function getExamplesMessages() {
- return array(
+ return [
'action=query&prop=info&titles=Main%20Page'
=> 'apihelp-query+info-example-simple',
'action=query&prop=info&inprop=protection&titles=Main%20Page'
=> 'apihelp-query+info-example-protection',
- );
+ ];
}
public function getHelpUrls() {