using composer package format, decoupling bsr libraries and implementation

master
Guillermo Dev 7 years ago
parent adb8552602
commit 1478569426

@ -1,15 +0,0 @@
<?php
namespace BSR\Lib\Exception;
/**
* This exception should be raised when trying to access or
* manipulate a non-existing book.
*
* @package BSR\Lib\Exception
*/
class BookNotFoundException extends WebException {
public function __construct($code) {
parent::__construct('BookNotFound', "The book with code $code was not found.", 404);
}
}

@ -1,327 +0,0 @@
<?php
namespace BSR\Lib\Search;
use BSR\Lib\Configuration;
use BSR\Lib\Exception\WebException;
use BSR\Lib\Logger;
mb_http_output('UTF-8');
class BookSearch
{
/** @var \SolrClient */
private $client;
/** @var \SolrQuery */
private $query;
/** @var array parts of the query, parameter 'q' */
private $queryParts = array();
/** @var array parts of the filter query, parameter 'fq' */
private $filterQueryParts = array();
public function __construct($edismax = true)
{
$options = array
(
'hostname' => Configuration::get('solr.server'),
'port' => Configuration::get('solr.port'),
'login' => Configuration::get('solr.username'),
'password' => Configuration::get('solr.password'),
'path' => Configuration::get('solr.path'),
);
$this->client = new \SolrClient($options);
if($edismax) {
$this->query = new \SolrDisMaxQuery();
$this->query->useEDisMaxQueryParser();
} else {
$this->query = new \SolrQuery();
}
// most options like search fields, sorting, etc are already set
// as default in the Solr config and thus should be set only on a
// per request basis when needed
/* if sometime we need to set the fields explicitly, those should be the ones we want :
$this->query->addField('id, code, isbn');
$this->query->addField('editor, editorTown, year, producer, producerCode, availabilityDate, collection');
$this->query->addField('title, author, reader, summary');
$this->query->addField('jeunesse, genre, genreCode, motsMatieres, cdu');
$this->query->addField('media, mediaType, cover, samples, zip, zip_size');
*/
}
public function setHandler($handler)
{
$this->client->setServlet(\SolrClient::SEARCH_SERVLET_TYPE, $handler);
}
public function addCompoundQuery(array $texts, $field, $operator)
{
if(count($texts) > 0) {
$texts = array_map(array('SolrUtils', 'escapeQueryChars'), $texts);
$query = sprintf('%s:("%s")', $field, implode('" '.$operator.'"', $texts));
$this->addQuery($query, null, false);
}
}
public function addOrQuery(array $texts, $field)
{
$this->addCompoundQuery($texts, $field, 'OR');
}
public function addAndQuery(array $texts, $field)
{
$this->addCompoundQuery($texts, $field, 'AND');
}
public function addQuery($queryText, $queryField = null, $escape = true)
{
if($escape) {
$queryText = \SolrUtils::escapeQueryChars($queryText);
}
if($queryField == 'mediaType' and $queryText=='noCDS'){
$queryText='-mediaType:CDS';
} else if (strlen($queryField) > 0) {
$queryText = sprintf('%s:"%s"', $queryField, $queryText);
}
$this->queryParts[] = $queryText;
}
public function addFilterQuery($text, $field, $escape = true)
{
if($escape) {
$text = \SolrUtils::escapeQueryChars($text);
}
$this->filterQueryParts[] = sprintf('%s:"%s"', $field, $text);
}
public function addRange($field, $min = '*', $max = '*')
{
$this->filterQueryParts[] = sprintf('%s:[%s TO %s]', $field, $min, $max);
}
public function addSortField($field, $order = \SolrQuery::ORDER_DESC)
{
$this->query->addSortField($field, $order);
}
public function addFacetField($field)
{
$this->query->addFacetField($field);
}
public function setFacetRangeField($field)
{
$this->query->setParam('facet.range', $field);
}
public function setFacetLimits($limit = null, $count = null)
{
if(! is_null($limit)) {
$this->query->setFacetLimit($limit);
}
if(! is_null($count)) {
$this->query->setFacetMinCount($count);
}
}
public function setFacetRange($start, $end, $gap)
{
$this->query->setParam('facet.range.start', $start);
$this->query->setParam('facet.range.end', $end);
$this->query->setParam('facet.range.gap', $gap);
}
/**
* @param int $start
* @param int $count
* @param bool $facets activate faceting ?
* @param bool $spellcheck activate spellcheck ?
* @param bool $highlight activate highlighting ?
* @return array
* @throws WebException
*/
public function getResults($start = 0, $count = 15, $facets = false, $spellcheck = false, $highlight = false)
{
//Logger::log(print_r($this->queryParts, true), $verbosity = Logger::QUIET);
if (count($this->queryParts) == 0)
$query = '*:*';
else {
$query = implode(' AND ', $this->queryParts);
}
foreach($this->filterQueryParts as $fq) {
$this->query->addFilterQuery($fq);
}
$this->query->setQuery($query);
$this->query->setStart($start);
$this->query->setRows($count);
$this->query->setParam('facet', $facets ? 'true' : 'false');
$this->query->setParam('hl', $highlight ? 'true' : 'false');
$this->query->setParam('spellcheck', $spellcheck ? 'true' : 'false');
try {
$results = $this->client->query($this->query)->getArrayResponse();
} catch(\SolrException $e) {
throw new WebException ("SolrError", $e->getMessage(), -700);
}
$books = array();
if(isset($results['response']['docs']) && is_array($results['response']['docs'])) {
foreach($results['response']['docs'] as $r) {
$books[$r['id']] = $r;
}
}
$highlighting = array();
if(isset($results['highlighting'])) {
foreach($results['highlighting'] as $k => $h) {
$data = array();
foreach($h as $f => $v) {
$data[str_replace('_fr', '', $f)] = reset($v);
}
$highlighting[$k] = $data;
}
}
$spelling = array();
if(isset($results['spellcheck']['suggestions'])) {
foreach($results['spellcheck']['suggestions'] as $word => $s) {
$spelling[$word] = $s['suggestion'];
}
}
$facets = array();
if(isset($results['facet_counts']['facet_fields'])) {
foreach($results['facet_counts']['facet_fields'] as $f => $d) {
$facets[$f] = $d;
}
}
if(isset($results['facet_counts']['facet_ranges'])) {
$integer = strpos($this->query->getParam('facet.range.gap'), '.') === false;
foreach($results['facet_counts']['facet_ranges'] as $f => $d) {
if($integer) {
$facets[$f] = array();
foreach($d['counts'] as $k => $v) {
$facets[$f][intval($k)] = $v;
}
} else {
$facets[$f] = $d['counts'];
}
}
}
return array(
'count' => $results['response']['numFound'],
'facets' => array(
'facets' => $facets,
'highlighting' => $highlighting,
'spelling' => $spelling,
),
'books' => $books,
);
}
/**
* Return a list of suggested titles for the given text
* @param string $text
* @return array
* @throws WebException
*/
public function suggest($text) {
$this->query->setQuery($text);
$this->query->setStart(0);
$this->query->setRows(0);
$this->query->setParam('suggest', 'true');
try {
$results = $this->client->query($this->query)->getArrayResponse();
} catch(\SolrClientException $e) {
throw new WebException ("SolrError", $e->getMessage(), -700);
}
$text = mb_strtolower ($text, 'UTF-8');
$suggestions = array();
if(isset($results['suggest'])) {
foreach($results['suggest'] as $suggester) {
foreach($suggester[$text]['suggestions'] as $s) {
$s['term'] = strip_tags($s['term']);
$pos = strpos(mb_strtolower($s['term'], 'UTF-8'), $text);
if($pos !== false) {
// increase weight of proposition that have the words at the beginning
$s['weight'] += (int) ((1 - $pos / strlen($s['term'])) * 100);
}
$suggestions[$s['term']] = (array) $s;
}
}
}
usort($suggestions, function($a, $b) {
return $b['weight'] - $a['weight'];
});
return $suggestions;
}
/**
* Retrieve books from Solr based on their code (NoticeNr).
*
* @param array $codes
* @param string $field the field to use for the search
* @return array Books information
* @throws WebException
*/
public static function GetBooks(array $codes, $field = 'code') {
// it is faster to do multiple small request to Solr rather than one big so separate
// in chunks if we are above the limit. 15 was found by testing and seems to be a sweet spot
$limit = 15;
$count = count($codes);
if($count > $limit) {
$parts = array_chunk($codes, $limit);
$books = array();
foreach($parts as $p) {
// if we use array_merge here the numerical keys (book code) will be lost
$books += self::GetBooks($p, $field);
}
return $books;
}
$bs = new static();
$bs->addOrQuery($codes, $field);
$results = $bs->getResults(0, $count);
return $results['books'];
}
public static function GetTerms($field) {
$s = new BookSearch();
$s->addFilterQuery(1, 'visible');
$s->addFacetField($field);
$s->setFacetLimits(2000, 2);
$results = $s->getResults(0, 0, true);
return $results['facets']['facets'][$field];
}
public static function GetTermsRange($field) {
$s = new BookSearch();
$s->addFilterQuery(1, 'visible');
$s->setFacetRangeField($field);
$s->setFacetRange(0, 250 * 60, 30);
// to avoid useless calculation, only set this 'normal' facet
$s->addFacetField('visible');
$results = $s->getResults(0, 0, true);
return $results['facets']['facets'][$field];
}
}

@ -1,194 +0,0 @@
<?php
namespace BSR\Lib\db;
use BSR\Lib\Configuration;
use BSR\Lib\Exception\SqlException;
class Connection
{
// Internal variable to hold the connection
private static $db;
final private function __construct() {}
/**
* @param $query
* @param bool $throw_error
* @return OdbcResultSet|resource|string
* @throws SqlException
*/
public static function execute($query, $throw_error = false)
{
$result = odbc_exec(self::get(), utf8_decode($query));
$result = new OdbcResultSet($result);
if ($result->is_error()) {
if($throw_error) {
throw new SqlException($result->get_error(), $query);
}
return $result->get_error();
}
return $result;
}
public static function get()
{
if (is_null(self::$db)) {
$dsn = sprintf(
"Driver={%s};Server=%s,%s;Database=%s;",
Configuration::get('db.driver'),
Configuration::get('db.server'),
Configuration::get('db.port'),
Configuration::get('db.name')
);
self::$db = odbc_pconnect($dsn, Configuration::get('db.username'), Configuration::get('db.password'));
if (self::$db === false) {
throw new SqlException("Unable to connect to the server.");
}
}
// Return the connection
return self::$db;
}
final private function __clone() {}
}
class OdbcResultSet implements \Iterator, \ArrayAccess
{
public $length;
private $results;
private $error;
private $num_fields;
private $num_rows;
private $cursor_index;
public function __construct($odbc_result)
{
if ($odbc_result === false) {
$this->error = odbc_errormsg(Connection::get());
} else {
try {
$this->results = array();
$this->num_fields = odbc_num_fields($odbc_result);
$this->num_rows = odbc_num_rows($odbc_result);
if ($this->num_fields > 0) {
while ($row = odbc_fetch_row($odbc_result)) {
$data = array();
for ($i = 1; $i <= $this->num_fields; ++$i) {
$data[odbc_field_name($odbc_result, $i)] = utf8_encode(odbc_result($odbc_result, $i));
}
$this->results[] = $data;
}
};
} catch (\Exception $e) {
print($e->getMessage());
}
$this->cursor_index = 0;
$this->length = count($this->results);
odbc_free_result($odbc_result);
}
}
public function get_num_rows()
{
return $this->num_rows;
}
public function is_error()
{
return ($this->error ? true : false);
}
public function get_error()
{
return $this->error;
}
public function get_row()
{
return $this->current();
}
public function to_array()
{
return $this->results;
}
// ArrayAccess
/**
* @param int $offset
* @return bool
*/
public function offsetExists($offset)
{
return !$this->error && $this->cursor_index < $this->length && $this->cursor_index >= 0;
}
/**
* @param int $offset
* @return bool|array
*/
public function offsetGet($offset)
{
return $this->offsetExists($offset) ? $this->results[$offset] : false;
}
public function offsetSet($offset, $value)
{
if($this->offsetExists($offset)) {
$this->results[$offset] = $value;
}
}
public function offsetUnset($offset)
{
throw new \RuntimeException("This makes no sense at all.");
}
// Iterator
/**
* @return bool|array
*/
public function current()
{
return $this->offsetGet($this->cursor_index);
}
/**
* @return int
*/
public function key()
{
return $this->cursor_index;
}
/**
* @return array|bool
*/
public function next()
{
$current = $this->current();
++$this->cursor_index;
return $current;
}
public function rewind()
{
$this->cursor_index = 0;
}
/**
* @return bool
*/
public function valid()
{
return $this->offsetExists($this->cursor_index);
}
}

