DokuWiki PHP Cross Reference Collaborative Wikis

Source: /feed.php - 510 lines - 19091 bytes - Summary - Text - Print

Description: XML feed export

   1  <?php
   2  /**
   3   * XML feed export
   4   *
   5   * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
   6   * @author     Andreas Gohr <andi@splitbrain.org>
   7   *
   8   * @global array $conf
   9   * @global Input $INPUT
  10   */
  11  
  12  if(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__).'/');
  13  require_once (DOKU_INC.'inc/init.php');
  14  
  15  //close session
  16  session_write_close();
  17  
  18  //feed disabled?
  19  if(!actionOK('rss')) {
  20      http_status(404);
  21      echo '<error>RSS feed is disabled.</error>';
  22      exit;
  23  }
  24  
  25  // get params
  26  $opt = rss_parseOptions();
  27  
  28  // the feed is dynamic - we need a cache for each combo
  29  // (but most people just use the default feed so it's still effective)
  30  $key   = join('', array_values($opt)).'$'.$_SERVER['REMOTE_USER'].'$'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'];
  31  $cache = new cache($key, '.feed');
  32  
  33  // prepare cache depends
  34  $depends['files'] = getConfigFiles('main');
  35  $depends['age']   = $conf['rss_update'];
  36  $depends['purge'] = $INPUT->bool('purge');
  37  
  38  // check cacheage and deliver if nothing has changed since last
  39  // time or the update interval has not passed, also handles conditional requests
  40  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  41  header('Pragma: public');
  42  header('Content-Type: application/xml; charset=utf-8');
  43  header('X-Robots-Tag: noindex');
  44  if($cache->useCache($depends)) {
  45      http_conditionalRequest($cache->_time);
  46      if($conf['allowdebug']) header("X-CacheUsed: $cache->cache");
  47      print $cache->retrieveCache();
  48      exit;
  49  } else {
  50      http_conditionalRequest(time());
  51  }
  52  
  53  // create new feed
  54  $rss                 = new DokuWikiFeedCreator();
  55  $rss->title          = $conf['title'].(($opt['namespace']) ? ' '.$opt['namespace'] : '');
  56  $rss->link           = DOKU_URL;
  57  $rss->syndicationURL = DOKU_URL.'feed.php';
  58  $rss->cssStyleSheet  = DOKU_URL.'lib/exe/css.php?s=feed';
  59  
  60  $image        = new FeedImage();
  61  $image->title = $conf['title'];
  62  $image->url   = tpl_getMediaFile(array(':wiki:favicon.ico', ':favicon.ico', 'images/favicon.ico'), true);
  63  $image->link  = DOKU_URL;
  64  $rss->image   = $image;
  65  
  66  $data  = null;
  67  $modes = array(
  68      'list'   => 'rssListNamespace',
  69      'search' => 'rssSearch',
  70      'recent' => 'rssRecentChanges'
  71  );
  72  if(isset($modes[$opt['feed_mode']])) {
  73      $data = $modes[$opt['feed_mode']]($opt);
  74  } else {
  75      $eventData = array(
  76          'opt'  => &$opt,
  77          'data' => &$data,
  78      );
  79      $event     = new Doku_Event('FEED_MODE_UNKNOWN', $eventData);
  80      if($event->advise_before(true)) {
  81          echo sprintf('<error>Unknown feed mode %s</error>', hsc($opt['feed_mode']));
  82          exit;
  83      }
  84      $event->advise_after();
  85  }
  86  
  87  rss_buildItems($rss, $data, $opt);
  88  $feed = $rss->createFeed($opt['feed_type'], 'utf-8');
  89  
  90  // save cachefile
  91  $cache->storeCache($feed);
  92  
  93  // finally deliver
  94  print $feed;
  95  
  96  // ---------------------------------------------------------------- //
  97  
  98  /**
  99   * Get URL parameters and config options and return an initialized option array
 100   *
 101   * @author Andreas Gohr <andi@splitbrain.org>
 102   */
 103  function rss_parseOptions() {
 104      global $conf;
 105      global $INPUT;
 106  
 107      $opt = array();
 108  
 109      foreach(array(
 110                  // Basic feed properties
 111                  // Plugins may probably want to add new values to these
 112                  // properties for implementing own feeds
 113  
 114                  // One of: list, search, recent
 115                  'feed_mode'    => array('str', 'mode', 'recent'),
 116                  // One of: diff, page, rev, current
 117                  'link_to'      => array('str', 'linkto', $conf['rss_linkto']),
 118                  // One of: abstract, diff, htmldiff, html
 119                  'item_content' => array('str', 'content', $conf['rss_content']),
 120  
 121                  // Special feed properties
 122                  // These are only used by certain feed_modes
 123  
 124                  // String, used for feed title, in list and rc mode
 125                  'namespace'    => array('str', 'ns', null),
 126                  // Positive integer, only used in rc mode
 127                  'items'        => array('int', 'num', $conf['recent']),
 128                  // Boolean, only used in rc mode
 129                  'show_minor'   => array('bool', 'minor', false),
 130                  // String, only used in search mode
 131                  'search_query' => array('str', 'q', null),
 132                  // One of: pages, media, both
 133                  'content_type' => array('str', 'view', $conf['rss_media'])
 134  
 135              ) as $name => $val) {
 136          $opt[$name] = $INPUT->$val[0]($val[1], $val[2], true);
 137      }
 138  
 139      $opt['items']      = max(0, (int) $opt['items']);
 140      $opt['show_minor'] = (bool) $opt['show_minor'];
 141  
 142      $opt['guardmail'] = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none');
 143  
 144      $type = $INPUT->valid(
 145          'type',
 146          array( 'rss', 'rss2', 'atom', 'atom1', 'rss1'),
 147          $conf['rss_type']
 148      );
 149      switch($type) {
 150          case 'rss':
 151              $opt['feed_type'] = 'RSS0.91';
 152              $opt['mime_type'] = 'text/xml';
 153              break;
 154          case 'rss2':
 155              $opt['feed_type'] = 'RSS2.0';
 156              $opt['mime_type'] = 'text/xml';
 157              break;
 158          case 'atom':
 159              $opt['feed_type'] = 'ATOM0.3';
 160              $opt['mime_type'] = 'application/xml';
 161              break;
 162          case 'atom1':
 163              $opt['feed_type'] = 'ATOM1.0';
 164              $opt['mime_type'] = 'application/atom+xml';
 165              break;
 166          default:
 167              $opt['feed_type'] = 'RSS1.0';
 168              $opt['mime_type'] = 'application/xml';
 169      }
 170  
 171      $eventData = array(
 172          'opt' => &$opt,
 173      );
 174      trigger_event('FEED_OPTS_POSTPROCESS', $eventData);
 175      return $opt;
 176  }
 177  
 178  /**
 179   * Add recent changed pages to a feed object
 180   *
 181   * @author Andreas Gohr <andi@splitbrain.org>
 182   * @param  FeedCreator $rss the FeedCreator Object
 183   * @param  array       $data the items to add
 184   * @param  array       $opt  the feed options
 185   */
 186  function rss_buildItems(&$rss, &$data, $opt) {
 187      global $conf;
 188      global $lang;
 189      /* @var DokuWiki_Auth_Plugin $auth */
 190      global $auth;
 191  
 192      $eventData = array(
 193          'rss'  => &$rss,
 194          'data' => &$data,
 195          'opt'  => &$opt,
 196      );
 197      $event     = new Doku_Event('FEED_DATA_PROCESS', $eventData);
 198      if($event->advise_before(false)) {
 199          foreach($data as $ditem) {
 200              if(!is_array($ditem)) {
 201                  // not an array? then only a list of IDs was given
 202                  $ditem = array('id' => $ditem);
 203              }
 204  
 205              $item = new FeedItem();
 206              $id   = $ditem['id'];
 207              if(!$ditem['media']) {
 208                  $meta = p_get_metadata($id);
 209              } else {
 210                  $meta = array();
 211              }
 212  
 213              // add date
 214              if($ditem['date']) {
 215                  $date = $ditem['date'];
 216              } elseif ($ditem['media']) {
 217                  $date = @filemtime(mediaFN($id));
 218              } elseif (@file_exists(wikiFN($id))) {
 219                  $date = @filemtime(wikiFN($id));
 220              } elseif($meta['date']['modified']) {
 221                  $date = $meta['date']['modified'];
 222              } else {
 223                  $date = 0;
 224              }
 225              if($date) $item->date = date('r', $date);
 226  
 227              // add title
 228              if($conf['useheading'] && $meta['title']) {
 229                  $item->title = $meta['title'];
 230              } else {
 231                  $item->title = $ditem['id'];
 232              }
 233              if($conf['rss_show_summary'] && !empty($ditem['sum'])) {
 234                  $item->title .= ' - '.strip_tags($ditem['sum']);
 235              }
 236  
 237              // add item link
 238              switch($opt['link_to']) {
 239                  case 'page':
 240                      if($ditem['media']) {
 241                          $item->link = media_managerURL(
 242                              array(
 243                                   'image' => $id,
 244                                   'ns'    => getNS($id),
 245                                   'rev'   => $date
 246                              ), '&', true
 247                          );
 248                      } else {
 249                          $item->link = wl($id, 'rev='.$date, true, '&');
 250                      }
 251                      break;
 252                  case 'rev':
 253                      if($ditem['media']) {
 254                          $item->link = media_managerURL(
 255                              array(
 256                                   'image'       => $id,
 257                                   'ns'          => getNS($id),
 258                                   'rev'         => $date,
 259                                   'tab_details' => 'history'
 260                              ), '&', true
 261                          );
 262                      } else {
 263                          $item->link = wl($id, 'do=revisions&rev='.$date, true, '&');
 264                      }
 265                      break;
 266                  case 'current':
 267                      if($ditem['media']) {
 268                          $item->link = media_managerURL(
 269                              array(
 270                                   'image' => $id,
 271                                   'ns'    => getNS($id)
 272                              ), '&', true
 273                          );
 274                      } else {
 275                          $item->link = wl($id, '', true, '&');
 276                      }
 277                      break;
 278                  case 'diff':
 279                  default:
 280                      if($ditem['media']) {
 281                          $item->link = media_managerURL(
 282                              array(
 283                                   'image'       => $id,
 284                                   'ns'          => getNS($id),
 285                                   'rev'         => $date,
 286                                   'tab_details' => 'history',
 287                                   'mediado'     => 'diff'
 288                              ), '&', true
 289                          );
 290                      } else {
 291                          $item->link = wl($id, 'rev='.$date.'&do=diff', true, '&');
 292                      }
 293              }
 294  
 295              // add item content
 296              switch($opt['item_content']) {
 297                  case 'diff':
 298                  case 'htmldiff':
 299                      if($ditem['media']) {
 300                          $medialog = new MediaChangeLog($id);
 301                          $revs  = $medialog->getRevisions(0, 1);
 302                          $rev   = $revs[0];
 303                          $src_r = '';
 304                          $src_l = '';
 305  
 306                          if($size = media_image_preview_size($id, false, new JpegMeta(mediaFN($id)), 300)) {
 307                              $more  = 'w='.$size[0].'&h='.$size[1].'&t='.@filemtime(mediaFN($id));
 308                              $src_r = ml($id, $more, true, '&amp;', true);
 309                          }
 310                          if($rev && $size = media_image_preview_size($id, $rev, new JpegMeta(mediaFN($id, $rev)), 300)) {
 311                              $more  = 'rev='.$rev.'&w='.$size[0].'&h='.$size[1];
 312                              $src_l = ml($id, $more, true, '&amp;', true);
 313                          }
 314                          $content = '';
 315                          if($src_r) {
 316                              $content = '<table>';
 317                              $content .= '<tr><th width="50%">'.$rev.'</th>';
 318                              $content .= '<th width="50%">'.$lang['current'].'</th></tr>';
 319                              $content .= '<tr align="center"><td><img src="'.$src_l.'" alt="" /></td><td>';
 320                              $content .= '<img src="'.$src_r.'" alt="'.$id.'" /></td></tr>';
 321                              $content .= '</table>';
 322                          }
 323  
 324                      } else {
 325                          require_once (DOKU_INC.'inc/DifferenceEngine.php');
 326                          $pagelog = new PageChangeLog($id);
 327                          $revs = $pagelog->getRevisions(0, 1);
 328                          $rev  = $revs[0];
 329  
 330                          if($rev) {
 331                              $df = new Diff(explode("\n", rawWiki($id, $rev)),
 332                                             explode("\n", rawWiki($id, '')));
 333                          } else {
 334                              $df = new Diff(array(''),
 335                                             explode("\n", rawWiki($id, '')));
 336                          }
 337  
 338                          if($opt['item_content'] == 'htmldiff') {
 339                              // note: no need to escape diff output, TableDiffFormatter provides 'safe' html
 340                              $tdf     = new TableDiffFormatter();
 341                              $content = '<table>';
 342                              $content .= '<tr><th colspan="2" width="50%">'.$rev.'</th>';
 343                              $content .= '<th colspan="2" width="50%">'.$lang['current'].'</th></tr>';
 344                              $content .= $tdf->format($df);
 345                              $content .= '</table>';
 346                          } else {
 347                              // note: diff output must be escaped, UnifiedDiffFormatter provides plain text
 348                              $udf     = new UnifiedDiffFormatter();
 349                              $content = "<pre>\n".hsc($udf->format($df))."\n</pre>";
 350                          }
 351                      }
 352                      break;
 353                  case 'html':
 354                      if($ditem['media']) {
 355                          if($size = media_image_preview_size($id, false, new JpegMeta(mediaFN($id)))) {
 356                              $more    = 'w='.$size[0].'&h='.$size[1].'&t='.@filemtime(mediaFN($id));
 357                              $src     = ml($id, $more, true, '&amp;', true);
 358                              $content = '<img src="'.$src.'" alt="'.$id.'" />';
 359                          } else {
 360                              $content = '';
 361                          }
 362                      } else {
 363                          if (@filemtime(wikiFN($id)) === $date) {
 364                              $content = p_wiki_xhtml($id, '', false);
 365                          } else {
 366                              $content = p_wiki_xhtml($id, $date, false);
 367                          }
 368                          // no TOC in feeds
 369                          $content = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s', '', $content);
 370  
 371                          // add alignment for images
 372                          $content = preg_replace('/(<img .*?class="medialeft")/s', '\\1 align="left"', $content);
 373                          $content = preg_replace('/(<img .*?class="mediaright")/s', '\\1 align="right"', $content);
 374  
 375                          // make URLs work when canonical is not set, regexp instead of rerendering!
 376                          if(!$conf['canonical']) {
 377                              $base    = preg_quote(DOKU_REL, '/');
 378                              $content = preg_replace('/(<a href|<img src)="('.$base.')/s', '$1="'.DOKU_URL, $content);
 379                          }
 380                      }
 381  
 382                      break;
 383                  case 'abstract':
 384                  default:
 385                      if($ditem['media']) {
 386                          if($size = media_image_preview_size($id, false, new JpegMeta(mediaFN($id)))) {
 387                              $more    = 'w='.$size[0].'&h='.$size[1].'&t='.@filemtime(mediaFN($id));
 388                              $src     = ml($id, $more, true, '&amp;', true);
 389                              $content = '<img src="'.$src.'" alt="'.$id.'" />';
 390                          } else {
 391                              $content = '';
 392                          }
 393                      } else {
 394                          $content = $meta['description']['abstract'];
 395                      }
 396              }
 397              $item->description = $content; //FIXME a plugin hook here could be senseful
 398  
 399              // add user
 400              # FIXME should the user be pulled from metadata as well?
 401              $user         = @$ditem['user']; // the @ spares time repeating lookup
 402              $item->author = '';
 403              if($user && $conf['useacl'] && $auth) {
 404                  $userInfo = $auth->getUserData($user);
 405                  if($userInfo) {
 406                      switch($conf['showuseras']) {
 407                          case 'username':
 408                              $item->author = $userInfo['name'];
 409                              break;
 410                          default:
 411                              $item->author = $user;
 412                              break;
 413                      }
 414                  } else {
 415                      $item->author = $user;
 416                  }
 417                  if($userInfo && !$opt['guardmail']) {
 418                      $item->authorEmail = $userInfo['mail'];
 419                  } else {
 420                      //cannot obfuscate because some RSS readers may check validity
 421                      $item->authorEmail = $user.'@'.$ditem['ip'];
 422                  }
 423              } elseif($user) {
 424                  // this happens when no ACL but some Apache auth is used
 425                  $item->author      = $user;
 426                  $item->authorEmail = $user.'@'.$ditem['ip'];
 427              } else {
 428                  $item->authorEmail = 'anonymous@'.$ditem['ip'];
 429              }
 430  
 431              // add category
 432              if(isset($meta['subject'])) {
 433                  $item->category = $meta['subject'];
 434              } else {
 435                  $cat = getNS($id);
 436                  if($cat) $item->category = $cat;
 437              }
 438  
 439              // finally add the item to the feed object, after handing it to registered plugins
 440              $evdata = array(
 441                  'item'  => &$item,
 442                  'opt'   => &$opt,
 443                  'ditem' => &$ditem,
 444                  'rss'   => &$rss
 445              );
 446              $evt    = new Doku_Event('FEED_ITEM_ADD', $evdata);
 447              if($evt->advise_before()) {
 448                  $rss->addItem($item);
 449              }
 450              $evt->advise_after(); // for completeness
 451          }
 452      }
 453      $event->advise_after();
 454  }
 455  
 456  /**
 457   * Add recent changed pages to the feed object
 458   *
 459   * @author Andreas Gohr <andi@splitbrain.org>
 460   */
 461  function rssRecentChanges($opt) {
 462      global $conf;
 463      $flags = RECENTS_SKIP_DELETED;
 464      if(!$opt['show_minor']) $flags += RECENTS_SKIP_MINORS;
 465      if($opt['content_type'] == 'media' && $conf['mediarevisions']) $flags += RECENTS_MEDIA_CHANGES;
 466      if($opt['content_type'] == 'both' && $conf['mediarevisions']) $flags += RECENTS_MEDIA_PAGES_MIXED;
 467  
 468      $recents = getRecents(0, $opt['items'], $opt['namespace'], $flags);
 469      return $recents;
 470  }
 471  
 472  /**
 473   * Add all pages of a namespace to the feed object
 474   *
 475   * @author Andreas Gohr <andi@splitbrain.org>
 476   */
 477  function rssListNamespace($opt) {
 478      require_once (DOKU_INC.'inc/search.php');
 479      global $conf;
 480  
 481      $ns = ':'.cleanID($opt['namespace']);
 482      $ns = str_replace(':', '/', $ns);
 483  
 484      $data = array();
 485      $search_opts = array(
 486          'depth' => 1,
 487          'pagesonly' => true,
 488          'listfiles' => true
 489      );
 490      search($data, $conf['datadir'], 'search_universal', $search_opts, $ns);
 491  
 492      return $data;
 493  }
 494  
 495  /**
 496   * Add the result of a full text search to the feed object
 497   *
 498   * @author Andreas Gohr <andi@splitbrain.org>
 499   */
 500  function rssSearch($opt) {
 501      if(!$opt['search_query']) return array();
 502  
 503      require_once (DOKU_INC.'inc/fulltext.php');
 504      $data = ft_pageSearch($opt['search_query'], $poswords);
 505      $data = array_keys($data);
 506  
 507      return $data;
 508  }
 509  
 510  //Setup VIM: ex: et ts=4 :

title

Description

title

Description

title

Description

title

title

Body