Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
@@ -11,16 +11,32 @@
|
||||
namespace IPS\core\api;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use DateInterval;
|
||||
use IPS\Api\Controller;
|
||||
use IPS\Api\Response;
|
||||
use IPS\Content\Search\ApiResponse;
|
||||
use IPS\Content\Search\ContentFilter;
|
||||
use IPS\Content\Search\Query;
|
||||
use IPS\core\modules\front\search\search as FrontModuleSearch;
|
||||
use IPS\DateTime;
|
||||
use IPS\Member;
|
||||
use IPS\Request;
|
||||
use IPS\Settings;
|
||||
use OutOfRangeException;
|
||||
use function defined;
|
||||
use function in_array;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Search API
|
||||
*/
|
||||
class _search extends \IPS\Api\Controller
|
||||
class search extends Controller
|
||||
{
|
||||
/**
|
||||
* GET /core/search
|
||||
@@ -49,21 +65,22 @@ class _search extends \IPS\Api\Controller
|
||||
* @apiparam string search_in Specify "titles" to search in titles only, otherwise titles and content are both searched
|
||||
* @apiparam int search_as Member ID to perform the search as (guest permissions will be used when this parameter is omitted)
|
||||
* @apiparam bool doNotTrack If doNotTrack is passed with a value of 1, the search will not be tracked for statistical purposes
|
||||
* @return array
|
||||
* @apireturn array
|
||||
* @apiresponse int page api_int_page
|
||||
* @apiresponse int perPage api_int_perpage
|
||||
* @apiresponse int totalResults api_int_totalresults
|
||||
* @apiresponse int totalPages api_int_totalpages
|
||||
* @apiresponse [\IPS\Content\Search\Result] results api_results_thispage
|
||||
* @note For requests using an OAuth Access Token for a particular member, only content the authorized user can view will be included and the "search_as" parameter will be ignored.
|
||||
* @return ApiResponse
|
||||
*/
|
||||
public function GETindex()
|
||||
public function GETindex(): ApiResponse
|
||||
{
|
||||
$memberPermissions = $this->member;
|
||||
|
||||
if( !$this->member AND isset( \IPS\Request::i()->search_as ) )
|
||||
if( !$this->member AND isset( Request::i()->search_as ) )
|
||||
{
|
||||
$memberPermissions = \IPS\Member::load( \IPS\Request::i()->search_as );
|
||||
$memberPermissions = Member::load( Request::i()->search_as );
|
||||
|
||||
if( !$memberPermissions->member_id )
|
||||
{
|
||||
@@ -72,48 +89,48 @@ class _search extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Get valid content types */
|
||||
$contentTypes = \IPS\core\modules\front\search\search::contentTypes( $memberPermissions ?: TRUE );
|
||||
$contentTypes = FrontModuleSearch::contentTypes( $memberPermissions ?: TRUE );
|
||||
|
||||
/* Initialize search */
|
||||
$query = \IPS\Content\Search\Query::init( $memberPermissions ?: NULL );
|
||||
$query = Query::init( $memberPermissions ?: NULL );
|
||||
|
||||
/* Set content type */
|
||||
if ( isset( \IPS\Request::i()->type ) and array_key_exists( \IPS\Request::i()->type, $contentTypes ) )
|
||||
if ( isset( Request::i()->type ) and array_key_exists( Request::i()->type, $contentTypes ) )
|
||||
{
|
||||
if ( isset( \IPS\Request::i()->item ) )
|
||||
if ( isset( Request::i()->item ) )
|
||||
{
|
||||
$class = $contentTypes[ \IPS\Request::i()->type ];
|
||||
$class = $contentTypes[ Request::i()->type ];
|
||||
try
|
||||
{
|
||||
$item = $class::loadAndCheckPerms( \IPS\Request::i()->item );
|
||||
$query->filterByContent( array( \IPS\Content\Search\ContentFilter::init( $class )->onlyInItems( array( \IPS\Request::i()->item ) ) ) );
|
||||
$item = $class::loadAndCheckPerms( Request::i()->item );
|
||||
$query->filterByContent( array( ContentFilter::init( $class )->onlyInItems( array( Request::i()->item ) ) ) );
|
||||
}
|
||||
catch ( \OutOfRangeException $e ) { }
|
||||
catch ( OutOfRangeException $e ) { }
|
||||
}
|
||||
else
|
||||
{
|
||||
$filter = \IPS\Content\Search\ContentFilter::init( $contentTypes[ \IPS\Request::i()->type ] );
|
||||
$filter = ContentFilter::init( $contentTypes[ Request::i()->type ] );
|
||||
|
||||
if ( isset( \IPS\Request::i()->nodes ) )
|
||||
if ( isset( Request::i()->nodes ) )
|
||||
{
|
||||
$filter->onlyInContainers( explode( ',', \IPS\Request::i()->nodes ) );
|
||||
$filter->onlyInContainers( explode( ',', Request::i()->nodes ) );
|
||||
}
|
||||
|
||||
if ( isset( \IPS\Request::i()->search_min_comments ) )
|
||||
if ( isset( Request::i()->search_min_comments ) )
|
||||
{
|
||||
$filter->minimumComments( \IPS\Request::i()->search_min_comments );
|
||||
$filter->minimumComments( Request::i()->search_min_comments );
|
||||
}
|
||||
if ( isset( \IPS\Request::i()->search_min_replies ) )
|
||||
if ( isset( Request::i()->search_min_replies ) )
|
||||
{
|
||||
$filter->minimumComments( \IPS\Request::i()->search_min_replies + 1 );
|
||||
$filter->minimumComments( Request::i()->search_min_replies + 1 );
|
||||
}
|
||||
if ( isset( \IPS\Request::i()->search_min_reviews ) )
|
||||
if ( isset( Request::i()->search_min_reviews ) )
|
||||
{
|
||||
$filter->minimumReviews( \IPS\Request::i()->search_min_reviews );
|
||||
$filter->minimumReviews( Request::i()->search_min_reviews );
|
||||
}
|
||||
if ( isset( \IPS\Request::i()->search_min_views ) )
|
||||
if ( isset( Request::i()->search_min_views ) )
|
||||
{
|
||||
$filter->minimumViews( \IPS\Request::i()->search_min_views );
|
||||
$filter->minimumViews( Request::i()->search_min_views );
|
||||
}
|
||||
|
||||
$query->filterByContent( array( $filter ) );
|
||||
@@ -121,9 +138,9 @@ class _search extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Filter by author */
|
||||
if ( isset( \IPS\Request::i()->author ) )
|
||||
if ( isset( Request::i()->author ) )
|
||||
{
|
||||
$author = \IPS\Member::load( \IPS\Request::i()->author, 'name' );
|
||||
$author = Member::load( Request::i()->author, 'name' );
|
||||
if ( $author->member_id )
|
||||
{
|
||||
$query->filterByAuthor( $author );
|
||||
@@ -131,9 +148,9 @@ class _search extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Filter by club */
|
||||
if ( isset( \IPS\Request::i()->club ) AND \IPS\Settings::i()->clubs )
|
||||
if ( isset( Request::i()->club ) AND Settings::i()->clubs )
|
||||
{
|
||||
$query->filterByClub( explode( ',', \IPS\Request::i()->club ) );
|
||||
$query->filterByClub( explode( ',', Request::i()->club ) );
|
||||
}
|
||||
|
||||
/* Set time cutoffs */
|
||||
@@ -141,38 +158,38 @@ class _search extends \IPS\Api\Controller
|
||||
{
|
||||
$beforeKey = "{$k}_before";
|
||||
$afterKey = "{$k}_after";
|
||||
if ( isset( \IPS\Request::i()->$beforeKey ) or isset( \IPS\Request::i()->$afterKey ) )
|
||||
if ( isset( Request::i()->$beforeKey ) or isset( Request::i()->$afterKey ) )
|
||||
{
|
||||
foreach ( array( 'before', 'after' ) as $l )
|
||||
{
|
||||
$$l = NULL;
|
||||
$key = "{$l}Key";
|
||||
if ( isset( \IPS\Request::i()->$$key ) AND \IPS\Request::i()->$$key != 'any' )
|
||||
if ( isset( Request::i()->$$key ) AND Request::i()->$$key != 'any' )
|
||||
{
|
||||
switch ( \IPS\Request::i()->$$key )
|
||||
switch ( Request::i()->$$key )
|
||||
{
|
||||
case 'day':
|
||||
$$l = \IPS\DateTime::create()->sub( new \DateInterval( 'P1D' ) );
|
||||
$$l = DateTime::create()->sub( new DateInterval( 'P1D' ) );
|
||||
break;
|
||||
|
||||
case 'week':
|
||||
$$l = \IPS\DateTime::create()->sub( new \DateInterval( 'P1W' ) );
|
||||
$$l = DateTime::create()->sub( new DateInterval( 'P1W' ) );
|
||||
break;
|
||||
|
||||
case 'month':
|
||||
$$l = \IPS\DateTime::create()->sub( new \DateInterval( 'P1M' ) );
|
||||
$$l = DateTime::create()->sub( new DateInterval( 'P1M' ) );
|
||||
break;
|
||||
|
||||
case 'six_months':
|
||||
$$l = \IPS\DateTime::create()->sub( new \DateInterval( 'P6M' ) );
|
||||
$$l = DateTime::create()->sub( new DateInterval( 'P6M' ) );
|
||||
break;
|
||||
|
||||
case 'year':
|
||||
$$l = \IPS\DateTime::create()->sub( new \DateInterval( 'P1Y' ) );
|
||||
$$l = DateTime::create()->sub( new DateInterval( 'P1Y' ) );
|
||||
break;
|
||||
|
||||
default:
|
||||
$$l = \IPS\DateTime::ts( \IPS\Request::i()->$$key );
|
||||
$$l = DateTime::ts( Request::i()->$$key );
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -183,44 +200,44 @@ class _search extends \IPS\Api\Controller
|
||||
}
|
||||
|
||||
/* Set Order */
|
||||
if ( ! isset( \IPS\Request::i()->sortby ) )
|
||||
if ( ! isset( Request::i()->sortby ) )
|
||||
{
|
||||
\IPS\Request::i()->sortby = $query->getDefaultSortMethod();
|
||||
Request::i()->sortby = $query->getDefaultSortMethod();
|
||||
}
|
||||
|
||||
switch( \IPS\Request::i()->sortby )
|
||||
switch( Request::i()->sortby )
|
||||
{
|
||||
case 'newest':
|
||||
$query->setOrder( \IPS\Content\Search\Query::ORDER_NEWEST_CREATED );
|
||||
$query->setOrder( Query::ORDER_NEWEST_CREATED );
|
||||
break;
|
||||
|
||||
case 'relevancy':
|
||||
$query->setOrder( \IPS\Content\Search\Query::ORDER_RELEVANCY );
|
||||
$query->setOrder( Query::ORDER_RELEVANCY );
|
||||
break;
|
||||
}
|
||||
|
||||
$flags = ( isset( \IPS\Request::i()->eitherTermsOrTags ) and \IPS\Request::i()->eitherTermsOrTags === 'and' ) ? \IPS\Content\Search\Query::TERM_AND_TAGS : \IPS\Content\Search\Query::TERM_OR_TAGS;
|
||||
$flags = ( isset( Request::i()->eitherTermsOrTags ) and Request::i()->eitherTermsOrTags === 'and' ) ? Query::TERM_AND_TAGS : Query::TERM_OR_TAGS;
|
||||
$operator = NULL;
|
||||
|
||||
if ( isset( \IPS\Request::i()->search_and_or ) and \in_array( \IPS\Request::i()->search_and_or, array( \IPS\Content\Search\Query::OPERATOR_OR, \IPS\Content\Search\Query::OPERATOR_AND ) ) )
|
||||
if ( isset( Request::i()->search_and_or ) and in_array( Request::i()->search_and_or, array( Query::OPERATOR_OR, Query::OPERATOR_AND ) ) )
|
||||
{
|
||||
$operator = \IPS\Request::i()->search_and_or;
|
||||
$operator = Request::i()->search_and_or;
|
||||
}
|
||||
|
||||
if ( isset( \IPS\Request::i()->search_in ) and \IPS\Request::i()->search_in === 'titles' )
|
||||
if ( isset( Request::i()->search_in ) and Request::i()->search_in === 'titles' )
|
||||
{
|
||||
$flags = $flags | \IPS\Content\Search\Query::TERM_TITLES_ONLY;
|
||||
$flags = $flags | Query::TERM_TITLES_ONLY;
|
||||
}
|
||||
|
||||
/* Return */
|
||||
return new \IPS\Content\Search\ApiResponse(
|
||||
return new ApiResponse(
|
||||
200,
|
||||
array( $query, $flags, isset( \IPS\Request::i()->q ) ? ( \IPS\Request::i()->q ) : NULL, isset( \IPS\Request::i()->tags ) ? explode( ',', \IPS\Request::i()->tags ) : NULL, $operator ),
|
||||
isset( \IPS\Request::i()->page ) ? \IPS\Request::i()->page : 1,
|
||||
array( $query, $flags, isset( Request::i()->q ) ? ( Request::i()->q ) : NULL, isset( Request::i()->tags ) ? explode( ',', Request::i()->tags ) : NULL, $operator ),
|
||||
isset( Request::i()->page ) ? Request::i()->page : 1,
|
||||
NULL,
|
||||
0,
|
||||
$this->member,
|
||||
isset( \IPS\Request::i()->perPage ) ? \IPS\Request::i()->perPage : NULL
|
||||
isset( Request::i()->perPage ) ? Request::i()->perPage : NULL
|
||||
);
|
||||
}
|
||||
|
||||
@@ -229,16 +246,17 @@ class _search extends \IPS\Api\Controller
|
||||
* Get list of content types that can be searched
|
||||
*
|
||||
* @clientapiparam int search_as Member ID to perform the search as (by default, search results are based on guest permissions)
|
||||
* @return array
|
||||
* @apireturn array
|
||||
* @apiresponse array contenttypes Content types that can be used in /search requests in the 'type' parameter
|
||||
* @return Response
|
||||
*/
|
||||
public function GETitem()
|
||||
public function GETitem(): Response
|
||||
{
|
||||
$memberPermissions = $this->member;
|
||||
|
||||
if( !$this->member AND isset( \IPS\Request::i()->search_as ) )
|
||||
if( !$this->member AND isset( Request::i()->search_as ) )
|
||||
{
|
||||
$memberPermissions = \IPS\Member::load( \IPS\Request::i()->search_as );
|
||||
$memberPermissions = Member::load( Request::i()->search_as );
|
||||
|
||||
if( !$memberPermissions->member_id )
|
||||
{
|
||||
@@ -246,6 +264,6 @@ class _search extends \IPS\Api\Controller
|
||||
}
|
||||
}
|
||||
|
||||
return new \IPS\Api\Response( 200, array( 'contenttypes' => array_keys( \IPS\core\modules\front\search\search::contentTypes( $memberPermissions ?: TRUE ) ) ) );
|
||||
return new Response( 200, array( 'contenttypes' => array_keys( FrontModuleSearch::contentTypes( $memberPermissions ?: TRUE ) ) ) );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user