@ -1,63 +0,0 @@
<?php
namespace BSR\Lib\db;
class DbHelper
{
/**
* Retrieve the list of all type available in the database.
* @param boolean $withJeunesse add 'Jeunesse' to the list
* @return array
*/
public static function ListOfGenres($withJeunesse = false)
{
$sql = "SELECT DISTINCT
LTRIM(RTRIM(Codes.Code)) as code,
LTRIM(RTRIM(Codes.TextFre)) AS text
FROM Codes
INNER JOIN Notices ON Codes.Code = Notices.MediaType2Code
WHERE
Codes.Type = 2
AND Notices.NoticeNr NOT LIKE '%~%'
AND Notices.NoticeNr NOT LIKE '%V%'
AND Notices.NoticeNr NOT LIKE '%T%'
AND Codes.TextFre !=''
AND Notices.MediaType1Code = 'CDD'
ORDER BY text;";
$results = Connection::execute($sql)->to_array();
if($withJeunesse) {
array_unshift($results, array('code' => 'J', 'text' => 'Jeunesse'));
}
return $results;
}
/**
* Retrieve the list of all books currently lent to readers.
*/
public static function InReading()
{
$sql = "SELECT
NoticeNr, title, author, displayName
FROM notices, items, circulations, UserAccounts
WHERE
MediaType1code='N' and NoticeNr not like '%~%'
AND items.NoticeID = notices.NoticeID
AND items.ItemID = circulations.ItemID
AND UserAccounts.UserAccountID = circulations.UserAccountID
ORDER BY author, title;";
$results = Connection::execute($sql);
return array_map(function($row) {
return array(
"NoticeNr" => $row['NoticeNr'],
"auteur" => $row['author'],
"titre" => $row['title'],
"lecteur" => $row['displayName']
);
}, $results->to_array());
}
}

@ -1,117 +0,0 @@
<?php
namespace BSR\Lib\db;
use BSR\Lib\Exception\InvalidAttributeException;
/**
* Base class for mapping objects. inherit you database filled objects from here.
*
* @property int $id
*/
abstract class DbMapping
{
protected $attributes;
protected $attributeNames = '';
protected $privateAttributeNames = '';
/**
* @param array $attributes
*/
public function __construct(array $attributes)
{
$this->setAttributes($attributes);
}
/**
* Define a bunch of attribute given by an associative array
* @param array $attributes
*/
public function setAttributes(array $attributes)
{
$this->assertAttributes($attributes);
foreach ($attributes as $key => $value) {
$this->__set($key, $value);
}
}
/**
* Ensure that all keys from attributes are authorized
* @param array $attributes
*/
private function assertAttributes(array $attributes)
{
foreach ($attributes as $key => $value) {
$this->assertAttribute($key);
}
}
/**
* Ensure that name attribute is authorized
* If public_only is false, check against PRIVATE_ATTRIBUTES_NAME too.
* Those one cannot be accessed via setAttributes and other batch methods.
* @param string $name
* @param bool $public_only
* @throws InvalidAttributeException if the attribute is not a valid one
*/
private function assertAttribute($name, $public_only = TRUE)
{
if (strpos($this->attributeNames, $name) === false && ($public_only || strpos($this->privateAttributeNames, $name) === false)) {
throw(new InvalidAttributeException("The attribute $name is invalid"));
}
}
/**
* Get a user attribute or the linked wishes
* @param string $name
* @return mixed
*/
public function __get($name)
{
$sql_safe = FALSE;
if (strpos($name, 'sql_') === 0) {
$name = substr($name, 4);
$sql_safe = TRUE;
}
$this->assertAttribute($name, false);
if (isset($this->attributes[$name])) {
$value = $this->attributes[$name];
if ($sql_safe) {
$value = str_replace("'", "''", $value);
}
return $value;
} else {
return NULL;
}
}
public function to_array() {
return $this->attributes;
}
/**
* Set a user attribute
* @param string $name
* @param mixed $value
*/
public function __set($name, $value)
{
$this->assertAttribute($name, false);
$this->attributes[$name] = $value;
}
/**
* Return all the public attributes in an array;
*/
public function toArray()
{
$result = array();
foreach ($this->attributes as $name => $value) {
if (strpos($this->attributeNames, $name) !== false) {
$result[$name] = $value;
}
}
return $result;
}
}

