PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/ChangeTags.php

https://bitbucket.org/brunodefraine/mediawiki
PHP | 262 lines | 140 code | 35 blank | 87 comment | 19 complexity | ba2126de6f3a27f2728295d3341b107b MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * Functions related to change tags.
  4. *
  5. * @file
  6. */
  7. class ChangeTags {
  8. /**
  9. * Creates HTML for the given tags
  10. *
  11. * @param $tags String: Comma-separated list of tags
  12. * @param $page String: A label for the type of action which is being displayed,
  13. * for example: 'history', 'contributions' or 'newpages'
  14. *
  15. * @return Array with two items: (html, classes)
  16. * - html: String: HTML for displaying the tags (empty string when param $tags is empty)
  17. * - classes: Array of strings: CSS classes used in the generated html, one class for each tag
  18. *
  19. */
  20. static function formatSummaryRow( $tags, $page ) {
  21. if( !$tags )
  22. return array( '', array() );
  23. $classes = array();
  24. $tags = explode( ',', $tags );
  25. $displayTags = array();
  26. foreach( $tags as $tag ) {
  27. $displayTags[] = Xml::tags(
  28. 'span',
  29. array( 'class' => 'mw-tag-marker ' .
  30. Sanitizer::escapeClass( "mw-tag-marker-$tag" ) ),
  31. self::tagDescription( $tag )
  32. );
  33. $classes[] = Sanitizer::escapeClass( "mw-tag-$tag" );
  34. }
  35. $markers = '(' . implode( ', ', $displayTags ) . ')';
  36. $markers = Xml::tags( 'span', array( 'class' => 'mw-tag-markers' ), $markers );
  37. return array( $markers, $classes );
  38. }
  39. /**
  40. * Get a short description for a tag
  41. *
  42. * @param $tag String: tag
  43. *
  44. * @return String: Short description of the tag from "mediawiki:tag-$tag" if this message exists,
  45. * html-escaped version of $tag otherwise
  46. */
  47. static function tagDescription( $tag ) {
  48. $msg = wfMessage( "tag-$tag" );
  49. return $msg->exists() ? $msg->parse() : htmlspecialchars( $tag );
  50. }
  51. /**
  52. * Add tags to a change given its rc_id, rev_id and/or log_id
  53. *
  54. * @param $tags String|Array: Tags to add to the change
  55. * @param $rc_id int: rc_id of the change to add the tags to
  56. * @param $rev_id int: rev_id of the change to add the tags to
  57. * @param $log_id int: log_id of the change to add the tags to
  58. * @param $params String: params to put in the ct_params field of tabel 'change_tag'
  59. *
  60. * @return bool: false if no changes are made, otherwise true
  61. *
  62. * @exception MWException when $rc_id, $rev_id and $log_id are all null
  63. */
  64. static function addTags( $tags, $rc_id = null, $rev_id = null, $log_id = null, $params = null ) {
  65. if ( !is_array( $tags ) ) {
  66. $tags = array( $tags );
  67. }
  68. $tags = array_filter( $tags ); // Make sure we're submitting all tags...
  69. if( !$rc_id && !$rev_id && !$log_id ) {
  70. throw new MWException( "At least one of: RCID, revision ID, and log ID MUST be specified when adding a tag to a change!" );
  71. }
  72. $dbr = wfGetDB( DB_SLAVE );
  73. // Might as well look for rcids and so on.
  74. if( !$rc_id ) {
  75. $dbr = wfGetDB( DB_MASTER ); // Info might be out of date, somewhat fractionally, on slave.
  76. if( $log_id ) {
  77. $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_logid' => $log_id ), __METHOD__ );
  78. } elseif( $rev_id ) {
  79. $rc_id = $dbr->selectField( 'recentchanges', 'rc_id', array( 'rc_this_oldid' => $rev_id ), __METHOD__ );
  80. }
  81. } elseif( !$log_id && !$rev_id ) {
  82. $dbr = wfGetDB( DB_MASTER ); // Info might be out of date, somewhat fractionally, on slave.
  83. $log_id = $dbr->selectField( 'recentchanges', 'rc_logid', array( 'rc_id' => $rc_id ), __METHOD__ );
  84. $rev_id = $dbr->selectField( 'recentchanges', 'rc_this_oldid', array( 'rc_id' => $rc_id ), __METHOD__ );
  85. }
  86. $tsConds = array_filter( array( 'ts_rc_id' => $rc_id, 'ts_rev_id' => $rev_id, 'ts_log_id' => $log_id ) );
  87. ## Update the summary row.
  88. $prevTags = $dbr->selectField( 'tag_summary', 'ts_tags', $tsConds, __METHOD__ );
  89. $prevTags = $prevTags ? $prevTags : '';
  90. $prevTags = array_filter( explode( ',', $prevTags ) );
  91. $newTags = array_unique( array_merge( $prevTags, $tags ) );
  92. sort( $prevTags );
  93. sort( $newTags );
  94. if ( $prevTags == $newTags ) {
  95. // No change.
  96. return false;
  97. }
  98. $dbw = wfGetDB( DB_MASTER );
  99. $dbw->replace(
  100. 'tag_summary',
  101. array( 'ts_rev_id', 'ts_rc_id', 'ts_log_id' ),
  102. array_filter( array_merge( $tsConds, array( 'ts_tags' => implode( ',', $newTags ) ) ) ),
  103. __METHOD__
  104. );
  105. // Insert the tags rows.
  106. $tagsRows = array();
  107. foreach( $tags as $tag ) { // Filter so we don't insert NULLs as zero accidentally.
  108. $tagsRows[] = array_filter(
  109. array(
  110. 'ct_tag' => $tag,
  111. 'ct_rc_id' => $rc_id,
  112. 'ct_log_id' => $log_id,
  113. 'ct_rev_id' => $rev_id,
  114. 'ct_params' => $params
  115. )
  116. );
  117. }
  118. $dbw->insert( 'change_tag', $tagsRows, __METHOD__, array( 'IGNORE' ) );
  119. return true;
  120. }
  121. /**
  122. * Applies all tags-related changes to a query.
  123. * Handles selecting tags, and filtering.
  124. * Needs $tables to be set up properly, so we can figure out which join conditions to use.
  125. *
  126. * @param $tables String|Array: Tabel names, see DatabaseBase::select
  127. * @param $fields String|Array: Fields used in query, see DatabaseBase::select
  128. * @param $conds String|Array: conditions used in query, see DatabaseBase::select
  129. * @param $join_conds Array: join conditions, see DatabaseBase::select
  130. * @param $options Array: options, see Database::select
  131. * @param $filter_tag String: tag to select on
  132. *
  133. * @exception MWException when unable to determine appropriate JOIN condition for tagging
  134. *
  135. */
  136. static function modifyDisplayQuery( &$tables, &$fields, &$conds,
  137. &$join_conds, &$options, $filter_tag = false ) {
  138. global $wgRequest, $wgUseTagFilter;
  139. if( $filter_tag === false ) {
  140. $filter_tag = $wgRequest->getVal( 'tagfilter' );
  141. }
  142. // Figure out which conditions can be done.
  143. if ( in_array( 'recentchanges', $tables ) ) {
  144. $join_cond = 'rc_id';
  145. } elseif( in_array( 'logging', $tables ) ) {
  146. $join_cond = 'log_id';
  147. } elseif ( in_array( 'revision', $tables ) ) {
  148. $join_cond = 'rev_id';
  149. } else {
  150. throw new MWException( 'Unable to determine appropriate JOIN condition for tagging.' );
  151. }
  152. // JOIN on tag_summary
  153. $tables[] = 'tag_summary';
  154. $join_conds['tag_summary'] = array( 'LEFT JOIN', "ts_$join_cond=$join_cond" );
  155. $fields[] = 'ts_tags';
  156. if( $wgUseTagFilter && $filter_tag ) {
  157. // Somebody wants to filter on a tag.
  158. // Add an INNER JOIN on change_tag
  159. // FORCE INDEX -- change_tags will almost ALWAYS be the correct query plan.
  160. global $wgOldChangeTagsIndex;
  161. $index = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
  162. $options['USE INDEX'] = array( 'change_tag' => $index );
  163. unset( $options['FORCE INDEX'] );
  164. $tables[] = 'change_tag';
  165. $join_conds['change_tag'] = array( 'INNER JOIN', "ct_$join_cond=$join_cond" );
  166. $conds['ct_tag'] = $filter_tag;
  167. }
  168. }
  169. /**
  170. * Build a text box to select a change tag
  171. *
  172. * @param $selected String: tag to select by default
  173. * @param $fullForm Boolean:
  174. * - if false, then it returns an array of (label, form).
  175. * - if true, it returns an entire form around the selector.
  176. * @param $title Title object to send the form to.
  177. * Used when, and only when $fullForm is true.
  178. * @return String or array:
  179. * - if $fullForm is false: Array with
  180. * - if $fullForm is true: String, html fragment
  181. */
  182. public static function buildTagFilterSelector( $selected='', $fullForm = false, Title $title = null ) {
  183. global $wgUseTagFilter;
  184. if ( !$wgUseTagFilter || !count( self::listDefinedTags() ) )
  185. return $fullForm ? '' : array();
  186. $data = array( Html::rawElement( 'label', array( 'for' => 'tagfilter' ), wfMsgExt( 'tag-filter', 'parseinline' ) ),
  187. Xml::input( 'tagfilter', 20, $selected ) );
  188. if ( !$fullForm ) {
  189. return $data;
  190. }
  191. $html = implode( '&#160;', $data );
  192. $html .= "\n" . Xml::element( 'input', array( 'type' => 'submit', 'value' => wfMsg( 'tag-filter-submit' ) ) );
  193. $html .= "\n" . Html::hidden( 'title', $title->getPrefixedText() );
  194. $html = Xml::tags( 'form', array( 'action' => $title->getLocalURL(), 'method' => 'get' ), $html );
  195. return $html;
  196. }
  197. /**
  198. * Basically lists defined tags which count even if they aren't applied to anything.
  199. * Tags on items in table 'change_tag' which are not (or no longer) in table 'valid_tag'
  200. * are not included.
  201. *
  202. * Tries memcached first.
  203. *
  204. * @return Array of strings: tags
  205. */
  206. static function listDefinedTags() {
  207. // Caching...
  208. global $wgMemc;
  209. $key = wfMemcKey( 'valid-tags' );
  210. $tags = $wgMemc->get( $key );
  211. if ( $tags ) {
  212. return $tags;
  213. }
  214. $emptyTags = array();
  215. // Some DB stuff
  216. $dbr = wfGetDB( DB_SLAVE );
  217. $res = $dbr->select( 'valid_tag', 'vt_tag', array(), __METHOD__ );
  218. foreach ( $res as $row ) {
  219. $emptyTags[] = $row->vt_tag;
  220. }
  221. wfRunHooks( 'ListDefinedTags', array( &$emptyTags ) );
  222. $emptyTags = array_filter( array_unique( $emptyTags ) );
  223. // Short-term caching.
  224. $wgMemc->set( $key, $emptyTags, 300 );
  225. return $emptyTags;
  226. }
  227. }