Skip to content

Commit

Permalink
Fix getRss..() for plxFeed (#746)
Browse files Browse the repository at this point in the history
Prints directly the result. No returned value.
New function plxRecord::lastUpdateDate()

Co-authored-by: Jean-Pierre Pourrez <[email protected]>
  • Loading branch information
kazimentou and bazooka07 committed Feb 8, 2024
1 parent 47cf52d commit e4dcd48
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 73 deletions.
98 changes: 41 additions & 57 deletions core/lib/class.plx.feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ protected function __construct($filename) {
parent::__construct($filename);
$this->bypage = $this->aConf['bypage_feed'];
$this->tri = 'desc'; # pour les articles
$this->rssLastBuildDate = '';
$this->clef = $this->aConf['clef'];

# Hook plugins
Expand Down Expand Up @@ -101,7 +100,6 @@ public function fprechauffage() {
$this->motif = '#('.implode('|', $ids).')\.(?:pin,|home,|\d{3},)*(?:'.$this->activeCats.')(?:,\d{3})*\.\d{3}\.\d{12}\.[\w-]+\.xml$#';
} else
$this->motif = '';

}
elseif($this->get AND preg_match('#^(?:atom/|rss/)?commentaires/article(\d+)/?$#',$this->get,$capture)) {
$this->mode = 'commentaire'; # Mode du flux
Expand All @@ -112,18 +110,19 @@ public function fprechauffage() {
}
elseif($this->get AND preg_match('#^admin([\w-]+)/commentaires/(hors|en)-ligne/?$#',$this->get,$capture)) {
$this->mode = 'admin'; # Mode du flux
$this->cible = '-'; # /!\: il ne faut pas initialiser à blanc sinon ça prend par défaut les commentaires en ligne (faille sécurité)
if ($capture[1] == $this->clef) {
if($capture[2] == 'hors')
$this->cible = '_';
elseif($capture[2] == 'en')
$this->cible = '';
$this->cible = ($capture[2] == 'hors') ? '_' : '';
} else {
header('Content-Type: text/plain; charset='.PLX_CHARSET);
echo L_FEED_NO_PRIVATE_URL;
exit;
}
} else {
$this->mode = 'article'; # Mode du flux
# On modifie le motif de recherche
$this->motif = '#^\d{4}\.(?:pin,|home,|\d{3},)*(?:'.$this->activeCats.')(?:,\d{3})*\.\d{3}\.\d{12}\.[\w-]+\.xml$#';
}

# Hook plugins
eval($this->plxPlugins->callHook('plxFeedPreChauffageEnd'));

Expand Down Expand Up @@ -153,32 +152,15 @@ public function fdemarrage() {
}
} else {
# Flux de commentaires global
$regex = '#^\d{4}.\d{10}-\d+.xml$#';
$regex = '#^\d{4}\.\d{10}-\d+.xml$#';
}
$this->getCommentaires($regex, 'rsort', 0, $this->bypage);
$items = $this->getRssComments();
$this->getRssComments();
} elseif($this->mode == 'admin') {
# Flux admin
if(empty($this->clef)) { # Clef non initialisée
header('Content-Type: text/plain; charset='.PLX_CHARSET);
echo L_FEED_NO_PRIVATE_URL;
exit;
}

# On recherche le dernier commentaire soumis
$aFiles = $this->plxGlob_coms->query('#^_?\d{4}\.\d{10}-\d+\.xml$#','com', 'rsort', 0, 1, 'all');
if($aFiles) {
$array = array();
foreach($aFiles as $k=>$v) {
$array[$k] = $this->parseCommentaire(PLX_ROOT.$this->aConf['racine_commentaires'].$v);
}
$this->rssLastBuildDate = $array[0]['date'];
unset($array);
}

# On récupère les commentaires
$this->getCommentaires('#^' . $this->cible . '\d{4}\.\d{10}-\d+\.xml$#', 'rsort', 0, false, 'all');
$items = $this->getAdminComments();
$this->getAdminComments();
} else {
# Flux des articles d'une catégorie ou d'un utilisateur précis
if($this->cible) {
Expand Down Expand Up @@ -212,9 +194,20 @@ public function fdemarrage() {
}
}
$this->getArticles(); # Récupération des articles (on les parse)
$items = $this->getRssArticles();
$this->getRssArticles();
}