@ -1,330 +0,0 @@
<?php
namespace BSR\Lib\db;
use BSR\Lib\Configuration;
use BSR\Lib\Search\BookSearch;
use BSR\Lib\Logger;
/**
* User is mapped on the UserAccounts table. Contains user information : id, login, firstName, lastName, displayName.
*
* @property int id
* @property string $login
* @property string $sql_login
* @property string $password
* @property string $sql_password
* @property string $privatePhone
* @property string $sql_privatePhone
* @property string $officePhone
* @property string $sql_officePhone
* @property string $mobilePhone
* @property string $sql_mobilePhone
* @property string $addressId
* @property string $sql_addressId
* @property string $displayName
* @property string $sql_displayName
* @property string $firstName
* @property string $sql_firstName
* @property string $lastName
* @property string $sql_lastName
* @property string $mail
* @property string $sql_mail
*/
class User extends DbMapping
{
protected $attributeNames = 'id login firstName lastName displayName freeOne mail addressId mobilePhone officePhone privatePhone';
protected $privateAttributeNames = 'password';
/**
* @param string $login Login for the user
* @param string $password Password for the user
* @return User|null User object if we were able to authenticate
*/
public static function authenticate($login, $password)
{
$password = str_replace("'", "''", $password);
return User::find($login, " UPPER(password) = UPPER('$password') ", false);
}
/**
* Retrieve a user by its login. Do not represent a valid authentication.
*
* Cond has to be safe because no check are made inside.
*
* @param string $login login the login name
* @param string $cond a condition to restrict the choice, optional
* @param bool $raiseError
* @return User the User object or NULL if no user found.
*/
public static function find($login, $cond = '', $raiseError = true)
{
$login = str_replace("'", "''", $login);
if(strlen($cond) > 0) {
$cond = " AND $cond";
}
$sql = sprintf("SELECT TOP 1
[FirstName] AS firstName,
[LastName] AS lastName,
[DisplayName] AS displayName,
[UserDefined1] AS freeOne,
[ActualAddressID] AS addressId,
[Email] AS mail,
[TelephoneMobile] AS mobilePhone,
[TelephonePrivate] AS privatePhone,
[Telephone] AS officePhone,
[UserAccountID] AS id,
REPLACE(UserAccountNr, ' ', '') AS login
FROM [UserAccounts] AS u
LEFT JOIN [Addresses] AS a ON a.[AddressID] = u.[ActualAddressID]
WHERE LTRIM(RTRIM(UserAccountNr)) = '%s' AND disabled = 1 %s AND CategoryCode in ('A', 'M', 'G', 'D', 'I', 'EMP');",
$login, $cond);
$results = Connection::execute($sql, $raiseError);
return $results->current() !== false ? new User($results->current()) : null;
}
/**
* Circulations as needed for the BSR internal tools
*/
public function GetCirculations()
{
$sql = sprintf("SELECT
n.NoticeID,
ItemNr,
LTRIM(RTRIM(n.NoticeNr)) AS code,
LTRIM(RTRIM(n.Title)) AS Title,
LTRIM(RTRIM(n.Author)) AS author,
Fields.[300] AS media,
Fields.[901] AS readBy
FROM Circulations AS c
INNER JOIN Items AS i ON i.ItemId = c.ItemId
INNER JOIN Notices AS n ON n.NoticeID = i.NoticeID
LEFT OUTER JOIN (
SELECT *
FROM (
SELECT
NoticeID,
Tag AS Field,
NoticeFields.ContentShortPart AS Data
FROM NoticeFields
WHERE Tag IN ('901', '300')
) AS src
PIVOT (
MIN(Data)
FOR Field IN ([901], [300])
) AS pvt
) Fields ON n.NoticeID = Fields.NoticeID
WHERE
c.UserAccountID = %s
ORDER BY ItemNr ASC", $this->id);
$result = Connection::execute($sql);
return $result ? $result->to_array() : array();
}
public function GetOldLoansNrs()
{
$sql = sprintf("SELECT
n.NoticeNr
FROM OldCirculations AS c
INNER JOIN Items AS i ON i.ItemId = c.ItemId
INNER JOIN Notices AS n ON n.NoticeID = i.NoticeID
WHERE
c.UserAccountID = %s AND
n.MediaType1Code in ('CDD', 'CDA', 'DVD', 'CDS') AND n.deleted=1
ORDER BY ItemNr ASC", $this->id);
$result = Connection::execute($sql);
return $result ? $result->to_array() : array();
}
public function getLoansData($table, $sort = "acquisitiondate DESC")
{
$sql = sprintf("SELECT top 50
realn.NoticeId as NoticeID,
realn.NoticeNr,
CheckOutDate,
c.Remark,
ItemNr
FROM %s AS c
INNER JOIN Items AS i ON i.ItemId = c.ItemId
INNER JOIN Notices AS lentn ON lentn.NoticeID = i.NoticeID
INNER JOIN Notices AS realn ON REPLACE(ltrim(rtrim(lentn.noticenr)), 'V', '') = ltrim(rtrim(realn.noticenr))
WHERE
c.UserAccountID = %s
ORDER BY %s", $table, $this->id, $sort);
return Connection::execute($sql)->to_array();
}
private function _getLoans($table, $count, $sort)
{
$circulations = $this->getLoansData($table, $sort);
//Logger::log(print_r($circulations, true));
// getting the intval of the NoticeNr will remove any 'V' or 'T' and thus we will have no issues with
// the virtual books that are used for Downloads and so.
$codes = array_unique(array_map(function($c) {
return trim($c['NoticeNr']); }, $circulations));
if($count) {
return count($circulations);
}
$books = count($codes) > 0 ? BookSearch::GetBooks($codes) : array();
//Logger::log(print_r($books, true));
foreach($circulations as $c) {
$id = $c['NoticeID'];
if(isset($books[$id])) {
$books[$id]['checkoutDate'] = $c['CheckOutDate'];
$books[$id]['remark'] = $c['Remark'];
}
}
return $books;
}
/**
* Loans (Circulations) as needed on the website
* @param boolean $count return only the count
* @return array
*/
public function GetLoans($count = false)
{
return $this->_getLoans('Circulations', $count, "ItemNr ASC");
}
/**
* Old loans (OldCirculations) as needed on the website
* @param boolean $count return only the count
* @return array
*/
public function GetOldLoans($count = false)
{
return $this->_getLoans('OldCirculations', $count, 'CheckOutDate DESC');
}
/**
* Books eligible for feedback by the user. Lent or downloaded more than 2 weeks ago and less than 5 monthes.
* @return array
*/
public function GetBooksForFeedback()
{
$sql = sprintf("SELECT n.NoticeNr
FROM OldCirculations AS c
INNER JOIN Items AS i ON i.ItemId = c.ItemId
INNER JOIN Notices AS n ON n.NoticeID = i.NoticeID
WHERE
c.UserAccountID = %s
AND DATEDIFF(month, CheckOutDate, GETDATE()) < 5
", $this->id);
return Connection::execute($sql)->to_array(); }
/**
* Add a book to the wish list if it is not already inside.
* @param string $noticeNr
* @return bool
*/
public function addWish($noticeNr)
{
if ($this->hasWish($noticeNr)) {
return false;
}
$sql = "UPDATE Counters
SET WishID = WishID + 1
OUTPUT INSERTED.WishID;";
$result = Connection::execute($sql, true);
$row = $result->current();
$employee_id = Configuration::get('www_employee_id');
$library_id = Configuration::get('www_library_id');
$sql = sprintf("INSERT INTO Wishes
(WishID, NoticeID, UserAccountID, CreationDate, EmployeeID, BranchOfficeID, Remark, ModificationDate)
SELECT %s , NoticeID, %s, GETDATE() , %s , %s , '' , GETDATE()
FROM Notices
WHERE LTRIM(RTRIM(NoticeNr)) = '%s';",
$row['WishID'], $this->id, $employee_id, $library_id, $noticeNr);
$status = Connection::execute($sql);
return $status && ! $status->is_error() && $status->get_num_rows() > 0;
}
/**
* Return true if the book is in the wish list
* @param string $noticeNr
* @return bool
*/
private function hasWish($noticeNr)
{
$sql = sprintf("SELECT w.NoticeID
FROM Wishes AS w
INNER JOIN Notices AS n ON n.NoticeID = w.NoticeID
WHERE
LTRIM(RTRIM(n.NoticeNr)) = '%s'
AND w.UserAccountID = %s;", $noticeNr, $this->id);
$result = Connection::execute($sql);
return $result->current() !== false;
}
/**
* Wishes are all the books that this user want to read.
* @param boolean $count return only the count
* @param int $limit
* @return array
*/
public function getWishes($count = false, $limit = 200)
{
$sql = sprintf("SELECT TOP $limit
NoticeID, CreationDate
FROM Wishes
WHERE UserAccountID = %s
ORDER BY CreationDate DESC", $this->id);
$result = Connection::execute($sql);
$wishList = $result->to_array();
$ids = array_map(function($r) { return $r['NoticeID']; }, $wishList);
if($count) {
return count($ids);
}
$books = BookSearch::GetBooks($ids, 'id');
foreach($wishList as $w) {
$id = $w['NoticeID'];
if(isset($books[$id])) {
$books[$id]['creationDate'] = $w['CreationDate'];
}
}
$creationDates = array();
foreach ($books as $key => $book)
{
$creationDates[$key] = $book['creationDate'];
}
array_multisort($creationDates, SORT_DESC, $books);
return $books;
}
/**
* Remove a book from the wish list
* @param string $noticeNr
* @return boolean Was the deletion was successful or not ?
*/
public function deleteWish($noticeNr)
{
$sql = sprintf("DELETE w
FROM Wishes AS w
INNER JOIN Notices AS n ON n.NoticeID = w.NoticeID
WHERE
LTRIM(RTRIM(n.NoticeNr)) = '%s'
AND UserAccountID = %s;", $noticeNr, $this->id);
$status = Connection::execute($sql, true);
return $status && ! $status->is_error() && $status->get_num_rows() > 0;
}
}

@ -1,985 +0,0 @@
<?php
namespace BSR;
use BSR\Lib\Configuration;
use BSR\Lib\db\DBHelper;
use BSR\Lib\db\Connection;
use BSR\Lib\db\User;
use BSR\Lib\Exception\AuthenticationException;
use BSR\Lib\Exception\WebException;
use BSR\Lib\Search\BookSearch;
use BSR\Lib\WebService;
use BSR\Lib\Logger;
class NetBiblio extends WebService
{
/** @var string $version version number */
public static $version = '1.2.0';
private $login = '';
private $client = 'website';
public function __construct() {
parent::__construct(self::$version);
}
/**
* Set the current login and client based on information
* from the session.
*/
private function CheckSession()
{
if (! isset ($_SESSION["user"]["login"]) || empty($_SESSION["user"]["login"])) {
return;
}
$this->login = $_SESSION["user"]["login"];
$this->client = isset($_SESSION["user"]["client"]) ? $_SESSION["user"]["client"] : 'website';
}
/**
* Retrieve information about the current user from the database.
* If a username is given, first validate that it is the same
* currently in the session.
*
* @param string|null $login
* @return User
* @throws AuthenticationException
*/
private function getUser($login = null)
{
if(! is_null($login) && $_SESSION["user"]["login"] !== $login) {
throw new AuthenticationException("BadLogin", "Login '$login' is invalid.", AuthenticationException::BAD_LOGIN);
}
$this->checkSession();
if(strlen($this->login) == 0) {
throw new AuthenticationException("LoginEmpty", "No login information found in session.", AuthenticationException::LOGIN_EMPTY);
}
$user = User::find($this->login);
if (!$user) {
throw new AuthenticationException("UserNotFound", "No user found for '{$this->login}'.", AuthenticationException::USER_NOT_FOUND);
}
return $user;
}
/**
* Retrieve file information (samples and zip) for a list of books based
* on their code (NoticeNr).
* This should be called only if those information are not already in Solr
* for the given books.
*
* @param array $codes
* @return array File information indexed by book code
*/
private function GetFiles(array $codes)
{
$codes = array_map('intval', $codes);
$uri = sprintf("%s%s",
Configuration::get('checkfile_url'),
http_build_query(array("book" => implode(',', $codes)))
);
// Logger::log($uri);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$json = curl_exec($ch);
curl_close($ch);
return json_decode($json, true);
}
/**
* Save files information (samples and zip) for books into Solr
* based on their id (NoticeId).
*
* @param $files
*/
private function SetFiles($files) {
$json = json_encode(array_values(array_map(function($f) {
return array(
'id' => $f['id'],
'samples' => array('set' => isset($f['samples']) ? $f['samples'] : array()),
'zip' => array('set' => isset($f['zip']['uri']) ? $f['zip']['uri'] : ''),
'zip_size' => array('set' => isset($f['zip']['size']) ? $f['zip']['size'] : 0),
);
}, $files)));
$uri = sprintf('%s:%s/%s/update?commitWithin=500',
Configuration::get('solr.server'),
Configuration::get('solr.port'),
Configuration::get('solr.path')
);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($json)
));
curl_exec($ch);
curl_close($ch);
}
/**
* Add some information to each books :
* 1° File information if not already their (@see GetFiles),
* those information are then saved into Solr (@see SetFiles)
* 2° Correctly set hash on zip file to authenticate download
* 3° Compatibility fields for mobile apps
*
* You can pass either a single book or an array of books.
*
* @param array $books either one or a list of books
* @return array either one or a list of books
*/
private function AddBookData(array $books)
{
if(isset($books['code'])) {
$result = $this->AddBookData(array($books));
return reset($result);
}
// add complementary data to each book
$books = array_map(function($b) {
// add files if we already have them
$files = array();
if(isset($b['samples']) && count($b['zip']) > 0) {
$files['samples'] = $b['samples'];
}
unset($b['samples']);
if(isset($b['zip']) && strlen($b['zip']) > 0) {
$files['zip'] = array(
'uri' => $b['zip'],
'size' => $b['zip_size'],
);
}
unset($b['zip']);
unset($b['zip_size']);
if(! empty($files)) {
$b['files'] = $files;
}
// date will be already set if we are updating Circulations
if(! isset($b['date'])) {
$b['date'] = date('Y.m.d', strtotime($b['availabilityDate']));
}
// add fields for mobile apps compatibility
$b['readBy'] = isset($b['reader']) ? $b['reader'] : 'Lecteur inconnu';
$b['category'] = $b['genre'];
$b['code3'] = $b['producerCode'];
$b['code3Long'] = $b['producer'];
$b['typeMedia1'] = $b['mediaType'];
return $b;
}, $books);
// retrieve files information for the book that don't have all of them at this time
$booksWithoutFiles = array_filter($books, function($b) {
return ! (
isset($b['files']) &&
isset($b['files']['samples']) &&
count($b['files']['samples']) == 2 && // we want two samples .wav and ogg)
isset($b['files']['zip']) // we want a zip file
);
});
if(count($booksWithoutFiles) > 0) {
$ids = array_map(function($b) { return $b['code']; }, $booksWithoutFiles);
$files = $this->GetFiles($ids);
if(count($files) > 0) {
foreach($booksWithoutFiles as $k => $b) {
$fileCode = sprintf("%05u", $b['code']);
if(isset($files[$fileCode])) {
$books[$k]['files'] = $files[$fileCode];
$files[$fileCode]['id'] = $b['id'];
} else {
// we need to have an empty array for mobile apps compatibility.
$books[$k]['files'] = array();
}
}
}
$this->SetFiles($files);
}
// retrieve login information if they exists to generate the URL
$this->CheckSession();
// add hash, client and login into zip file uri
$books = array_map(function($b) {
$b['files']['cacheable_uri'] = isset($b['files']['zip']['uri']) ? $b['files']['zip']['uri'] : false;
if(strlen($this->login) > 0 && isset($b['files']['zip']['uri'])) {
$key = 'babf2cfbe2633c3082f8cfffdb3d9008b4b3b300';
$code = sprintf("%05u", $b['code']);
$hash = sha1($this->client.$this->login.$key.$code.date('Ymd'));
$b['files']['zip']['uri'] = str_replace(array(
'{client}',
'{login}',
'{hash}',
), array(
$this->client,
$this->login,
$hash,
), $b['files']['zip']['uri']);
} else {
unset($b['files']['zip']);
}
return $b;
}, $books);
return $books;
}
/**
* Return true if the value matches a book code (a number with 1 to 5 digits)
* @param $val
* @return int
*/
protected function IsBookCode($val) {
return preg_match('/^[0-9]{1,5}$/', $val);
}
// **********************************
// * Public methods *
// **********************************
/**
* This method register a download made through the website or mobile application
* as a lent and returned book (OldCirculation).
*
* @param string $client
* @param string $login
* @param int $code
* @return array
* @throws Lib\Exception\SqlException
* @throws WebException
*/
public function AddDownloadLog($client, $login, $code)
{
$dl_alert = 80;
$client = str_replace("'", "", $client);
$login = str_replace("'", "", $login);
$code = ltrim(str_replace("'", "", $code), '0');
$itemNr = $code . 'V';
$sql = "SELECT itemID FROM Items WHERE LTRIM(RTRIM(ItemNr)) = '$itemNr';";
$result = Connection::execute($sql, false);
if ($row = $result->current()) {
$itemId = $row['itemID'];
} else {
throw new WebException("ItemNotFound", "cannot find item", -1030);
}
$sql = "SELECT UserAccountID, DisplayName FROM UserAccounts WHERE LTRIM(RTRIM(UserAccountNr)) = '$login';";
$result = Connection::execute($sql, false);
if ($row = $result->current()) {
$userId = $row['UserAccountID'];
$username = $row['DisplayName'];
} else {
throw new WebException("UserNotFound", "cannot find user", -1031);
}
$sql = "SELECT circulationId
FROM OldCirculations
WHERE
UserAccountID= $userId AND
itemID = $itemId AND
LTRIM(RTRIM(remark)) = '$client';";
$result = Connection::execute($sql, false);
if ($row = $result->current()) {
$id = $row['circulationId'];
$sql = "UPDATE OldCirculations
SET
CheckInDate=GETDATE(),
CheckOutDate=GETDATE()
WHERE circulationID = $id";
Connection::execute($sql);
return array('success' => true);
}
$sql = "SELECT TOP 1 circulationID FROM OldCirculations ORDER BY CirculationID DESC";
$result = Connection::execute($sql, false);
if ($row = $result->current()) {
$nextId = $row['circulationID'] + 1;
} else {
$nextId = 1;
}
$sql = "SELECT count(*) as DLs FROM OldCirculations WHERE UserAccountID = $userId
AND checkoutdate >= Dateadd(month, Datediff(month, 0, Getdate()), 0);";
$result = Connection::execute($sql, false);
if ($row = $result->current()) {
$DLs = $row['DLs'];
if($DLs >= $dl_alert){
$to = 'g@lespagesweb.ch';
$subject = 'Limite atteinte pour '.$username.", ".$login;
$message = "Nombre de livres ce mois: ".($DLs+1);
$headers = 'From: webmaster@abage.ch' . "\r\n" .
'Reply-To: webmaster@abage.ch' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
}
$sql = "UPDATE UserAccounts
SET
Circulations = Circulations + 1,
TotalCirculations = TotalCirculations + 1
WHERE UserAccountID = $userId;";
Connection::execute($sql);
$sql = "UPDATE Items
SET
Circulations = Circulations + 1,
TotalCirculations = TotalCirculations + 1
WHERE ItemID = $itemId;";
Connection::execute($sql);
$worker_id = Configuration::get('netbiblio_worker_id');
$sql = "INSERT INTO OldCirculations (
CirculationID, ItemID, UserAccountID,
Remark,
DueDate, CheckOutDate, CheckInDate,
CheckOutBranchOfficeID, CheckOutEmployeeID, CheckInBranchOfficeID, CheckInEmployeeID,
Reminders, Renewals, PreReminder, InfoCode, CheckOutSIP2Info, CheckInSIP2Info
) VALUES (
$nextId, $itemId, $userId,
'$client',
DATEADD(month, 2, GETDATE()), GETDATE(), GETDATE(),
2, $worker_id, 2, $worker_id,
0, 0, 1, '-', 1, 1
);";
$status = Connection::execute($sql);
return array('success' => $status && ! $status->is_error() && $status->get_num_rows() > 0);
}
/**
* This method authenticates and store the login information into the session so
* that the next calls can be made for this user.
*
* @param $login
* @param $password
* @param string $client
* @return array
* @throws AuthenticationException
*/
public function Authenticate($login, $password, $client = "website")
{
session_unset(); /* destroy all session vars */
$user = User::authenticate($login, $password);
if (! $user) {
throw new AuthenticationException("AuthenticationFailed", "Invalid login or password.", AuthenticationException::AUTHENTICATION_FAILED);
}
$_SESSION["user"]["login"] = $login;
$_SESSION["user"]["client"] = $client;
$this->login = $login;
$this->client = $client;
return $user->toArray();
}
/**
* This method disconnects the current user and clear all session data.
*
* @return array
*/
public function Disconnect()
{
$_SESSION = array();
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]);
}
return array('success' => true);
}
/**
* Return the information associated with the currently authenticated user
* if any.
*
* @return array
* @throws AuthenticationException
*/
public function IsAuthenticated()
{
return $this->getUser()->toArray();
}
/**
* This method returns the account information for the given login, but only
* if it matches the currently authenticated user.
*
* @param string $login
* @return array
* @throws AuthenticationException
*/
public function FindAccount($login)
{
return $this->getUser($login)->toArray();
}
/**
* This method returns the books on the currently authenticated user wishlist.
*
* @return array
* @throws AuthenticationException
*/
public function GetWishes()
{
$books = $this->getUser()->getWishes();
return array_values($this->AddBookData($books));
}
/**
* This method returns the list of all Circulations for the currently
* authenticated user in a format suited for BSR internal tools
* (CD engraving for example).
*
* @return array
* @throws AuthenticationException
*/
public function GetCirculations()
{
return $this->getUser()->GetCirculations();
}
/**
* This method returns the list of books that are currently lent to the
* authenticated user.
*
* @return array
* @throws AuthenticationException
*/
public function GetLoans()
{
$circulations = $this->getUser()->GetLoans();
return array_values($this->AddBookData($circulations));
}
/**
* This method returns the list of books that the currently authenticated user
* has downloaded or that were lent to him.
*
* @return array
* @throws AuthenticationException
*/
public function GetOldLoans()
{
$circulations = $this->getUser()->GetOldLoans();
return $circulations; //array_values($this->AddBookData($circulations));
}
/**
* This method returns the list of noticenr that
* were lent to the currently authenticated user.
*
* @return array
* @throws AuthenticationException
*/
public function GetOldLoansNrs()
{
$circulations = $this->getUser()->GetOldLoansNrs();
return $circulations;
}
/**
* This method returns the books lent to the current user in the time period
* 2 weeks to 4 month.
*
* @return array
* @throws AuthenticationException
*/
public function GetBooksForFeedback()
{
return array_values($this->getUser()->GetBooksForFeedback());
}
/**
* This method return information for the user dashboard :
*
* ° 'loans' : number of current loans
* ° 'oldLoans' : number of past loans
* ° 'wishes' : number of wishes
* ° 'novelties' : new books
* ° 'recommendations' : recommended books
*
* @return array
* @throws AuthenticationException
*/
public function GetDashboardData()
{
$user = $this->getUser();
return array(
'loans' => $user->GetLoans(true),
'oldLoans' => $user->GetOldLoans(true),
'wishes' => $user->getWishes(true),
'novelties' => $this->LastBooksByType('', 12),
'recommendations' => $this->MoreLikeLoans(8),
);
}
/**
* This method adds a book to the currently authenticated user wishlist
* based on its code.
*
* @param int $code
* @return array
* @throws AuthenticationException
*/
public function AddWish($code)
{
$status = $this->getUser()->addWish($code);
return array('success' => $status);
}
/**
* This method deletes the given book from the currently authenticated
* user wishlist based on its code.
*
* @param int $code
* @return array
* @throws AuthenticationException
*/
public function DeleteWish($code)
{
$status = $this->getUser()->deleteWish($code);
return array('success' => $status);
}
/**
* This method returns an array of books based on their code
*
* @param array|int[] $codes
* @return array
*/
public function FindBooks($codes)
{
return array_values($this->AddBookData(BookSearch::GetBooks(json_decode($codes))));
}
/**
* This method return a book based on its code
*
* @param int $code
* @return array
*/
public function FindBook($code)
{
$books = $this->AddBookData(BookSearch::GetBooks(array($code)));
return reset($books);
}
/**
* This method returns a list of random books.
* For a given seed, the list should be always the same at least while
* Solr is not restarted (or documents re-indexed)
*
* @param int $number Number of random books to return
* @param null $seed
* @param int $page
* @return array
* @throws WebException
*/
public function GetRandomBooks($number = 100, $seed = null, $page = 0) {
if(is_null($seed)) {
$seed = time();
}
$bs = new BookSearch();
$bs->addSortField('random_'.$seed);
$bs->addFilterQuery(1, 'visible');
$results = $bs->getResults($page * $number, $number);
return $this->AddBookData($results['books']);
}
/**
* This method is used by the iOS application to perform searching.
* If a number is given, search in the book codes, otherwise perform
* a full text search.
*
* @param string $text Text to search
* @param int $start
* @param int $limit
* @return array an array of books
* @throws WebException
*/
public function Search($text, $start, $limit)
{
$query = array(
'queryText' => $text,
'count' => $limit,
'page' => max(intval($start) - 1, 0),
);
if($this->IsBookCode($text)) {
$query['queryType'] = 'code';
}
$data = $this->NewSearch(json_encode($query));
// remove fields that are not used in "old" search
unset($data['count']);
unset($data['facets']);
return $data;
}
/**
* This method is used by the website and the Android application to perform
* book search.
*
* Search parameters :
*
* ° queryText : the text to search for
* ° queryType : the field to search in, defaults to 'text'
*
* ° jeunesse : only display books for kids (must have format 'jeunesse' => array('filtrer' => 'filtrer')
*
* ° producerCode : filter by 'producerCode'
* ° genreCode : filter by 'genreCode'
* ° author : filter by 'author'
* ° reader : filter by 'reader'
* ° motsMatieres : filter by 'motsMatieres'
*
* ° duration : exact duration in minutes
* ° durationMin : minimal duration in minutes
* ° durationMax : maximal duration in minutes
*
* ° count : number of results we want
* ° page : page to start at (0 is the first)
*
* Shortcuts :
*
* ° producer : synonym for 'producerCode' (see above)
* ° genre : synonym for 'genreCode' (see above)
*
* Deprecated, but still in use on mobile apps :
*
* ° category : synonym for 'genre' (see above)
*
* Return value :
*
* The return value start with two keys :
*
* ° 'count' : which is the total number of available results
* ° 'facets' : which contains all other relevent information (facets, spellchecking,
* highlighting, etc). This name is used for compatibility reasons.
*
* Then, the books come right after.
*
* @param string $values JSON encoded object
* @return array
* @throws WebException
*/
public function NewSearch($values)
{
// we need the client to perform some query field replacement
$this->CheckSession();
$queryArray = json_decode($values, true);
if(! is_array($queryArray)) {
throw new WebException("CallArg", "Argument must be valid JSON.", -42);
}
// shortcuts and the iOS app still uses 'category'
$compatibility = array(
'genre' => 'genreCode',
'category' => 'genreCode',
'producer' => 'producerCode'
);
foreach($compatibility as $old => $new) {
if(isset($queryArray[$old])) {
$queryArray[$new] = $queryArray[$old];
unset($queryArray[$old]);
}
}
$bs = new BookSearch();
// when search on a particular field, put results in descending date order
if(!isset($queryArray['queryText']) && !isset($queryArray['author_fr']) && !isset($queryArray['title_fr']) ) {
$bs->addSortField('availabilityDate');
}
if (isset($queryArray['queryText']) && strlen($queryArray['queryText']) > 0) {
$type = isset($queryArray['queryType']) ? $queryArray['queryType'] : null;
if(in_array($type, array('title', 'author', 'reader'))) {
// we don't want an exact search on mobile apps
$type = $type.'_fr';
} else if($type == 'text') {
// The field 'text' is still used by the Android app but does not exists anymore
// We use the default search fields in this case.
$type = null;
}
$bs->addQuery($queryArray['queryText'], $type);
}
if(isset($queryArray['genreCode']) && is_array($queryArray['genreCode'])) {
// Jeunesse is a particular genre with it's own way of being searched
if(($key = array_search('J', $queryArray['genreCode'])) !== false) {
unset($queryArray['genreCode']['J']);
$queryArray['jeunesse'] = array('filtrer' => 'filtrer');
}
}
if(isset($queryArray['jeunesse']) && $queryArray['jeunesse']['filtrer'] === 'filtrer') {
$bs->addFilterQuery(1, 'jeunesse');
}
if(isset($queryArray['duration'])) {
$bs->addQuery($queryArray['duration'], 'duration');
} else if(isset($queryArray['durationMin']) || isset($queryArray['durationMax'])) {
$min = isset($queryArray['durationMin']) ? $queryArray['durationMin'] : '*';
$max = isset($queryArray['durationMax']) ? $queryArray['durationMax'] : '*';
$bs->addRange('duration', $min, $max);
}
$availableFields = array('producerCode', 'genreCode', 'author', 'author_fr', 'title_fr', 'reader', 'reader_fr', 'motsMatieres', 'mediaType');
foreach($availableFields as $q) {
if(isset($queryArray[$q]) && (
(is_string($queryArray[$q]) && strlen($queryArray[$q]) > 0) ||
(is_array($queryArray[$q]) && count($queryArray[$q]) > 0)
)) {
if(is_array($queryArray[$q])) {
// Genres cannot overlap, so we use 'OR', otherwise use 'AND'
$bs->addCompoundQuery($queryArray[$q], $q, $q == 'genreCode' ? 'OR' : 'AND');
} else {
if($q == 'reader'){
$q2 = 'reader_fr';
$queryArray[$q] = ucfirst($queryArray[$q]);
} else{$q2 = $q;}
$bs->addQuery($queryArray[$q], $q2);
}
}
}
// we only want visible books in search results
$bs->addFilterQuery(1, 'visible');
$count = isset($queryArray['count']) ? (int) $queryArray['count'] : Configuration::get('solr.result_count');
$start = isset($queryArray['page']) ? $queryArray['page'] * $count : 0;
$facets = isset($queryArray['facets']) && $queryArray['facets'];
$spellcheck = isset($queryArray['spellcheck']) && $queryArray['spellcheck'];
$highlight = isset($queryArray['highlight']) && $queryArray['highlight'];
$results = $bs->getResults($start, $count, $facets, $spellcheck, $highlight);
$data = array(
'count' => $results['count'],
'facets' => $results['facets'],
);
$finalResult = array_merge($data, $this->AddBookData($results['books']));
for($i = 0; $i < count($finalResult)-2 ; $i++){
$finalResult[$i]['position']=$i;
}
return $finalResult;
}
/**
* This method return books similar to the one given.
*
* @param int|array $ids One or multiple book ids
* @param int number of books
* @return array
*/
public function MoreLikeThis($ids, $number = 8)
{
$bs = new BookSearch(false);
$bs->addOrQuery(is_array($ids) ? $ids : array($ids), 'id');
$bs->setHandler('more');
$results = $bs->getResults(0, $number);
return $this->AddBookData($results['books']);
}
/**
* This method return books similar to the one given.
*
* @param int|array $ids One or multiple book ids
* @param int number of books
* @return array
*/
public function MoreLikeThisByCode($codes, $number = 8)
{
$bs = new BookSearch(false);
$bs->addOrQuery(is_array($code) ? $codes : array($codes), 'code');
$bs->setHandler('more');
$results = $bs->getResults(0, $number);
return array_values($this->AddBookData($results['books']));
}
/**
* This method returns books similar to the books already
* loaned by the current user.
*
* @param int number of books
* @return array
* @throws AuthenticationException
*/
public function MoreLikeLoans($number = 8)
{
$circulations = $this->getUser()->getLoansData('OldCirculations');
$ids = array_map(function($c) { return $c['NoticeId']; }, $circulations);
return $this->MoreLikeThis($ids, $number);
}
/**
* This method return a list of suggested titles for the given search terms
* @param string $text
* @return array
*/
public function Suggest($text)
{
$bs = new BookSearch();
return $bs->suggest($text);
}
/**
* This method returns the list of durations in minutes but with 30 minutes gaps
* and the count of books in each group.
*
* @return array
*/
public function ListOfDurations()
{
return BookSearch::GetTermsRange('duration');
}
/**
* This method returns the list of all volunteer readers that read book
* in the database.
* @return array
*/
public function ListOfReaders()
{
$readers = array();
foreach(BookSearch::GetTerms('reader') as $name => $count) {
$parts = explode(" ", $name);
$firstname = array_shift($parts);
$lastname = implode(" ", $parts);
$fullname = trim($lastname.' '.$firstname);
$readers[$fullname] = array(
'lastname' => $lastname,
'firstname' => $firstname,
'count' => $count,
);
}
// sort readers by lastname
ksort($readers);
return array_values($readers);
}
/**
* This method is called by the website in Drupal to get the list
* of available 'Genres'.
* @return array
*/
public function ListOfGenres()
{
return DBHelper::ListOfGenres();
}
/**
* This method is called by the Android application to get the list
* of available 'Genres'.
* @return array
*/
public function ListOfCategories()
{
return DBHelper::ListOfGenres(true);
}
/**
* This method is called by the iOS application to get the list
* of available 'Genres'. 'Jeunesse' must be a part of them.
* @return array
*/
public function ListOfTypes()
{
return array_map(function($g) {
return $g['text'];
}, DBHelper::ListOfGenres(true));
}
/**
* This method returns the list of all books that are currently
* being read by the volunteers.
*
* @return array
*/
public function InReadingBooks()
{
return DBHelper::InReading();
}
/**
* This method is used by the iOS application to retrieve the last
* books for a given Genre. It may receives the genre 'Jeunesse' which
* is a boolean value on Solr documents.
*
* @param string $genre Genre for which we want books (not the code), this can be empty
* @param int $number number of books
* @return array
* @throws WebException
*/
public function LastBooksByType($genre, $number)
{
$s = new BookSearch();
if($genre == 'Jeunesse') {
$s->addQuery(1, 'jeunesse');
} else if(! empty($genre)) {
$s->addQuery($genre, 'genre');
}
$s->addSortField('availabilityDate');
// we only want visible books
$s->addFilterQuery(1, 'visible');
$results = $s->getResults(0, $number);
$books = $this->AddBookData($results['books']);
if(empty($genre)) {
return $books;
}
$data = array();
foreach($books as $b) {
$data[$genre][] = $b;
}
return $data;
}
}

@ -1,6 +1,6 @@
{ {
"name" : "bsr/webservice", "name" : "bsr/webservice",
"description" : "Webservice a library", "description" : "Webservice for a library",
"repositories" : [ "repositories" : [
{ {
"type" : "vcs", "type" : "vcs",
@ -20,5 +20,8 @@
}, },
"autoload": { "autoload": {
"psr-4": {"BSR\\Lib\\" : "Lib"} "psr-4": {"BSR\\Lib\\" : "Lib"}
},
"require-dev": {
"pds/skeleton": "^1.0"
} }
} }

50
composer.lock generated

@ -0,0 +1,50 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "bae5c4c1d23da7b19effa06573632e18",
"content-hash": "442d9e936a1cf311fd26a8bf5bfad65e",
"packages": [],
"packages-dev": [
{
"name": "pds/skeleton",
"version": "1.0.0",
"source": {
"type": "git",
"url": "https://github.com/php-pds/skeleton.git",
"reference": "95e476e5d629eadacbd721c5a9553e537514a231"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-pds/skeleton/zipball/95e476e5d629eadacbd721c5a9553e537514a231",
"reference": "95e476e5d629eadacbd721c5a9553e537514a231",
"shasum": ""
},
"bin": [
"bin/pds-skeleton"
],
"type": "standard",
"autoload": {
"psr-4": {
"Pds\\Skeleton\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"CC-BY-SA-4.0"
],
"description": "Standard for PHP package skeletons.",
"homepage": "https://github.com/php-pds/skeleton",
"time": "2017-01-25 23:30:41"
}
],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": []
}

@ -1,10 +1,10 @@
<?php <?php
/** /**
* 'odbc:Driver=FreeTDS; * 'odbc:Driver=FreeTDS;
* Server=192.168.0.8; * Server=192.168.0.8;
* Port=1218; * Port=1218;
* Database=netbiblio; * Database=netbiblio;
* TDS_Version=7.4; * TDS_Version=7.4;
* ClientCharset=UTF-8'* 'alcoda', 'alcodaonly' * ClientCharset=UTF-8'* 'alcoda', 'alcodaonly'
*/ */
return array( return array(

@ -1,14 +0,0 @@
<?php
namespace BSR;
use BSR\Lib\Formatter\Html;
use BSR\Lib\Help;
require_once('Lib/autoloader.php');
echo Html::template(array(
'version' => NetBiblio::$version,
'title' => 'Help',
'content' => Help::content(new NetBiblio()),
));

@ -1,16 +0,0 @@
<?php
namespace BSR;
use BSR\Lib\Formatter\Html;
use BSR\Lib\Logger;
require_once('Lib/autoloader.php');
$logs = Logger::getLastLogs();
echo Html::template(array(
'version' => NetBiblio::$version,
'title' => 'Logs',
'content' => "<pre>$logs</pre>",
));

@ -1,4 +0,0 @@
<?php
// this file is here for compatibility purpose, do not delete
require_once('index.php');

@ -1,3 +0,0 @@
<?php
phpinfo();

@ -1,5 +0,0 @@
<?php
require_once('./../Lib/autoloader.php');
$web = new \BSR\NetBiblio();
$web->Run();

@ -17,41 +17,7 @@ class Configuration {
* *
* @var array configuration values * @var array configuration values
*/ */
private $values = array( private $values = array();
'db' => array(
'driver' => 'SQL Server Native Client 11.0',
'server' => 'BSR2012\SQLEXPRESS',
'port' => '1433',
'username' => 'alcoda',
'password' => 'alcodaonly',
'name' => 'NetBiblio3',
),
'solr' => array(
'server' => '192.168.1.250',
'port' => '8983',
'username' => '',
'password' => '',
'path' => 'solr/bsr1',
'result_count' => 10,
),
'log' => array(
'file' => 'C:\inetpub\wwwroot\WebService\logs\log.txt',
'format' => '%ip% - [%date%] - %status% %error% - %time% - %func%',
// The greater the verbosity, the more is displayed
// 0 : no log at all
// 1 : log summary
// 2 : log response
'verbosity' => 0,
),
'session' => array(
'save_path' => ''
),
'checkfile_url' => 'http://medias.bibliothequesonore.ch/checkfile.php?',
'checkfile_url_old' => 'http://fichiers.bibliothequesonore.ch/checkfile.php?',
'netbiblio_worker_id' => 45,
'www_employee_id' => 45,
'www_library_id' => 2,
);
private $custom_config_filename = 'configuration.local.php'; private $custom_config_filename = 'configuration.local.php';
@ -63,7 +29,7 @@ class Configuration {
throw new \RuntimeException('No configuration.local.php file was found. Create it with the proper config.'); throw new \RuntimeException('No configuration.local.php file was found. Create it with the proper config.');
} }
$configuration = include_once realpath(dirname(__FILE__) . '/../') . $this->custom_config_filename; $configuration = include_once realpath(dirname(__FILE__) . '/../config/') . $this->custom_config_filename;
if(!is_array($configuration)) { if(!is_array($configuration)) {
throw new \RuntimeException("You custom configuration in '{$this->custom_config_filename}' must be in a variable named '\$configuration' and be an array."); throw new \RuntimeException("You custom configuration in '{$this->custom_config_filename}' must be in a variable named '\$configuration' and be an array.");

@ -1,14 +0,0 @@
<h2>{{ func }}</h2>
<h3>Parameters</h3>
<dl>
{{ parameters }}
</dl>
<h3>Return</h3>
{{ return }}
<h3>Description</h3>
{{ help }}

@ -1,40 +0,0 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>BSR WebService - {{ title }}</title>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-default navbar-static-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">BSR WebService [{{ version }}]</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="help.php">Help</a></li>
<li><a href="logs.php">Logs</a></li>
<li><a href="phpinfo.php">PHPInfo</a></li>
</ul>
</div>
</div>
</nav>
<div class="container-fluid">
{{ content }}
</div>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>
</body>
</html>

@ -1,11 +0,0 @@
<div class="panel panel-{{ status }}">
<div class="panel-heading">
<h3 class="panel-title">{{ title }}</h3>
</div>
<div class="panel-body">
{{ content }}
</div>
<div class="panel-footer">
Generated in : {{ time }}
</div>
</div>

@ -1,5 +0,0 @@
<dt>{{ name }} {{ optional }}</dt>
<dd>
<bold>{{ type }}</bold>
{{ doc }}
</dd>

@ -1,2 +0,0 @@
<bold>{{ type }}</bold>
{{ doc }}

@ -0,0 +1,7 @@
<?php
// autoload.php @generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInit22f37628aeb6bdf37979bf902b8ee9cd::getLoader();

@ -0,0 +1 @@
../pds/skeleton/bin/pds-skeleton

@ -0,0 +1,413 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Autoload;
/**
* ClassLoader implements a PSR-0, PSR-4 and classmap class loader.
*
* $loader = new \Composer\Autoload\ClassLoader();
*
* // register classes with namespaces
* $loader->add('Symfony\Component', __DIR__.'/component');
* $loader->add('Symfony', __DIR__.'/framework');
*
* // activate the autoloader
* $loader->register();
*
* // to enable searching the include path (eg. for PEAR packages)
* $loader->setUseIncludePath(true);
*
* In this example, if you try to use a class in the Symfony\Component
* namespace or one of its children (Symfony\Component\Console for instance),
* the autoloader will first look for the class under the component/
* directory, and it will then fallback to the framework/ directory if not
* found before giving up.
*
* This class is loosely based on the Symfony UniversalClassLoader.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Jordi Boggiano <j.boggiano@seld.be>
* @see http://www.php-fig.org/psr/psr-0/
* @see http://www.php-fig.org/psr/psr-4/
*/
class ClassLoader
{
// PSR-4
private $prefixLengthsPsr4 = array();
private $prefixDirsPsr4 = array();
private $fallbackDirsPsr4 = array();
// PSR-0
private $prefixesPsr0 = array();
private $fallbackDirsPsr0 = array();
private $useIncludePath = false;
private $classMap = array();
private $classMapAuthoritative = false;
public function getPrefixes()
{
if (!empty($this->prefixesPsr0)) {
return call_user_func_array('array_merge', $this->prefixesPsr0);
}
return array();
}
public function getPrefixesPsr4()
{
return $this->prefixDirsPsr4;
}
public function getFallbackDirs()
{
return $this->fallbackDirsPsr0;
}
public function getFallbackDirsPsr4()
{
return $this->fallbackDirsPsr4;
}
public function getClassMap()
{
return $this->classMap;
}
/**
* @param array $classMap Class to filename map
*/
public function addClassMap(array $classMap)
{
if ($this->classMap) {
$this->classMap = array_merge($this->classMap, $classMap);
} else {
$this->classMap = $classMap;
}
}
/**
* Registers a set of PSR-0 directories for a given prefix, either
* appending or prepending to the ones previously set for this prefix.
*
* @param string $prefix The prefix
* @param array|string $paths The PSR-0 root directories
* @param bool $prepend Whether to prepend the directories
*/
public function add($prefix, $paths, $prepend = false)
{
if (!$prefix) {
if ($prepend) {
$this->fallbackDirsPsr0 = array_merge(
(array) $paths,
$this->fallbackDirsPsr0
);
} else {
$this->fallbackDirsPsr0 = array_merge(
$this->fallbackDirsPsr0,
(array) $paths
);
}
return;
}
$first = $prefix[0];
if (!isset($this->prefixesPsr0[$first][$prefix])) {
$this->prefixesPsr0[$first][$prefix] = (array) $paths;
return;
}
if ($prepend) {
$this->prefixesPsr0[$first][$prefix] = array_merge(
(array) $paths,
$this->prefixesPsr0[$first][$prefix]
);
} else {
$this->prefixesPsr0[$first][$prefix] = array_merge(
$this->prefixesPsr0[$first][$prefix],
(array) $paths
);
}
}
/**
* Registers a set of PSR-4 directories for a given namespace, either
* appending or prepending to the ones previously set for this namespace.
*
* @param string $prefix The prefix/namespace, with trailing '\\'
* @param array|string $paths The PSR-4 base directories
* @param bool $prepend Whether to prepend the directories
*
* @throws \InvalidArgumentException
*/
public function addPsr4($prefix, $paths, $prepend = false)
{
if (!$prefix) {
// Register directories for the root namespace.
if ($prepend) {
$this->fallbackDirsPsr4 = array_merge(
(array) $paths,
$this->fallbackDirsPsr4
);
} else {
$this->fallbackDirsPsr4 = array_merge(
$this->fallbackDirsPsr4,
(array) $paths
);
}
} elseif (!isset($this->prefixDirsPsr4[$prefix])) {
// Register directories for a new namespace.
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) {
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
$this->prefixDirsPsr4[$prefix] = (array) $paths;
} elseif ($prepend) {
// Prepend directories for an already registered namespace.
$this->prefixDirsPsr4[$prefix] = array_merge(
(array) $paths,
$this->prefixDirsPsr4[$prefix]
);
} else {
// Append directories for an already registered namespace.
$this->prefixDirsPsr4[$prefix] = array_merge(
$this->prefixDirsPsr4[$prefix],
(array) $paths
);
}
}
/**
* Registers a set of PSR-0 directories for a given prefix,
* replacing any others previously set for this prefix.
*
* @param string $prefix The prefix
* @param array|string $paths The PSR-0 base directories
*/
public function set($prefix, $paths)
{
if (!$prefix) {
$this->fallbackDirsPsr0 = (array) $paths;
} else {
$this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
}
}
/**
* Registers a set of PSR-4 directories for a given namespace,
* replacing any others previously set for this namespace.
*
* @param string $prefix The prefix/namespace, with trailing '\\'
* @param array|string $paths The PSR-4 base directories
*
* @throws \InvalidArgumentException
*/
public function setPsr4($prefix, $paths)
{
if (!$prefix) {
$this->fallbackDirsPsr4 = (array) $paths;
} else {
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) {
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
$this->prefixDirsPsr4[$prefix] = (array) $paths;
}
}
/**
* Turns on searching the include path for class files.
*
* @param bool $useIncludePath
*/
public function setUseIncludePath($useIncludePath)
{
$this->useIncludePath = $useIncludePath;
}
/**
* Can be used to check if the autoloader uses the include path to check
* for classes.
*
* @return bool
*/
public function getUseIncludePath()
{
return $this->useIncludePath;
}
/**
* Turns off searching the prefix and fallback directories for classes
* that have not been registered with the class map.
*
* @param bool $classMapAuthoritative
*/
public function setClassMapAuthoritative($classMapAuthoritative)
{
$this->classMapAuthoritative = $classMapAuthoritative;
}
/**
* Should class lookup fail if not found in the current class map?
*
* @return bool
*/
public function isClassMapAuthoritative()
{
return $this->classMapAuthoritative;
}
/**
* Registers this instance as an autoloader.
*
* @param bool $prepend Whether to prepend the autoloader or not
*/
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}
/**
* Unregisters this instance as an autoloader.
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}
/**
* Loads the given class or interface.
*
* @param string $class The name of the class
* @return bool|null True if loaded, null otherwise
*/
public function loadClass($class)
{
if ($file = $this->findFile($class)) {
includeFile($file);
return true;
}
}
/**
* Finds the path to the file where the class is defined.
*
* @param string $class The name of the class
*
* @return string|false The path if found, false otherwise
*/
public function findFile($class)
{
// work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731
if ('\\' == $class[0]) {
$class = substr($class, 1);
}
// class map lookup
if (isset($this->classMap[$class])) {
return $this->classMap[$class];
}
if ($this->classMapAuthoritative) {
return false;
}
$file = $this->findFileWithExtension($class, '.php');
// Search for Hack files if we are running on HHVM
if ($file === null && defined('HHVM_VERSION')) {
$file = $this->findFileWithExtension($class, '.hh');
}
if ($file === null) {
// Remember that this class does not exist.
return $this->classMap[$class] = false;
}
return $file;
}
private function findFileWithExtension($class, $ext)
{
// PSR-4 lookup
$logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
$first = $class[0];
if (isset($this->prefixLengthsPsr4[$first])) {
foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) {
if (0 === strpos($class, $prefix)) {
foreach ($this->prefixDirsPsr4[$prefix] as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
return $file;
}
}
}
}
}
// PSR-4 fallback dirs
foreach ($this->fallbackDirsPsr4 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
return $file;
}
}
// PSR-0 lookup
if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name
$logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
. strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
} else {
// PEAR-like class name
$logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
}
if (isset($this->prefixesPsr0[$first])) {
foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
if (0 === strpos($class, $prefix)) {
foreach ($dirs as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
}
}
}
// PSR-0 fallback dirs
foreach ($this->fallbackDirsPsr0 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
// PSR-0 include paths.
if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
return $file;
}
}
}
/**
* Scope isolated include.
*
* Prevents access to $this/self from included files.
*/
function includeFile($file)
{
include $file;
}

@ -0,0 +1,21 @@
Copyright (c) 2016 Nils Adermann, Jordi Boggiano
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

@ -0,0 +1,9 @@
<?php
// autoload_classmap.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
);

@ -0,0 +1,9 @@
<?php
// autoload_namespaces.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
);

@ -0,0 +1,11 @@
<?php
// autoload_psr4.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Pds\\Skeleton\\' => array($vendorDir . '/pds/skeleton/src'),
'BSR\\Lib\\' => array($baseDir . '/Lib'),
);

