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-02-07 01:52:58 +00:00
|
|
|
|
|
|
|
|
$this->getResult()->beginContinuation( $params['continue'], array(), array() );
|
|
|
|
|
|
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'] ) ) {
|
2014-05-13 03:00:48 +00:00
|
|
|
$pageSet->execute();
|
|
|
|
|
$res = $pageSet->getInvalidTitlesAndRevisions( array(
|
|
|
|
|
'invalidTitles',
|
|
|
|
|
'special',
|
|
|
|
|
'missingIds',
|
|
|
|
|
'missingRevIds',
|
|
|
|
|
'interwikiTitles'
|
|
|
|
|
) );
|
|
|
|
|
|
|
|
|
|
foreach ( $pageSet->getMissingTitles() as $title ) {
|
|
|
|
|
$r = $this->watchTitle( $title, $user, $params );
|
|
|
|
|
$r['missing'] = 1;
|
|
|
|
|
$res[] = $r;
|
2014-01-29 18:10:36 +00:00
|
|
|
}
|
2014-02-05 10:20:17 +00:00
|
|
|
|
2014-05-13 03:00:48 +00:00
|
|
|
foreach ( $pageSet->getGoodTitles() as $title ) {
|
|
|
|
|
$r = $this->watchTitle( $title, $user, $params );
|
|
|
|
|
$res[] = $r;
|
2014-01-29 18:10:36 +00:00
|
|
|
}
|
2014-05-13 03:00:48 +00:00
|
|
|
$this->getResult()->setIndexedTagName( $res, 'w' );
|
2014-01-29 18:10:36 +00:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
|
2014-08-14 18:15:23 +00:00
|
|
|
$this->logFeatureUsage( 'action=watch&title' );
|
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 );
|
2014-02-07 01:52:58 +00:00
|
|
|
$this->getResult()->endContinuation();
|
2014-01-29 18:10:36 +00:00
|
|
|
}
|
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
|
|
|
|
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
|
|
|
|
2013-06-13 17:56:29 +00:00
|
|
|
if ( !$status->isOK() ) {
|
2014-01-29 18:10:36 +00:00
|
|
|
if ( $compatibilityMode ) {
|
|
|
|
|
$this->dieStatus( $status );
|
|
|
|
|
}
|
2014-02-05 11:02:29 +00:00
|
|
|
$res['error'] = $this->getErrorFromStatus( $status );
|
2010-02-26 13:18:56 +00:00
|
|
|
}
|
2014-02-05 11:02:29 +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 );
|
|
|
|
|
}
|
2014-02-05 11:02:29 +00:00
|
|
|
|
2014-01-29 18:10:36 +00:00
|
|
|
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 '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,
|
2014-09-18 17:38:23 +00:00
|
|
|
'continue' => array(
|
|
|
|
|
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
|
|
|
|
|
),
|
2008-08-25 17:57:20 +00:00
|
|
|
);
|
2014-01-29 18:10:36 +00:00
|
|
|
if ( $flags ) {
|
|
|
|
|
$result += $this->getPageSet()->getFinalParams( $flags );
|
|
|
|
|
}
|
2014-02-05 11:02:29 +00:00
|
|
|
|
2014-01-29 18:10:36 +00:00
|
|
|
return $result;
|
2008-08-25 17:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
2014-10-28 17:17:02 +00:00
|
|
|
protected function getExamplesMessages() {
|
2008-08-25 17:57:20 +00:00
|
|
|
return array(
|
2014-09-18 17:38:23 +00:00
|
|
|
'action=watch&titles=Main_Page&token=123ABC'
|
|
|
|
|
=> 'apihelp-watch-example-watch',
|
|
|
|
|
'action=watch&titles=Main_Page&unwatch=&token=123ABC'
|
|
|
|
|
=> 'apihelp-watch-example-unwatch',
|
|
|
|
|
'action=watch&generator=allpages&gapnamespace=0&token=123ABC'
|
|
|
|
|
=> 'apihelp-watch-example-generator',
|
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
|
|
|
}
|