PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/ChangeTags.php

https://github.com/tav/confluence
PHP | 183 lines | 122 code | 38 blank | 23 comment | 22 complexity | b3cd4f0b7ff67d61a9a384d8a79ce8a8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. if (!defined( 'MEDIAWIKI' ))
  3. die;
  4. class ChangeTags {
  5. static function formatSummaryRow( $tags, $page ) {
  6. if (!$tags)
  7. return array('',array());
  8. $classes = array();
  9. $tags = explode( ',', $tags );
  10. $displayTags = array();
  11. foreach( $tags as $tag ) {
  12. $displayTags[] = self::tagDescription( $tag );
  13. $classes[] = "mw-tag-$tag";
  14. }
  15. return array( '(' . implode( ', ', $displayTags ) . ')', $classes );
  16. }
  17. static function tagDescription( $tag ) {
  18. $msg = wfMsgExt( "tag-$tag", 'parseinline' );
  19. if ( wfEmptyMsg( "tag-$tag", $msg ) ) {
  20. return htmlspecialchars($tag);
  21. }
  22. return $msg;
  23. }
  24. ## Basic utility method to add tags to a particular change, given its rc_id, rev_id and/or log_id.
  25. static function addTags( $tags, $rc_id=null, $rev_id=null, $log_id=null, $params = null ) {
  26. if ( !is_array($tags) ) {
  27. $tags = array( $tags );
  28. }
  29. $tags = array_filter( $tags ); // Make sure we're submitting all tags...
  30. if (!$rc_id && !$rev_id && !$log_id) {
  31. throw new MWException( "At least one of: RCID, revision ID, and log ID MUST be specified when adding a tag to a change!" );
  32. }
  33. $dbr = wfGetDB( DB_SLAVE );
  34. // Might as well look for rcids and so on.
  35. if (!$rc_id) {
  36. $dbr = wfGetDB( DB_MASTER ); // Info might be out of date, somewhat fractionally, on slave.
  37. if ($log_id) {
  38. $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_logid' => $log_id ), __METHOD__ );
  39. } elseif ($rev_id) {
  40. $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_this_oldid' => $rev_id ), __METHOD__ );
  41. }
  42. } elseif (!$log_id && !$rev_id) {
  43. $dbr = wfGetDB( DB_MASTER ); // Info might be out of date, somewhat fractionally, on slave.
  44. $log_id = $dbr->selectField( 'recentchanges', 'rc_logid', array( 'rc_id' => $rc_id ), __METHOD__ );
  45. $rev_id = $dbr->selectField( 'recentchanges', 'rc_this_oldid', array( 'rc_id' => $rc_id ), __METHOD__ );
  46. }
  47. $tsConds = array_filter( array( 'ts_rc_id' => $rc_id, 'ts_rev_id' => $rev_id, 'ts_log_id' => $log_id ) );
  48. ## Update the summary row.
  49. $prevTags = $dbr->selectField( 'tag_summary', 'ts_tags', $tsConds, __METHOD__ );
  50. $prevTags = $prevTags ? $prevTags : '';
  51. $prevTags = array_filter( explode( ',', $prevTags ) );
  52. $newTags = array_unique( array_merge( $prevTags, $tags ) );
  53. sort($prevTags);
  54. sort($newTags);
  55. if ( $prevTags == $newTags ) {
  56. // No change.
  57. return false;
  58. }
  59. $dbw = wfGetDB( DB_MASTER );
  60. $dbw->replace( 'tag_summary', array( 'ts_rev_id', 'ts_rc_id', 'ts_log_id' ), array_filter( array_merge( $tsConds, array( 'ts_tags' => implode( ',', $newTags ) ) ) ), __METHOD__ );
  61. // Insert the tags rows.
  62. $tagsRows = array();
  63. foreach( $tags as $tag ) { // Filter so we don't insert NULLs as zero accidentally.
  64. $tagsRows[] = array_filter( array( 'ct_tag' => $tag, 'ct_rc_id' => $rc_id, 'ct_log_id' => $log_id, 'ct_rev_id' => $rev_id, 'ct_params' => $params ) );
  65. }
  66. $dbw->insert( 'change_tag', $tagsRows, __METHOD__, array('IGNORE') );
  67. return true;
  68. }
  69. /**
  70. * Applies all tags-related changes to a query.
  71. * Handles selecting tags, and filtering.
  72. * Needs $tables to be set up properly, so we can figure out which join conditions to use.
  73. */
  74. static function modifyDisplayQuery( &$tables, &$fields, &$conds,
  75. &$join_conds, &$options, $filter_tag = false ) {
  76. global $wgRequest, $wgUseTagFilter;
  77. if ($filter_tag === false) {
  78. $filter_tag = $wgRequest->getVal( 'tagfilter' );
  79. }
  80. // Figure out which conditions can be done.
  81. $join_field = '';
  82. if ( in_array('recentchanges', $tables) ) {
  83. $join_cond = 'rc_id';
  84. } elseif( in_array('logging', $tables) ) {
  85. $join_cond = 'log_id';
  86. } elseif ( in_array('revision', $tables) ) {
  87. $join_cond = 'rev_id';
  88. } else {
  89. throw new MWException( "Unable to determine appropriate JOIN condition for tagging." );
  90. }
  91. // JOIN on tag_summary
  92. $tables[] = 'tag_summary';
  93. $join_conds['tag_summary'] = array( 'LEFT JOIN', "ts_$join_cond=$join_cond" );
  94. $fields[] = 'ts_tags';
  95. if ($wgUseTagFilter && $filter_tag) {
  96. // Somebody wants to filter on a tag.
  97. // Add an INNER JOIN on change_tag
  98. // FORCE INDEX -- change_tags will almost ALWAYS be the correct query plan.
  99. $options['USE INDEX'] = array( 'change_tag' => 'change_tag_tag_id' );
  100. unset( $options['FORCE INDEX'] );
  101. $tables[] = 'change_tag';
  102. $join_conds['change_tag'] = array( 'INNER JOIN', "ct_$join_cond=$join_cond" );
  103. $conds['ct_tag'] = $filter_tag;
  104. }
  105. }
  106. /**
  107. * If $fullForm is set to false, then it returns an array of (label, form).
  108. * If $fullForm is true, it returns an entire form.
  109. */
  110. static function buildTagFilterSelector( $selected='', $fullForm = false /* used to put a full form around the selector */ ) {
  111. global $wgUseTagFilter;
  112. if ( !$wgUseTagFilter || !count( self::listDefinedTags() ) )
  113. return $fullForm ? '' : array();
  114. global $wgTitle;
  115. $data = array( wfMsgExt( 'tag-filter', 'parseinline' ), Xml::input( 'tagfilter', 20, $selected ) );
  116. if (!$fullForm) {
  117. return $data;
  118. }
  119. $html = implode( '&nbsp;', $data );
  120. $html .= "\n" . Xml::element( 'input', array( 'type' => 'submit', 'value' => wfMsg( 'tag-filter-submit' ) ) );
  121. $html .= "\n" . Xml::hidden( 'title', $wgTitle-> getPrefixedText() );
  122. $html = Xml::tags( 'form', array( 'action' => $wgTitle->getLocalURL(), 'method' => 'get' ), $html );
  123. return $html;
  124. }
  125. /** Basically lists defined tags which count even if they aren't applied to anything */
  126. static function listDefinedTags() {
  127. // Caching...
  128. global $wgMemc;
  129. $key = wfMemcKey( 'valid-tags' );
  130. if ($tags = $wgMemc->get( $key ))
  131. return $tags;
  132. $emptyTags = array();
  133. // Some DB stuff
  134. $dbr = wfGetDB( DB_SLAVE );
  135. $res = $dbr->select( 'valid_tag', 'vt_tag', array(), __METHOD__ );
  136. while( $row = $res->fetchObject() ) {
  137. $emptyTags[] = $row->vt_tag;
  138. }
  139. wfRunHooks( 'ListDefinedTags', array(&$emptyTags) );
  140. $emptyTags = array_filter( array_unique( $emptyTags ) );
  141. // Short-term caching.
  142. $wgMemc->set( $key, $emptyTags, 300 );
  143. return $emptyTags;
  144. }
  145. }