2008-08-25 17:57:20 +00:00
|
|
|
<?php
|
2010-02-26 13:18:56 +00:00
|
|
|
/**
|
2010-12-22 20:52:06 +00:00
|
|
|
*
|
2008-08-25 17:57:20 +00:00
|
|
|
*
|
2010-08-07 19:59:42 +00:00
|
|
|
* Created on Jan 4, 2008
|
|
|
|
|
*
|
2012-07-15 20:13:02 +00:00
|
|
|
* Copyright © 2008 Yuri Astrakhan "<Firstname><Lastname>@gmail.com",
|
2008-08-25 17:57:20 +00:00
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* 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.,
|
2010-06-21 13:13:32 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2008-08-25 17:57:20 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-07 19:59:42 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
2008-08-25 17:57:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
2009-01-14 00:00:40 +00:00
|
|
|
* API module to allow users to watch a page
|
2008-08-25 17:57:20 +00:00
|
|
|
*
|
|
|
|
|
* @ingroup API
|
|
|
|
|
*/
|
|
|
|
|
class ApiWatch extends ApiBase {
|
2014-01-29 18:10:36 +00:00
|
|
|
private $mPageSet = null;
|
2008-08-25 17:57:20 +00:00
|
|
|
|
|
|
|
|
public function execute() {
|
2011-10-26 23:27:01 +00:00
|
|
|
$user = $this->getUser();
|
|
|
|
|
if ( !$user->isLoggedIn() ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' );
|
2010-02-26 13:18:56 +00:00
|
|
|
}
|
2014-01-29 18:10:36 +00:00
|
|
|
|
2013-06-13 18:02:55 +00:00
|
|
|
if ( !$user->isAllowed( 'editmywatchlist' ) ) {
|
|
|
|
|
$this->dieUsage( 'You don\'t have permission to edit your watchlist', 'permissiondenied' );
|
|
|
|
|
}
|
2012-08-01 03:19:12 +00:00
|
|
|
|
2008-08-25 17:57:20 +00:00
|
|
|
$params = $this->extractRequestParams();
|
2014-01-29 18:10:36 +00:00
|
|
|
$pageSet = $this->getPageSet();
|
|
|
|
|
// by default we use pageset to extract the page to work on.
|
|
|
|
|
// title is still supported for backward compatibility
|
|
|
|
|
if ( !isset( $params['title'] ) ) {
|
|
|
|
|
$pageSet->execute();
|
2014-02-05 10:20:17 +00:00
|
|
|
$res = $pageSet->getInvalidTitlesAndRevisions( array(
|
|
|
|
|
'invalidTitles',
|
|
|
|
|
'special',
|
|
|
|
|
'missingIds',
|
|
|
|
|
'missingRevIds',
|
|
|
|
|
'interwikiTitles'
|
|
|
|
|
) );
|
|
|
|
|
|
2014-01-29 18:10:36 +00:00
|
|
|
foreach ( $pageSet->getMissingTitles() as $title ) {
|
|
|
|
|
$r = $this->watchTitle( $title, $user, $params );
|
|
|
|
|
$r['missing'] = 1;
|
|
|
|
|
$res[] = $r;
|
|
|
|
|
}
|
2014-02-05 10:20:17 +00:00
|
|
|
|
2014-01-29 18:10:36 +00:00
|
|
|
foreach ( $pageSet->getGoodTitles() as $title ) {
|
|
|
|
|
$r = $this->watchTitle( $title, $user, $params );
|
|
|
|
|
$res[] = $r;
|
|
|
|
|
}
|
|
|
|
|
$this->getResult()->setIndexedTagName( $res, 'w' );
|
|
|
|
|
} else {
|
|
|
|
|
// dont allow use of old title parameter with new pageset parameters.
|
|
|
|
|
$extraParams = array_keys( array_filter( $pageSet->extractRequestParams(), function ( $x ) {
|
|
|
|
|
return $x !== null && $x !== false;
|
|
|
|
|
} ) );
|
2014-02-05 10:20:17 +00:00
|
|
|
|
2014-01-29 18:10:36 +00:00
|
|
|
if ( $extraParams ) {
|
|
|
|
|
$p = $this->getModulePrefix();
|
2014-02-05 10:20:17 +00:00
|
|
|
$this->dieUsage(
|
|
|
|
|
"The parameter {$p}title can not be used with " . implode( ", ", $extraParams ),
|
|
|
|
|
'invalidparammix'
|
|
|
|
|
);
|
2014-01-29 18:10:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$title = Title::newFromText( $params['title'] );
|
|
|
|
|
if ( !$title || !$title->isWatchable() ) {
|
|
|
|
|
$this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
|
|
|
|
|
}
|
|
|
|
|
$res = $this->watchTitle( $title, $user, $params, true );
|
|
|
|
|
}
|
|
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $res );
|
|
|
|
|
}
|
2012-08-01 03:19:12 +00:00
|
|
|
|
2014-02-05 10:20:17 +00:00
|
|
|
private function watchTitle( Title $title, User $user, array $params,
|
|
|
|
|
$compatibilityMode = false
|
|
|
|
|
) {
|
2014-01-29 18:10:36 +00:00
|
|
|
if ( !$title->isWatchable() ) {
|
|
|
|
|
return array( 'title' => $title->getPrefixedText(), 'watchable' => 0 );
|
2010-02-26 13:18:56 +00:00
|
|
|
}
|
2012-08-01 03:19:12 +00:00
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
$res = array( 'title' => $title->getPrefixedText() );
|
2010-01-23 22:26:40 +00:00
|
|
|
|
2013-11-14 13:31:56 +00:00
|
|
|
// Currently unnecessary, code to act as a safeguard against any change
|
|
|
|
|
// in current behavior of uselang.
|
2012-11-21 21:17:29 +00:00
|
|
|
// Copy from ApiParse
|
|
|
|
|
$oldLang = null;
|
2013-11-14 13:31:56 +00:00
|
|
|
if ( isset( $params['uselang'] ) &&
|
|
|
|
|
$params['uselang'] != $this->getContext()->getLanguage()->getCode()
|
|
|
|
|
) {
|
2012-11-21 21:17:29 +00:00
|
|
|
$oldLang = $this->getContext()->getLanguage(); // Backup language
|
|
|
|
|
$this->getContext()->setLanguage( Language::factory( $params['uselang'] ) );
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-26 13:18:56 +00:00
|
|
|
if ( $params['unwatch'] ) {
|
2013-06-13 17:56:29 +00:00
|
|
|
$status = UnwatchAction::doUnwatch( $title, $user );
|
2014-01-29 18:10:36 +00:00
|
|
|
if ( $status->isOK() ) {
|
|
|
|
|
$res['unwatched'] = '';
|
|
|
|
|
$res['message'] = $this->msg( 'removedwatchtext', $title->getPrefixedText() )
|
|
|
|
|
->title( $title )->parseAsBlock();
|
|
|
|
|
}
|
2010-02-26 13:18:56 +00:00
|
|
|
} else {
|
2013-06-13 17:56:29 +00:00
|
|
|
$status = WatchAction::doWatch( $title, $user );
|
2014-01-29 18:10:36 +00:00
|
|
|
if ( $status->isOK() ) {
|
|
|
|
|
$res['watched'] = '';
|
|
|
|
|
$res['message'] = $this->msg( 'addedwatchtext', $title->getPrefixedText() )
|
|
|
|
|
->title( $title )->parseAsBlock();
|
|
|
|
|
}
|
2008-08-25 17:57:20 +00:00
|
|
|
}
|
2012-11-21 21:17:29 +00:00
|
|
|
|
|
|
|
|
if ( !is_null( $oldLang ) ) {
|
|
|
|
|
$this->getContext()->setLanguage( $oldLang ); // Reset language to $oldLang
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-13 17:56:29 +00:00
|
|
|
if ( !$status->isOK() ) {
|
2014-01-29 18:10:36 +00:00
|
|
|
if ( $compatibilityMode ) {
|
|
|
|
|
$this->dieStatus( $status );
|
|
|
|
|
}
|
|
|
|
|
$res['error'] = $this->getErrorFromStatus( $status );
|
2010-02-26 13:18:56 +00:00
|
|
|
}
|
2014-01-29 18:10:36 +00:00
|
|
|
return $res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a cached instance of an ApiPageSet object
|
|
|
|
|
* @return ApiPageSet
|
|
|
|
|
*/
|
|
|
|
|
private function getPageSet() {
|
|
|
|
|
if ( $this->mPageSet === null ) {
|
|
|
|
|
$this->mPageSet = new ApiPageSet( $this );
|
|
|
|
|
}
|
|
|
|
|
return $this->mPageSet;
|
2008-08-25 17:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-21 16:38:40 +00:00
|
|
|
public function mustBePosted() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 13:49:44 +00:00
|
|
|
public function isWriteMode() {
|
|
|
|
|
return true;
|
2010-01-11 15:55:52 +00:00
|
|
|
}
|
2009-03-06 13:49:44 +00:00
|
|
|
|
2011-05-21 16:38:40 +00:00
|
|
|
public function needsToken() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getTokenSalt() {
|
|
|
|
|
return 'watch';
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-29 18:10:36 +00:00
|
|
|
public function getAllowedParams( $flags = 0 ) {
|
|
|
|
|
$result = array(
|
2010-08-04 20:47:58 +00:00
|
|
|
'title' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
2014-01-29 18:10:36 +00:00
|
|
|
ApiBase::PARAM_DEPRECATED => true
|
2010-08-04 20:47:58 +00:00
|
|
|
),
|
2008-08-25 17:57:20 +00:00
|
|
|
'unwatch' => false,
|
2012-11-21 21:17:29 +00:00
|
|
|
'uselang' => null,
|
2012-07-18 17:24:38 +00:00
|
|
|
'token' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
|
ApiBase::PARAM_REQUIRED => true
|
|
|
|
|
),
|
2008-08-25 17:57:20 +00:00
|
|
|
);
|
2014-01-29 18:10:36 +00:00
|
|
|
if ( $flags ) {
|
|
|
|
|
$result += $this->getPageSet()->getFinalParams( $flags );
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
2008-08-25 17:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getParamDescription() {
|
2014-01-29 18:10:36 +00:00
|
|
|
$psModule = $this->getPageSet();
|
|
|
|
|
return $psModule->getParamDescription() + array(
|
|
|
|
|
'title' => 'The page to (un)watch. use titles instead',
|
2008-08-25 17:57:20 +00:00
|
|
|
'unwatch' => 'If set the page will be unwatched rather than watched',
|
2012-11-21 21:17:29 +00:00
|
|
|
'uselang' => 'Language to show the message in',
|
2011-05-21 16:38:40 +00:00
|
|
|
'token' => 'A token previously acquired via prop=info',
|
2008-08-25 17:57:20 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
Added result properties to action=paraminfo
Added information about the properties of the results of API calls
to action=paraminfo, including information about "property groups":
what should the prop parameter be set to to get that property.
Uses the same format for types as parameters already do.
The output format of some modules doesn't fit this, so the result
properties for them weren't added, or only partially.
Partially implemented modules:
* expandtemplates:
parsetree is in its own tag
* protect, allusers, backlinks, deletedrevs, info, imageinfo,
logevents, querypage, recentchanges, revisions, searchinfo,
usercontribs, userinfo, users, watchlist, upload:
response with partially complex structure
Not implemented modules:
* feedcontributions, feedwatchlist, opensearch, rds:
non-standard reponse
* help:
error is normal response; not very useful for automated tools anyway
* paraminfo, parse, pageprops, siteinfo, userrights:
response with complex structure
Change-Id: Iff2a9bef79f994e73eef3062b4dd5461bff968ab
2012-05-02 15:00:30 +00:00
|
|
|
public function getResultProperties() {
|
|
|
|
|
return array(
|
|
|
|
|
'' => array(
|
|
|
|
|
'title' => 'string',
|
|
|
|
|
'unwatched' => 'boolean',
|
|
|
|
|
'watched' => 'boolean',
|
|
|
|
|
'message' => 'string'
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-25 17:57:20 +00:00
|
|
|
public function getDescription() {
|
2014-01-29 18:10:36 +00:00
|
|
|
return 'Add or remove pages from/to the current user\'s watchlist';
|
2008-08-25 17:57:20 +00:00
|
|
|
}
|
2010-02-26 13:18:56 +00:00
|
|
|
|
2010-02-13 00:28:27 +00:00
|
|
|
public function getPossibleErrors() {
|
|
|
|
|
return array_merge( parent::getPossibleErrors(), array(
|
|
|
|
|
array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
|
|
|
|
|
array( 'invalidtitle', 'title' ),
|
|
|
|
|
array( 'hookaborted' ),
|
|
|
|
|
) );
|
|
|
|
|
}
|
2008-08-25 17:57:20 +00:00
|
|
|
|
2011-08-17 22:24:21 +00:00
|
|
|
public function getExamples() {
|
2008-08-25 17:57:20 +00:00
|
|
|
return array(
|
2014-01-29 18:10:36 +00:00
|
|
|
'api.php?action=watch&titles=Main_Page' => 'Watch the page "Main Page"',
|
|
|
|
|
'api.php?action=watch&titles=Main_Page&unwatch=' => 'Unwatch the page "Main Page"',
|
2008-08-25 17:57:20 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-17 16:38:24 +00:00
|
|
|
public function getHelpUrls() {
|
2011-11-28 15:43:11 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/API:Watch';
|
2011-07-17 16:38:24 +00:00
|
|
|
}
|
2008-08-25 17:57:20 +00:00
|
|
|
}
|