[ Index ] |
|
Code source de SPIP Agora 1.4 |
1 <?php 2 /***************************************************** 3 * This file is part of Agora, web based content management system. 4 * 5 * Agora is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; version 2 of the License. 8 * 9 * Agora is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details (file "COPYING"). 13 * 14 * Copyright © Arnaud Martin, Antoine Pitrou et Philippe Rivière. 15 * List of authors detailed in "copyright_fr.html" file. 16 * E-mail : agora@sig.premier-ministre.gouv.fr 17 * Web site : http://www.agora.gouv.fr 18 *****************************************************/ 19 // Base class for Article business persistence abstraction. 20 // 21 22 if (defined("_BD_ARTICLEMYSQL")) 23 return; 24 25 define("_BD_ARTICLEMYSQL", "1"); 26 27 require_once dirname(__FILE__). "/../article.php"; 28 29 /** 30 * BD_article_mysql is an implementation for MySQL of BD_article business class. 31 * @package BD_mysql 32 * @author Erwan Le Bescond <elebescond@clever-age.com> 33 * @access public 34 */ 35 class BD_article_mysql extends BD_article { 36 function getRubriqueIdsByStatus ($statut, $postDated) { 37 $rubriqueIds = array(); 38 $db = &$this->_getDB(); 39 40 if (DB::isError($db)) { 41 return PEAR::raiseError( 42 "[" . get_class($this). " DB_article : getRubriqueIdsByStatus()] " . $db->getMessage(). "", null, 43 null, null, 44 null, null, 45 false); 46 } 47 48 $query = "SELECT DISTINCT id_rubrique FROM " . $GLOBALS['table_prefix']. "_articles WHERE statut = '$statut'"; 49 50 if ($postDated == "non") { 51 $query .= " AND date_heure <= NOW()"; 52 } 53 54 $result = $db->query($query); 55 56 if (DB::isError($result)) { 57 return PEAR::raiseError( 58 "[" . get_class($this). " DB_article : getRubriqueIdsByStatus()] " . $result->getMessage(). "", 59 null, 60 null, 61 null, 62 null, 63 null, 64 false); 65 } 66 67 while ($row = $result->fetchRow()) { 68 $rubriqueIds[] = $row['id_rubrique']; 69 } 70 71 $result->free(); 72 return $rubriqueIds; 73 } 74 75 function getMaxDateByRubriqueAndStatus ($rubriqueId, $statut, $postDated) { 76 $dateResult = ''; 77 $db = &$this->_getDB(); 78 79 if (DB::isError($db)) { 80 return PEAR::raiseError( 81 "[" . get_class( 82 $this). " DB_article : getMaxDateByRubriqueAndStatus()] " . $db->getMessage(). "", 83 null, 84 null, 85 null, 86 null, 87 null, 88 false); 89 } 90 91 $query 92 = "SELECT MAX(date_heure) AS date_h FROM " . $GLOBALS['table_prefix']. "_articles " . "WHERE id_rubrique=$rubriqueId AND statut = '$status'"; 93 94 if ($postDated == "non") { 95 $query .= " AND date_heure <= NOW()"; 96 } 97 98 $result = $db->query($query); 99 100 if (DB::isError($result)) { 101 return PEAR::raiseError( 102 "[" . get_class( 103 $this). " DB_article : getMaxDateByRubriqueAndStatus()] " . $result->getMessage(). "", 104 null, 105 null, 106 null, 107 null, 108 null, 109 false); 110 } 111 112 while ($row = $result->fetchRow()) { 113 $dateResult = $row['date_h']; 114 } 115 116 $result->free(); 117 return $dateResult; 118 } 119 120 function isPostDated () { 121 $db = &$this->_getDB(); 122 123 if (DB::isError($db)) { 124 return PEAR::raiseError("[" . get_class($this). " DB_article : isPostDated()] " . $db->getMessage(). "", 125 null, 126 null, 127 null, 128 null, 129 null, 130 false); 131 } 132 133 $query 134 = "SELECT id_article FROM " . $GLOBALS['table_prefix']. "_articles WHERE id_article=" . $this->_articleId . " AND date_heure<=NOW()"; 135 136 $result = $db->query($query); 137 138 if (DB::isError($result)) { 139 return PEAR::raiseError("[" . get_class($this). " DB_article : isPostDated()] " . $result->getMessage(). "", 140 null, 141 null, 142 null, 143 null, 144 null, 145 false); 146 } 147 148 $isPostDated = !($result->numRows()); 149 $result->free(); 150 return $isPostDated; 151 } 152 153 function hasLastModified ($auteurId) { 154 $hasLastModified = true; 155 $db = &$this->_getDB(); 156 157 if (DB::isError($db)) { 158 return PEAR::raiseError("[" . get_class($this). " DB_article : hasLastModified()] " . $db->getMessage(). "", 159 null, 160 null, 161 null, 162 null, 163 null, 164 false); 165 } 166 167 $query 168 = "SELECT auteur_modif, UNIX_TIMESTAMP(date_modif) AS modification, UNIX_TIMESTAMP(NOW()) AS maintenant FROM " . $GLOBALS['table_prefix']. "_articles WHERE id_article='" . $db->quoteString( 169 $this->_articleId). "'"; 170 171 $result = $db->query($query); 172 173 if (DB::isError($result)) { 174 return PEAR::raiseError( 175 "[" . get_class($this). " DB_article : hasLastModified()] " . $result->getMessage(). "", null, 176 null, null, 177 null, null, 178 false); 179 } 180 181 if ($row = $result->fetchRow()) { 182 $auteur_modif = $row["auteur_modif"]; 183 $this->_auteurModif = $auteur_modif; 184 185 $modification = $row["modifications"]; 186 $maintenant = $row["maintenant"]; 187 188 $result->free(); 189 $date_diff = floor(($maintenant - $modification) / 60); 190 $this->_whenWasModified = $date_diff; 191 if ($date_diff >= 0 AND $date_diff < 60 AND $auteur_modif > 0 AND $auteur_modif != $auteurId) { 192 $hasLastModified = false; 193 $query_auteur 194 = "SELECT nom FROM " . $GLOBALS['table_prefix']. "_auteurs WHERE id_auteur='$auteur_modif'"; 195 196 $result_auteur = $db->query($query_auteur); 197 198 if (DB::isError($result_auteur)) { 199 return PEAR::raiseError( 200 "[" . get_class( 201 $this). " DB_article : hasLastModified()] " . $result_auteur->getMessage(). "", 202 null, 203 null, 204 null, 205 null, 206 null, 207 false); 208 } 209 if ($row_auteur = $result_auteur->fetchRow()) { 210 $this->_computedLastModified = true; 211 $this->_lastModifiedUserName = $row_auteur["nom"]; 212 } 213 } 214 } 215 216 return $hasLastModified; 217 } 218 219 // {{{ getToday() 220 221 /** 222 * Returns an array of Article representing 223 * the current day published articles. 224 * 225 * 226 * @return Array of Article 227 * @param $month 228 * @param $year 229 * @access public 230 */ 231 232 function &getToday ($month, $year) { 233 $result = array(); 234 $db = &$this->_getDB(); 235 236 if (DB::isError($db)) { 237 return PEAR::raiseError("[" . get_class($this). " DB_article : getToday()] " . $db->getMessage(). "", null, 238 null, null, 239 null, null, 240 false); 241 } 242 243 $query 244 = "SELECT id_article, titre, date_heure FROM " . $GLOBALS['table_prefix']. "_articles WHERE statut='publie' AND date_heure >='$year-$month-0' AND date_heure < DATE_ADD('$year-$month-1', INTERVAL 1 MONTH) ORDER BY date_heure"; 245 246 $queryResult = $db->query($query); 247 248 if (DB::isError($queryResult)) { 249 return PEAR::raiseError( 250 "[" . get_class($this). " DB_article : getToday()] " . $queryResult->getMessage(). "", null, 251 null, null, 252 null, null, 253 false); 254 } 255 256 while ($row = $queryResult->fetchRow()) { 257 $resultArticle = &BD_article::factory($this->getDbParameters(), $this->getDbOptions()); 258 $resultArticle->setArticleId($row['id_article']); 259 $resultArticle->setTitre($row['titre']); 260 $resultArticle->setDate($row['date_heure']); 261 $result[] = &$resultArticle; 262 } 263 264 $queryResult->free(); 265 return $result; 266 } 267 268 // }}} 269 270 // {{{ getOpened() 271 272 /** 273 * Returns an array of Article representing 274 * the currently opened articles for a given author 275 * 276 * This method is abstract and will be implemented 277 * in a future version, when the data model has been 278 * updated. 279 * 280 * @return Array of Article 281 * @param int $authorId 282 * @access public 283 */ 284 285 function &getOpened ($authorId) { 286 $result = array(); 287 $db = &$this->_getDB(); 288 289 if (DB::isError($db)) { 290 return PEAR::raiseError("[" . get_class($this). " DB_article : getOpened()] " . $db->getMessage(). "", null, 291 null, null, 292 null, null, 293 false); 294 } 295 296 $query 297 = "SELECT id_article, titre FROM " . $GLOBALS['table_prefix']. "_articles WHERE auteur_modif = '$authorId' AND id_rubrique > 0 AND date_modif > DATE_SUB(NOW(), INTERVAL 1 HOUR) ORDER BY date_modif DESC"; 298 299 $queryResult = $db->query($query); 300 301 if (DB::isError($queryResult)) { 302 return PEAR::raiseError( 303 "[" . get_class($this). " DB_article : getOpened()] " . $queryResult->getMessage(). "", null, 304 null, null, 305 null, null, 306 false); 307 } 308 309 while ($row = $queryResult->fetchRow()) { 310 $resultArticle = &BD_article::factory($this->getDbParameters(), $this->getDbOptions()); 311 $resultArticle->setArticleId($row['id_article']); 312 $resultArticle->setTitre($row['titre']); 313 $result[] = &$resultArticle; 314 } 315 316 $queryResult->free(); 317 return $result; 318 } 319 320 // }}} 321 322 // {{{ howManyArticleForMot($conf_mot, $aff_articles, 'refuse') 323 324 function HowManyArticleForMot ($conf_mot, $aff_articles, $statut) { 325 $howManyArticle = 0; 326 $db = &$this->_getDB(); 327 328 if (DB::isError($db)) { 329 return PEAR::raiseError( 330 "[" . get_class($this). " DB_article : HowManyArticleForMot()] " . $db->getMessage(). "", null, 331 null, null, 332 null, null, 333 false); 334 } 335 336 $query 337 = "SELECT COUNT(*) as cnt FROM " . $GLOBALS['table_prefix']. "_mots_articles lien, " . $GLOBALS['table_prefix']. "_articles article WHERE lien.id_mot=$conf_mot AND article.id_article=lien.id_article AND FIND_IN_SET(article.statut,'$aff_articles')>0 AND article.statut!='$statut'"; 338 339 $result = $db->query($query); 340 341 if (DB::isError($result)) { 342 return PEAR::raiseError( 343 "[" . get_class($this). " DB_article : HowManyArticleForMot()] " . $result->getMessage(). "", 344 null, 345 null, 346 null, 347 null, 348 null, 349 false); 350 } 351 352 if ($row = $result->fetchRow()) { 353 $howManyArticle = intval($row["cnt"]); 354 } 355 356 $result->free(); 357 return $howManyArticle; 358 } 359 // }}} 360 361 // {{{ computePopularity()) 362 363 /** 364 * Updates all articles to compute new popularity value 365 * 366 * @return mixed DB::Error if an error occured, nothin otherwise 367 * @access public 368 */ 369 370 //D�ol�pour le code, mais c'est toujours parail : on essaye de garder 371 //au maximum le code original... 372 373 function computePopularity () { 374 $db = &$this->_getDB(); 375 376 if (DB::isError($db)) { 377 return PEAR::raiseError( 378 "[" . get_class($this). " DB_article : computePopularity()] " . $db->getMessage(). "", null, 379 null, null, 380 null, null, 381 false); 382 } 383 384 $date = lire_meta('date_stats_popularite'); 385 include_once (dirname(__FILE__). "/../../../inc_meta.php"); 386 387 ecrire_meta("date_stats_popularite", time()); 388 ecrire_metas(); // il faut le marquer de suite pour eviter les acces concurrents 389 390 $duree = time() - $date; 391 // duree de demi-vie d'une visite dans le calcul de la popularite (en jours) 392 $demivie = 1; 393 // periode de reference en jours 394 $periode = 1; 395 // $a est le coefficient d'amortissement depuis la derniere mesure 396 $a = pow(2, -$duree / ($demivie * 24 * 3600)); 397 // $b est la constante multiplicative permettant d'avoir 398 // une visite par jour (periode de reference) = un point de popularite 399 // (en regime stationnaire) 400 // or, magie des maths, ca vaut log(2) * duree journee/demi-vie 401 // si la demi-vie n'est pas trop proche de la seconde ;) 402 $b = log(2) * $periode / $demivie; 403 404 // oublier un peu le passe 405 $query = "UPDATE " . $GLOBALS['table_prefix']. "_articles SET popularite = popularite * $a"; 406 407 spip_log ("Execute: $query"); 408 409 $result = $db->query($query); 410 411 if (DB::isError($result)) { 412 return PEAR::raiseError( 413 "[" . get_class($this). " DB_article : computePopularity()] " . $result->getMessage(). "", null, 414 null, null, 415 null, null, 416 false); 417 } 418 419 // ajouter les points visites 420 $count_article = array(); 421 422 //----------Modification Clever Age elebescond---------- 423 $visiteMetier = &recuperer_instance_visite(); 424 $allVisites = $visiteMetier->getAllVisiteForTypeSinceXsec($duree, 'article'); 425 426 if (DB::isError($allVisites)) { 427 return PEAR::raiseError( 428 "[" . get_class($this). " DB_article : computePopularity()] " . $allVisites->getMessage(). "", 429 null, 430 null, 431 null, 432 null, 433 null, 434 false); 435 } 436 437 $updatePopQuery 438 = "UPDATE " . $GLOBALS['table_prefix']. "_articles " . "SET popularite = GREATEST(1,popularite) + $b * ! " . "WHERE id_article IN (0!)"; 439 440 while (list(, $maVisite) = each($allVisites)) { 441 $count_article[$maVisite['count']] .= ',' . $maVisite['id_objet']; // l'objet a count visites 442 } 443 /* 444 $query = "SELECT COUNT(*) as count,id_objet FROM ".$GLOBALS['table_prefix']."_visites_temp WHERE maj > DATE_SUB(NOW(), INTERVAL $duree SECOND) AND type='article' GROUP BY id_objet"; 445 $result = $db->query($query); 446 if (DB::isError($result)) { 447 return $result; 448 } 449 450 $updatePopQuery = "UPDATE ".$GLOBALS['table_prefix']."_articles " 451 ."SET popularite = GREATEST(1,popularite) + $b * ! " 452 ."WHERE id_article IN (0!)"; 453 454 while ($row = $result->fetchRow()) { 455 $count_article[$row['count']] .= ','.$row['id_objet']; // l'objet a count visites 456 } 457 */ 458 //---------Fin modification Clever Age------- 459 460 reset ($count_article); 461 while (list($count,$articles) = each($count_article)) { 462 $params = array(); 463 $params[] = $count; 464 $params[] = $articles; 465 $query = $this->_traiteQuery($updatePopQuery); 466 $db->query($query, $params); 467 } 468 469 // ajouter les points referers 470 $count_article = array(); 471 472 //----------Modification Clever Age elebescond---------- 473 $refererMetier = &recuperer_instance_referer(); 474 $allReferers = $refererMetier->getAllRefererForTypeSinceXsec($duree, 'article'); 475 if (DB::isError($allReferers)) { 476 die($allReferers->getMessage()); 477 } 478 while (list(, $monReferer) = each($allReferers)) { 479 $count_article[$monReferer['count']] .= ','.$monReferer['id_objet']; // l'objet a count visites 480 } 481 /* 482 $query = "SELECT COUNT(*) as count,id_objet FROM ".$GLOBALS['table_prefix']."_referers_temp WHERE maj > DATE_SUB(NOW(), INTERVAL $duree SECOND) AND type='article' GROUP BY id_objet"; 483 $result = $db->query($query); 484 if (DB::isError($result)) { 485 return $result; 486 } 487 488 while ($row = $result->fetchRow()) { 489 $count_article[$row['count']] .= ','.$row['id_objet']; // l'objet a count visites 490 } 491 */ 492 //---------Fin modification Clever Age------- 493 494 reset ($count_article); 495 while (list($count,$articles) = each($count_article)) { 496 $params = array(); 497 $params[] = $count; 498 $params[] = $articles; 499 $query = $this->_traiteQuery($updatePopQuery); 500 $db->query($updatePopQuery, $params); 501 } 502 503 // et enregistrer les metas... 504 505 $query = "SELECT MAX(popularite) AS maxpop, SUM(popularite) AS sumpop FROM ".$GLOBALS['table_prefix']."_articles"; 506 507 $result = $db->query($query); 508 if (DB::isError($result)) { 509 return PEAR::raiseError("[".get_class($this)." DB_article : computePopularity()] ".$result->getMessage()."", null, 510 null, null, null, null, false); 511 } 512 if ($row = $result->fetchRow()) { 513 $maxpop = $row['maxpop']; 514 $totalpop = $row['sumpop']; 515 ecrire_meta("popularite_max", $maxpop); 516 ecrire_meta("popularite_total", $totalpop); 517 ecrire_metas(); 518 } 519 $result->free(); 520 } 521 522 // }}} 523 524 // {{{ calculatePublishedRubrique() 525 526 /** 527 * This method return la liste des langues utilisees par les articles. 528 * @return Array 529 */ 530 531 function &calculatePublishedRubrique($trad_lang) { 532 $rubriqueIds = array(); 533 $db = &$this->_getDB(); 534 535 if (DB::isError($db)) { 536 return PEAR::raiseError("[".get_class($this)." DB_article_mysql : calculatePublishedRubrique()] ".$db->getMessage()."", null, 537 null, null, null, null, false); 538 } 539 540 $query = "SELECT DISTINCT a.id_rubrique ". 541 "FROM ".$GLOBALS['table_prefix']."_articles AS a LEFT JOIN ".$GLOBALS['table_prefix']."_articles AS t ". 542 "ON (a.id_article = t.id_trad AND t.lang = '$trad_lang') ". 543 "WHERE a.statut='publie' AND a.lang!='$trad_lang' AND (a.id_trad=0 OR a.id_trad=a.id_article) ". 544 "AND (t.id_article IS NULL OR t.statut!='publie' OR t.date_modif < a.date_modif)"; 545 546 //echo '<br />' . $query . '<br />'; 547 548 $queryResult = $db->query($query); 549 550 if (DB::isError($queryResult)) { 551 return PEAR::raiseError("[".get_class($this)." DB_article_mysql : calculatePublishedRubrique()] ".$queryResult->getMessage()."", null, 552 null, null, null, null, false); 553 } 554 555 while ($row = $queryResult->fetchRow()) { 556 $rubriqueIds[] = $row['id_rubrique']; 557 } 558 $queryResult->free(); 559 return $rubriqueIds; 560 } 561 562 // }}} 563 564 // {{{ calculateNoTraductionForRubrique() 565 566 /** 567 * This method return la liste des langues utilisees par les articles. 568 * @return Array 569 */ 570 571 function calculateNoTraductionForRubrique($trad_lang, $id_parent) { 572 $rows = array(); 573 $db = &$this->_getDB(); 574 575 if (DB::isError($db)) { 576 return PEAR::raiseError("[".get_class($this)." DB_article_mysql : calculateNoTraductionForRubrique()] ".$db->getMessage()."", null, 577 null, null, null, null, false); 578 } 579 580 $query = "SELECT a.id_article, a.titre, a.date_heure, a.descriptif, a.lang, t.id_article AS trad_id_article, t.statut AS trad_statut, (t.date_modif >= a.date_modif) AS trad_a_jour ". 581 "FROM ".$GLOBALS['table_prefix']."_articles AS a LEFT JOIN ".$GLOBALS['table_prefix']."_articles AS t ". 582 "ON (a.id_article = t.id_trad AND t.lang = '$trad_lang') ". 583 "WHERE a.id_rubrique=$id_parent AND a.statut='publie' AND a.lang!='$trad_lang' AND (a.id_trad=0 OR a.id_trad=a.id_article) ". 584 "AND (t.id_article IS NULL OR t.statut!='publie' OR t.date_modif < a.date_modif) ". 585 "ORDER BY t.statut='publie' DESC, trad_a_jour DESC, a.titre"; 586 587 //echo '<br />' . $query . '<br />'; 588 589 $queryResult = $db->query($query); 590 591 if (DB::isError($queryResult)) { 592 return PEAR::raiseError("[".get_class($this)." DB_article_mysql : calculateNoTraductionForRubrique()] ".$queryResult->getMessage()."", null, 593 null, null, null, null, false); 594 } 595 596 while ($row = $queryResult->fetchRow()) { 597 $rows[] = $row; 598 } 599 600 $queryResult->free(); 601 return $rows; 602 } 603 604 // }}} 605 606 // {{{ getTradStatistics() 607 608 /** 609 * This method return la liste des langues utilisees par les articles. 610 * @return Array 611 */ 612 613 function getTradStatistics($trad_lang) { 614 $row = array(); 615 $db = &$this->_getDB(); 616 617 if (DB::isError($db)) { 618 return PEAR::raiseError("[".get_class($this)." DB_article_mysql : calculateNoTraductionForRubrique()] ".$db->getMessage()."", null, 619 null, null, null, null, false); 620 } 621 622 $query = "SELECT COUNT(*) AS total, SUM(t.statut='publie') AS traduits, SUM(t.statut='publie' AND t.date_modif < a.date_modif) AS conflits ". 623 "FROM ".$GLOBALS['table_prefix']."_articles AS a LEFT JOIN ".$GLOBALS['table_prefix']."_articles AS t ". 624 "ON (a.id_article = t.id_trad AND t.lang = '$trad_lang') ". 625 "WHERE a.statut='publie' AND a.lang!='$trad_lang' AND (a.id_trad=0 OR a.id_trad=a.id_article)"; 626 627 628 $queryResult = $db->query($query); 629 630 if (DB::isError($queryResult)) { 631 return PEAR::raiseError("[".get_class($this)." DB_article_mysql : calculateNoTraductionForRubrique()] ".$queryResult->getMessage()."", null, 632 null, null, null, null, false); 633 } 634 635 if($row = $queryResult->fetchRow()) { 636 return $row; 637 } 638 639 $queryResult->free(); 640 return false; 641 } 642 643 // }}} 644 645 } 646 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 14:40:03 2007 | par Balluche grâce à PHPXref 0.7 |