@ -0,0 +1,52 @@
<?php
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit22f37628aeb6bdf37979bf902b8ee9cd
{
private static $loader;
public static function loadClassLoader($class)
{
if ('Composer\Autoload\ClassLoader' === $class) {
require __DIR__ . '/ClassLoader.php';
}
}
public static function getLoader()
{
if (null !== self::$loader) {
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit22f37628aeb6bdf37979bf902b8ee9cd', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
spl_autoload_unregister(array('ComposerAutoloaderInit22f37628aeb6bdf37979bf902b8ee9cd', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION');
if ($useStaticLoader) {
require_once __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit22f37628aeb6bdf37979bf902b8ee9cd::getInitializer($loader));
} else {
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->set($namespace, $path);
}
$map = require __DIR__ . '/autoload_psr4.php';
foreach ($map as $namespace => $path) {
$loader->setPsr4($namespace, $path);
}
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);
}
}
$loader->register(true);
return $loader;
}
}

@ -0,0 +1,39 @@
<?php
// autoload_static.php @generated by Composer
namespace Composer\Autoload;
class ComposerStaticInit22f37628aeb6bdf37979bf902b8ee9cd
{
public static $prefixLengthsPsr4 = array (
'P' =>
array (
'Pds\\Skeleton\\' => 13,
),
'B' =>
array (
'BSR\\Lib\\' => 8,
),
);
public static $prefixDirsPsr4 = array (
'Pds\\Skeleton\\' =>
array (
0 => __DIR__ . '/..' . '/pds/skeleton/src',
),
'BSR\\Lib\\' =>
array (
0 => __DIR__ . '/../..' . '/Lib',
),
);
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit22f37628aeb6bdf37979bf902b8ee9cd::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit22f37628aeb6bdf37979bf902b8ee9cd::$prefixDirsPsr4;
}, null, ClassLoader::class);
}
}

