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 {
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
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();
|
2012-08-01 03:19:12 +00:00
|
|
|
$title = Title::newFromText( $params['title'] );
|
|
|
|
|
|
2013-03-01 15:01:26 +00:00
|
|
|
if ( !$title || $title->isExternal() || !$title->canExist() ) {
|
2012-08-01 03:19:12 +00:00
|
|
|
$this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
|
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-03-04 08:44:38 +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;
|
|
|
|
|
if ( isset( $params['uselang'] ) && $params['uselang'] != $this->getContext()->getLanguage()->getCode() ) {
|
|
|
|
|
$oldLang = $this->getContext()->getLanguage(); // Backup language
|
|
|
|
|
$this->getContext()->setLanguage( Language::factory( $params['uselang'] ) );
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-26 13:18:56 +00:00
|
|
|
if ( $params['unwatch'] ) {
|
2008-08-25 17:57:20 +00:00
|
|
|
$res['unwatched'] = '';
|
2012-02-13 18:42:35 +00:00
|
|
|
$res['message'] = $this->msg( 'removedwatchtext', $title->getPrefixedText() )->title( $title )->parseAsBlock();
|
2013-06-13 17:56:29 +00:00
|
|
|
$status = UnwatchAction::doUnwatch( $title, $user );
|
2010-02-26 13:18:56 +00:00
|
|
|
} else {
|
2008-08-25 17:57:20 +00:00
|
|
|
$res['watched'] = '';
|
2012-02-13 18:42:35 +00:00
|
|
|
$res['message'] = $this->msg( 'addedwatchtext', $title->getPrefixedText() )->title( $title )->parseAsBlock();
|
2013-06-13 17:56:29 +00:00
|
|
|
$status = WatchAction::doWatch( $title, $user );
|
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() ) {
|
|
|
|
|
$this->dieStatus( $status );
|
2010-02-26 13:18:56 +00:00
|
|
|
}
|
2012-08-01 03:19:12 +00:00
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $res );
|
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';
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-25 17:57:20 +00:00
|
|
|
public function getAllowedParams() {
|
2010-02-26 13:18:56 +00:00
|
|
|
return array(
|
2010-08-04 20:47:58 +00:00
|
|
|
'title' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
2012-08-01 03:19:12 +00:00
|
|
|
ApiBase::PARAM_REQUIRED => 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
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getParamDescription() {
|
2010-02-26 13:18:56 +00:00
|
|
|
return array(
|
2008-08-25 17:57:20 +00:00
|
|
|
'title' => 'The page to (un)watch',
|
|
|
|
|
'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() {
|
2010-05-25 20:46:09 +00:00
|
|
|
return 'Add or remove a page 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(
|
2011-12-27 16:22:35 +00:00
|
|
|
'api.php?action=watch&title=Main_Page' => 'Watch the page "Main Page"',
|
|
|
|
|
'api.php?action=watch&title=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
|
|
|
}
|