Permalink
Browse files

Automatically determine table name

  • Loading branch information...
Thoulah committed Feb 8, 2019
1 parent 534141d commit ecd0c2713445f91b4d30cb528222c69366cbfabb

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -25,13 +25,13 @@ public function init(): void {
if ($this->artist && $this->year && $this->album) :
$this->page = self::PAGE_ALBUM;
$this->lastModified = Lyrics3Tracks::lastModified($this->artist, $this->year, $this->album);
$this->lastModified = Lyrics3Tracks::getLastModified($this->artist, $this->year, $this->album);
elseif ($this->artist) :
$this->page = self::PAGE_ARTIST;
$this->lastModified = Lyrics2Albums::lastModified($this->artist);
$this->lastModified = Lyrics2Albums::getLastModified($this->artist);
else :
$this->page = self::PAGE_INDEX;
$this->lastModified = Lyrics1Artists::lastModified();
$this->lastModified = Lyrics1Artists::getLastModified();
endif;
parent::init();
@@ -68,11 +68,11 @@ private static function getChangefreq(float $priority): string {
}
private static function getPriority(int $age): float {
if ($age > strtotime("-1 week")) :
if ($age > strtotime('-1 week')) :
$prio = 0.9;
elseif ($age > strtotime("-1 month")) :
elseif ($age > strtotime('-1 month')) :
$prio = 0.8;
elseif ($age > strtotime("-1 year")) :
elseif ($age > strtotime('-1 year')) :
$prio = 0.7;
endif;
return $prio ?? 0.5;
@@ -17,7 +17,7 @@ public function afterFind(): void {
public function artistsList(): array {
return self::find()
->active(self::tableName())
->active()
->orderBy('name')
->all();
}
@@ -29,16 +29,16 @@ public static function albumsList(): BatchQueryResult {
->each();
}
public static function lastModified(): int {
$data = self::find()
->active(self::tableName())
->max('updated');
return Yii::$app->formatter->asTimestamp($data);
}
public function getAlbums(): LyricsQuery {
return $this->hasMany(Lyrics2Albums::className(), ['parent' => 'id'])
->active(Lyrics2Albums::tableName());
->active();
}
public static function getLastModified(): int {
$data = self::find()
->active()
->max('updated');
return strtotime($data);
}
public static function find(): LyricsQuery {
@@ -29,30 +29,20 @@ public static function albumsList(string $artist): array {
->innerJoinWith('artist', 'tracks')
->where(['or', Lyrics1Artists::tableName().'.`name`=:artist', Lyrics1Artists::tableName().'.`url`=:artist'])
->addParams([':artist' => $artist])
->active(self::tableName())
->active()
->all();
}
public static function lastModified(string $artist): int {
$data = self::find()
->innerJoinWith('artist')
->where(['or', Lyrics1Artists::tableName().'.`name`=:artist', Lyrics1Artists::tableName().'.`url`=:artist'])
->addParams([':artist' => $artist])
->active(self::tableName())
->max(self::tableName().'.updated');
return Yii::$app->formatter->asTimestamp($data);
}
public static function buildPdf(object $album, string $html): string {
$pdf = new Pdf();
return $pdf->create(
'@runtime/PDF/lyrics/'.implode(' - ', [$album->artist->url, $album->year, $album->url]),
$html,
Lyrics3Tracks::lastModified($album->artist->url, $album->year, $album->url, (object) ['item' => (object) ['album' => $album]]),
Lyrics3Tracks::getLastModified($album->artist->url, $album->year, $album->url),
[
'author' => $album->artist->name,
'footer' => Html::a(Yii::$app->name, Url::to(['site/index'], true)).'|'.$album->year.'|Page {PAGENO} of {nb}',
'header' => $album->artist->name.'|Lyrics|'.$album->name,
'footer' => implode('|', [Html::a(Yii::$app->name, Url::to(['site/index'], true)), $album->year, 'Page {PAGENO} of {nb}']),
'header' => implode('|', [$album->artist->name, 'Lyrics', $album->name]),
'keywords' => implode(', ', [$album->artist->name, $album->name, 'lyrics']),
'subject' => $album->artist->name.' - '.$album->name,
'title' => implode(' - ', [$album->artist->name, $album->name, 'Lyrics']),
@@ -70,9 +60,19 @@ public static function getCover(int $size, array $album): array {
return [$fileName, Image::resize($album->image, $size)];
}
public static function getLastModified(string $artist): int {
$data = self::find()
->innerJoinWith('artist')
->where(['or', Lyrics1Artists::tableName().'.`name`=:artist', Lyrics1Artists::tableName().'.`url`=:artist'])
->addParams([':artist' => $artist])
->active()
->max(self::tableName().'.updated');
return Yii::$app->formatter->asTimestamp($data);
}
public function getArtist(): LyricsQuery {
return $this->hasOne(Lyrics1Artists::className(), ['id' => 'parent'])
->active(Lyrics1Artists::tableName());
->active();
}
public function getTracks(): LyricsQuery {
@@ -26,7 +26,6 @@ public static function tracksList(string $artist, string $year, string $name): a
return self::find()
->orderBy('track')
->joinWith('artist')
->with('album')
->innerJoinWith('lyrics')
->where(['or', Lyrics1Artists::tableName().'.name=:artist', Lyrics1Artists::tableName().'.url=:artist'])
->andWhere(Lyrics2Albums::tableName().'.year=:year')
@@ -35,11 +34,10 @@ public static function tracksList(string $artist, string $year, string $name): a
->all();
}
public static function lastModified(string $artist, string $year, string $name): int {
public static function getLastModified(string $artist, string $year, string $name): int {
$max = self::find()
->select('GREATEST(MAX('.Lyrics2Albums::tableName().'.`updated`), MAX('.Lyrics4Lyrics::tableName().'.`updated`)) AS `max`')
->joinWith('artist')
->with('album')
->innerJoinWith('lyrics')
->where(['or', Lyrics1Artists::tableName().'.name=:artist', Lyrics1Artists::tableName().'.url=:artist'])
->andWhere(Lyrics2Albums::tableName().'.year=:year')
@@ -51,13 +49,13 @@ public static function lastModified(string $artist, string $year, string $name):
public function getArtist(): ActiveQuery {
return $this->hasOne(Lyrics1Artists::class, ['id' => 'parent'])
->active(Lyrics1Artists::tableName())
->active()
->via('album');
}
public function getAlbum(): ActiveQuery {
return $this->hasOne(Lyrics2Albums::className(), ['id' => 'parent'])
->active(Lyrics2Albums::tableName());
->active();
}
public function getLyrics(): ActiveQuery {
@@ -3,13 +3,10 @@
use Yii;
class LyricsQuery extends \yii\db\ActiveQuery {
const STATUS_INACTIVE = '0';
const STATUS_ACTIVE = '1';
public function active(string $table): self {
public function active(): self {
if (php_sapi_name() === 'cli' || (!Yii::$app->user->isGuest && Yii::$app->user->identity->isAdmin))
return $this->onCondition(['or', [$table.'.active' => [Self::STATUS_INACTIVE, Self::STATUS_ACTIVE]]]);
return $this->onCondition([]);
return $this->onCondition([$table.'.active' => Self::STATUS_ACTIVE]);
return $this->onCondition([$this->modelClass::tableName().'.active' => true]);
}
}
@@ -4,13 +4,13 @@
$doc = Sitemap::beginDoc();
Sitemap::lineItem($doc, ['lyrics/index'], ['age' => Lyrics1Artists::lastModified(), 'locale' => true]);
Sitemap::lineItem($doc, ['lyrics/index'], ['age' => Lyrics1Artists::getLastModified(), 'locale' => true]);
foreach (Lyrics1Artists::albumsList() as $artist) :
$lastModified = Lyrics2Albums::lastModified($artist->url, $artist->albums);
$lastModified = Lyrics2Albums::getLastModified($artist->url);
Sitemap::lineItem($doc, ['lyrics/index', 'artist' => $artist->url], ['age' => $lastModified, 0.65, 'locale' => true]);
foreach ($artist->albums as $album) :
$lastModified = Lyrics3Tracks::lastModified($album->artist->url, $album->year, $album->url, (object) ['item' => (object) ['album' => $album]]);
$lastModified = Lyrics3Tracks::getLastModified($album->artist->url, $album->year, $album->url);
Sitemap::lineItem($doc, ['lyrics/index', 'artist' => $album->artist->url, 'year' => $album->year, 'album' => $album->url], ['age' => $lastModified, 'priority' => 0.5, 'locale' => true]);
Sitemap::lineItem($doc, ['lyrics/albumpdf', 'artist' => $album->artist->url, 'year' => $album->year, 'album' => $album->url], ['age' => $lastModified, 'priority' => 0.5]);
endforeach;
@@ -71,7 +71,7 @@
[
'thumb' => ['albumcover', 'artist' => $album->artist->url, 'year' => $album->year, 'album' => $album->url, 'size' => '100'],
'image' => ['albumcover', 'artist' => $album->artist->url, 'year' => $album->year, 'album' => $album->url, 'size' => '800'],
'title' => implode(' · ', [$album->artist->name, $album->name]),
'title' => implode(' - ', [$album->artist->name, $album->name]),
'group' => $album->artist->url,
],
],

0 comments on commit ecd0c27

Please sign in to comment.