@ -0,0 +1,35 @@
[
{
"name": "pds/skeleton",
"version": "1.0.0",
"version_normalized": "1.0.0.0",
"source": {
"type": "git",
"url": "https://github.com/php-pds/skeleton.git",
"reference": "95e476e5d629eadacbd721c5a9553e537514a231"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-pds/skeleton/zipball/95e476e5d629eadacbd721c5a9553e537514a231",
"reference": "95e476e5d629eadacbd721c5a9553e537514a231",
"shasum": ""
},
"time": "2017-01-25 23:30:41",
"bin": [
"bin/pds-skeleton"
],
"type": "standard",
"installation-source": "dist",
"autoload": {
"psr-4": {
"Pds\\Skeleton\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"CC-BY-SA-4.0"
],
"description": "Standard for PHP package skeletons.",
"homepage": "https://github.com/php-pds/skeleton"
}
]

@ -0,0 +1 @@
/vendor/

@ -0,0 +1,7 @@
# Change Log
All notable changes to this publication will be documented in this file.
## 1.0.0 - 2017-25-01
First stable release.

@ -0,0 +1,427 @@
Attribution-ShareAlike 4.0 International
=======================================================================
Creative Commons Corporation ("Creative Commons") is not a law firm and
does not provide legal services or legal advice. Distribution of
Creative Commons public licenses does not create a lawyer-client or
other relationship. Creative Commons makes its licenses and related
information available on an "as-is" basis. Creative Commons gives no
warranties regarding its licenses, any material licensed under their
terms and conditions, or any related information. Creative Commons
disclaims all liability for damages resulting from their use to the
fullest extent possible.
Using Creative Commons Public Licenses
Creative Commons public licenses provide a standard set of terms and
conditions that creators and other rights holders may use to share
original works of authorship and other material subject to copyright
and certain other rights specified in the public license below. The
following considerations are for informational purposes only, are not
exhaustive, and do not form part of our licenses.
Considerations for licensors: Our public licenses are
intended for use by those authorized to give the public
permission to use material in ways otherwise restricted by
copyright and certain other rights. Our licenses are
irrevocable. Licensors should read and understand the terms
and conditions of the license they choose before applying it.
Licensors should also secure all rights necessary before
applying our licenses so that the public can reuse the
material as expected. Licensors should clearly mark any
material not subject to the license. This includes other CC-
licensed material, or material used under an exception or
limitation to copyright. More considerations for licensors:
wiki.creativecommons.org/Considerations_for_licensors
Considerations for the public: By using one of our public
licenses, a licensor grants the public permission to use the
licensed material under specified terms and conditions. If
the licensor's permission is not necessary for any reason--for
example, because of any applicable exception or limitation to
copyright--then that use is not regulated by the license. Our
licenses grant only permissions under copyright and certain
other rights that a licensor has authority to grant. Use of
the licensed material may still be restricted for other
reasons, including because others have copyright or other
rights in the material. A licensor may make special requests,
such as asking that all changes be marked or described.
Although not required by our licenses, you are encouraged to
respect those requests where reasonable. More_considerations
for the public:
wiki.creativecommons.org/Considerations_for_licensees
=======================================================================
Creative Commons Attribution-ShareAlike 4.0 International Public
License
By exercising the Licensed Rights (defined below), You accept and agree
to be bound by the terms and conditions of this Creative Commons
Attribution-ShareAlike 4.0 International Public License ("Public
License"). To the extent this Public License may be interpreted as a
contract, You are granted the Licensed Rights in consideration of Your
acceptance of these terms and conditions, and the Licensor grants You
such rights in consideration of benefits the Licensor receives from
making the Licensed Material available under these terms and
conditions.
Section 1 -- Definitions.
a. Adapted Material means material subject to Copyright and Similar
Rights that is derived from or based upon the Licensed Material
and in which the Licensed Material is translated, altered,
arranged, transformed, or otherwise modified in a manner requiring
permission under the Copyright and Similar Rights held by the
Licensor. For purposes of this Public License, where the Licensed
Material is a musical work, performance, or sound recording,
Adapted Material is always produced where the Licensed Material is
synched in timed relation with a moving image.
b. Adapter's License means the license You apply to Your Copyright
and Similar Rights in Your contributions to Adapted Material in
accordance with the terms and conditions of this Public License.
c. BY-SA Compatible License means a license listed at
creativecommons.org/compatiblelicenses, approved by Creative
Commons as essentially the equivalent of this Public License.
d. Copyright and Similar Rights means copyright and/or similar rights
closely related to copyright including, without limitation,
performance, broadcast, sound recording, and Sui Generis Database
Rights, without regard to how the rights are labeled or
categorized. For purposes of this Public License, the rights
specified in Section 2(b)(1)-(2) are not Copyright and Similar
Rights.
e. Effective Technological Measures means those measures that, in the
absence of proper authority, may not be circumvented under laws
fulfilling obligations under Article 11 of the WIPO Copyright
Treaty adopted on December 20, 1996, and/or similar international
agreements.
f. Exceptions and Limitations means fair use, fair dealing, and/or
any other exception or limitation to Copyright and Similar Rights
that applies to Your use of the Licensed Material.
g. License Elements means the license attributes listed in the name
of a Creative Commons Public License. The License Elements of this
Public License are Attribution and ShareAlike.
h. Licensed Material means the artistic or literary work, database,
or other material to which the Licensor applied this Public
License.
i. Licensed Rights means the rights granted to You subject to the
terms and conditions of this Public License, which are limited to
all Copyright and Similar Rights that apply to Your use of the
Licensed Material and that the Licensor has authority to license.
j. Licensor means the individual(s) or entity(ies) granting rights
under this Public License.
k. Share means to provide material to the public by any means or
process that requires permission under the Licensed Rights, such
as reproduction, public display, public performance, distribution,
dissemination, communication, or importation, and to make material
available to the public including in ways that members of the
public may access the material from a place and at a time
individually chosen by them.
l. Sui Generis Database Rights means rights other than copyright
resulting from Directive 96/9/EC of the European Parliament and of
the Council of 11 March 1996 on the legal protection of databases,
as amended and/or succeeded, as well as other essentially
equivalent rights anywhere in the world.
m. You means the individual or entity exercising the Licensed Rights
under this Public License. Your has a corresponding meaning.
Section 2 -- Scope.
a. License grant.
1. Subject to the terms and conditions of this Public License,
the Licensor hereby grants You a worldwide, royalty-free,
non-sublicensable, non-exclusive, irrevocable license to
exercise the Licensed Rights in the Licensed Material to:
a. reproduce and Share the Licensed Material, in whole or
in part; and
b. produce, reproduce, and Share Adapted Material.
2. Exceptions and Limitations. For the avoidance of doubt, where
Exceptions and Limitations apply to Your use, this Public
License does not apply, and You do not need to comply with
its terms and conditions.
3. Term. The term of this Public License is specified in Section
6(a).
4. Media and formats; technical modifications allowed. The
Licensor authorizes You to exercise the Licensed Rights in
all media and formats whether now known or hereafter created,
and to make technical modifications necessary to do so. The
Licensor waives and/or agrees not to assert any right or
authority to forbid You from making technical modifications
necessary to exercise the Licensed Rights, including
technical modifications necessary to circumvent Effective
Technological Measures. For purposes of this Public License,
simply making modifications authorized by this Section 2(a)
(4) never produces Adapted Material.
5. Downstream recipients.
a. Offer from the Licensor -- Licensed Material. Every
recipient of the Licensed Material automatically
receives an offer from the Licensor to exercise the
Licensed Rights under the terms and conditions of this
Public License.
b. Additional offer from the Licensor -- Adapted Material.
Every recipient of Adapted Material from You
automatically receives an offer from the Licensor to
exercise the Licensed Rights in the Adapted Material
under the conditions of the Adapter's License You apply.
c. No downstream restrictions. You may not offer or impose
any additional or different terms or conditions on, or
apply any Effective Technological Measures to, the
Licensed Material if doing so restricts exercise of the
Licensed Rights by any recipient of the Licensed
Material.
6. No endorsement. Nothing in this Public License constitutes or
may be construed as permission to assert or imply that You
are, or that Your use of the Licensed Material is, connected
with, or sponsored, endorsed, or granted official status by,
the Licensor or others designated to receive attribution as
provided in Section 3(a)(1)(A)(i).
b. Other rights.
1. Moral rights, such as the right of integrity, are not
licensed under this Public License, nor are publicity,
privacy, and/or other similar personality rights; however, to
the extent possible, the Licensor waives and/or agrees not to
assert any such rights held by the Licensor to the limited
extent necessary to allow You to exercise the Licensed
Rights, but not otherwise.
2. Patent and trademark rights are not licensed under this
Public License.
3. To the extent possible, the Licensor waives any right to
collect royalties from You for the exercise of the Licensed
Rights, whether directly or through a collecting society
under any voluntary or waivable statutory or compulsory
licensing scheme. In all other cases the Licensor expressly
reserves any right to collect such royalties.
Section 3 -- License Conditions.
Your exercise of the Licensed Rights is expressly made subject to the
following conditions.
a. Attribution.
1. If You Share the Licensed Material (including in modified
form), You must:
a. retain the following if it is supplied by the Licensor
with the Licensed Material:
i. identification of the creator(s) of the Licensed
Material and any others designated to receive
attribution, in any reasonable manner requested by
the Licensor (including by pseudonym if
designated);
ii. a copyright notice;
iii. a notice that refers to this Public License;
iv. a notice that refers to the disclaimer of
warranties;
v. a URI or hyperlink to the Licensed Material to the
extent reasonably practicable;
b. indicate if You modified the Licensed Material and
retain an indication of any previous modifications; and
c. indicate the Licensed Material is licensed under this
Public License, and include the text of, or the URI or
hyperlink to, this Public License.
2. You may satisfy the conditions in Section 3(a)(1) in any
reasonable manner based on the medium, means, and context in
which You Share the Licensed Material. For example, it may be
reasonable to satisfy the conditions by providing a URI or
hyperlink to a resource that includes the required
information.
3. If requested by the Licensor, You must remove any of the
information required by Section 3(a)(1)(A) to the extent
reasonably practicable.
b. ShareAlike.
In addition to the conditions in Section 3(a), if You Share
Adapted Material You produce, the following conditions also apply.
1. The Adapter's License You apply must be a Creative Commons
license with the same License Elements, this version or
later, or a BY-SA Compatible License.
2. You must include the text of, or the URI or hyperlink to, the
Adapter's License You apply. You may satisfy this condition
in any reasonable manner based on the medium, means, and
context in which You Share Adapted Material.
3. You may not offer or impose any additional or different terms
or conditions on, or apply any Effective Technological
Measures to, Adapted Material that restrict exercise of the
rights granted under the Adapter's License You apply.
Section 4 -- Sui Generis Database Rights.
Where the Licensed Rights include Sui Generis Database Rights that
apply to Your use of the Licensed Material:
a. for the avoidance of doubt, Section 2(a)(1) grants You the right
to extract, reuse, reproduce, and Share all or a substantial
portion of the contents of the database;
b. if You include all or a substantial portion of the database
contents in a database in which You have Sui Generis Database
Rights, then the database in which You have Sui Generis Database
Rights (but not its individual contents) is Adapted Material,
including for purposes of Section 3(b); and
c. You must comply with the conditions in Section 3(a) if You Share
all or a substantial portion of the contents of the database.
For the avoidance of doubt, this Section 4 supplements and does not
replace Your obligations under this Public License where the Licensed
Rights include other Copyright and Similar Rights.
Section 5 -- Disclaimer of Warranties and Limitation of Liability.
a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE
EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS
AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF
ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS,
IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION,
WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS,
ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT
KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT
ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU.
b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE
TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION,
NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT,
INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES,
COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR
USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR
DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR
IN PART, THIS LIMITATION MAY NOT APPLY TO YOU.
c. The disclaimer of warranties and limitation of liability provided
above shall be interpreted in a manner that, to the extent
possible, most closely approximates an absolute disclaimer and
waiver of all liability.
Section 6 -- Term and Termination.
a. This Public License applies for the term of the Copyright and
Similar Rights licensed here. However, if You fail to comply with
this Public License, then Your rights under this Public License
terminate automatically.
b. Where Your right to use the Licensed Material has terminated under
Section 6(a), it reinstates:
1. automatically as of the date the violation is cured, provided
it is cured within 30 days of Your discovery of the
violation; or
2. upon express reinstatement by the Licensor.
For the avoidance of doubt, this Section 6(b) does not affect any
right the Licensor may have to seek remedies for Your violations
of this Public License.
c. For the avoidance of doubt, the Licensor may also offer the
Licensed Material under separate terms or conditions or stop
distributing the Licensed Material at any time; however, doing so
will not terminate this Public License.
d. Sections 1, 5, 6, 7, and 8 survive termination of this Public
License.
Section 7 -- Other Terms and Conditions.
a. The Licensor shall not be bound by any additional or different
terms or conditions communicated by You unless expressly agreed.
b. Any arrangements, understandings, or agreements regarding the
Licensed Material not stated herein are separate from and
independent of the terms and conditions of this Public License.
Section 8 -- Interpretation.
a. For the avoidance of doubt, this Public License does not, and
shall not be interpreted to, reduce, limit, restrict, or impose
conditions on any use of the Licensed Material that could lawfully
be made without permission under this Public License.
b. To the extent possible, if any provision of this Public License is
deemed unenforceable, it shall be automatically reformed to the
minimum extent necessary to make it enforceable. If the provision
cannot be reformed, it shall be severed from this Public License
without affecting the enforceability of the remaining terms and
conditions.
c. No term or condition of this Public License will be waived and no
failure to comply consented to unless expressly agreed to by the
Licensor.
d. Nothing in this Public License constitutes or may be interpreted
as a limitation upon, or waiver of, any privileges and immunities
that apply to the Licensor or You, including from the legal
processes of any jurisdiction or authority.
=======================================================================
Creative Commons is not a party to its public
licenses. Notwithstanding, Creative Commons may elect to apply one of
its public licenses to material it publishes and in those instances
will be considered the “Licensor.” The text of the Creative Commons
public licenses is dedicated to the public domain under the CC0 Public
Domain Dedication. Except for the limited purpose of indicating that
material is shared under a Creative Commons public license or as
otherwise permitted by the Creative Commons policies published at
creativecommons.org/policies, Creative Commons does not authorize the
use of the trademark "Creative Commons" or any other trademark or logo
of Creative Commons without its prior written consent including,
without limitation, in connection with any unauthorized modifications
to any of its public licenses or any other arrangements,
understandings, or agreements concerning use of licensed material. For
the avoidance of doubt, this paragraph does not form part of the
public licenses.
Creative Commons may be contacted at creativecommons.org.

@ -0,0 +1,164 @@
# pds/skeleton
This publication describes a standard filesystem skeleton suitable for all PHP
packages.
Please see <https://github.com/php-pds/skeleton_research> for background
information.
Command-line tools included with this standard are documented [here](./docs/tools.md).
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
"SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this publication are to be
interpreted as described in [RFC 2119](http://tools.ietf.org/html/rfc2119).
## Summary
A package MUST use these names for these root-level directories:
| If a package has a root-level directory for ... | ... then it MUST be named: |
| ----------------------------------------------- | -------------------------- |
| command-line executables | `bin/` |
| configuration files | `config/` |
| documentation files | `docs/` |
| web server files | `public/` |
| other resource files | `resources/` |
| PHP source code | `src/` |
| test code | `tests/` |
A package MUST use these names for these root-level files:
| If a package has a root-level file for ... | ... then it MUST be named: |
| ----------------------------------------------- | -------------------------- |
| a log of changes between releases | `CHANGELOG(.*)` |
| guidelines for contributors | `CONTRIBUTING(.*)` |
| licensing information | `LICENSE(.*)` |
| information about the package itself | `README(.*)` |
A package SHOULD include a root-level file indicating the licensing and
copyright terms of the package contents.
## Root-Level Directories
### bin/
If the package provides a root-level directory for command-line executable
files, it MUST be named `bin/`.
This publication does not otherwise define the structure and contents of the
directory.
### config/
If the package provides a root-level directory for configuration files, it MUST
be named `config/`.
This publication does not otherwise define the structure and contents of the
directory.
### docs/
If the package provides a root-level directory for documentation files, it MUST
be named `docs/`.
This publication does not otherwise define the structure and contents of the
directory.
### public/
If the package provides a root-level directory for web server files, it MUST be
named `public/`.
This publication does not otherwise define the structure and contents of the
directory.
> N.b.: This directory MAY be intended as a web server document root.
> Alternatively, it MAY be that the files will be served dynamically via other
> code, copied or symlinked to the "real" document root, or otherwise managed so
> that they become publicly available on the web.
### resources/
If the package provides a root-level directory for other resource files, it MUST
be named `resources/`.
This publication does not otherwise define the structure and contents of the
directory.
### src/
If the package provides a root-level directory for PHP source code files, it
MUST be named `src/`.
This publication does not otherwise define the structure and contents of the
directory.
### tests/
If the package provides a root-level directory for test files, it MUST be named
`tests/`.
This publication does not otherwise define the structure and contents of the
directory.
### Other Directories
The package MAY contain other root-level directories for purposes not described
by this publication.
This publication does not define the structure and contents of the other
root-level directories.
## Root-Level Files
### CHANGELOG
If the package provides a root-level file with a list of changes since the last
release or version, it MUST be named `CHANGELOG`.
It MAY have a lowercase filename extension indicating the file format.
This publication does not otherwise define the structure and contents of the
file.
### CONTRIBUTING
If the package provides a root-level file that describes how to contribute to
the package, it MUST be named `CONTRIBUTING`.
It MAY have a lowercase filename extension indicating the file format.
This publication does not otherwise define the structure and contents of the
file.
### LICENSE
Whereas package consumers might be in violation of copyright law when copying
unlicensed intellectual property, the package SHOULD include a root-level file
indicating the licensing and copyright terms of the package contents.
If the package provides a root-level file indicating the licensing and copyright
terms of the package contents, it MUST be named `LICENSE`.
It MAY have a lowercase filename extension indicating the file format.
This publication does not otherwise define the structure and contents of the
file.
### README
If the package provides a root-level file with information about the package
itself, it MUST be named `README`.
It MAY have a lowercase filename extension indicating the file format.
This publication does not otherwise define the structure and contents of the
file.
### Other Files
The package MAY contain other root-level files for purposes not described in
this publication.
This publication does not define the structure and contents of the other
root-level files.

@ -0,0 +1,25 @@
#!/usr/bin/env php
<?php
$autoload = null;
$autoloadFiles = [
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../../autoload.php'
];
foreach ($autoloadFiles as $autoloadFile) {
if (file_exists($autoloadFile)) {
$autoload = $autoloadFile;
break;
}
}
if (! $autoload) {
echo "Autoload file not found; try 'composer dump-autoload' first." . PHP_EOL;
exit(1);
}
require $autoload;
$console = new \Pds\Skeleton\Console();
$console->execute($argv);

@ -0,0 +1,18 @@
{
"name": "pds/skeleton",
"type": "standard",
"description": "Standard for PHP package skeletons.",
"homepage": "https://github.com/php-pds/skeleton",
"license": "CC-BY-SA-4.0",
"autoload": {
"psr-4": {
"Pds\\Skeleton\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Pds\\Skeleton\\": "tests/"
}
},
"bin": ["bin/pds-skeleton"]
}

@ -0,0 +1,19 @@
# Command-Line Tools
## Validator
Validate your project's compliance by following these steps:
- Install package in your project: `composer require pds/skeleton @dev`
- Run the validator: `vendor/bin/pds-skeleton validate [path]`
If no path is specified, the project in which pds-skeleton is installed will be used.
## Generator
Generate a compliant package skeleton by following these steps:
- Install package in your project: `composer require pds/skeleton @dev`
- Run the generator: `vendor/bin/pds-skeleton generate [path]`
If no path is specified, the project in which pds-skeleton is installed will be used.

@ -0,0 +1,318 @@
<?php
namespace Pds\Skeleton;
class ComplianceValidator
{
const STATE_OPTIONAL_NOT_PRESENT = 1;
const STATE_CORRECT_PRESENT = 2;
const STATE_RECOMMENDED_NOT_PRESENT = 3;
const STATE_INCORRECT_PRESENT = 4;
protected $files = null;
public function execute($root = null)
{
$lines = $this->getFiles($root);
$results = $this->validate($lines);
$this->outputResults($results);
return true;
}
public function validate($lines)
{
$complianceTests = [
"Command-line executables" => $this->checkBin($lines),
"Configuration files" => $this->checkConfig($lines),
"Documentation files" => $this->checkDocs($lines),
"Web server files" => $this->checkPublic($lines),
"Other resource files" => $this->checkResources($lines),
"PHP source code" => $this->checkSrc($lines),
"Test code" => $this->checkTests($lines),
"Log of changes between releases" => $this->checkChangelog($lines),
"Guidelines for contributors" => $this->checkContributing($lines),
"Licensing information" => $this->checkLicense($lines),
"Information about the package itself" => $this->checkReadme($lines),
];
$results = [];
foreach ($complianceTests as $label => $complianceResult) {
$state = $complianceResult[0];
$expected = $complianceResult[1];
$actual = $complianceResult[2];
$results[$expected] = [
'label' => $label,
'state' => $state,
'expected' => $expected,
'actual' => $actual,
];
}
return $results;
}
/**
* Get list of files and directories previously set, or generate from parent project.
*/
public function getFiles($root = null)
{
$root = $root ?: __DIR__ . '/../../../../';
$root = realpath($root);
if ($this->files == null) {
$files = scandir($root);
foreach ($files as $i => $file) {
if (is_dir("$root/$file")) {
$files[$i] .= "/";
}
}
$this->files = $files;
}
return $this->files;
}
public function outputResults($results)
{
foreach ($results as $result) {
$this->outputResultLine($result['label'], $result['state'], $result['expected'], $result['actual']);
}
}
protected function outputResultLine($label, $complianceState, $expected, $actual)
{
$messages = [
self::STATE_OPTIONAL_NOT_PRESENT => "Optional {$expected} not present",
self::STATE_CORRECT_PRESENT => "Correct {$actual} present",
self::STATE_INCORRECT_PRESENT => "Incorrect {$actual} present",
self::STATE_RECOMMENDED_NOT_PRESENT => "Recommended {$expected} not present",
];
echo $this->colorConsoleText("- " . $label . ": " . $messages[$complianceState], $complianceState) . PHP_EOL;
}
protected function colorConsoleText($text, $complianceState)
{
$colors = [
self::STATE_OPTIONAL_NOT_PRESENT => "\033[43;30m",
self::STATE_CORRECT_PRESENT => "\033[42;30m",
self::STATE_INCORRECT_PRESENT => "\033[41m",
self::STATE_RECOMMENDED_NOT_PRESENT => "\033[41m",
];
if (!array_key_exists($complianceState, $colors)) {
return $text;
}
return $colors[$complianceState] . " " . $text . " \033[0m";
}
protected function checkDir($lines, $pass, array $fail)
{
foreach ($lines as $line) {
$line = trim($line);
if ($line == $pass) {
return [self::STATE_CORRECT_PRESENT, $pass, $line];
}
if (in_array($line, $fail)) {
return [self::STATE_INCORRECT_PRESENT, $pass, $line];
}
}
return [self::STATE_OPTIONAL_NOT_PRESENT, $pass, null];
}
protected function checkFile($lines, $pass, array $fail, $state = self::STATE_OPTIONAL_NOT_PRESENT)
{
foreach ($lines as $line) {
$line = trim($line);
if (preg_match("/^{$pass}(\.[a-z]+)?$/", $line)) {
return [self::STATE_CORRECT_PRESENT, $pass, $line];
}
foreach ($fail as $regex) {
if (preg_match($regex, $line)) {
return [self::STATE_INCORRECT_PRESENT, $pass, $line];
}
}
}
return [$state, $pass, null];
}
protected function checkChangelog($lines)
{
return $this->checkFile($lines, 'CHANGELOG', [
'/^.*CHANGLOG.*$/i',
'/^.*CAHNGELOG.*$/i',
'/^WHATSNEW(\.[a-z]+)?$/i',
'/^RELEASE((_|-)?NOTES)?(\.[a-z]+)?$/i',
'/^RELEASES(\.[a-z]+)?$/i',
'/^CHANGES(\.[a-z]+)?$/i',
'/^CHANGE(\.[a-z]+)?$/i',
'/^HISTORY(\.[a-z]+)?$/i',
]);
}
protected function checkContributing($lines)
{
return $this->checkFile($lines, 'CONTRIBUTING', [
'/^DEVELOPMENT(\.[a-z]+)?$/i',
'/^README\.CONTRIBUTING(\.[a-z]+)?$/i',
'/^DEVELOPMENT_README(\.[a-z]+)?$/i',
'/^CONTRIBUTE(\.[a-z]+)?$/i',
'/^HACKING(\.[a-z]+)?$/i',
]);
}
protected function checkLicense($lines)
{
return $this->checkFile(
$lines,
'LICENSE',
[
'/^.*EULA.*$/i',
'/^.*(GPL|BSD).*$/i',
'/^([A-Z-]+)?LI(N)?(S|C)(E|A)N(S|C)(E|A)(_[A-Z_]+)?(\.[a-z]+)?$/i',
'/^COPY(I)?NG(\.[a-z]+)?$/i',
'/^COPYRIGHT(\.[a-z]+)?$/i',
],
self::STATE_RECOMMENDED_NOT_PRESENT
);
}
protected function checkReadme($lines)
{
return $this->checkFile($lines, 'README', [
'/^USAGE(\.[a-z]+)?$/i',
'/^SUMMARY(\.[a-z]+)?$/i',
'/^DESCRIPTION(\.[a-z]+)?$/i',
'/^IMPORTANT(\.[a-z]+)?$/i',
'/^NOTICE(\.[a-z]+)?$/i',
'/^GETTING(_|-)STARTED(\.[a-z]+)?$/i',
]);
}
protected function checkBin($lines)
{
return $this->checkDir($lines, 'bin/', [
'cli/',
'scripts/',
'console/',
'shell/',
'script/',
]);
}
protected function checkConfig($lines)
{
return $this->checkDir($lines, 'config/', [
'etc/',
'settings/',
'configuration/',
'configs/',
'_config/',
'conf/',
]);
}
protected function checkDocs($lines)
{
return $this->checkDir($lines, 'docs/', [
'manual/',
'documentation/',
'usage/',
'doc/',
'guide/',
'phpdoc/',
'apidocs/',
'apidoc/',
'api-reference/',
'user_guide/',
'manuals/',
'phpdocs/',
]);
}
protected function checkPublic($lines)
{
return $this->checkDir($lines, 'public/', [
'assets/',
'static/',
'html/',
'httpdocs/',
'media/',
'docroot/',
'css/',
'fonts/',
'styles/',
'style/',
'js/',
'javascript/',
'images/',
'site/',
'mysite/',
'img/',
'web/',
'pub/',
'webroot/',
'www/',
'htdocs/',
'asset/',
'public_html/',
'publish/',
'pages/',
'javascripts/',
'icons/',
'imgs/',
'wwwroot/',
'font/',
]);
}
protected function checkSrc($lines)
{
return $this->checkDir($lines, 'src/', [
'exception/',
'exceptions/',
'src-files/',
'traits/',
'interfaces/',
'common/',
'sources/',
'php/',
'inc/',
'libraries/',
'autoloads/',
'autoload/',
'source/',
'includes/',
'include/',
'lib/',
'libs/',
'library/',
'code/',
'classes/',
'func/',
'src-dev/',
]);
}
protected function checkTests($lines)
{
return $this->checkDir($lines, 'tests/', [
'test/',
'unit-tests/',
'phpunit/',
'testing/',
'unittest/',
'unit_tests/',
'unit_test/',
'phpunit-tests/',
]);
}
protected function checkResources($lines)
{
return $this->checkDir($lines, 'resources/', [
'Resources/',
'res/',
'resource/',
'Resource/',
'ressources/',
'Ressources/',
]);
}
}

@ -0,0 +1,44 @@
<?php
namespace Pds\Skeleton;
class Console
{
protected $commandsWhitelist = [
'validate' => 'Pds\Skeleton\ComplianceValidator',
'generate' => 'Pds\Skeleton\PackageGenerator',
'test' => 'Pds\Skeleton\TestRunner',
];
public function execute($args)
{
if (count($args) > 1) {
$executable = array_shift($args);
$commandName = array_shift($args);
if (array_key_exists($commandName, $this->commandsWhitelist)) {
return $this->executeCommand($this->commandsWhitelist[$commandName], $args);
}
$this->outputHelp();
return false;
}
$this->outputHelp();
return false;
}
protected function executeCommand($commandClass, $args)
{
$command = new $commandClass();
return $command->execute(...$args);
}
protected function outputHelp()
{
echo 'Available commands:' . PHP_EOL;
foreach ($this->commandsWhitelist as $key => $value) {
echo 'pds-skeleton ' . $key . PHP_EOL;
}
}
}

@ -0,0 +1,61 @@
<?php
namespace Pds\Skeleton;
class PackageGenerator
{
public function execute($root = null)
{
$validator = new ComplianceValidator();
$lines = $validator->getFiles();
$validatorResults = $validator->validate($lines);
$files = $this->createFiles($validatorResults, $root);
$this->outputResults($files);
return true;
}
public function createFiles($validatorResults, $root = null)
{
$root = $root ?: __DIR__ . '/../../../../';
$root = realpath($root);
$files = $this->createFileList($validatorResults);
$createdFiles = [];
foreach ($files as $i => $file) {
$isDir = substr($file, -1, 1) == '/';
if ($isDir) {
$path = $root . '/' . substr($file, 0, -1);
$createdFiles[$file] = $path;
mkdir($path, 0755);
continue;
}
$path = $root . '/' . $file . '.md';
$createdFiles[$file] = $file . '.md';
file_put_contents($path, '');
chmod($path, 0644);
}
return $createdFiles;
}
public function createFileList($validatorResults)
{
$files = [];
foreach ($validatorResults as $label => $complianceResult) {
if (in_array($complianceResult['state'], [
ComplianceValidator::STATE_OPTIONAL_NOT_PRESENT,
ComplianceValidator::STATE_RECOMMENDED_NOT_PRESENT,
])) {
$files[$label] = $complianceResult['expected'];
}
}
return $files;
}
public function outputResults($results)
{
foreach ($results as $file) {
echo "Created {$file}" . PHP_EOL;
}
}
}

@ -0,0 +1,11 @@
<?php
namespace Pds\Skeleton;
class TestRunner
{
public function execute()
{
ComplianceValidatorTest::run();
PackageGeneratorTest::run();
}
}

@ -0,0 +1,54 @@
<?php
namespace Pds\Skeleton;
class ComplianceValidatorTest
{
public $numErrors = 0;
public static function run()
{
$tester = new ComplianceValidatorTest();
$tester->testValidate_WithIncorrectBin_ReturnsIncorrectBin();
echo __CLASS__ . " errors: {$tester->numErrors}" . PHP_EOL;
}
public function testValidate_WithIncorrectBin_ReturnsIncorrectBin()
{
$paths = [
'cli/',
'src/',
];
$validator = new ComplianceValidator();
$results = $validator->validate($paths);
foreach ($results as $expected => $result) {
if ($expected == "bin/") {
if ($result['state'] != ComplianceValidator::STATE_INCORRECT_PRESENT) {
$this->numErrors++;
echo __FUNCTION__ . ": Expected state of {$result['expected']} to be STATE_INCORRECT_PRESENT" . PHP_EOL;
}
continue;
}
if ($expected == "src/") {
if ($result['state'] != ComplianceValidator::STATE_CORRECT_PRESENT) {
$this->numErrors++;
echo __FUNCTION__ . ": Expected state of {$result['expected']} to be STATE_CORRECT_PRESENT" . PHP_EOL;
}
continue;
}
if ($expected == "LICENSE") {
if ($result['state'] != ComplianceValidator::STATE_RECOMMENDED_NOT_PRESENT) {
$this->numErrors++;
echo __FUNCTION__ . ": Expected state of {$result['expected']} to be STATE_RECOMMENDED_NOT_PRESENT" . PHP_EOL;
}
continue;
}
if ($result['state'] != ComplianceValidator::STATE_OPTIONAL_NOT_PRESENT) {
$this->numErrors++;
echo __FUNCTION__ . ": Expected state of {$result['expected']} to be STATE_OPTIONAL_NOT_PRESENT" . PHP_EOL;
continue;
}
}
}
}

@ -0,0 +1,41 @@
<?php
namespace Pds\Skeleton;
class PackageGeneratorTest
{
public $numErrors = 0;
public static function run()
{
$tester = new PackageGeneratorTest();
$tester->testGenerate_WithMissingBin_ReturnsBin();
echo __CLASS__ . " errors: {$tester->numErrors}" . PHP_EOL;
}
public function testGenerate_WithMissingBin_ReturnsBin()
{
$validatorResults = [
'bin/' => [
'state' => ComplianceValidator::STATE_OPTIONAL_NOT_PRESENT,
'expected' => 'bin/',
],
'config/' => [
'state' => ComplianceValidator::STATE_INCORRECT_PRESENT,
'expected' => 'config/',
],
];
$generator = new PackageGenerator();
$files = $generator->createFileList($validatorResults);
if (!array_key_exists('bin/', $files)) {
$this->numErrors++;
echo __FUNCTION__ . ": Expected bin/ to be present" . PHP_EOL;
}
if (array_key_exists('config/', $files)) {
$this->numErrors++;
echo __FUNCTION__ . ": Expected config/ to be absent" . PHP_EOL;
}
}
}
Loading…
Cancel
Save