DatabaseFunctions.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. /**
  3. * Legacy database functions, for compatibility with pre-1.3 code
  4. * NOTE: this file is no longer loaded by default.
  5. * @file
  6. * @ingroup Database
  7. */
  8. /**
  9. * Usually aborts on failure
  10. * If errors are explicitly ignored, returns success
  11. * @param $sql String: SQL query
  12. * @param $db Mixed: database handler
  13. * @param $fname String: name of the php function calling
  14. */
  15. function wfQuery( $sql, $db, $fname = '' ) {
  16. if ( !is_numeric( $db ) ) {
  17. # Someone has tried to call this the old way
  18. throw new FatalError( wfMsgNoDB( 'wrong_wfQuery_params', $db, $sql ) );
  19. }
  20. $c = wfGetDB( $db );
  21. if ( $c !== false ) {
  22. return $c->query( $sql, $fname );
  23. } else {
  24. return false;
  25. }
  26. }
  27. /**
  28. *
  29. * @param $sql String: SQL query
  30. * @param $dbi
  31. * @param $fname String: name of the php function calling
  32. * @return Array: first row from the database
  33. */
  34. function wfSingleQuery( $sql, $dbi, $fname = '' ) {
  35. $db = wfGetDB( $dbi );
  36. $res = $db->query($sql, $fname );
  37. $row = $db->fetchRow( $res );
  38. $ret = $row[0];
  39. $db->freeResult( $res );
  40. return $ret;
  41. }
  42. /**
  43. * Turns on (false) or off (true) the automatic generation and sending
  44. * of a "we're sorry, but there has been a database error" page on
  45. * database errors. Default is on (false). When turned off, the
  46. * code should use wfLastErrno() and wfLastError() to handle the
  47. * situation as appropriate.
  48. *
  49. * @param $newstate
  50. * @param $dbi
  51. * @return Returns the previous state.
  52. */
  53. function wfIgnoreSQLErrors( $newstate, $dbi = DB_LAST ) {
  54. $db = wfGetDB( $dbi );
  55. if ( $db !== false ) {
  56. return $db->ignoreErrors( $newstate );
  57. } else {
  58. return NULL;
  59. }
  60. }
  61. /**#@+
  62. * @param $res Database result handler
  63. * @param $dbi
  64. */
  65. /**
  66. * Free a database result
  67. * @return Bool: whether result is sucessful or not.
  68. */
  69. function wfFreeResult( $res, $dbi = DB_LAST )
  70. {
  71. $db = wfGetDB( $dbi );
  72. if ( $db !== false ) {
  73. $db->freeResult( $res );
  74. return true;
  75. } else {
  76. return false;
  77. }
  78. }
  79. /**
  80. * Get an object from a database result
  81. * @return object|false object we requested
  82. */
  83. function wfFetchObject( $res, $dbi = DB_LAST ) {
  84. $db = wfGetDB( $dbi );
  85. if ( $db !== false ) {
  86. return $db->fetchObject( $res, $dbi = DB_LAST );
  87. } else {
  88. return false;
  89. }
  90. }
  91. /**
  92. * Get a row from a database result
  93. * @return object|false row we requested
  94. */
  95. function wfFetchRow( $res, $dbi = DB_LAST ) {
  96. $db = wfGetDB( $dbi );
  97. if ( $db !== false ) {
  98. return $db->fetchRow ( $res, $dbi = DB_LAST );
  99. } else {
  100. return false;
  101. }
  102. }
  103. /**
  104. * Get a number of rows from a database result
  105. * @return integer|false number of rows
  106. */
  107. function wfNumRows( $res, $dbi = DB_LAST ) {
  108. $db = wfGetDB( $dbi );
  109. if ( $db !== false ) {
  110. return $db->numRows( $res, $dbi = DB_LAST );
  111. } else {
  112. return false;
  113. }
  114. }
  115. /**
  116. * Get the number of fields from a database result
  117. * @return integer|false number of fields
  118. */
  119. function wfNumFields( $res, $dbi = DB_LAST ) {
  120. $db = wfGetDB( $dbi );
  121. if ( $db !== false ) {
  122. return $db->numFields( $res );
  123. } else {
  124. return false;
  125. }
  126. }
  127. /**
  128. * Return name of a field in a result
  129. * @param $res Mixed: Ressource link see Database::fieldName()
  130. * @param $n Integer: id of the field
  131. * @param $dbi Default DB_LAST
  132. * @return string|false name of field
  133. */
  134. function wfFieldName( $res, $n, $dbi = DB_LAST )
  135. {
  136. $db = wfGetDB( $dbi );
  137. if ( $db !== false ) {
  138. return $db->fieldName( $res, $n, $dbi = DB_LAST );
  139. } else {
  140. return false;
  141. }
  142. }
  143. /**#@-*/
  144. /**
  145. * @todo document function
  146. * @see Database::insertId()
  147. */
  148. function wfInsertId( $dbi = DB_LAST ) {
  149. $db = wfGetDB( $dbi );
  150. if ( $db !== false ) {
  151. return $db->insertId();
  152. } else {
  153. return false;
  154. }
  155. }
  156. /**
  157. * @todo document function
  158. * @see Database::dataSeek()
  159. */
  160. function wfDataSeek( $res, $row, $dbi = DB_LAST ) {
  161. $db = wfGetDB( $dbi );
  162. if ( $db !== false ) {
  163. return $db->dataSeek( $res, $row );
  164. } else {
  165. return false;
  166. }
  167. }
  168. /**
  169. * Get the last error number
  170. * @see Database::lastErrno()
  171. */
  172. function wfLastErrno( $dbi = DB_LAST ) {
  173. $db = wfGetDB( $dbi );
  174. if ( $db !== false ) {
  175. return $db->lastErrno();
  176. } else {
  177. return false;
  178. }
  179. }
  180. /**
  181. * Get the last error
  182. * @see Database::lastError()
  183. */
  184. function wfLastError( $dbi = DB_LAST ) {
  185. $db = wfGetDB( $dbi );
  186. if ( $db !== false ) {
  187. return $db->lastError();
  188. } else {
  189. return false;
  190. }
  191. }
  192. /**
  193. * Get the number of affected rows
  194. * @see Database::affectedRows()
  195. */
  196. function wfAffectedRows( $dbi = DB_LAST ) {
  197. $db = wfGetDB( $dbi );
  198. if ( $db !== false ) {
  199. return $db->affectedRows();
  200. } else {
  201. return false;
  202. }
  203. }
  204. /**
  205. * Get the last query ran
  206. * @see Database::lastQuery
  207. */
  208. function wfLastDBquery( $dbi = DB_LAST ) {
  209. $db = wfGetDB( $dbi );
  210. if ( $db !== false ) {
  211. return $db->lastQuery();
  212. } else {
  213. return false;
  214. }
  215. }
  216. /**
  217. * @see Database::Set()
  218. * @todo document function
  219. * @param $table
  220. * @param $var
  221. * @param $value
  222. * @param $cond
  223. * @param $dbi Default DB_MASTER
  224. */
  225. function wfSetSQL( $table, $var, $value, $cond, $dbi = DB_MASTER )
  226. {
  227. $db = wfGetDB( $dbi );
  228. if ( $db !== false ) {
  229. return $db->set( $table, $var, $value, $cond );
  230. } else {
  231. return false;
  232. }
  233. }
  234. /**
  235. * Simple select wrapper, return one field
  236. * @see Database::selectField()
  237. * @param $table
  238. * @param $var
  239. * @param $cond Default ''
  240. * @param $dbi Default DB_LAST
  241. */
  242. function wfGetSQL( $table, $var, $cond='', $dbi = DB_LAST )
  243. {
  244. $db = wfGetDB( $dbi );
  245. if ( $db !== false ) {
  246. return $db->selectField( $table, $var, $cond );
  247. } else {
  248. return false;
  249. }
  250. }
  251. /**
  252. * Does a given field exist on the specified table?
  253. * @see Database::fieldExists()
  254. * @param $table
  255. * @param $field
  256. * @param $dbi Default DB_LAST
  257. * @return Result of Database::fieldExists() or false.
  258. */
  259. function wfFieldExists( $table, $field, $dbi = DB_LAST ) {
  260. $db = wfGetDB( $dbi );
  261. if ( $db !== false ) {
  262. return $db->fieldExists( $table, $field );
  263. } else {
  264. return false;
  265. }
  266. }
  267. /**
  268. * Does the requested index exist on the specified table?
  269. * @see Database::indexExists()
  270. * @param $table String
  271. * @param $index
  272. * @param $dbi Default DB_LAST
  273. * @return Result of Database::indexExists() or false.
  274. */
  275. function wfIndexExists( $table, $index, $dbi = DB_LAST ) {
  276. $db = wfGetDB( $dbi );
  277. if ( $db !== false ) {
  278. return $db->indexExists( $table, $index );
  279. } else {
  280. return false;
  281. }
  282. }
  283. /**
  284. * @see Database::insert()
  285. * @todo document function
  286. * @param $table String
  287. * @param $array Array
  288. * @param $fname String, default 'wfInsertArray'.
  289. * @param $dbi Default DB_MASTER
  290. * @return result of Database::insert() or false.
  291. */
  292. function wfInsertArray( $table, $array, $fname = 'wfInsertArray', $dbi = DB_MASTER ) {
  293. $db = wfGetDB( $dbi );
  294. if ( $db !== false ) {
  295. return $db->insert( $table, $array, $fname );
  296. } else {
  297. return false;
  298. }
  299. }
  300. /**
  301. * @see Database::getArray()
  302. * @todo document function
  303. * @param $table String
  304. * @param $vars
  305. * @param $conds
  306. * @param $fname String, default 'wfGetArray'.
  307. * @param $dbi Default DB_LAST
  308. * @return result of Database::getArray() or false.
  309. */
  310. function wfGetArray( $table, $vars, $conds, $fname = 'wfGetArray', $dbi = DB_LAST ) {
  311. $db = wfGetDB( $dbi );
  312. if ( $db !== false ) {
  313. return $db->getArray( $table, $vars, $conds, $fname );
  314. } else {
  315. return false;
  316. }
  317. }
  318. /**
  319. * @see Database::update()
  320. * @param $table String
  321. * @param $values
  322. * @param $conds
  323. * @param $fname String, default 'wfUpdateArray'
  324. * @param $dbi Default DB_MASTER
  325. * @return Result of Database::update()) or false;
  326. * @todo document function
  327. */
  328. function wfUpdateArray( $table, $values, $conds, $fname = 'wfUpdateArray', $dbi = DB_MASTER ) {
  329. $db = wfGetDB( $dbi );
  330. if ( $db !== false ) {
  331. $db->update( $table, $values, $conds, $fname );
  332. return true;
  333. } else {
  334. return false;
  335. }
  336. }
  337. /**
  338. * Get fully usable table name
  339. * @see Database::tableName()
  340. */
  341. function wfTableName( $name, $dbi = DB_LAST ) {
  342. $db = wfGetDB( $dbi );
  343. if ( $db !== false ) {
  344. return $db->tableName( $name );
  345. } else {
  346. return false;
  347. }
  348. }
  349. /**
  350. * @todo document function
  351. * @see Database::strencode()
  352. */
  353. function wfStrencode( $s, $dbi = DB_LAST ) {
  354. $db = wfGetDB( $dbi );
  355. if ( $db !== false ) {
  356. return $db->strencode( $s );
  357. } else {
  358. return false;
  359. }
  360. }
  361. /**
  362. * @todo document function
  363. * @see Database::nextSequenceValue()
  364. */
  365. function wfNextSequenceValue( $seqName, $dbi = DB_MASTER ) {
  366. $db = wfGetDB( $dbi );
  367. if ( $db !== false ) {
  368. return $db->nextSequenceValue( $seqName );
  369. } else {
  370. return false;
  371. }
  372. }
  373. /**
  374. * @todo document function
  375. * @see Database::useIndexClause()
  376. */
  377. function wfUseIndexClause( $index, $dbi = DB_SLAVE ) {
  378. $db = wfGetDB( $dbi );
  379. if ( $db !== false ) {
  380. return $db->useIndexClause( $index );
  381. } else {
  382. return false;
  383. }
  384. }