# Hook plugins
eval($this->plxPlugins->callHook('plxFeedDemarrageEnd'));

}

/**
* Méthode qui imprime le début du flux RSS
*
* @author J.P. Pourrez @bazooka07
**/
public function printRSSTop() {
if(empty($this->lastBuildDate)) {
$this->lastBuildDate = date('YmdHi');
}
Expand All @@ -224,18 +217,23 @@ public function fdemarrage() {
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="<?= $this->urlRewrite('feed.php?' . $this->get) ?>" />
<title><?= plxUtils::strCheck($this->rssTitle, true, null) ?></title>
<link><?= $this->rssLink ?></link>
<lastBuildDate><?= plxDate::dateIso2rfc822($this->rssLastBuildDate) ?></lastBuildDate>
<lastBuildDate><?= plxDate::dateIso2rfc822($this->lastBuildDate) ?></lastBuildDate>
<language><?= $this->aConf['default_lang'] ?></language>
<description><?= plxUtils::strCheck($this->aConf['description'], true, null) ?></description>
<generator>PluXml</generator>
<?= $items ?>
<?php
}

/**
* Méthode qui imprime la fin du flux RSS
*
* @author J.P. Pourrez @bazooka07
**/
public function printRSSBottom() {
?>
</channel>
</rss>
<?php

# Hook plugins
eval($this->plxPlugins->callHook('plxFeedDemarrageEnd'));

}

/**
Expand Down Expand Up @@ -270,11 +268,10 @@ public function getRssArticles() {
default:
}

# On affiche le flux dans le tampon de sortie
ob_start();

# On va boucler sur les articles si possible
if($this->plxRecord_arts) {
$this->lastBuildDate = $this->plxRecord_arts->lastUpdateDate();
$this->printRSSTop();
while($this->plxRecord_arts->loop()) {
$length = '';
$mimetype = '';
Expand Down Expand Up @@ -331,10 +328,8 @@ public function getRssArticles() {
</item>
<?php
}
$this->printRSSBottom();
}

# On récupère le contenu du tampon de sortie
return ob_get_clean();
}

/**
Expand All @@ -346,7 +341,6 @@ public function getRssArticles() {
public function getRssComments() {

# Traitement initial
$last_updated = '197001010100';
$entry_link = '';
$entry = '';
if($this->cible) { # Commentaires d'un article
Expand All @@ -360,11 +354,10 @@ public function getRssComments() {
$this->rssAttachment = 'comments.rss';
}

# On affiche le flux dans le tampon de sortie
ob_start();

# On va boucler sur les commentaires (s'il y en a)
if($this->plxRecord_coms) {
$this->lastBuildDate = $this->plxRecord_coms->lastUpdateDate();
$this->printRSSTop();
while($this->plxRecord_coms->loop()) {
# Traitement initial
if(isset($this->activeArts[$this->plxRecord_coms->f('article')])) {
Expand All @@ -381,9 +374,6 @@ public function getRssComments() {
$artInfo = $this->artInfoFromFilename($this->plxGlob_arts->aFiles[$this->plxRecord_coms->f('article')]);
$link_com = $this->urlRewrite('?article'.$artId.'/'.$artInfo['artUrl'].'#'.$comId);
}
# On vérifie la date de publication
if($this->plxRecord_coms->f('date') > $last_updated)
$last_updated = $this->plxRecord_coms->f('date');

# On affiche le flux dans un buffer
?>
Expand All @@ -402,12 +392,8 @@ public function getRssComments() {
<?php
}
}
$this->printRSSBottom();
}

$this->rssLastBuildDate = $last_updated;

# On récupère le contenu du tampon de sortie
return ob_get_clean();
}

/**
Expand All @@ -430,10 +416,10 @@ public function getAdminComments() {
$this->rssAttachment = 'comments-online.rss';
}

# On affiche le flux dans le tampon de sortie
ob_start();
# On va boucler sur les commentaires (s'il y en a)
if($this->plxRecord_coms) {
$this->lastBuildDate = $this->plxRecord_coms->lastUpdateDate();
$this->printRSSTop();
while($this->plxRecord_coms->loop()) {
$artId = $this->plxRecord_coms->f('article') + 0;
$comId = $this->cible.$this->plxRecord_coms->f('article').'.'.$this->plxRecord_coms->f('numero');
Expand All @@ -455,9 +441,7 @@ public function getAdminComments() {
</item>
<?php
}
$this->printRSSBottom();
}

# On récupère le contenu du tampon de sortie
return ob_get_clean();
}
}
36 changes: 21 additions & 15 deletions core/lib/class.plx.motor.php
Original file line number Diff line number Diff line change
Expand Up @@ -739,17 +739,22 @@ public function getArticles($publi='before') {

# On calcule la valeur start
$bypage = $this->bypage;
$start = $bypage * ($this->page - 1);
if(
$this->mode == 'home' and
!empty($this->aConf['byhomepage']) and
$this->aConf['byhomepage'] != $this->aConf['bypage']
) {
if($this->page > 1) {
$start = $this->aConf['byhomepage'] + $this->bypage * ($this->page - 2);
} else {
$start = 0;
$bypage = $this->aConf['byhomepage'];
if(defined('PLX_FEED')) {
# Pour flux RSS
$start = 0;
} else {
$start = $bypage * ($this->page - 1);
if(
$this->mode == 'home' and
!empty($this->aConf['byhomepage']) and
$this->aConf['byhomepage'] != $this->aConf['bypage']
) {
if($this->page > 1) {
$start = $this->aConf['byhomepage'] + $this->bypage * ($this->page - 2);
} else {
$start = 0;
$bypage = $this->aConf['byhomepage'];
}
}
}

Expand Down Expand Up @@ -984,17 +989,18 @@ public function getCommentaires($motif,$ordre='sort',$start=0,$limite=false,$pub
# On récupère les fichiers des commentaires
$aFiles = $this->plxGlob_coms->query($motif,'com',$ordre,$start,$limite,$publi);
if($aFiles) { # On a des fichiers
foreach($aFiles as $k=>$v) {
$array[$k] = $this->parseCommentaire(PLX_ROOT.$this->aConf['racine_commentaires'].$v);
$comsList = array();
foreach($aFiles as $v) {
$comsList[] = $this->parseCommentaire(PLX_ROOT.$this->aConf['racine_commentaires'].$v);
}

# hiérarchisation et indentation des commentaires seulement sur les écrans requis
if (!preg_match('#comments?$#',basename($_SERVER['SCRIPT_NAME'], '.php'))) {
$array = $this->parentChildSort_r('index', 'parent', $array);
$comsList = $this->parentChildSort_r('index', 'parent', $comsList);
}

# On stocke les enregistrements dans un objet plxRecord
$this->plxRecord_coms = new plxRecord($array);
$this->plxRecord_coms = new plxRecord($comsList);

}

Expand Down
26 changes: 25 additions & 1 deletion core/lib/class.plx.record.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,28 @@ public function f($field) {
return false;
}

}
/**
* Méthode qui retourne la date de l'enregistrement le plus récent
*
* @$update prendre en compte la date de mise à jour (articles,...)
* @return date au format 'YmdHi' ou null
* @author J.P. Pourrez @bazooka07
**/
public function lastUpdateDate($update=false) {
return array_reduce($this->result, function($carry, $item) use($update) {
if($update and array_key_exists('date_update', $item)) {
# pour les articles si date miseà jour prise en compte
$dt = $item['date_update'];
} elseif(array_key_exists('date', $item)) {
# pour les commentaires
$dt = $item['date'];
} else {
# aucune clé trouvée
return $carry;
}

return ($carry > $dt) ? $carry : $dt;
});
}

}

0 comments on commit e4dcd48

Please sign in to comment.